You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
354 lines
10 KiB
354 lines
10 KiB
3 years ago
|
<?php
|
||
|
/*
|
||
|
* @Author: witersen
|
||
|
* @Date: 2022-04-24 23:37:05
|
||
|
* @LastEditors: witersen
|
||
3 years ago
|
* @LastEditTime: 2022-05-06 21:38:12
|
||
3 years ago
|
* @Description: QQ:1801168257
|
||
|
*/
|
||
|
|
||
3 years ago
|
namespace app\service;
|
||
3 years ago
|
|
||
3 years ago
|
class Svn extends Base
|
||
|
{
|
||
|
function __construct()
|
||
|
{
|
||
|
parent::__construct();
|
||
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
|
* 获取Subversion运行状态 用于页头提醒
|
||
|
*/
|
||
3 years ago
|
public function GetStatus()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = FunShellExec("ps auxf | grep -v 'grep' | grep svnserve");
|
||
3 years ago
|
$result = $result['result'];
|
||
|
|
||
|
if ($result == '') {
|
||
|
return message(200, 0, 'svnserve服务未在运行,SVN用户将无法使用仓库浏览功能');
|
||
|
} else {
|
||
|
return message();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取Subversion的检出地址前缀
|
||
|
*
|
||
|
* 先从Subversion配置文件获取绑定端口和主机
|
||
|
* 然后与listen.json配置文件中的端口和主机进行对比和同步
|
||
|
*/
|
||
3 years ago
|
public function GetCheckout()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = $this->GetSvnserveListen();
|
||
3 years ago
|
$checkoutHost = $result[$result['enable']];
|
||
|
if ($result['bindPort'] != 3690) {
|
||
|
$checkoutHost .= ':' . $result['bindPort'];
|
||
|
}
|
||
|
return message(200, 1, '成功', [
|
||
|
'protocal' => 'svn://',
|
||
|
'prefix' => $checkoutHost
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取Subversion的详细信息
|
||
|
*/
|
||
3 years ago
|
public function GetDetail()
|
||
3 years ago
|
{
|
||
|
//获取绑定主机、端口等信息
|
||
3 years ago
|
$bindInfo = $this->GetSvnserveListen();
|
||
3 years ago
|
|
||
|
//检测运行信息
|
||
3 years ago
|
$runInfo = FunShellExec('ps auxf|grep -v "grep"|grep svnserve');
|
||
3 years ago
|
$runInfo = $runInfo['result'];
|
||
|
|
||
|
//检测安装信息
|
||
|
$installInfo = file_exists('/usr/bin/svnserve');
|
||
|
|
||
|
//检测安装状态
|
||
|
//未知状态
|
||
|
$installInfo = -1;
|
||
|
if ($runInfo == '' && !$installInfo) {
|
||
|
//未安装
|
||
|
$installed = 0;
|
||
|
} else if ($runInfo == '' && $installInfo) {
|
||
|
//安装未启动
|
||
|
$installed = 1;
|
||
|
} else if ($runInfo != '' && $installInfo) {
|
||
|
//运行中
|
||
|
$installed = 2;
|
||
|
}
|
||
|
|
||
|
//检测subversion版本
|
||
|
$version = '-';
|
||
|
if ($installed != 0) {
|
||
3 years ago
|
$versionInfo = FunShellExec('svnserve --version');
|
||
3 years ago
|
$versionInfo = $versionInfo['result'];
|
||
3 years ago
|
preg_match_all($this->config_reg['REG_SUBVERSION_VERSION'], $versionInfo, $versionInfoPreg);
|
||
3 years ago
|
if (array_key_exists(0, $versionInfoPreg[0])) {
|
||
|
$version = trim($versionInfoPreg[1][0]);
|
||
|
} else {
|
||
|
$version = '-';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return message(200, 1, '成功', [
|
||
|
'version' => $version,
|
||
|
'installed' => $installed,
|
||
|
'bindPort' => (int)$bindInfo['bindPort'],
|
||
|
'bindHost' => $bindInfo['bindHost'],
|
||
|
'manageHost' => $bindInfo['manageHost'],
|
||
|
'enable' => $bindInfo['enable'],
|
||
3 years ago
|
'svnserveLog' => $this->config_svn['svnserve_log_file']
|
||
3 years ago
|
]);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* 获取svnserve端口和主机情况
|
||
|
*
|
||
|
* 先从svnserve配置文件获取绑定端口和主机
|
||
|
* 然后向数据库同步
|
||
|
*
|
||
|
* 绑定端口
|
||
|
* 绑定地址
|
||
|
* 管理地址
|
||
|
* 检出地址的启用地址
|
||
|
*/
|
||
|
public function GetSvnserveListen()
|
||
|
{
|
||
|
$bindPort = '';
|
||
|
$bindHost = '';
|
||
|
|
||
|
$svnserveContent = FunShellExec(sprintf("cat '%s'", $this->config_svn['svnserve_env_file']));
|
||
|
$svnserveContent = $svnserveContent['result'];
|
||
|
|
||
|
//匹配端口
|
||
|
if (preg_match('/--listen-port[\s]+([0-9]+)/', $svnserveContent, $portMatchs) != 0) {
|
||
|
$bindPort = trim($portMatchs[1]);
|
||
|
}
|
||
|
|
||
|
//匹配地址
|
||
|
if (preg_match('/--listen-host[\s]+([\S]+)\b/', $svnserveContent, $hostMatchs) != 0) {
|
||
|
$bindHost = trim($hostMatchs[1]);
|
||
|
}
|
||
|
|
||
|
$svnserve_listen = $this->database->get('options', [
|
||
|
'option_value'
|
||
|
], [
|
||
|
'option_name' => 'svnserve_listen'
|
||
|
]);
|
||
|
|
||
|
$insert = [
|
||
|
"bindPort" => $bindPort == '' ? 3690 : $bindPort,
|
||
|
"bindHost" => $bindHost == '' ? '0.0.0.0' : $bindHost,
|
||
|
"manageHost" => "127.0.0.1",
|
||
|
"enable" => "manageHost"
|
||
|
];
|
||
|
|
||
|
if ($svnserve_listen == null) {
|
||
|
//插入
|
||
|
$this->database->insert('options', [
|
||
|
'option_name' => 'svnserve_listen',
|
||
|
'option_value' => serialize($insert),
|
||
|
'option_description' => ''
|
||
|
]);
|
||
|
} else if ($svnserve_listen['option_value'] == '') {
|
||
|
//更新
|
||
|
$this->database->update('options', [
|
||
|
'option_value' => serialize($insert),
|
||
|
], [
|
||
|
'option_name' => 'svnserve_listen',
|
||
|
]);
|
||
|
} else {
|
||
|
//更新
|
||
|
$svnserve_listen = unserialize($svnserve_listen['option_value']);
|
||
|
$insert['manageHost'] = $svnserve_listen['manageHost'] == '' ? '127.0.0.1' : $svnserve_listen['manageHost'];
|
||
|
$insert['enable'] = $svnserve_listen['enable'] == '' ? 'manageHost' : $svnserve_listen['enable'];
|
||
|
$this->database->update('options', [
|
||
|
'option_value' => serialize($insert),
|
||
|
], [
|
||
|
'option_name' => 'svnserve_listen',
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
return $insert;
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* 安装SVN
|
||
|
*/
|
||
3 years ago
|
public function Install()
|
||
3 years ago
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 卸载SVN
|
||
|
*/
|
||
3 years ago
|
public function UnInstall()
|
||
3 years ago
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 启动SVN
|
||
|
*/
|
||
3 years ago
|
public function Start()
|
||
3 years ago
|
{
|
||
3 years ago
|
FunShellExec("systemctl start svnserve");
|
||
3 years ago
|
return message();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 停止SVN
|
||
|
*/
|
||
3 years ago
|
public function Stop()
|
||
3 years ago
|
{
|
||
3 years ago
|
FunShellExec("systemctl stop svnserve");
|
||
3 years ago
|
return message();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 修改svnserve的绑定端口
|
||
|
*/
|
||
3 years ago
|
public function EditPort()
|
||
3 years ago
|
{
|
||
|
//port不能为空
|
||
|
|
||
|
//获取现在的端口与要修改的端口对比检查是否相同
|
||
3 years ago
|
$result = $this->GetSvnserveListen();
|
||
3 years ago
|
|
||
|
if ($this->payload['bindPort'] == $result['bindPort']) {
|
||
|
return message(200, 0, '无需更换,端口相同');
|
||
|
}
|
||
|
|
||
|
//停止svnserve
|
||
3 years ago
|
FunShellExec('systemctl stop svnserve');
|
||
3 years ago
|
|
||
|
//重新构建配置文件内容
|
||
3 years ago
|
$config = sprintf("OPTIONS=\"-r '%s' --config-file '%s' --log-file '%s' --listen-port %s --listen-host %s\"", $this->config_svn['rep_base_path'], $this->config_svn['svn_conf_file'], $this->config_svn['svnserve_log_file'], $this->payload['bindPort'], $result['bindHost']);
|
||
3 years ago
|
|
||
|
//写入配置文件
|
||
3 years ago
|
FunShellExec('echo \'' . $config . '\' > ' . $this->config_svn['svnserve_env_file']);
|
||
3 years ago
|
|
||
|
//启动svnserve
|
||
3 years ago
|
$result = FunShellExec('systemctl start svnserve');
|
||
3 years ago
|
|
||
|
if ($result['resultCode'] != 0) {
|
||
|
return message(200, 0, '启动异常' . $result['error']);
|
||
|
} else {
|
||
|
return message();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 修改svnserve的绑定主机
|
||
|
*/
|
||
3 years ago
|
public function EditHost()
|
||
3 years ago
|
{
|
||
|
//host不能为空
|
||
|
//不能带前缀如http或者https
|
||
|
|
||
|
//获取现在的绑定主机与要修改的主机对比检查是否相同
|
||
3 years ago
|
$result = $this->GetSvnserveListen();
|
||
3 years ago
|
|
||
|
if ($this->payload['bindHost'] == $result['bindHost']) {
|
||
|
return message(200, 0, '无需更换,地址相同');
|
||
|
}
|
||
|
|
||
|
//停止svnserve
|
||
3 years ago
|
FunShellExec('systemctl stop svnserve');
|
||
3 years ago
|
|
||
|
//重新构建配置文件内容
|
||
3 years ago
|
$config = sprintf("OPTIONS=\"-r '%s' --config-file '%s' --log-file '%s' --listen-port %s --listen-host %s\"", $this->config_svn['rep_base_path'], $this->config_svn['svn_conf_file'], $this->config_svn['svnserve_log_file'], $result['bindPort'], $this->payload['bindHost']);
|
||
3 years ago
|
|
||
|
//写入配置文件
|
||
3 years ago
|
FunShellExec('echo \'' . $config . '\' > ' . $this->config_svn['svnserve_env_file']);
|
||
3 years ago
|
|
||
|
//启动svnserve
|
||
3 years ago
|
$result = FunShellExec('systemctl start svnserve');
|
||
3 years ago
|
|
||
|
if ($result['resultCode'] != 0) {
|
||
|
return message(200, 0, '启动异常' . $result['error']);
|
||
|
} else {
|
||
|
return message();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 修改管理系统主机名
|
||
|
*/
|
||
3 years ago
|
public function EditManageHost()
|
||
3 years ago
|
{
|
||
|
//不能为空
|
||
|
//不能带前缀如http或者https
|
||
|
|
||
3 years ago
|
$result = $this->GetSvnserveListen();
|
||
3 years ago
|
|
||
|
if ($this->payload['manageHost'] == $result['manageHost']) {
|
||
|
return message(200, 0, '无需更换,地址相同');
|
||
|
}
|
||
|
|
||
|
//更新
|
||
|
$result['manageHost'] = $this->payload['manageHost'];
|
||
|
$this->database->update('options', [
|
||
|
'option_value' => serialize($result),
|
||
|
], [
|
||
|
'option_name' => 'svnserve_listen',
|
||
|
]);
|
||
|
|
||
|
return message();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 修改检出地址
|
||
|
*/
|
||
3 years ago
|
public function EditEnable()
|
||
3 years ago
|
{
|
||
3 years ago
|
$result = $this->GetSvnserveListen();
|
||
3 years ago
|
|
||
|
//enable的值可为 manageHost、bindHost
|
||
|
|
||
|
//更新
|
||
|
$result['enable'] = $this->payload['enable'];
|
||
|
$this->database->update('options', [
|
||
|
'option_value' => serialize($result),
|
||
|
], [
|
||
|
'option_name' => 'svnserve_listen',
|
||
|
]);
|
||
|
|
||
|
return message();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取配置文件列表
|
||
|
*/
|
||
3 years ago
|
public function GetConfig()
|
||
3 years ago
|
{
|
||
|
return message(200, 1, '成功', [
|
||
|
[
|
||
|
'key' => '仓库父目录',
|
||
3 years ago
|
'value' => $this->config_svn['rep_base_path']
|
||
3 years ago
|
],
|
||
|
[
|
||
|
'key' => '仓库配置文件',
|
||
3 years ago
|
'value' => $this->config_svn['svn_conf_file']
|
||
3 years ago
|
],
|
||
|
[
|
||
|
'key' => '仓库权限文件',
|
||
3 years ago
|
'value' => $this->config_svn['svn_authz_file']
|
||
3 years ago
|
],
|
||
|
[
|
||
|
'key' => '用户账号文件',
|
||
3 years ago
|
'value' => $this->config_svn['svn_passwd_file']
|
||
3 years ago
|
],
|
||
|
[
|
||
|
'key' => '备份目录',
|
||
3 years ago
|
'value' => $this->config_svn['backup_base_path']
|
||
3 years ago
|
],
|
||
|
[
|
||
|
'key' => 'svnserve环境变量文件',
|
||
3 years ago
|
'value' => $this->config_svn['svnserve_env_file']
|
||
3 years ago
|
],
|
||
|
]);
|
||
|
}
|
||
|
}
|