From bd3fbc8c028711201f636280f2597eda9e92c56f Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 25 Oct 2023 11:56:30 +0300 Subject: [PATCH] Improving rsync related routines --- deploy.inc.php | 66 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/deploy.inc.php b/deploy.inc.php index 000a691..7e8d90a 100644 --- a/deploy.inc.php +++ b/deploy.inc.php @@ -272,6 +272,7 @@ function deploy_put_file($name, $path, $contents, $opts = 0) } } +//TODO: use deploy_scp_put_file_async(..) internally? function deploy_scp_put_file($name, $local_path, $remote_path, $opts = 0) { $decl = deploy_get_node($name); @@ -359,12 +360,13 @@ function deploy_scp_put_file_async($name, $local_path, $remote_path, $opts = 0) }); } -function deploy_rsync_async($name, $src_dir, $dst_dir, $rsync_opts = '', $opts = 0) +//TODO: use deploy_rsync_async(..) internally? +function deploy_rsync($name, $src, $dst, $rsync_opts = '', $opts = 0) { - return Amp\call(function() use($name, $src_dir, $dst_dir, $rsync_opts, $opts) { - $decl = deploy_get_node($name); + $dst = deploy_str($decl, $dst); + $tmp_key_file = tempnam("/tmp", "ssh_"); $key_str = $decl->get('ssh_key_str'); @@ -372,13 +374,65 @@ function deploy_rsync_async($name, $src_dir, $dst_dir, $rsync_opts = '', $opts = { file_put_contents($tmp_key_file, $key_str); - $ssh_transport = "ssh -A -o StrictHostKeyChecking=no -o ConnectTimeout=90 -o ConnectionAttempts=30 -i $tmp_key_file"; + foreach($decl->get('hosts') as $host) + { + list($ssh_host, $ssh_port) = deploy_ssh_host_port($host); + + $ssh_transport = "-e 'ssh -A -o StrictHostKeyChecking=no -o ConnectTimeout=90 -o ConnectionAttempts=30 -i $tmp_key_file -p $ssh_port'"; + $ssh_host_prefix = $decl->get('user') . '@' . $ssh_host . ':'; + + if($ssh_host === 'localhost') + { + $ssh_transport = ''; + $ssh_host_prefix = ''; + } + + $cmd = "rsync $ssh_transport -a $rsync_opts $src {$ssh_host_prefix}$dst"; + + if(($opts & DEPLOY_OPT_SILENT) == 0) + echo "[RSN] $host: $cmd\n"; + + system($cmd, $ret); + + if($ret !== 0) + throw new Exception("Could not rsync $src to remote $dst: $ret"); + } + } + finally + { + unlink($tmp_key_file); + } +} + +function deploy_rsync_async($name, $src, $dst, $rsync_opts = '', $opts = 0) +{ + return Amp\call(function() use($name, $src, $dst, $rsync_opts, $opts) { + + $decl = deploy_get_node($name); + + $dst = deploy_str($decl, $dst); + + $tmp_key_file = tempnam("/tmp", "ssh_"); + $key_str = $decl->get('ssh_key_str'); + + try + { + file_put_contents($tmp_key_file, $key_str); foreach($decl->get('hosts') as $host) { list($ssh_host, $ssh_port) = deploy_ssh_host_port($host); - $proc_cmd = "rsync -e '" . $ssh_transport . " -p $ssh_port' -a $rsync_opts $src_dir/ " . $decl->get('user') . '@' . $ssh_host . ":$dst_dir/"; + $ssh_transport = "-e 'ssh -A -o StrictHostKeyChecking=no -o ConnectTimeout=90 -o ConnectionAttempts=30 -i $tmp_key_file -p $ssh_port'"; + $ssh_host_prefix = $decl->get('user') . '@' . $ssh_host . ':'; + + if($ssh_host === 'localhost') + { + $ssh_transport = ''; + $ssh_host_prefix = ''; + } + + $proc_cmd = "rsync $ssh_transport -a $rsync_opts $src {$ssh_host_prefix}$dst"; if(($opts & DEPLOY_OPT_SILENT) == 0) echo "[RSN] $host: $proc_cmd\n"; @@ -394,7 +448,7 @@ function deploy_rsync_async($name, $src_dir, $dst_dir, $rsync_opts = '', $opts = $status = yield $proc->join(); if($status !== 0) - throw new Exception("Could not rsync $src_dir to remote $dst_dir: $status"); + throw new Exception("Could not rsync $src to remote $dst: $status"); } } finally