84
84
echo "`ch_unit_id $JUJU_UNIT_NAME`"
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]
93
# distribute a list of file to all slave of the peer relation
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)
106
# 1 when copy is complete on the slave side
107
# FALSE if an error was encountered
109
# This executes in multiple passes between the leader and each peer
112
# <------------------- set scp-hostname
113
# set scp-ssh-key ----------------->
115
# <------------------- set scp-ssh-key-saved
117
# set scp-copy-done --------------->
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
122
alias ch_peer_scp=ch_peer_copy
123
alias ch_peer_rsync='ch_peer_copy --rsync'
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"
131
if [ $# -eq 0 ]; then
133
juju-log "ch_peer_copy: please provide at least one argument (path)"
139
if ch_peer_i_am_leader ; then
140
juju-log "ch_peer_copy: This is our leader"
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"
149
local scp_options="-o StrictHostKeyChecking=no -B"
150
local rsync_options=""
152
local copy_command="scp"
154
while [ "$#" -gt 0 ];
158
scp_options="$scp_options -r"
163
scp_options="$scp_options -P $1"
164
rsync_options="$rsync_options -e 'ssh -p $1 -o StrictHostKeyChecking=no'"
167
"-o") # passthrough option
169
scp_options="$scp_options $1"
170
rsync_options="$rsync_options $1"
173
"--rsync") # rsync secure (-e ssh)
180
*) # should be a pair of file
181
if [ -e "`echo "$1" | sed 's/\*$//'`" ]; then
184
paths="$paths $sourcep $USER@$remote:$1"
185
juju-log "ch_peer_copy: paths found: $sourcep -> $1"
187
juju-log "ch_peer_copy: unknown option, skipping: $1"
193
if [ ! -n "$paths" ]; then
195
juju-log "ch_peer_copy: please provide at least one path"
199
if [ -n $remote ]; then
200
# We know where to send file to
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
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
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
217
relation-set scp-ssh-key="`cat $ssh_key_p/id_rsa.pub`"
225
else # Not the leader
226
juju-log "ch_peer_copy: This is a slave"
228
local scp_copy_done=`relation-get scp-copy-done`
229
local scp_ssh_key="`relation-get scp-ssh-key`"
231
if [ -n "$scp_copy_done" ] && [ $scp_copy_done = 1 ]; then
232
juju-log "ch_peer_copy: copy done, thanks"
235
if [ -n "$scp_ssh_key" ]; then
236
juju-log "ssh key dir: $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
244
juju-log "ch_peer_copy: ssh keys already saved, thanks"
245
relation-set scp-ssh-key-saved=1
247
chmod 600 "$ssh_key_p/authorized_keys"
249
juju-log "ch_peer_copy: ssh_keys not set yet, later"
250
relation-set scp-hostname=`unit-get private-address`
254
juju-log "ch_peer_copy: returning: $result"