setup-ssh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env bash
  2. #=============================================================================
  3. # Copyright 2010-2012 Kitware, Inc.
  4. #
  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
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  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. #=============================================================================
  17. # Run this script to set up ssh push access to the repository host.
  18. # Project configuration instructions:
  19. #
  20. # - Populate adjacent "config" file with:
  21. # ssh.host = Repository host name
  22. # ssh.user = Username on host, if not "git"
  23. # ssh.key = Local ssh key name
  24. # ssh.request-url = Web page URL to request ssh access
  25. egrep-q() {
  26. egrep "$@" >/dev/null 2>/dev/null
  27. }
  28. die() {
  29. echo 1>&2 "$@" ; exit 1
  30. }
  31. # Make sure we are inside the repository.
  32. cd "${BASH_SOURCE%/*}" &&
  33. # Load the project configuration.
  34. host=$(git config -f config --get ssh.host) &&
  35. user=$(git config -f config --get ssh.user || echo git) &&
  36. key=$(git config -f config --get ssh.key) &&
  37. request_url=$(git config -f config --get ssh.request-url) ||
  38. die 'This project is not configured for ssh push access.'
  39. # Check for existing configuration.
  40. if test -r ~/.ssh/config &&
  41. egrep-q 'Host[= ]'"${host//\./\\.}" ~/.ssh/config; then
  42. echo 'Host "'"$host"'" is already in ~/.ssh/config' &&
  43. setup= &&
  44. question='Test'
  45. else
  46. echo 'Host "'"$host"'" not found in ~/.ssh/config' &&
  47. setup=1 &&
  48. question='Setup and test'
  49. fi &&
  50. # Ask the user whether to make changes.
  51. echo '' &&
  52. read -ep "${question} push access by ssh to $user@$host? [y/N]: " access &&
  53. if test "$access" != "y" -a "$access" != "Y"; then
  54. exit 0
  55. fi &&
  56. # Setup host configuration if necessary.
  57. if test -n "$setup"; then
  58. if ! test -d ~/.ssh; then
  59. mkdir -p ~/.ssh &&
  60. chmod 700 ~/.ssh
  61. fi &&
  62. if ! test -f ~/.ssh/config; then
  63. touch ~/.ssh/config &&
  64. chmod 600 ~/.ssh/config
  65. fi &&
  66. ssh_config='Host='"$host"'
  67. IdentityFile ~/.ssh/'"$key" &&
  68. echo "Adding to ~/.ssh/config:
  69. $ssh_config
  70. " &&
  71. echo "$ssh_config" >> ~/.ssh/config &&
  72. if ! test -e ~/.ssh/"$key"; then
  73. if test -f ~/.ssh/id_rsa; then
  74. # Take care of the common case.
  75. ln -s id_rsa ~/.ssh/"$key"
  76. echo '
  77. Assuming ~/.ssh/id_rsa is the private key corresponding to the public key for
  78. '"$user@$host"'
  79. If this is incorrect place private key at "~/.ssh/'"$key"'".'
  80. else
  81. echo '
  82. Place the private key corresponding to the public key registered for
  83. '"$user@$host"'
  84. at "~/.ssh/'"$key"'".'
  85. fi
  86. read -e -n 1 -p 'Press any key to continue...'
  87. fi
  88. fi || exit 1
  89. # Test access configuration.
  90. echo 'Testing ssh push access to "'"$user@$host"'"...' &&
  91. if ! ssh "$user@$host" info; then
  92. die 'No ssh push access to "'"$user@$host"'". You may need to request access at
  93. '"$request_url"'
  94. '
  95. fi