From cedab83e0d95e0caff550a8d81cbb5d255a7d465 Mon Sep 17 00:00:00 2001 From: zhaoke Date: Mon, 7 Aug 2023 15:01:33 +0800 Subject: [PATCH] * Add auto insert pipeline data when install devops app. --- module/cne/model.php | 20 +++++- module/instance/config.php | 4 +- module/instance/control.php | 5 +- module/instance/model.php | 6 ++ module/instance/zen.php | 71 ++++++++++++++++++++++ module/pipeline/model.php | 27 ++++++++ module/space/control.php | 3 +- module/space/js/createapplication.ui.js | 16 +++-- module/space/ui/createapplication.html.php | 1 + 9 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 module/instance/zen.php diff --git a/module/cne/model.php b/module/cne/model.php index 493286c8c1..0e36b35e16 100644 --- a/module/cne/model.php +++ b/module/cne/model.php @@ -823,8 +823,26 @@ class cneModel extends model * @access public * @return object|null */ - public function getSettingsMapping($instance, $mappings) + public function getSettingsMapping($instance, $mappings = array()) { + if(empty($mappings)) $mappings = array( + array( + "key" => "admin_username", + "type" => "helm", + "path" => "auth.username" + ), + array( + "key" => "admin_password", + "type" => "helm", + "path" => "auth.password" + ), + array( + "key" => "admin_token", + "type" => "secret", + "path" => "api_token" + ), + ); + $apiParams = new stdclass; $apiParams->cluster = ''; $apiParams->namespace = $instance->spaceData->k8space; diff --git a/module/instance/config.php b/module/instance/config.php index 1b3ba07cb3..76bb387628 100644 --- a/module/instance/config.php +++ b/module/instance/config.php @@ -57,4 +57,6 @@ $config->instance->actionList['upgrade']['url'] = helper::createLink('in $config->instance->actions = new stdclass(); $config->instance->actions->view = array(); $config->instance->actions->view['mainActions'] = array('visit', 'start', 'stop', 'upgrade'); -$config->instance->actions->view['suffixActions'] = array('uninstall'); \ No newline at end of file +$config->instance->actions->view['suffixActions'] = array('uninstall'); + +$config->instance->devopsApps = array(89 => 'gitea', 58 => 'gitlab', 57 => 'gogs', 59 => 'jenkins', 60 => 'sonarqube'); \ No newline at end of file diff --git a/module/instance/control.php b/module/instance/control.php index 8d379d9e52..24daa9f687 100644 --- a/module/instance/control.php +++ b/module/instance/control.php @@ -77,13 +77,12 @@ class instance extends control } } - $dbList = new stdclass; - $currentResource = new stdclass; - $customItems = array(); $dbList = $this->cne->appDBList($instance); $currentResource = $this->cne->getAppConfig($instance); $customItems = $this->cne->getCustomItems($instance); + $this->instanceZen->saveAuthInfo($instance); + $this->view->title = $instance->appName; $this->view->instance = $instance; $this->view->cloudApp = $this->loadModel('store')->getAppInfoByChart($instance->chart, $instance->channel, false); diff --git a/module/instance/model.php b/module/instance/model.php index 33e34b1f1a..2d3095e065 100644 --- a/module/instance/model.php +++ b/module/instance/model.php @@ -575,6 +575,12 @@ class InstanceModel extends model $settingsMap->global->ingress->host = $settingsMap->ingress->host; } + if(!empty($this->config->instance->devopsApps[$instance->appID])) + { + $settingsMap->ci = new stdclass(); + $settingsMap->ci->enabled = true; + } + if(empty($customData->dbType) || $customData->dbType == 'unsharedDB' || empty($customData->dbService)) return $settingsMap; /* Set DB settings. */ diff --git a/module/instance/zen.php b/module/instance/zen.php new file mode 100644 index 0000000000..aeff34d95b --- /dev/null +++ b/module/instance/zen.php @@ -0,0 +1,71 @@ + + * @package instance + * @link https://www.zentao.net + */ +class instanceZen extends instance +{ + /** + * 自动保存devops应用授权信息。 + * Auto save auth info of devops. + * + * @param object $instance + * @access protected + * @return void + */ + protected function saveAuthInfo(object $instance): void + { + if(empty($this->config->instance->devopsApps[$instance->appID])) return; + + $url = 'https://' . $instance->domain; + $pipeline = $this->loadModel('pipeline')->getByUrl($url); + if(!empty($pipeline)) return; + + $tempMappings = $this->loadModel('cne')->getSettingsMapping($instance); + if(empty($tempMappings)) return; + + + $pipeline = new stdclass(); + $instance->type = $this->config->instance->devopsApps[$instance->appID]; + $pipeline->type = $instance->type; + $pipeline->private = md5(strval(rand(10,113450))); + $pipeline->createdBy = 'system'; + $pipeline->createdDate = helper::now(); + $pipeline->url = $url; + $pipeline->name = $this->generatePipelineName($instance); + $pipeline->token = zget($tempMappings, 'admin_token', ''); + $pipeline->account = zget($tempMappings, 'admin_username', ''); + $pipeline->password = zget($tempMappings, 'admin_password', ''); + $pipeline->token = zget($tempMappings, 'admin_token', ''); + + $this->pipeline->create($pipeline); + } + + /** + * 自动保存devops应用授权信息。 + * Auto save auth info of devops. + * + * @param object $instance + * @access protected + * @return string + */ + protected function generatePipelineName(object $instance): string + { + $name = $instance->name; + $type = $instance->type; + if(empty($this->loadModel('pipeline')->getByNameAndType($name, $type))) return $name; + if(empty($this->loadModel('pipeline')->getByNameAndType($name . '-' . $instance->appVersion, $type))) return $name . '-' . $instance->appVersion; + + for($times = 1; $times < 5; $times ++) + { + if(empty($this->loadModel('pipeline')->getByNameAndType($name . '-' . $times, $name))) return $name . '-' . $times; + } + } +} + diff --git a/module/pipeline/model.php b/module/pipeline/model.php index a806f18a68..38ce9c7927 100644 --- a/module/pipeline/model.php +++ b/module/pipeline/model.php @@ -29,6 +29,33 @@ class pipelineModel extends model return $pipeline; } + /** + * 根据名称及类型获取一条流水线记录 + * Get a pipeline by name and type. + * + * @param string $name + * @param string $type + * @access public + * @return object + */ + public function getByNameAndType(string $name, string $type) + { + return $this->dao->select('id')->from(TABLE_PIPELINE)->where('name')->eq($name)->andWhere('type')->eq($type)->fetch(); + } + + /** + * 根据url获取渠成创建的代码库。 + * Get a pipeline by url which created by quickon. + * + * @param string $url + * @access public + * @return object + */ + public function getByUrl(string $url) + { + return $this->dao->select('id')->from(TABLE_PIPELINE)->where('url')->eq($url)->andWhere('createdBy')->eq('system')->fetch(); + } + /** * Get pipeline list. * diff --git a/module/space/control.php b/module/space/control.php index 0c6d20762a..1125f56bdb 100644 --- a/module/space/control.php +++ b/module/space/control.php @@ -55,8 +55,9 @@ class space extends control $pipelines = $this->loadModel('pipeline')->getList('', 'id_desc'); $maxID = 0; if(!empty($instances)) $maxID = max(array_keys($instances)); - foreach($pipelines as $pipeline) + foreach($pipelines as $key => $pipeline) { + if($pipeline->createdBy == 'system') unset($pipelines[$key]); $pipeline->createdAt = $pipeline->createdDate; $pipeline->appName = ucfirst($pipeline->type); $pipeline->status = ''; diff --git a/module/space/js/createapplication.ui.js b/module/space/js/createapplication.ui.js index 78d1c23a90..40a6c20f98 100644 --- a/module/space/js/createapplication.ui.js +++ b/module/space/js/createapplication.ui.js @@ -72,13 +72,17 @@ function onChangeAppType(event) function onChangeStoreAppType(event) { - if(typeof(event) == 'undefined') + var storeApp = appID; + if(!storeApp) { - var storeApp = defaultApp; - } - else - { - var storeApp = $('[name=storeAppType]').val(); + if(typeof(event) == 'undefined') + { + storeApp = defaultApp; + } + else + { + storeApp = $('[name=storeAppType]').val(); + } } $('#createStoreAppForm').attr('action', $.createLink('instance', 'install', 'appID=' + storeApp)); diff --git a/module/space/ui/createapplication.html.php b/module/space/ui/createapplication.html.php index c2720c3a95..2829def9ca 100644 --- a/module/space/ui/createapplication.html.php +++ b/module/space/ui/createapplication.html.php @@ -20,6 +20,7 @@ jsVar('apps', $apps); jsVar('mysqlList', $mysqlList); jsVar('pgList', $pgList); jsVar('defaultApp', $defaultApp); +jsVar('appID', $appID); $showVersion = getenv('ALLOW_SELECT_VERSION') && (strtolower(getenv('ALLOW_SELECT_VERSION')) == 'true' || strtolower(getenv('ALLOW_SELECT_VERSION')) == '1'); $dbTypeItems = array();