From 16ad9da7688876c1b16cd5b54cd5a535f4137cbf Mon Sep 17 00:00:00 2001 From: wangjianhua Date: Tue, 13 Sep 2022 17:00:27 +0800 Subject: [PATCH] * Batch fresh instances status. --- frontend/module/cne/model.php | 9 +++-- frontend/module/instance/control.php | 9 +++-- frontend/module/instance/model.php | 53 +++++++++++++++++++++------- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/frontend/module/cne/model.php b/frontend/module/cne/model.php index 249fcca9..3fb3abcf 100644 --- a/frontend/module/cne/model.php +++ b/frontend/module/cne/model.php @@ -525,10 +525,13 @@ public function batchQueryStatus($instanceList) } $apiUrl = "/api/cne/app/status/multi"; - $result = $this->apiGet($apiUrl, $apiParams, $this->config->CNE->api->headers); - if($result && $result->code == 200) return $result; + $result = $this->apiPost($apiUrl, $apiParams, $this->config->CNE->api->headers); + if(empty($result) || $result->code != 200) return array(); - return $result; + $statusList = array(); + foreach($result->data as $status) $statusList[$status->name] = $status; + + return $statusList; } /** diff --git a/frontend/module/instance/control.php b/frontend/module/instance/control.php index 5881452a..c9da95b1 100644 --- a/frontend/module/instance/control.php +++ b/frontend/module/instance/control.php @@ -543,15 +543,18 @@ public function apiInstall() $this->app->user = $user; - $name = zget($requestBody , 'name', ''); - $channel = zget($requestBody , 'channel', 'stable'); + $name = zget($requestBody , 'name', ''); + $channel = zget($requestBody , 'channel', 'stable'); + $k8name = zget($requestBody , 'k8name', ''); + if($k8name && $this->instance->k8nameExists($k8name)) return print(json_encode(array('code' => 706, 'message' => $k8name . ' has been used, please change it and try again.'))); + $thirdDomain = zget($requestBody , 'domain', ''); if($this->instance->domainExists($thirdDomain)) return print(json_encode(array('code' => 705, 'message' => $thirdDomain . ' has been used, please change it and try again.'))); $cloudApp = $this->store->getAppInfoByChart($chart, $channel, false); if(empty($cloudApp)) return print(json_encode(array('code' => 702, 'message' => 'App not found.'))); - $result = $this->instance->apiInstall($cloudApp, $thirdDomain, $name, $channel); + $result = $this->instance->apiInstall($cloudApp, $thirdDomain, $name, $k8name, $channel); if($result) return print(json_encode(array('code' => 200, 'message' => 'success', 'data' => new stdclass))); diff --git a/frontend/module/instance/model.php b/frontend/module/instance/model.php index 082b4839..4a5e6a77 100644 --- a/frontend/module/instance/model.php +++ b/frontend/module/instance/model.php @@ -221,7 +221,7 @@ public function fullDomain($thirdDomain) /** * Check if the domain exists. * - * @param int $thirdDomain + * @param string $thirdDomain * @access public * @return bool true: exists, false: not exist. */ @@ -231,6 +231,18 @@ public function domainExists($thirdDomain) return boolval($this->dao->select('id')->from(TABLE_INSTANCE)->where('domain')->eq($domain)->andWhere('deleted')->eq(0)->fetch()); } + /** + * Check if the k8name exists. + * + * @param string $k8name + * @access public + * @return bool true: exists, false: not exist. + */ + public function k8nameExists($k8name) + { + return boolval($this->dao->select('id')->from(TABLE_INSTANCE)->where('k8name')->eq($k8name)->andWhere('deleted')->eq(0)->fetch()); + } + /** * Mount installation settings by custom data. * @@ -333,7 +345,7 @@ public function install($app, $dbList, $customData, $spaceID = null) } $channel = $this->app->session->cloudChannel ? $this->app->session->cloudChannel : $this->config->cloud->api->channel; - $instance = $this->createInstance($app, $space, $customData->customDomain, $customData->customName, $channel); + $instance = $this->createInstance($app, $space, $customData->customDomain, $customData->customName, '',$channel); if(!$instance) return false; @@ -350,7 +362,7 @@ public function install($app, $dbList, $customData, $spaceID = null) * @access public * @return bool|object */ - public function apiInstall($app, $thirdDomain = '', $name = '', $channel = 'stable') + public function apiInstall($app, $thirdDomain = '', $name = '', $k8name = '', $channel = 'stable') { $this->loadModel('space'); $space = $this->space->defaultSpace($this->app->user->account); @@ -366,7 +378,7 @@ public function apiInstall($app, $thirdDomain = '', $name = '', $channel = 'stab $customData->dbService = reset($dbList)->name; // Use first shared database. } - $instance = $this->createInstance($app, $space, $customData->customDomain, $name, $channel); + $instance = $this->createInstance($app, $space, $customData->customDomain, $name, $k8name, $channel); if(!$instance) return false; return $this->doCneInstall($app, $instance, $space, $customData, $dbList); @@ -383,9 +395,9 @@ public function apiInstall($app, $thirdDomain = '', $name = '', $channel = 'stab * @access public * @return bool|object */ - public function createInstance($app, $space, $thirdDomain, $name = '', $channel = 'stable') + public function createInstance($app, $space, $thirdDomain, $name = '', $k8name = '', $channel = 'stable') { - $k8name = "{$app->chart}-{$this->app->user->account}-" . date('YmdHis'); //name rule: chartName-userAccount-YmdHis; + if(empty($k8name)) $k8name = "{$app->chart}-{$this->app->user->account}-" . date('YmdHis'); //name rule: chartName-userAccount-YmdHis; $instanceData = new stdclass; $instanceData->appId = $app->id; @@ -551,19 +563,36 @@ public function upgrade($instance, $toVersion, $appVersion) */ public function batchFresh(&$instances) { - $statusList = array(); - foreach($instances as $instance) + $statusList = $this->cne->batchQueryStatus($instances); + + $newStatusList = array(); + + foreach($instances as $instance) { - $instance = $this->freshStatus($instance); + $statusData = zget($statusList, $instance->k8name, ''); + if($statusData) + { + if($instance->status != $statusData->status || $instance->version != $statusData->version || $instance->domain != $statusData->access_host) + { + $this->dao->update(TABLE_INSTANCE) + ->set('status')->eq($statusData->status) + ->beginIF($statusData->version)->set('version')->eq($statusData->version)->fi() + ->beginIF($statusData->access_host)->set('domain')->eq($statusData->access_host)->fi() + ->where('id')->eq($instance->id) + ->autoCheck() + ->exec(); + $instance->status = $statusData->status; + } + } $status = new stdclass; $status->id = $instance->id; $status->status = $instance->status; - $statusList[] = $status; + $newStatusList[] = $status; } - return $statusList; + return $newStatusList; } /* @@ -580,7 +609,7 @@ public function freshStatus($instance) if($statusResponse->code != 200) return $instance; $statusData = $statusResponse->data; - $instance->runDuration = intval($statusData->age); + $instance->runDuration = intval($statusData->age); // Run duration used in view page. if($instance->status != $statusData->status || $instance->version != $statusData->version || $instance->domain != $statusData->access_host) {