~charmers/charm-tools/trunk

« back to all changes in this revision

Viewing changes to helpers/sh/peer.sh

  • Committer: Nick Barcet
  • Date: 2011-12-22 07:02:09 UTC
  • mfrom: (92.2.16 charm-tools-scp)
  • Revision ID: nick.barcet@canonical.com-20111222070209-f81g9g4u02hzqyvp
mergingĀ lp:~nijaba/charm-tools/peer-scp

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
{
84
84
    echo "`ch_unit_id $JUJU_UNIT_NAME`"
85
85
}
 
86
 
 
87
 
 
88
##
 
89
# ch_peer_copy [-r|--rsync][-p <port>][-o "<opt>"] sourcepath1 destpath1 [... sourcepathN destpathN]
 
90
# ch_peer_scp [-r][-p <port>][-o "<opt>"] sourcepath1 destpath1 [... sourcepathN destpathN]
 
91
# ch_peer_rsync [-p <port>] sourcepath1 destpath1 [... sourcepathN destpathN]
 
92
#
 
93
# distribute a list of file to all slave of the peer relation
 
94
#
 
95
# param
 
96
#  -r            recursive scp copy (scp only, always on with rsync)
 
97
#  -p <port>     destination port to connect to
 
98
#  -o "<opt>"    any pathttrough options to the copy util
 
99
#  --rsync       use rsync instead of scp
 
100
#  sourcepath    path from which to copy (do not specify host, it will always
 
101
#                be coming from the leader of the peer relation)
 
102
#  destpath      path to which to copy (do not specify host, it will always
 
103
#                be the slaves of the peer relation)
 
104
#
 
105
# returns
 
106
#  1      when copy is complete on the slave side
 
107
#  FALSE  if an error was encountered
 
108
#
 
109
# This executes in multiple passes between the leader and each peer
 
110
#
 
111
#    LEADER                      SLAVE
 
112
#       <------------------- set scp-hostname
 
113
# set scp-ssh-key -----------------> 
 
114
#                            save shh-keys
 
115
#       <------------------- set scp-ssh-key-saved
 
116
# do file copy
 
117
# set scp-copy-done --------------->
 
118
#
 
119
# This function is idempotent and should be called for each JOINED or 
 
120
# CHANGED event for slave or leader in the peer relation exactly the same way
 
121
 
 
122
alias ch_peer_scp=ch_peer_copy
 
123
alias ch_peer_rsync='ch_peer_copy --rsync'
 
124
ch_peer_copy() {
 
125
  local USAGE="ERROR in $*
 
126
USAGE: ch_peer_scp [-r][-p <port>][-o \"<opt>\"] sourcepath1 destpath1 [... sourcepathN destpathN]
 
127
USAGE: ch_peer_rsync [-p <port>][-o \"<opt>\"] sourcepath1 destpath1 [... sourcepathN destpathN]"
 
128
  local ssh_key_p="$HOME/.ssh"
 
129
  local result=0
 
130
  
 
131
  if [ $# -eq 0 ]; then
 
132
    juju-log "$USAGE"
 
133
    juju-log "ch_peer_copy: please provide at least one argument (path)"
 
134
    exit 1
 
135
  fi
 
136
 
 
137
  ## LEADER ##
 
138
  
 
139
  if ch_peer_i_am_leader ; then
 
140
    juju-log "ch_peer_copy: This is our leader"
 
141
    
 
142
    local remote=`relation-get scp-hostname`
 
143
    local ssh_key_saved=`relation-get scp-ssh-key-saved`
 
144
    if [ -z $remote ]; then
 
145
      juju-log "ch_peer_copy: We do not have a remote hostname yet"
 
146
      remote=0
 
147
    fi  
 
148
  
 
149
    local scp_options="-o StrictHostKeyChecking=no -B"
 
150
    local rsync_options=""
 
151
    local paths=""
 
152
    local copy_command="scp"
 
153
    
 
154
    while [ "$#" -gt 0 ]; 
 
155
    do
 
156
      case "$1" in
 
157
      "-r") # scp recure
 
158
        scp_options="$scp_options -r"
 
159
        shift
 
160
        ;;
 
161
      "-p") # port number
 
162
        shift
 
163
        scp_options="$scp_options -P $1"
 
164
        rsync_options="$rsync_options -e 'ssh -p $1 -o StrictHostKeyChecking=no'"
 
165
        shift
 
166
        ;;
 
167
      "-o") # passthrough option
 
168
        shift
 
169
        scp_options="$scp_options $1"
 
170
        rsync_options="$rsync_options $1"
 
171
        shift
 
172
        ;;
 
173
      "--rsync") # rsync secure (-e ssh)
 
174
        copy_command="rsync"
 
175
        shift
 
176
        ;;
 
177
      "$0")
 
178
        shift
 
179
        ;;
 
180
      *) # should be a pair of file
 
181
        if [ -e "`echo "$1" | sed 's/\*$//'`" ]; then
 
182
          local sourcep="$1"
 
183
          shift
 
184
          paths="$paths $sourcep $USER@$remote:$1"
 
185
          juju-log "ch_peer_copy: paths found: $sourcep -> $1"
 
186
        else
 
187
          juju-log "ch_peer_copy: unknown option, skipping: $1"
 
188
        fi
 
189
        shift
 
190
        ;;
 
191
      esac
 
192
    done
 
193
    if [ ! -n "$paths" ]; then
 
194
      juju-log "$USAGE"
 
195
      juju-log "ch_peer_copy: please provide at least one path"
 
196
      exit 1
 
197
    fi
 
198
    
 
199
    if [ -n $remote ]; then
 
200
      # We know where to send file to
 
201
      
 
202
      case $ssh_key_saved in
 
203
      1) # ssh keys have been save, let's copy
 
204
        if [ x"$copy_command" = x"rsync" ]; then
 
205
          scp_options=$rsync_options
 
206
        fi
 
207
        juju-log "ch_peer_copy: $copy_command $scp_options $paths"
 
208
        eval "$copy_command $scp_options $paths"
 
209
        relation-set scp-copy-done=1
 
210
        ;;
 
211
        
 
212
      *) # we need to first distribute our ssh key files
 
213
        juju-log "ch_peer_copy: distributing ssh key"
 
214
        if [ ! -f "$ssh_key_p/id_rsa" ]; then
 
215
          ssh-keygen -q -N '' -t rsa -b 2048 -f $ssh_key_p/id_rsa
 
216
        fi
 
217
        relation-set scp-ssh-key="`cat $ssh_key_p/id_rsa.pub`"
 
218
        ;;
 
219
        
 
220
      esac
 
221
    fi 
 
222
    
 
223
  ## REMOTE ##
 
224
      
 
225
  else # Not the leader
 
226
    juju-log "ch_peer_copy: This is a slave"
 
227
 
 
228
    local scp_copy_done=`relation-get scp-copy-done`
 
229
    local scp_ssh_key="`relation-get scp-ssh-key`"
 
230
 
 
231
    if [ -n "$scp_copy_done" ] && [ $scp_copy_done = 1 ]; then
 
232
      juju-log "ch_peer_copy: copy done, thanks"
 
233
      result=1
 
234
    else
 
235
      if [ -n "$scp_ssh_key" ]; then
 
236
        juju-log "ssh key dir: $ssh_key_p"
 
237
        mkdir -p $ssh_key_p
 
238
        chmod 700 $ssh_key_p
 
239
        if ! grep -q -F "$scp_ssh_key" $ssh_key_p/authorized_keys ; then
 
240
          juju-log "ch_peer_copy: saving ssh key $scp_ssh_key"
 
241
          echo "$scp_ssh_key" >> $ssh_key_p/authorized_keys
 
242
          relation-set scp-ssh-key-saved=1
 
243
        else
 
244
          juju-log "ch_peer_copy: ssh keys already saved, thanks"
 
245
          relation-set scp-ssh-key-saved=1
 
246
        fi
 
247
        chmod 600 "$ssh_key_p/authorized_keys"
 
248
      else
 
249
        juju-log "ch_peer_copy: ssh_keys not set yet, later"
 
250
        relation-set scp-hostname=`unit-get private-address`
 
251
      fi 
 
252
    fi 
 
253
  fi 
 
254
  juju-log "ch_peer_copy: returning: $result"
 
255
  echo $result
 
256
}
 
257