Skip to content

Commit

Permalink
* Batch fresh instances status.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjianhua committed Sep 13, 2022
1 parent 8972341 commit 16ad9da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
9 changes: 6 additions & 3 deletions frontend/module/cne/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions frontend/module/instance/control.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down
53 changes: 41 additions & 12 deletions frontend/module/instance/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

/*
Expand All @@ -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)
{
Expand Down

0 comments on commit 16ad9da

Please sign in to comment.