Skip to content

Commit

Permalink
在有svn扩展的时候,使用svn扩展
Browse files Browse the repository at this point in the history
:)
  • Loading branch information
xsir317 committed Nov 28, 2014
1 parent 2afdaf0 commit 2b97970
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 86 deletions.
50 changes: 2 additions & 48 deletions publisher/app/commands/TaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,59 +52,13 @@ public function fire()
$tasks = Tasks::where('status' , 'created')->lists('id');
foreach ($tasks as $_id)
{
$this->_run($_id);
//TaskHelper
Task::run($_id);
}
sleep(1);
}
}

private function _run($id)
{
$task = Tasks::find($id);
//如果task不存在或者状态不对
if(!$task || $task->status != 'created')
{
return;
}
//如果task有前置任务,而且前置任务没完成
if($task->pre_task && $task->pre()->status != 'success')
{
return;
}
$task->status = 'execute';
$task->save();
$_func = sprintf('_run_%s',$task->type);
$result = $this->$_func($task);
$task->execute_time = date('Y-m-d H:i:s');
$task->status = $result['result'] ? 'success':'failed';
$task->output = $result['output'];
$task->save();
}

private function _run_checkout($task)
{
//如果目录非空,失败
//checkout到指定目录
//返回
}

private function _run_update($task)
{
//根据命令,运行update指令
//返回
}

private function _run_delete($task)
{
//清空指定目录
}

private function _run_rsync($task)
{
//运行rsync
}


/**
* Get the console command arguments.
*
Expand Down
104 changes: 69 additions & 35 deletions publisher/app/lib/TaskHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public function create($action,$uid,$taskdata,$pre_task=0)
* 任务种类 'checkout', 'update', 'delete', 'rsync'
*
*/
public function run($task)
public function run($task_id)
{
$task = \Tasks::find($task_id);
if($task->status != 'created')
{
return $this->err('供执行的任务必须是初始状态');
Expand All @@ -87,10 +88,35 @@ public function run($task)
*/
private function _runCheckout($task)
{
//get the project record
//get the project source path
//run the checkout command
//write the output to db, and status
$currdir = getcwd();
//如果目录非空,失败
if($task->project_id)
{
$dir = Project::getTempDir($task->project_id);
if(!file_exists($dir))
{
if(!mkdir($dir,0750))
{
return array('result'=>false,'output'=>"mkdir $dir failed!");
}
}
$project = Project::find($project_id);
switch ($project->vcs_type) {
case 'svn':
$command = "svn checkout {$project->svn_addr} ./ ";
$command .= " --no-auth-cache --username={$project->username} --password={$project->password}";
$output = shell_exec($command);
break;
case 'git':
break;
default:
# code...
break;
}
}
//checkout到指定目录
chdir($currdir);
//返回
}

/**
Expand Down Expand Up @@ -122,20 +148,6 @@ private function _runRsync($task)

}

/**
*
* 获得指定id的project的目录路径
*/
private function _get_work_path($project_id)
{
$path = app_path().'/storage/project_base_'.$project_id;
if(!file_exists($path))
{
mkdir($path);
}
return $path;
}

/**
*
* 调用svn或git的log,获取版本号和更新文字日志
Expand All @@ -149,28 +161,50 @@ public function get_log($src_path,$type='svn',$auth=null,$limit=10,$last='')
{
//如果last没指定,则取最新limit个
//如果指定了,则取limit+1个,去掉last
$out = array();
switch ($type) {
case 'svn':
$cmd = "svn log {$src_path} --xml --non-interactive --stop-on-copy";
if(!empty($auth))
if(function_exists('svn_log'))
{
$cmd .= " --username {$auth['username']} --password {$auth['password']}";
}
if($last)
{
$cmd .= " -r {$last}:1 --limit ".($limit +1);
if($auth)
{
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $auth['username']);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $auth['password']);
}
if($last)
{
$_return = svn_log($src_path,$last,1,($limit+1),SVN_STOP_ON_COPY);
}
else
{
$_return = svn_log($src_path,null,null,$limit,SVN_STOP_ON_COPY);
}
foreach ($_return as $_log) {
$out[$_log['rev']] = "{$_log['author']}:{$_log['msg']}";
}
}
else
{
$cmd .= " --limit {$limit}";
}
$cmdresult = `$cmd`;
/*//$cmdresult = '<?xml version="1.0" encoding="UTF-8"?><log><logentry revision="530"><author>测试用户</author><msg>测试文字</msg></logentry><logentry revision="529"><author>测试用户</author><msg>测试文字</msg></logentry></log>';*/
$loadxml = simplexml_load_string(trim($cmdresult), 'SimpleXMLElement', LIBXML_NOCDATA);
$out = array();
foreach($loadxml->children() as $elem)
{
$out[(string)$elem->attributes()->revision] = $elem->author.":".$elem->msg;
$cmd = "svn log {$src_path} --xml --non-interactive --stop-on-copy";
if(!empty($auth))
{
$cmd .= " --username {$auth['username']} --password {$auth['password']}";
}
if($last)
{
$cmd .= " -r {$last}:1 --limit ".($limit +1);
}
else
{
$cmd .= " --limit {$limit}";
}
$cmdresult = shell_exec($cmd);
/*//$cmdresult = '<?xml version="1.0" encoding="UTF-8"?><log><logentry revision="530"><author>测试用户</author><msg>测试文字</msg></logentry><logentry revision="529"><author>测试用户</author><msg>测试文字</msg></logentry></log>';*/
$loadxml = simplexml_load_string(trim($cmdresult), 'SimpleXMLElement', LIBXML_NOCDATA);
foreach($loadxml->children() as $elem)
{
$out[(string)$elem->attributes()->revision] = $elem->author.":".$elem->msg;
}
}
if($last && isset($out[$last])) unset($out[$last]);
return $out;
Expand Down
6 changes: 6 additions & 0 deletions publisher/app/models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function servers()
return $this->hasMany('Server','project_id','id');
}

//project临时目录
public function getTempDir($_id)
{
return app_path().'/storage/pjfolder_'.intval($_id);
}

public function getUsernameAttribute()
{
if(!$this->auth_info) return '';
Expand Down
29 changes: 29 additions & 0 deletions publisher/app/models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,33 @@ public function project()
{
return $this->belongsTo('Project','project_id');
}

// 测试服务器通迅
// public static function pingServers($ip)
// {
// $result = array();
// $fp = @fsockopen($ip, 873, $errno, $errstr, 5);
// if (!$fp)
// {
// $result['result'] = false;
// $result['msg'] = "failure: $errstr ($errno)";
// }
// else
// {
// fwrite($fp, "\n");
// $ret = fread($fp, 8192);
// if(preg_match('/RSYNCD:\s*\d+/',$ret))
// {
// $result['result'] = true;
// $result['msg'] = "success: ".$ret;
// }
// elseif (preg_match ("/@ERROR:/i", $ret))
// {
// $result['result'] = false;
// $result['msg'] = "error: ".$ret;
// }
// fclose($fp);
// }
// return $result;
// }
}
25 changes: 25 additions & 0 deletions publisher/app/models/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,29 @@ public function pre()
{
return $this->belongsTo('Tasks','pre_task');
}

public function getProjectIdAttribute()
{
$_meta = json_decode($this->command,true);
if(isset($_meta['project_id']))
return $_meta['project_id'];
return null;
}

public function getServerIdAttribute()
{
$_meta = json_decode($this->command,true);
if(isset($_meta['server_id']))
return $_meta['server_id'];
return null;
}

public function getVersionAttribute()
{
$_meta = json_decode($this->command,true);
if(isset($_meta['version']))
return $_meta['version'];
return null;
}

}
10 changes: 7 additions & 3 deletions publisher/app/views/projects/publish.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@
$('<ul class="uk-list"></ul>').appendTo(version_log_box);
}
var _ul = version_log_box.find("ul");
for (var _key in _data.logs ) {
//由于js的for in 问题,临时采用数组处理这个排序问题
var _order = [];
for (var _key in _data.logs ) _order[_order.length] = _key;
_order.reverse();
for (var _key in _order ) {
$("<label/>").append(
$("<input/>").attr({
type: 'radio',
value: _key,
value: _order[_key],
name: "version_select"
})).append("version:"+_key + " " + _data.logs[_key])
})).append("version:"+_order[_key] + " " + _data.logs[_order[_key]])
.appendTo($("<li/>")
.appendTo(_ul));
}
Expand Down

0 comments on commit 2b97970

Please sign in to comment.