Skip to content

Commit

Permalink
Merge pull request #159 from meolu/fix-ansible-tar-bug
Browse files Browse the repository at this point in the history
Fix 1.1.0 bug
  • Loading branch information
littlehz committed Apr 11, 2016
2 parents 2955a88 + 2675544 commit 1c5271f
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 59 deletions.
17 changes: 14 additions & 3 deletions components/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ final public function runLocalCommand($command) {
/**
* 执行远程目标机器命令
*
* @param $command
* @param string $command
* @param integer $delay 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用
* @return bool
*/
final public function runRemoteCommand($command) {
final public function runRemoteCommand($command, $delay = 0) {
$this->log = '';
$needTTY = '-T';

Expand All @@ -98,14 +99,24 @@ final public function runRemoteCommand($command) {
escapeshellarg($command)
);

if ($delay > 0) {
// 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用
static::log(sprintf('Sleep: %d s', $delay));
sleep($delay);
}

static::log('Run remote command ' . $command);

$log = $this->log;
$this->status = $this->runLocalCommand($localCommand);

$this->log = $log . (($log ? PHP_EOL : '') . $remoteHost . ' : ' . $this->log);
if (!$this->status) return false;
if (!$this->status) {
return false;
}

}

return true;
}

Expand Down
14 changes: 8 additions & 6 deletions components/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public function copyFiles($version, $files = '*', $remoteHosts = []) {
// 1. 打包
$excludes = GlobalHelper::str2arr($this->getConfig()->excludes);
$packagePath = Project::getDeployPackagePath($version);
$packageCommand = sprintf('cd %s && tar %s --preserve-permissions -czf %s %s',
$packageCommand = sprintf('cd %s && tar -p %s -cz -f %s %s',
escapeshellarg(rtrim(Project::getDeployWorkspace($version), '/') . '/'),
$this->excludes($excludes),
$packagePath,
escapeshellarg($packagePath),
$files
);
$ret = $this->runLocalCommand($packageCommand);
Expand All @@ -114,9 +114,10 @@ public function copyFiles($version, $files = '*', $remoteHosts = []) {

// 3. 解压
$releasePath = Project::getReleaseVersionDir($version);
$unpackageCommand = sprintf('tar --preserve-permissions --touch --no-same-owner -xzf %s -C %s',
$releasePackage,
$releasePath);
$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;
Expand Down Expand Up @@ -166,9 +167,10 @@ public function getFileMd5($file) {
* @return string
*/
protected function excludes($excludes) {

$excludesRsync = '';
foreach ($excludes as $exclude) {
$excludesRsync .= sprintf(" --exclude=%s ", escapeshellarg(trim($exclude)));
$excludesRsync .= sprintf("--exclude=%s ", escapeshellarg(trim($exclude)));
}


Expand Down
2 changes: 1 addition & 1 deletion components/Svn.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function getFileAndVersionList(TaskModel $task) {
* @return string
*/
private function _getSvnCmd($cmd) {
return sprintf('/usr/bin/env LC_ALL=C %s --username=%s --password=%s --non-interactive --trust-server-cert',
return sprintf('/usr/bin/env %s --username=%s --password=%s --non-interactive --trust-server-cert',
$cmd, escapeshellarg($this->config->repo_username), escapeshellarg($this->config->repo_password));
}

Expand Down
9 changes: 5 additions & 4 deletions components/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ public static function getRemoteTaskCommand($task, $version) {
* 执行远程服务器任务集合
* 对于目标机器更多的时候是一台机器完成一组命令,而不是每条命令逐台机器执行
*
* @param $tasks
* @param array $tasks
* @param integer $delay 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用
* @return mixed
*/
public function runRemoteTaskCommandPackage($tasks) {
public function runRemoteTaskCommandPackage($tasks, $delay = 0) {

$task = join(' && ', $tasks);

if (Project::getAnsibleStatus()) {
if (Project::getAnsibleStatus() && !$delay) {
// ansible 并发执行远程命令
return $this->runRemoteCommandByAnsibleShell($task);
} else {
return $this->runRemoteCommand($task);
return $this->runRemoteCommand($task, $delay);
}

}
Expand Down
9 changes: 3 additions & 6 deletions controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use app\components\Repo;

class TaskController extends Controller {

protected $task;

public function actionIndex($page = 1, $size = 10) {
Expand All @@ -27,8 +27,7 @@ public function actionIndex($page = 1, $size = 10) {
if ($auditProjects) {
$list->orWhere(['project_id' => $auditProjects]);
}



$kw = \Yii::$app->request->post('kw');
if ($kw) {
$list->andWhere(['or', "commit_id like '%" . $kw . "%'", "title like '%" . $kw . "%'"]);
Expand All @@ -45,7 +44,6 @@ public function actionIndex($page = 1, $size = 10) {
]);
}


/**
* 提交任务
*
Expand Down Expand Up @@ -108,8 +106,7 @@ public function actionSubmit($projectId = null) {
'projects' => $projects,
]);
}



/**
* 任务删除
*
Expand Down
9 changes: 5 additions & 4 deletions controllers/WalleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function actionStartDeploy() {
$this->_revisionUpdate();
$this->_postDeploy();
$this->_transmission();
$this->_updateRemoteServers($this->task->link_id);
$this->_updateRemoteServers($this->task->link_id, $this->conf->post_release_delay);
$this->_cleanRemoteReleaseVersion();
$this->_cleanUpLocal($this->task->link_id);
} else {
Expand Down Expand Up @@ -518,10 +518,11 @@ private function _rsync() {
* 执行远程服务器任务集合
* 对于目标机器更多的时候是一台机器完成一组命令,而不是每条命令逐台机器执行
*
* @param $version
* @param string $version
* @param integer $delay 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用
* @throws \Exception
*/
private function _updateRemoteServers($version) {
private function _updateRemoteServers($version, $delay = 0) {
$cmd = [];
// pre-release task
if (($preRelease = WalleTask::getRemoteTaskCommand($this->conf->pre_release, $version))) {
Expand All @@ -538,7 +539,7 @@ private function _updateRemoteServers($version) {

$sTime = Command::getMs();
// run the task package
$ret = $this->walleTask->runRemoteTaskCommandPackage($cmd);
$ret = $this->walleTask->runRemoteTaskCommandPackage($cmd, $delay);
// 记录执行时间
$duration = Command::getMs() - $sTime;
Record::saveRecord($this->walleTask, $this->task->id, Record::ACTION_UPDATE_REMOTE, $duration);
Expand Down
3 changes: 2 additions & 1 deletion messages/en/conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
'pre_deploy tip' => 'job before checkout for preparation. one command per line',
'post_deploy tip' => 'job for compile or adaptation after checkoutgit. java\'s compile,php\'s vendor, change config(mv config-test.php config.php). one command per line',
'pre_release tip' => 'job between synchronize and switching version. such as a task of stop java\'s service(Double quotes will be escaped as \")',
'post_release tip' => 'do some jobs for cleaning after deployment. such as clean cache, restart service(nginx、php、task).one command per line(Double quotes will be escaped as \")',
'post_release tip' => 'do some jobs for cleaning after deployment. such as clean cache, reload/restart service(nginx、php、task).one command per line(Double quotes will be escaped as \")',
'post_release_delay tip' => 'In order to do advanced tasks on each target server, and it will be paused x seconds on each target server. default set to 0.',
'branch tip' => 'Recommended options of test,maybe you would chose branch+commit',
'tag tip' => 'Recommended options of production',
'audit tip' => 'development bill should be audited before deploying',
Expand Down
4 changes: 2 additions & 2 deletions messages/en/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'all branches' => 'View All branches',
'select branch' => 'Select Branch',
'submit title' => 'Title',
'file list placeholder' => '1.Distribute all files of project:* 2.Distribute specified file:file_name 3.Distribute specified file of specified version:file_name commit_id',
'file list placeholder' => '1.Distribute all files of project(except beginning of .):*<br />2.Distribute specified directory:dir_name<br />3.Distribute specified file:file_name<br />4.Distribute specified file of specified version:file_name commit_id',
'file list' => 'File list',
'diff tip' => 'Auto get the diff files between two commit id of branch/tag',
];
];
5 changes: 4 additions & 1 deletion messages/zh-CN/conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
'pre_deploy tip' => '在部署代码之前的准备工作,如git的一些前置检查、vendor的安装(更新),一行一条',
'post_deploy tip' => 'git代码检出之后,可能做一些调整处理,如vendor拷贝,环境适配(mv config-test.php config.php),一行一条',
'pre_release tip' => '同步完所有目标机器之后,更改版本软链之前触发任务。java可能要做一些暂停服务的操作(双引号将会被转义为\")',
'post_release tip' => '所有目标机器都部署完毕之后,做一些清理工作,如删除缓存、重启服务(nginx、php、task),一行一条(双引号将会被转义为\")',
'post_release tip' => '所有目标机器都部署完毕之后,做一些清理工作,如删除缓存、平滑重载/重启服务(nginx、php、task),一行一条(双引号将会被转义为\")',
'post_release_delay tip' => '按顺序在每台目标机执行高级任务,每台服务器执行完毕后暂停x秒。
默认设置为 0,应用服务使用平滑重载,仅当应用服务无法支持平滑重载必须重启时才配置此参数。
设置为大于 0 的值会出现代码发布阶段各个服务器业务代码逻辑不一致的情况,请谨慎配置',
'branch tip' => '测试环境推荐选项,可以选择branch+commit',
'tag tip' => '生产环境推荐选项',
'audit tip' => '开启时,用户提交上线任务需要审核方可上线',
Expand Down
4 changes: 2 additions & 2 deletions messages/zh-CN/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'all branches' => '查看所有分支',
'select branch' => '版本选取',
'submit title' => '上线单标题',
'file list placeholder' => '1.上线全量文件:* <br />2.增量上线指定文件:file_name <br />3.增量上线指定文件的指定版本:file_name commit_id',
'file list placeholder' => '1.上线所有文件:* (不包含.开头的文件)<br />2.上线指定目录:dir_name<br />3.上线指定文件:file_name<br />4.上线指定文件的指定版本:file_name commit_id',
'file list' => '文件列表',
'diff tip' => '自动获取该分支/tag下的两提交历史间的文件',
];
];
2 changes: 1 addition & 1 deletion migrations/m160307_082032_ansible.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
public function down()
{
$this->dropColumn(Project::tableName(), 'ansible');
echo "m160307_082032_ansible cannot be reverted.\n";
echo "m160307_082032_ansible was reverted.\n";

return true;
}
Expand Down
33 changes: 33 additions & 0 deletions migrations/m160402_173643_add_post_release_delay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use app\models\Project;
use yii\db\Schema;
use yii\db\Migration;

class m160402_173643_add_post_release_delay extends Migration
{
public function up()
{
$this->addColumn(Project::tableName(), 'post_release_delay', Schema::TYPE_INTEGER . '(11) NOT NULL DEFAULT 0 COMMENT "每台目标机执行post_release任务间隔/延迟时间 单位:秒" AFTER post_release');

}

public function down()
{
$this->dropColumn(Project::tableName(), 'post_release_delay');
echo "m160402_173643_add_post_release_delay was reverted.\n";

return true;
}

/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
63 changes: 35 additions & 28 deletions models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
* @property integer $level
* @property integer $status
* @property string $version
* @property integer $created_at
* @property string $repo_url
* @property string $repo_username
* @property string $repo_password
* @property string $repo_mode
* @property string $repo_type
* @property string $deploy_from
* @property string $excludes
* @property string $release_user
Expand All @@ -26,12 +30,14 @@
* @property string $hosts
* @property string $pre_deploy
* @property string $post_deploy
* @property string $pre_release
* @property string $post_release
* @property string $repo_mode
* @property string $repo_type
* @property string $post_release_delay
* @property integer $audit
* @property integer $ansible
* @property integer $keep_version_num
* @property \DateTime $created_at
* @property \DateTime $updated_at
*/
class Project extends \yii\db\ActiveRecord
{
Expand Down Expand Up @@ -98,7 +104,7 @@ public function rules()
{
return [
[['user_id', 'repo_url', 'name', 'level', 'deploy_from', 'release_user', 'release_to', 'release_library', 'hosts', 'keep_version_num'], 'required'],
[['user_id', 'level', 'status', 'audit', 'ansible', 'keep_version_num'], 'integer'],
[['user_id', 'level', 'status', 'post_release_delay', 'audit', 'ansible', 'keep_version_num'], 'integer'],
[['excludes', 'hosts', 'pre_deploy', 'post_deploy', 'pre_release', 'post_release'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['name', 'repo_password'], 'string', 'max' => 100],
Expand All @@ -116,30 +122,31 @@ public function rules()
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'name' => '项目名字',
'level' => '环境级别',
'status' => 'Status',
'version' => 'Version',
'created_at' => 'Created At',
'deploy_from' => '检出仓库',
'excludes' => '排除文件列表',
'release_user' => '目标机器部署代码用户',
'release_to' => '代码的webroot',
'release_library' => '发布版本库',
'hosts' => '目标机器',
'pre_deploy' => '宿主机代码检出前置任务',
'post_deploy' => '宿主机同步前置任务',
'pre_release' => '目标机更新版本前置任务',
'post_release' => '目标机更新版本后置任务',
'repo_url' => 'git/svn地址',
'repo_username' => 'svn用户名',
'repo_password' => 'svn密码',
'repo_mode' => '分支/tag',
'audit' => '任务需要审核?',
'ansible' => '开启Ansible?',
'keep_version_num' => '线上版本保留数',
'id' => 'ID',
'user_id' => 'User ID',
'name' => '项目名字',
'level' => '环境级别',
'status' => 'Status',
'version' => 'Version',
'created_at' => 'Created At',
'deploy_from' => '检出仓库',
'excludes' => '排除文件列表',
'release_user' => '目标机器部署代码用户',
'release_to' => '代码的webroot',
'release_library' => '发布版本库',
'hosts' => '目标机器',
'pre_deploy' => '宿主机代码检出前置任务',
'post_deploy' => '宿主机同步前置任务',
'pre_release' => '目标机更新版本前置任务',
'post_release' => '目标机更新版本后置任务',
'post_release_delay' => '后置任务时间间隔/延迟',
'repo_url' => 'git/svn地址',
'repo_username' => 'svn用户名',
'repo_password' => 'svn密码',
'repo_mode' => '分支/tag',
'audit' => '任务需要审核?',
'ansible' => '开启Ansible?',
'keep_version_num' => '线上版本保留数',
];
}

Expand Down
8 changes: 8 additions & 0 deletions views/conf/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@
'onblur' => "clearInterval(this.clock);",
])
->label('post_release', ['class' => 'control-label bolder']) ?>
<?= $form->field($conf, 'post_release_delay')
->textInput([
'placeholder' => '0',
'data-placement' => 'top',
'data-rel' => 'tooltip',
'data-title' => yii::t('conf', 'post_release_delay tip'),
])
->label(yii::t('conf', 'post_release_delay'), ['class' => 'control-label bolder']) ?>
</div>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions views/conf/preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@
<span><?= str_replace(PHP_EOL, "<br>", $conf->post_release) ?></span>
</div>
</div>

<div class="profile-info-row">
<div class="profile-info-name"> <?= yii::t('conf', 'post_release_delay') ?> </div>

<div class="profile-info-value">
<span><?= str_replace(PHP_EOL, "<br>", $conf->post_release_delay) ?></span>
</div>
</div>
<!-- 目标机器 配置 end-->

<div class="profile-info-row">
Expand Down

0 comments on commit 1c5271f

Please sign in to comment.