/**
* 将多个文件/目录通过ansible传输到指定的多个目标机
*
* ansible 不支持 rsync模块, 改用宿主机 tar 打包, ansible 并发传输到目标机临时目录, 目标机解压
*
* @param $version
* @param string $files 相对仓库文件/目录路径, 空格分割
* @param array $remoteHosts
*/
public function copyFiles($version, $files = '*', $remoteHosts = [])
{
// 1. 打包
$excludes = GlobalHelper::str2arr($this->getConfig()->excludes);
$packagePath = Project::getDeployPackagePath($version);
$packageCommand = sprintf('cd %s && tar -p %s -cz -f %s %s', escapeshellarg(rtrim(Project::getDeployWorkspace($version), '/') . '/'), $this->excludes($excludes), escapeshellarg($packagePath), $files);
$ret = $this->runLocalCommand($packageCommand);
if (!$ret) {
return false;
}
// 2. 传输文件
$releasePackage = Project::getReleaseVersionPackage($version);
$ret = $this->copyFilesByAnsibleCopy($packagePath, $releasePackage);
if (!$ret) {
return false;
}
// 3. 解压
$releasePath = Project::getReleaseVersionDir($version);
$unpackageCommand = sprintf('cd %1$s && tar --no-same-owner -pm -C %1$s -xz -f %2$s', $releasePath, $releasePackage);
$ret = $this->runRemoteCommandByAnsibleShell($unpackageCommand);
return $ret;
}