Skip to content

Commit

Permalink
测试环境支持分支选择、项目配置模块拆分
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuiyong committed Sep 28, 2015
1 parent 37cd0fc commit ddb3fed
Show file tree
Hide file tree
Showing 32 changed files with 1,688 additions and 495 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,31 @@ composer install # 如果缺少bower-asset的话, 先安装:composer instal
快速开始
-------
* 首先,配置邮箱,如果没有,好吧,先忽略,注册完手动修改user表的is_email_verified=1即可登录
```php
vi config/params.php
'mail-suffix' => '公司邮箱后缀.com'

vi config/web.php +20
# 配置mail smtp模块
```php
vi config/params.php
'mail-suffix' => [
'公司邮箱后缀.com',
]

vi config/web.php +20
# 配置mail smtp模块
'host' => 'ip or host', # smtp 发件地址
'username' => '[email protected]', # smtp 发件用户名
'password' => 'password', # smtp 发件人的密码
'port' => 25, # smtp 端口
'encryption' => 'tls', # smtp 协议
```
* 注册一个管理员身份用户,[配置一个项目](https://github.com/meolu/walle-web/blob/master/yml-config.md)
* 有公司邮箱的开发者注册,提交上线任务
* 注册一个管理员身份用户(已有`admin/admin`),[配置一个项目](https://github.com/meolu/walle-web/blob/master/yml-config.md)
* 有公司邮箱的开发者注册(已有`demo/demo`),提交上线任务
* 管理员审核上线任务
* 发起上线
* 开发者发起上线


To Do List
----------

* 部署出错详细信息优化
* 项目配置模块拆分
* 测试环境支持分支选择
* 项目的开发同学分组可见权限

截图
---
Expand All @@ -83,5 +84,5 @@ To Do List
瓦力的版本记录:[CHANGELOG](https://github.com/meolu/walle-web/blob/master/CHANGELOG.md)


## 有问题加群
## 交流群(有问必答)
**QQ:482939318**
180 changes: 180 additions & 0 deletions components/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/* *****************************************************************
* @Author: wushuiyong
* @Created Time : 五 7/31 22:42:32 2015
*
* @File Name: command/Command.php
* @Description:
* *****************************************************************/
namespace app\components;

use app\models\Conf;

abstract class Command {

protected static $LOGDIR = '';
/**
* Handler to the current Log File.
* @var mixed
*/
protected static $logFile = null;


/**
* Enables or Disables Logging
* @var boolean
*/
private static $logEnabled = true;

/**
* Config
* @var \walle\config\Config
*/
protected $config;

/**
* 命令运行返回值:0失败,1成功
* @var int
*/
protected $status = 1;

protected $command = '';

protected $log = null;


final protected function runLocalCommand($command) {
file_put_contents('/tmp/cmd', $command.PHP_EOL.PHP_EOL, 8);
$this->log('---------------------------------');
$this->log('---- Executing: $ ' . $command);

$status = 1;
$log = '';

exec($command . ' 2>&1', $log, $status);
// 执行过的命令
$this->command = $command;
// 执行的状态
$this->status = !$status;
// 操作日志
$log = implode(PHP_EOL, $log);
$this->log = trim($log);

$this->log($log);
$this->log('---------------------------------');

return $this->status;
}

final protected function runRemoteCommand($command) {
$this->log = '';
$needs_tty = ''; #($this->getConfig()->general('ssh_needs_tty', false) ? '-t' : '');

foreach (GlobalHelper::str2arr($this->getConfig()->hosts) as $remoteHost) {
$remoteHost = trim($remoteHost);
$localCommand = 'ssh ' . $needs_tty . ' -p 22 '
. '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no '
// . $this->getConfig()->getConnectTimeoutOption()
. ($this->getConfig()->release_user ? $this->getConfig()->release_user . '@' : '')
. $remoteHost;
$remoteCommand = str_replace('"', '\"', trim($command));
$localCommand .= ' "sh -c \"' . $remoteCommand . '\"" ';
static::log('Run remote command ' . $remoteCommand);

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

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

/**
* 加载配置
*
* @param $config
* @return $this
* @throws \Exception
*/
public function setConfig($config) {
if ($config) {
$this->config = $config;
} else {
throw new \Exception('未知的配置');
}
return $this;
}

/**
* 获取配置
* @return \walle\config\Config
*/
protected function getConfig() {
return $this->config;
}

/**
* rsync时,要排除的文件
*
* @param array $excludes
* @return string
*/
protected function excludes($excludes) {
$excludesRsync = '';
foreach ($excludes as $exclude) {
$excludesRsync .= ' --exclude=' . escapeshellarg($exclude) . ' ';
}

return trim($excludesRsync);
}

public static function log($message) {
if (empty(\Yii::$app->params['log.dir'])) return;

$logDir = \Yii::$app->params['log.dir'];
$logFile = realpath($logDir) . '/log-' . date('Ymd') . '.log';

if (self::$logFile === null && is_writable($logFile)) {
self::$logFile = fopen($logFile, 'w');
}

$message = date('Y-m-d H:i:s -- ') . $message;
fwrite(self::$logFile, $message . PHP_EOL);
}

/**
* 获取执行command
*
* @author wushuiyong
* @return string
*/
public function getExeCommand() {
return $this->command;
}

/**
* 获取执行log
*
* @author wushuiyong
* @return string
*/
public function getExeLog() {
return $this->log;
}

/**
* 获取执行log
*
* @author wushuiyong
* @return string
*/
public function getExeStatus() {
return $this->status;
}

public static function getMs() {
return intval(microtime(true) * 1000);
}

}
94 changes: 94 additions & 0 deletions components/Folder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/* *****************************************************************
* @Author: wushuiyong
* @Created Time : 五 7/31 22:21:23 2015
*
* @File Name: command/Folder.php
* @Description:
* *****************************************************************/
namespace app\components;

use app\models\Conf;

class Folder extends Command {


/**
* 初始化部署目录
*
* @return bool
*/
public function initDirector() {
$command = sprintf('mkdir -p %s',
rtrim($this->getConfig()->deploy_from, '/'));
return $this->runLocalCommand($command);
}

/**
* 目录、权限检查
*
* @author wushuiyong
* @param $log
* @return bool
*/
public function folderAndPermission($version) {
$command = sprintf('mkdir -p %s', Conf::getReleaseVersionDir($version));
return $this->runRemoteCommand($command);

}

/**
* rsync 同步文件
* 后续ssh -p参数的端口可以加入可配置,可能会出现非22端口
*
* @param $remoteHost 远程host,格式:host 、host:port
* @return bool
*/
public function syncFiles($remoteHost, $version) {
$excludes = GlobalHelper::str2arr($this->getConfig()->excludes);
$command = 'rsync -avz ' . '--rsh="ssh '
. '-p 22 ' . '" '
. $this->excludes($excludes) . ' '
. rtrim(Conf::getDeployFromDir(), '/') . '/ '
. ($this->getConfig()->release_user ? $this->getConfig()->release_user . '@' : '')
. $remoteHost . ':' . Conf::getReleaseVersionDir($version);

return $this->runLocalCommand($command);
}

/**
* 打软链
*
* @param null $version
* @return bool
*/
public function link($version) {
$user = $this->getConfig()->release_user;
$project = Conf::getGitProjectName($this->getConfig()->git_url);
$currentTmp = sprintf('%s/%s/current-%s.tmp', rtrim($this->getConfig()->release_library, '/'), $project, $project);
// 遇到回滚,则使用回滚的版本version
$linkFrom = Conf::getReleaseVersionDir($version);
$cmd[] = sprintf('ln -sfn %s %s', $linkFrom, $currentTmp);
$cmd[] = sprintf('chown -h %s %s', $user, $currentTmp);
$cmd[] = sprintf('mv -fT %s %s', $currentTmp, $this->getConfig()->release_to);
$command = join(' && ', $cmd);

return $this->runRemoteCommand($command);
}

/**
* 获取文件的MD5
*
* @param $file
* @return bool
*/
public function getFileMd5($file) {
$cmd[] = "test -f /usr/bin/md5sum && md5sum {$file}";
$command = join(' && ', $cmd);

return $this->runRemoteCommand($command);
}


}

Loading

0 comments on commit ddb3fed

Please sign in to comment.