|
|
@@ -1,233 +0,0 @@
|
|
|
-<?php
|
|
|
-namespace Proginn;
|
|
|
-
|
|
|
-use Exception;
|
|
|
-use Medoo\Medoo;
|
|
|
-use Proginn\Config;
|
|
|
-use Redis;
|
|
|
-
|
|
|
-class Start
|
|
|
-{
|
|
|
- const IP_LOCK_KEY = 'docker:ip:';
|
|
|
- /**
|
|
|
- * 数据库链接
|
|
|
- *
|
|
|
- * @var Medoo $connection
|
|
|
- *
|
|
|
- */
|
|
|
- protected static $connection;
|
|
|
- /**
|
|
|
- * 数据库链接
|
|
|
- *
|
|
|
- * @var Redis $redis
|
|
|
- *
|
|
|
- */
|
|
|
- protected static $redis;
|
|
|
- protected $startIP = 180936414; // 10.200.222.222
|
|
|
- protected $domainSuffix = '.test.proginn.com';
|
|
|
- protected $containersBasePath = '/workspace/project/';
|
|
|
-
|
|
|
- public function __construct($argv)
|
|
|
- {
|
|
|
- // 防止git仓库有人改了文件权限,容器里面shell脚本无法执行
|
|
|
- system("sudo chmod +x " . ROOT_DIR . '/shell/*');
|
|
|
- $params = $this->parseArgs($argv);
|
|
|
- $action = $params['action'];
|
|
|
- $name = "dev";
|
|
|
- echo $name;exit;
|
|
|
-
|
|
|
- switch ($action) {
|
|
|
- case 'start':
|
|
|
- $this->start($name);
|
|
|
- break;
|
|
|
- default:
|
|
|
- echo "Usage: php index.php --action=start|remove --name=mydev --proginn-branch=dev --rooter-branch=dev --proginn-frontend-branch dev\n";
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public function start($name)
|
|
|
- {
|
|
|
- if (preg_match('/[^a-zA-Z0-9_]+/', $name)) {
|
|
|
- echo "创建失败,容器名:{$name}只能字母、数字及下划线组成。\n";
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- $db = $this->getConnection();
|
|
|
- $row = $db->get('sites', '*', [
|
|
|
- 'name' => $name,
|
|
|
- ]);
|
|
|
- if (!empty($row)) {
|
|
|
- echo "创建失败,容器:{$name}已经存在,请确认。\n";
|
|
|
- return;
|
|
|
- }
|
|
|
- $ip = $this->getIp();
|
|
|
- $ipv4 = long2ip($ip);
|
|
|
- $domain = $name;
|
|
|
- $fullDomain = $name . $this->domainSuffix;
|
|
|
- $this->getConnection()->query('start transaction');
|
|
|
- try {
|
|
|
- $id = $this->getConnection()->insert('sites', [
|
|
|
- 'proginn_branch' => "dev",
|
|
|
- 'domain' => $domain,
|
|
|
- 'ip' => $ip,
|
|
|
- 'ipv4' => $ipv4,
|
|
|
- 'created_at' => time(),
|
|
|
- 'name' => $name,
|
|
|
- 'rooter_branch' => "dev",
|
|
|
- 'proginn_frontend_branch' => "dev",
|
|
|
- 'full_domain' => $fullDomain,
|
|
|
- ]);
|
|
|
- if (!$id) {
|
|
|
- throw new Exception('创建数据库记录失败');
|
|
|
- }
|
|
|
- // 容器存储目录
|
|
|
- $directory = $this->containersBasePath . $name;
|
|
|
-
|
|
|
- // 创建日志相关目录
|
|
|
- mkdir($directory . '/log/pm2', 0777, true); //
|
|
|
- mkdir($directory . '/log/proginn_cache', 0777, true); //
|
|
|
- // 配置存放目录
|
|
|
- mkdir($directory . '/config', 0777, true); //
|
|
|
- // yml 文件
|
|
|
- $template = file_get_contents(ROOT_DIR . '/dockerfile/template.yml');
|
|
|
- $template = str_replace('<containerName>', $name, $template);
|
|
|
- $template = str_replace('<ip>', $ipv4, $template);
|
|
|
- $template = str_replace('<proginn-branch>', "dev", $template);
|
|
|
- $template = str_replace('<rooter-branch>', "dev", $template);
|
|
|
- $template = str_replace('<proginn-frontend-branch>', "dev", $template);
|
|
|
- $template = str_replace('<domain>', $domain, $template);
|
|
|
- $template = str_replace('<fullDomain>', $fullDomain, $template);
|
|
|
- file_put_contents($directory . '/config/docker.yml', $template);
|
|
|
- // nginx 配置
|
|
|
- $proxy = file_get_contents(ROOT_DIR . '/config/nginx/template/proxy.nginx.conf');
|
|
|
- $proxy = str_replace('{{domain}}', $domain, $proxy);
|
|
|
- $proxy = str_replace('{{ip}}', $ipv4, $proxy);
|
|
|
- file_put_contents("/workspace/commonContainers/nginx/conf.d/{$name}.conf", $proxy);
|
|
|
-
|
|
|
- $proginn = file_get_contents(ROOT_DIR . '/config/nginx/template/proginn.nginx.conf');
|
|
|
- $proginn = str_replace('{{domain}}', $domain, $proginn);
|
|
|
- $proginn = str_replace('{{ip}}', $ipv4, $proginn);
|
|
|
- file_put_contents("{$directory}/config/nginx.conf", $proginn);
|
|
|
-
|
|
|
- $log = file_get_contents(ROOT_DIR . '/config/logrotate/log.conf');
|
|
|
- $log = str_replace('{{containerName}}', $name, $log);
|
|
|
- file_put_contents($directory . '/config/logrotate.conf', $log);
|
|
|
- system("sudo cp -f {$directory}/config/logrotate.conf /etc/logrotate.d/{$name}");
|
|
|
- // 写入项目变量 Found orphan containers
|
|
|
- file_put_contents($directory . '/config/.env', "COMPOSE_PROJECT_NAME={$name}");
|
|
|
- // 启动容器
|
|
|
- system("docker-compose -p {$name} -f {$directory}/config/docker.yml up -d");
|
|
|
- // 重载Nginx
|
|
|
- system("docker exec nginx nginx -s reload");
|
|
|
- $this->getConnection()->query('commit');
|
|
|
- } catch (\Throwable $e) {
|
|
|
- echo "exception:" . $e->__toString() . "\n";
|
|
|
- $this->getConnection()->query('rollback');
|
|
|
- if (file_exists($directory . '/config/docker.yml')) {
|
|
|
- system("docker-compose -p {$name} -f {$directory}/config/docker.yml rm -s -f");
|
|
|
- }
|
|
|
- $redis = $this->getRedis();
|
|
|
- $lockKey = static::IP_LOCK_KEY . $ip;
|
|
|
- $redis->del($lockKey);
|
|
|
-
|
|
|
- // 容器存储目录
|
|
|
- $directory = $this->containersBasePath . $name;
|
|
|
- system("sudo rm -rf {$directory}");
|
|
|
- system("rm -f /workspace/commonContainers/nginx/conf.d/{$name}.conf");
|
|
|
- system("docker exec nginx nginx -s reload");
|
|
|
- system("sudo rm -f /etc/logrotate.d/{$name}");
|
|
|
- }
|
|
|
- }
|
|
|
- protected function remove($name)
|
|
|
- {
|
|
|
- $db = $this->getConnection();
|
|
|
- $row = $db->get('sites', '*', [
|
|
|
- 'name' => $name,
|
|
|
- ]);
|
|
|
- if (empty($row)) {
|
|
|
- echo "删除失败,容器:{$name}不存在,请确认。\n";
|
|
|
- return;
|
|
|
- }
|
|
|
- $this->getConnection()->query('start transaction');
|
|
|
- try {
|
|
|
- // 容器存储目录
|
|
|
- $directory = $this->containersBasePath . $name;
|
|
|
- $db->delete('sites', [
|
|
|
- 'name' => $name,
|
|
|
- ]);
|
|
|
- if (file_exists($directory . '/config/docker.yml')) {
|
|
|
- system("docker-compose -p {$name} -f {$directory}/config/docker.yml rm -s -f");
|
|
|
- }
|
|
|
- // 容器存储目录
|
|
|
- $directory = $this->containersBasePath . $name;
|
|
|
- system("sudo rm -rf {$directory}");
|
|
|
- system("rm -f /workspace/commonContainers/nginx/conf.d/{$name}.conf");
|
|
|
- system("docker exec nginx nginx -s reload");
|
|
|
-
|
|
|
- $redis = $this->getRedis();
|
|
|
- $lockKey = static::IP_LOCK_KEY . $row['ip'];
|
|
|
- $redis->del($lockKey);
|
|
|
-
|
|
|
- $this->getConnection()->query('commit');
|
|
|
- } catch (\Throwable $e) {
|
|
|
- echo "exception:" . $e->__toString() . "\n";
|
|
|
- $this->getConnection()->query('rollback');
|
|
|
- }
|
|
|
- }
|
|
|
- protected function getIp()
|
|
|
- {
|
|
|
- $ip = $this->startIP;
|
|
|
- $redis = $this->getRedis();
|
|
|
- while (true) {
|
|
|
- $ip++;
|
|
|
- $lockKey = static::IP_LOCK_KEY . $ip;
|
|
|
- $lock = $redis->setnx($lockKey, $ip);
|
|
|
- if ($lock) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return $ip;
|
|
|
- }
|
|
|
- protected function parseArgs($argv)
|
|
|
- {
|
|
|
- unset($argv[0]);
|
|
|
- $params = [];
|
|
|
- foreach ($argv as $i => $arg) {
|
|
|
- $arg = explode('=', $arg);
|
|
|
- $key = trim($arg[0], "-");
|
|
|
- $val = $arg[1] ?? true;
|
|
|
- $params[$key] = $val;
|
|
|
- }
|
|
|
- return $params;
|
|
|
- }
|
|
|
- protected function getRedis()
|
|
|
- {
|
|
|
- if (static::$redis === null) {
|
|
|
- static::$redis = new Redis();
|
|
|
- static::$redis->connect(Config::REDIS_HOST, Config::REDIS_PORT, 3);
|
|
|
- static::$redis->auth(Config::REDIS_PASS);
|
|
|
- }
|
|
|
- return static::$redis;
|
|
|
- }
|
|
|
- /**
|
|
|
- * @return Medoo
|
|
|
- */
|
|
|
- protected function getConnection()
|
|
|
- {
|
|
|
- if (static::$connection === null) {
|
|
|
- static::$connection = new Medoo([
|
|
|
- 'database_type' => 'mysql',
|
|
|
- 'database_name' => Config::DB_NAME,
|
|
|
- 'server' => Config::DB_HOST,
|
|
|
- 'username' => Config::DB_USER,
|
|
|
- 'password' => Config::DB_PASS,
|
|
|
- 'charset' => Config::DB_CHAR,
|
|
|
- 'port' => Config::DB_PORT,
|
|
|
- 'prefix' => '',
|
|
|
- 'logging' => true,
|
|
|
- ]);
|
|
|
- }
|
|
|
- return static::$connection;
|
|
|
- }
|
|
|
-}
|