Merge topic 'remove-spaces-wc' into setup
[kiteware-gitsetup.git] / setup-ssh
blob8a5dbba7d950a595525bbb0e15b1cf33607fe841
1 #!/usr/bin/env bash
2 #=============================================================================
3 # Copyright 2010-2012 Kitware, Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #=============================================================================
18 # Run this script to set up ssh push access to the repository host.
20 # Project configuration instructions:
22 # - Populate adjacent "config" file with:
23 # ssh.host = Repository host name
24 # ssh.user = Username on host, if not "git"
25 # ssh.key = Local ssh key name
26 # ssh.request-url = Web page URL to request ssh access
28 egrep_q() {
29 grep -E "$@" >/dev/null 2>/dev/null
32 die() {
33 echo 1>&2 "$@" ; exit 1
36 # Make sure we are inside the repository.
37 cd "${BASH_SOURCE%/*}" &&
39 # Load the project configuration.
40 host=$(git config -f config --get ssh.host) &&
41 user=$(git config -f config --get ssh.user || echo git) &&
42 key=$(git config -f config --get ssh.key) &&
43 request_url=$(git config -f config --get ssh.request-url) ||
44 die 'This project is not configured for ssh push access.'
46 # Check for existing configuration.
47 if test -r ~/.ssh/config &&
48 egrep_q 'Host[= ]'"${host//\./\\.}" ~/.ssh/config; then
49 echo 'Host "'"$host"'" is already in ~/.ssh/config' &&
50 setup= &&
51 question='Test'
52 else
53 echo 'Host "'"$host"'" not found in ~/.ssh/config' &&
54 setup=1 &&
55 question='Setup and test'
56 fi &&
58 # Ask the user whether to make changes.
59 echo '' &&
60 read -ep "${question} push access by ssh to $user@$host? [y/N]: " access &&
61 if test "$access" != "y" -a "$access" != "Y"; then
62 exit 0
63 fi &&
65 # Setup host configuration if necessary.
66 if test -n "$setup"; then
67 if ! test -d ~/.ssh; then
68 mkdir -p ~/.ssh &&
69 chmod 700 ~/.ssh
70 fi &&
71 if ! test -f ~/.ssh/config; then
72 touch ~/.ssh/config &&
73 chmod 600 ~/.ssh/config
74 fi &&
75 ssh_config='Host='"$host"'
76 IdentityFile ~/.ssh/'"$key" &&
77 echo "Adding to ~/.ssh/config:
79 $ssh_config
80 " &&
81 echo "$ssh_config" >> ~/.ssh/config &&
82 if ! test -e ~/.ssh/"$key"; then
83 if test -f ~/.ssh/id_rsa; then
84 # Take care of the common case.
85 ln -s id_rsa ~/.ssh/"$key"
86 echo '
87 Assuming ~/.ssh/id_rsa is the private key corresponding to the public key for
89 '"$user@$host"'
91 If this is incorrect place private key at "~/.ssh/'"$key"'".'
92 else
93 echo '
94 Place the private key corresponding to the public key registered for
96 '"$user@$host"'
98 at "~/.ssh/'"$key"'".'
100 read -e -n 1 -p 'Press any key to continue...'
102 fi || exit 1
104 # Test access configuration.
105 echo 'Testing ssh push access to "'"$user@$host"'"...' &&
106 if ! ssh "$user@$host" info; then
107 die 'No ssh push access to "'"$user@$host"'". You may need to request access at
109 '"$request_url"'