README written
[vsorcdistro/.git] / clustersetup.sh
1 #!/usr/bin/env bash
2
3 # SSH authentication script for cluster edition
4 # This script will create a single key pair, which is then
5 # propagated throughout the entire cluster.
6 # There are two options for setup; temporary setup
7 # persistent setup. If no options are specified, and the script
8 # is only given ip addresses or host names, it will default to
9 # the temporary setup. An ssh directory is then created in
10 # /tmp/mn/ssh on each node, and mounted with the keys over the
11 # user's ssh directory. This setup can easily be torn down by running
12 # clustersetup with the -c option.
13 # If the -p option is used, the setup will be persistent. In this
14 # case, the key pair will be be distributed directly to each node's
15 # ssh directory, but will be called cluster_key. An option to
16 # specify this key for use will be added to the config file in each
17 # user's ssh directory.
18
19
20 set -e
21 num_options=0
22 persistent=false
23 showHelp=false
24 clean=false
25 declare -a hosts=()
26 user=$(whoami)
27 SSHDIR=/tmp/mn/ssh
28 USERDIR=$HOME/.ssh
29 usage="./clustersetup.sh [ -p|h|c ] [ host1 ] [ host2 ] ...\n
30         Authenticate yourself and other cluster nodes to each other
31         via ssh for mininet cluster edition. By default, we use a
32         temporary ssh setup. An ssh directory is mounted over
33         $USERDIR on each machine in the cluster.
34         
35                 -h: display this help
36                 -p: create a persistent ssh setup. This will add
37                     new ssh keys and known_hosts to each nodes
38                     $USERDIR directory
39                 -c: method to clean up a temporary ssh setup.
40                     Any hosts taken as arguments will be cleaned
41         "
42
43 persistentSetup() {
44     echo "***creating key pair"
45     ssh-keygen -t rsa -C "Cluster_Edition_Key" -f $USERDIR/cluster_key -N '' # &> /dev/null
46     cat $USERDIR/cluster_key.pub >> $USERDIR/authorized_keys
47     echo "***configuring ssh"
48     echo "IdentityFile $USERDIR/cluster_key" >> $USERDIR/config
49     echo "IdentityFile $USERDIR/id_rsa" >> $USERDIR/config
50
51     for host in $hosts; do
52         echo "***copying public key to $host"
53         ssh-copy-id  -i $USERDIR/cluster_key.pub $user@$host &> /dev/null
54         echo "***copying key pair to remote host"
55         scp $USERDIR/cluster_key $user@$host:$USERDIR
56         scp $USERDIR/cluster_key.pub $user@$host:$USERDIR
57         echo "***configuring remote host"
58         ssh -o ForwardAgent=yes  $user@$host "
59         echo 'IdentityFile $USERDIR/cluster_key' >> $USERDIR/config
60         echo 'IdentityFile $USERDIR/id_rsa' >> $USERDIR/config"
61     done
62
63     for host in $hosts; do
64         echo "***copying known_hosts to $host"
65         scp $USERDIR/known_hosts $user@$host:$USERDIR/cluster_known_hosts
66         ssh $user@$host "
67         cat $USERDIR/cluster_known_hosts >> $USERDIR/known_hosts
68         rm $USERDIR/cluster_known_hosts"
69     done
70 }
71
72 tempSetup() {
73     
74     echo "***creating temporary ssh directory"
75     mkdir -p $SSHDIR 
76     echo "***creating key pair"
77     ssh-keygen -t rsa -C "Cluster_Edition_Key" -f $SSHDIR/id_rsa -N '' &> /dev/null
78
79     echo "***mounting temporary ssh directory"
80     sudo mount --bind $SSHDIR $USERDIR
81     cp $SSHDIR/id_rsa.pub $SSHDIR/authorized_keys
82
83     for host in $hosts; do
84         echo "***copying public key to $host"
85         ssh-copy-id $user@$host &> /dev/null
86         echo "***mounting remote temporary ssh directory for $host"
87         ssh -o ForwardAgent=yes  $user@$host "
88         mkdir -p $SSHDIR
89         cp $USERDIR/authorized_keys $SSHDIR/authorized_keys
90         sudo mount --bind $SSHDIR $USERDIR"
91         echo "***copying key pair to $host"
92         scp $SSHDIR/{id_rsa,id_rsa.pub} $user@$host:$SSHDIR
93     done
94
95     for host in $hosts; do
96         echo "***copying known_hosts to $host"
97         scp $SSHDIR/known_hosts $user@$host:$SSHDIR
98     done
99 }
100
101 cleanup() {
102     
103     for host in $hosts; do
104     echo "***cleaning up $host"
105     ssh $user@$host "sudo umount $USERDIR
106                           sudo rm -rf $SSHDIR"
107     done
108
109     echo "**unmounting local directories"
110     sudo umount $USERDIR
111     echo "***removing temporary ssh directory"
112     sudo rm -rf $SSHDIR
113     echo "done!"
114
115 }
116
117
118 if [ $# -eq 0 ]; then
119     echo "ERROR: No Arguments"
120     echo "$usage"
121     exit
122 else
123     while getopts 'hpc' OPTION
124     do
125         ((num_options+=1))
126         case $OPTION in
127         h)  showHelp=true;;
128         p)  persistent=true;;
129         c)  clean=true;;
130         ?)  showHelp=true;;
131         esac
132     done
133     shift $(($OPTIND - 1))
134 fi
135
136 if [ "$num_options" -gt 1 ]; then
137     echo "ERROR: Too Many Options"
138     echo "$usage"
139     exit
140 fi
141
142 if $showHelp; then
143     echo "$usage"
144     exit
145 fi
146
147 for i in "$@"; do
148     output=$(getent ahostsv4 "$i")
149     if [ -z "$output" ]; then
150         echo '***WARNING: could not find hostname "$i"'
151         echo ""
152     else
153         hosts+="$i "
154     fi
155 done
156
157 if $clean; then
158     cleanup
159     exit
160 fi
161
162 echo "***authenticating to:"
163 for host in $hosts; do
164     echo "$host"
165 done
166
167 echo
168
169 if $persistent; then
170     echo '***Setting up persistent SSH configuration between all nodes'
171     persistentSetup
172     echo $'\n*** Sucessfully set up ssh throughout the cluster!'
173
174 else
175     echo '*** Setting up temporary SSH configuration between all nodes'
176     tempSetup
177     echo $'\n***Finished temporary setup. When you are done with your cluster'
178     echo $'   session, tear down the SSH connections with'
179     echo $'   ./clustersetup.sh -c '$hosts''
180 fi
181
182 echo