| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816 |
- <?php
- /**
- * The control file of instance module of ZenTaoPMS.
- *
- * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
- * @license ZPL (http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
- * @author Sun Guangming <sunguangming@easycorp.ltd>
- * @package instance
- * @version $Id$
- * @link https://www.zentao.net
- * @property instanceModel $instance
- * @property instanceZen $instanceZen
- * @property cneModel $cne
- */
- class instance extends control
- {
- /**
- * Construct function.
- *
- * @param string $moduleName
- * @param string $methodName
- * @access public
- * @return void
- */
- public function __construct($moduleName = '', $methodName = '')
- {
- parent::__construct($moduleName, $methodName);
- $this->loadModel('action');
- $this->loadModel('cne');
- $this->loadModel('store');
- }
- /**
- * 查看应用详情。
- * Show instance view.
- *
- * @param int $id
- * @param string $type
- * @param string $tab
- * @access public
- * @return void
- */
- public function view($id, $type = 'store', $tab = 'baseinfo')
- {
- if(!commonModel::hasPriv('space', 'browse')) $this->loadModel('common')->deny('space', 'browse', false);
- if($type === 'store')
- {
- $this->instanceZen->storeView($id, $tab);
- }
- else
- {
- $instance = $this->loadModel('gitea')->fetchByID($id);
- $instance->status = 'running';
- $instance->source = 'user';
- $instance->externalID = $instance->id;
- $instance->runDuration = 0;
- $instance->appName = $instance->type;
- $instance->createdAt = $instance->createdDate;
- $instanceMetric = new stdclass();
- $instanceMetric->cpu = 0;
- $instanceMetric->memory = 0;
- $instanceMetric->disk = 0;
- $this->view->title = $instance->name;
- $this->view->instance = $instance;
- $this->view->actions = $this->loadModel('action')->getList($instance->type, $id);
- $this->view->defaultAccount = '';
- $this->view->instanceMetric = $instanceMetric;
- $this->view->dbList = array();
- }
- $this->view->users = $this->loadModel('user')->getPairs('noletter');
- $this->view->tab = $tab;
- $this->view->type = $type;
- $this->display();
- }
- /**
- * 设置应用。
- * Setting instance.
- *
- * @param int $id
- * @param string $component 0|mysql
- * @access public
- * @return void
- */
- public function setting($id, $component = '0')
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $instance = $this->instance->getByID($id);
- $currentResource = $this->cne->getAppConfig($instance);
- if(empty($component)) $component = false;
- $diskSettings = $this->cne->getDiskSettings($instance, $component);
- $this->lang->instance->errors->invalidDiskSize = sprintf($this->lang->instance->errors->invalidDiskSize, $diskSettings->size, $diskSettings->limit);
- $this->lang->instance->tips->resizeDisk = sprintf($this->lang->instance->tips->resizeDisk, $diskSettings->size, $diskSettings->limit);
- if(!empty($_POST))
- {
- $newInstance = fixer::input('post')->trim('name')->get();
- $memoryKb = $this->post->memory_kb;
- if(intval($currentResource->max->memory / 1024) != $memoryKb)
- {
- /* Check free memory size is enough or not. */
- $clusterResource = $this->cne->cneMetrics();
- $freeMemory = intval($clusterResource->metrics->memory->allocatable * 0.9); // Remain 10% memory for system.
- if($memoryKb * 1024 > $freeMemory) $this->send(array('result' => 'fail', 'message' => $this->lang->instance->errors->notEnoughResource));
- /* Request CNE to adjust memory size. */
- $instance->oldValue = $currentResource->max->memory;
- if(!$this->instance->updateMemorySize($instance, $memoryKb * 1024))
- {
- return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- }
- }
- $cpu = $this->post->cpu;
- if($currentResource->max->cpu != $cpu)
- {
- $instance->oldValue = $currentResource->max->cpu;
- if(!$this->instance->updateCpuSize($instance, $cpu))
- {
- return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- }
- }
- $disk = $this->post->disk_gb;
- if(is_numeric($disk) && $disk != $diskSettings->size && $disk != $diskSettings->requestSize)
- {
- if($disk < $diskSettings->size || $disk > $diskSettings->limit) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->errors->invalidDiskSize));
- $instance->oldValue = $diskSettings->size;
- if(!$this->instance->updateVolSize($instance, $disk . 'Gi', $diskSettings->name)) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- }
- if(empty($component))
- {
- $this->instance->updateByID($id, $newInstance);
- if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- if($newInstance->name != $instance->name)
- {
- $this->action->create('instance', $instance->id, 'editName', '', json_encode(array('result' => array('result' => 'success'), 'data' => array('oldName' => $instance->name, 'newName' => $newInstance->name))));
- }
- }
- return $this->send(array('result' => 'success', 'load' => true, 'closeModal' => true));
- }
- $this->view->diskSettings = $diskSettings;
- $this->view->currentResource = $currentResource;
- $this->view->instance = $instance;
- $this->display();
- }
- /**
- * 升级一个应用。
- * Upgrade a instance.
- *
- * @param int $id
- * @access public
- * @return mixed
- */
- public function upgrade($id)
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $instance = $this->instance->getByID($id);
- $instance->latestVersion = $this->store->appLatestVersion($instance->appID, $instance->version);
- if($_POST)
- {
- if(empty($instance->latestVersion)) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->noHigherVersion, 'closeModal' => true));
- $postData = fixer::input('post')->get();
- if($postData->confirm == 'yes') $success = $this->instance->upgrade($instance, $instance->latestVersion->version, $instance->latestVersion->app_version);
- $logExtra = array('result' => 'success', 'data' => array('oldVersion' => $instance->appVersion, 'newVersion' => $instance->latestVersion->app_version));
- if(!$success)
- {
- $logExtra['result'] = 'fail';
- $this->action->create('instance', $instance->id, 'upgrade', '', json_encode($logExtra));
- return $this->send(array('result' => 'fail', 'message' => !empty($logExtra['message']) ? $logExtra['message'] : $this->lang->instance->notices['upgradeFail'], 'closeModal' => true));
- }
- $this->action->create('instance', $instance->id, 'upgrade', '', json_encode($logExtra));
- return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['upgradeSuccess'], 'load' => $this->createLink('instance', 'view', "id=$id"), 'closeModal' => true));
- }
- $this->view->title = $this->lang->instance->upgrade . $instance->name;
- $this->view->instance = $instance;
- $this->display();
- }
- /**
- * 访问一个应用。
- * Visit a app.
- *
- * @param int $id
- * @param int $externalID
- * @access public
- * @return void
- */
- public function visit($id, $externalID = 0)
- {
- if(!commonModel::hasPriv('space', 'browse')) $this->loadModel('common')->deny('space', 'browse', false);
- if(!$externalID)
- {
- $instance = $this->instance->getByID($id);
- $url = $this->instance->url($instance);
- }
- else
- {
- $pipeline = $this->loadModel('pipeline')->getByID($externalID);
- $url = $pipeline->url;
- }
- return $this->send(array('result' => 'success', 'callback' => "window.open('{$url}')"));
- }
- /**
- * 创建手工配置外部应用。
- * Create a external app.
- *
- * @param string $type
- * @access public
- * @return void
- */
- public function createExternalApp($type)
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $this->loadModel('sonarqube');
- $this->app->loadLang('pipeline');
- $externalApp = form::data($this->config->instance->form->create)
- ->add('createdBy', isset($this->app->user->account) ? $this->app->user->account : '')
- ->get();
- $externalApp->type = $type;
- $externalApp->url = rtrim($externalApp->url, '/');
- if(!$this->instance->checkAppNameUnique($externalApp->name)) return $this->send(array('result' => false, 'message' => array('name' => sprintf($this->lang->error->repeat, $this->lang->pipeline->name, $externalApp->name))));
- $appID = $this->loadModel('pipeline')->create($externalApp);
- if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- $this->loadModel('action')->create($type, $appID, 'created');
- return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->createLink('space', 'browse')));
- }
- /**
- * 编辑手工配置外部应用。
- * Edit a external app.
- *
- * @param int $externalID
- * @access public
- * @return void
- */
- public function editExternalApp($externalID)
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $oldApp = $this->loadModel('pipeline')->getByID($externalID);
- if($_POST)
- {
- $instance = form::data($this->config->instance->form->edit)
- ->add('editedBy', isset($this->app->user->account) ? $this->app->user->account : '')
- ->get();
- $this->pipeline->update($externalID, $instance);
- if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- $app = $this->pipeline->getByID($externalID);
- $actionID = $this->loadModel('action')->create($app->type, $externalID, 'edited');
- $changes = common::createChanges($oldApp, $app);
- $this->action->logHistory($actionID, $changes);
- return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
- }
- $this->app->loadLang('space');
- $this->app->loadLang('sonarqube');
- $this->view->app = $oldApp;
- $this->display();
- }
- /**
- * 删除一个外部应用。
- * Delete a external app.
- *
- * @param int $externalID
- * @access public
- * @return void
- */
- public function deleteExternalApp($externalID)
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $oldApp = $this->loadModel('pipeline')->getByID($externalID);
- $actionID = $this->pipeline->deleteByObject($externalID, $oldApp->type);
- if(!$actionID)
- {
- $response['result'] = 'fail';
- $response['callback'] = sprintf('zui.Modal.alert("%s");', $this->lang->pipeline->delError);
- return $this->send($response);
- }
- $app = $this->pipeline->getByID($externalID);
- $changes = common::createChanges($oldApp, $app);
- $this->loadModel('action')->logHistory($actionID, $changes);
- $response['load'] = true;
- $response['message'] = zget($this->lang->instance->notices, 'uninstallSuccess');
- $response['result'] = 'success';
- return $this->send($response);
- }
- /**
- * 安装应用。
- * Install app.
- *
- * @param int $appID
- * @param string $checkResource
- * @access public
- * @return void
- */
- public function install($appID, $checkResource = 'true')
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $cloudApp = $this->store->getAppInfo($appID);
- if(empty($cloudApp)) return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->instance->errors->noAppInfo)));
- $versionList = $this->store->appVersionList($cloudApp->id);
- $mysqlList = $this->cne->sharedDBList('mysql');
- $pgList = $this->cne->sharedDBList('postgresql');
- if(!empty($_POST))
- {
- $customData = form::data($this->config->instance->form->install)->get();
- if($customData->version && isset($versionList[$customData->version])) $customData->app_version = $versionList[$customData->version]->app_version;
- $this->instanceZen->checkForInstall($customData);
- if($checkResource == 'true')
- {
- $resource = new stdclass();
- $resource->cpu = $cloudApp->cpu;
- $resource->memory = $cloudApp->memory;
- $result = $this->cne->tryAllocate(array($resource));
- if(!isset($result->code) || $result->code != 200) return $this->send(array('callback' => 'alertResource()'));
- }
- /* If select the version, replace the latest version of App by selected version. */
- if($customData->version)
- {
- $cloudApp->version = $customData->version;
- $cloudApp->app_version = $customData->app_version;
- }
- $sharedDB = new stdclass();
- if(isset($cloudApp->dependencies->mysql) && $customData->dbType == 'sharedDB')
- {
- $sharedDB = zget($mysqlList, $customData->dbService, $sharedDB);
- }
- elseif(isset($cloudApp->dependencies->postgresql) && $customData->dbType == 'sharedDB')
- {
- $sharedDB = zget($pgList, $customData->dbService, $sharedDB);
- }
- $instance = $this->instance->install($cloudApp, $sharedDB, $customData);
- if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
- if(!$instance) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->notices['installFail']));
- return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['installSuccess'], 'load' => $this->createLink('instance', 'view', "id=$instance->id"), 'closeModal' => true));
- }
- $this->view->versionList = array();
- foreach($versionList as $version) $this->view->versionList[$version->version] = $version->app_version . " ({$version->version})";
- $this->view->title = $this->lang->instance->install . $cloudApp->alias;
- $this->view->cloudApp = $cloudApp;
- $this->view->thirdDomain = $this->instance->randThirdDomain();
- $this->view->mysqlList = $this->instance->dbListToOptions($mysqlList);
- $this->view->pgList = $this->instance->dbListToOptions($pgList);
- $this->display();
- }
- /**
- * 卸载应用。
- * Uninstall app instance.
- *
- * @param int $instanceID
- * @param string $type
- * @access public
- * @return void
- */
- public function ajaxUninstall($instanceID, $type = '')
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- if($type !== 'store')
- {
- $instance = $this->loadModel('pipeline')->getByID($instanceID);
- if(!$instance) return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['success'], 'load' => $this->createLink('space', 'browse')));
- if($instance->type == 'nexus') return $this->deleteExternalApp($instance->id);
- return $this->fetch($instance->type, 'delete', array('id' => $instance->id));
- }
- $instance = $this->instance->getByID($instanceID);
- if(!$instance) return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['success'], 'load' => $this->createLink('space', 'browse')));
- $externalApp = $this->loadModel('space')->getExternalAppByApp($instance);
- if($externalApp)
- {
- $actionID = $this->loadModel('pipeline')->deleteByObject($externalApp->id, strtolower($instance->appName));
- if(!$actionID) return $this->send(array('result' => 'fail', 'message' => $this->lang->pipeline->delError));
- }
- $success = $this->instance->uninstall($instance);
- $this->action->create('instance', $instance->id, 'uninstall', '', json_encode(array('result' => $success, 'app' => array('alias' => $instance->appName, 'app_version' => $instance->version))));
- /* Synchronize deletion of backup tasks. */
- /* 同步删除备份任务。 */
- $this->instance->deleteBackupCron($instance);
- if($success) return $this->send(array('result' => 'success', 'message' => zget($this->lang->instance->notices, 'uninstallSuccess'), 'load' => $this->createLink('space', 'browse')));
- return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'uninstallFail')));
- }
- /**
- * 启动应用实例。
- * Start app instance.
- *
- * @param int $instanceID
- * @access public
- * @return void
- */
- public function ajaxStart($instanceID)
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $instance = $this->instance->getByID($instanceID);
- if(!$instance) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->instanceNotExists));
- $result = $this->instance->start($instance);
- $this->action->create('instance', $instance->id, 'start', '', json_encode(array('result' => $result, 'app' => array('alias' => $instance->appName, 'app_version' => $instance->version))));
- if($result->code == 200) return $this->send(array('result' => 'success', 'load' => true, 'message' => zget($this->lang->instance->notices, 'startSuccess')));
- return $this->send(array('result' => 'fail', 'message' => !empty($result->message) ? $result->message : zget($this->lang->instance->notices, 'startFail')));
- }
- /**
- * 停止应用实例。
- * Stop app instance.
- *
- * @param int $instanceID
- * @access public
- * @return void
- */
- public function ajaxStop($instanceID)
- {
- if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $instance = $this->instance->getByID($instanceID);
- if(!$instance) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->instanceNotExists));
- $result = $this->instance->stop($instance);
- $this->action->create('instance', $instance->id, 'stop', '', json_encode(array('result' => $result, 'app' => array('alias' => $instance->appName, 'app_version' => $instance->version))));
- if($result->code == 200) return $this->send(array('result' => 'success', 'load' => true, 'message' => zget($this->lang->instance->notices, 'stopSuccess')));
- return $this->send(array('result' => 'fail', 'message' => !empty($result->message) ? $result->message : zget($this->lang->instance->notices, 'stopFail')));
- }
- /**
- * 查看应用的运行状态。
- * Query status of app instance.
- *
- * @access public
- * @return void
- */
- public function ajaxStatus()
- {
- $postData = fixer::input('post')->setDefault('idList', array())->get();
- $instances = $this->instance->getByIdList($postData->idList);
- $statusList = $this->instance->batchFresh($instances);
- return $this->send(array('result' => 'success', 'data' => $statusList));
- }
- /**
- * 授权数据库。
- * Generate database auth parameters and jump to login page.
- *
- * @access public
- * @return void
- */
- public function ajaxDBAuthUrl()
- {
- if(!commonModel::hasPriv('space', 'browse')) $this->loadModel('common')->deny('space', 'browse', false);
- $post = fixer::input('post')
- ->setDefault('namespace', 'default')
- ->setDefault('instanceID', 0)
- ->setDefault('dbType', '')
- ->get();
- if(empty($post->dbName)) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->errors->dbNameIsEmpty));
- $instance = $this->instance->getByID((int)$post->instanceID);
- if(empty($instance)) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->instanceNotExists));
- $detail = $this->loadModel('cne')->appDBDetail($instance, $post->dbName);
- if(empty($detail)) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->errors->notFoundDB));
- $dbAuth = array();
- $dbAuth['driver'] = zget($this->config->instance->adminer->dbTypes, $post->dbType, '');
- $dbAuth['server'] = $detail->host . ':' . $detail->port;
- $dbAuth['username'] = $detail->username;
- $dbAuth['db'] = $detail->database;
- $dbAuth['password'] = $detail->password;
- $url = '/adminer?' . http_build_query($dbAuth);
- $this->send(array('result' => 'success', 'message' => '', 'data' => array('url' => $url)));
- }
- /**
- * 定时任务:同步GitFox数据。(方法用于DevOps解决方案)
- * Sync GitFox data (for DevOps solution).
- *
- * @access public
- * @return void
- */
- public function syncGitFoxData()
- {
- if(!file_exists($this->app->basePath . 'extension' . DS . 'custom' . DS . 'gitfox' . DS . 'control.php')) return false;
- $this->loadModel('instance')->syncGitFoxData();
- return true;
- }
- /**
- * Instance Backup List.
- * 应用备份列表。
- * @param int $id
- * @param int $recPerPage
- * @param int $pageID
- * @return void
- */
- public function backupList($id, $recPerPage = 5, $pageID = 1)
- {
- /* Initialize Backup List. */
- $instance = $this->instance->getByID($id);
- $backupList = $this->instance->backupList($instance);
- /* Pager. */
- $this->app->loadClass('pager', true);
- $recTotal = count($backupList);
- $pager = new pager($recTotal, $recPerPage, $pageID);
- $backupList = array_chunk($backupList, $pager->recPerPage);
- $backupList = empty($backupList) ? array() : $backupList[$pageID - 1];
- $this->view->instance = $instance;
- $this->view->pager = $pager;
- $this->view->backupList = $backupList;
- $this->display();
- }
- /**
- * Restore instance by ajax.
- * 还原应用。
- * @access public
- * @return void
- * @param int $instanceID
- * @param string $backupName
- */
- public function ajaxRestore($instanceID, $backupName)
- {
- if(empty($instanceID) || empty($backupName)) return $this->send(array('result' => 'fail', 'message' => $this->lang->instance->errors->wrongRequestData));
- $backupName = base64_decode(helper::safe64Decode($backupName));
- $instance = $this->instance->getByID($instanceID);
- if(empty($instance)) return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->instance->instanceNotExists)));
- $success = $this->instance->restore($instance, $this->app->user, $backupName);
- if(!$success)
- {
- $this->action->create('instance', $instance->id, 'restore', '', json_encode(array('result' => array('result' => 'fail'))));
- return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'restoreFail') ));
- }
- $this->action->create('instance', $instance->id, 'restore', '', json_encode(array('result' => array('result' => 'success'))));
- return $this->send(array('result' => 'success', 'message' => zget($this->lang->instance->notices, 'restoreSuccess'), 'load' => true));
- }
- /**
- * 手动备份。
- * Manual backup.
- *
- * @param int $id
- * @param string $component 0|mysql
- * @access public
- * @return void
- */
- public function manualBackup($id)
- {
- $instance = $this->instance->getByID($id);
- $success = $this->instance->backup($instance, $this->app->user);
- if(!$success)
- {
- $this->action->create('instance', $instance->id, 'manualbackup', '', json_encode(array('result' => 'fail')));
- return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'backupFail')));
- }
- $this->action->create('instance', $instance->id, 'manualbackup', '', json_encode(array('result' => 'success')));
- return $this->send(array('result' => 'success', 'message' => zget($this->lang->instance->notices, 'backupSuccess')));
- }
- /**
- * 备份设置。
- * backup settings.
- *
- * @param int $id
- * @param string $component 0|mysql
- * @access public
- * @return void
- * @param int $instanceID
- */
- public function backupSettings($instanceID)
- {
- $instance = $this->instance->getByID($instanceID);
- if($_POST)
- {
- $this->instance->saveBackupSettings($instance);
- if(dao::isError()) return $this->send(array('result' => 'fail', 'load' => array('alert' => dao::getError())));
- $locate = $this->createLink('instance', 'view', 'id=' . $instanceID);
- return $this->send(array('result' => 'success', 'load' => array('alert' => $this->lang->saveSuccess, 'locate' => $locate, 'closeModal' => true)));
- }
- $this->view->instance = $instance;
- $this->view->backupSettings = $this->instance->getBackupSettings($instanceID);
- $this->display();
- }
- /**
- * Cron task of auto backup.
- * 定时备份。
- *
- * @param $instanceID
- * @return int|null
- * @param string $instanceID
- */
- public function cronBackup($instanceID)
- {
- $instance = $this->instance->getByID((int)$instanceID);
- if(empty($instance)) return $this->send(array('result' => 'success', 'message' => $this->lang->instance->instanceNotExists));
- $sysUser = new stdclass;
- $sysUser->account = 'system';
- $success = $this->instance->autoBackup($instance, $sysUser);
- if(!$success)
- {
- return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'backupFail')));
- }
- return $this->send(array('result' => 'success', 'message' => zget($this->lang->instance->notices, 'backupSuccess')));
- }
- /**
- * Cron cleaning backup.
- * 定时清理备份。
- *
- * @return void
- */
- public function cronCleanBackup()
- {
- if(!$this->config->inQuickon) return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['NoCleanBackupFiles']));
- /* Init instance list. */
- $instances = $this->loadModel('space')->getSpaceInstances(0, 'running');
- if(empty($instances)) return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['NoCleanBackupFiles']));
- /* Cycle cleaning backup. */
- $sysUser = new stdclass;
- $sysUser->account = 'system';
- foreach($instances as $instance) $this->instance->cleanBackup($instance, $sysUser);
- return $this->send(array('result' => 'success', 'message' => $this->lang->instance->notices['cleanBackupSuccess']));
- }
- /**
- * Delete backup by ajax.
- * 删除备份。
- * @param int $backupID
- * @access public
- * @return void
- * @param string $instanceID
- * @param string $backupName
- */
- public function ajaxDeleteBackup($instanceID, $backupName)
- {
- $instance = $this->instance->getByID((int)$instanceID);
- if(empty($instance)) $this->send(array('result' => 'success', 'message' => $this->lang->instance->instanceNotExists));
- $backupName = base64_decode(helper::safe64Decode($backupName));
- $success = $this->instance->deleteBackup($instance, $backupName);
- if(!$success) return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'deleteFail')));
- $this->action->create('instance', $instance->id, 'manualdeletebackup', '', json_encode(array('result' => 'success')));
- return $this->send(array('result' => 'success', 'message' => zget($this->lang->instance->notices, 'deleteSuccess'), 'load' => $this->createLink('instance', 'view', 'id=' . $instanceID)));
- }
- /**
- * Ajax 方式获取组件列表。
- * ajax Get Components.
- * @param int $id
- * @return void
- */
- public function ajaxGetComponents($id)
- {
- $componentList = array();
- $instance = $this->instance->getByID($id);
- $components = $this->cne->getComponents($instance);
- if(empty($components->data)) return print(json_encode($componentList));
- foreach($components->data as $component)
- {
- $componentList[] = array('value' => $component->name, 'text' => $component->name);
- }
- return print(json_encode($componentList));
- }
- /**
- * Ajax 方式获取 Pods 列表。
- * ajax Get Pods.
- * @param int $id
- * @return void
- */
- public function ajaxGetPods($id)
- {
- $podList = [];
- $instance = $this->instance->getByID($id);
- $formData = form::data($this->config->instance->form->events)->get();
- $pods = $this->cne->getPods($instance, $formData->component);
- if(empty($pods->data)) return print(json_encode($podList));
- foreach($pods->data as $pod)
- {
- $podList[] = array('value' => $pod->name, 'text' => $pod->name);
- }
- return print(json_encode($podList));
- }
- /**
- * watch Logs page.
- * 查看日志页面.
- *
- * @param int $id
- * @return void
- */
- public function logs($id)
- {
- if (!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
- $instance = $this->instance->getByID($id);
- $this->view->instance = $instance;
- $this->view->title = $this->lang->instance->log->viewButton;
- $this->display();
- }
- /**
- * Get logs api.
- * 获取日志接口。
- * @param int $id
- * @return void
- */
- public function showLogs($id)
- {
- $instance = $this->instance->getByID($id);
- $formData = form::data($this->config->instance->form->events)->get();
- $component = $formData->component;
- $pod = $formData->pod;
- $previous = $formData->previous == 1;
- $container = $formData->container;
- $data = $this->cne->getAppLogs($instance, $component, $pod, $container, $previous) ?? new stdClass();
- return print(json_encode($data));
- }
- /**
- * watch Events page.
- * 查看事件页面.
- *
- * @param int $id
- * @return void
- */
- public function events($id)
- {
- if (!commonModel::hasPriv('instance', 'manage'))
- {
- $this->loadModel('common')->deny('instance', 'manage', false);
- }
- $this->view->instance = $this->instance->getByID($id);
- $this->display();
- }
- /**
- * Get Events api.
- * 获取日志接口。
- * @param int $id
- * @return void
- */
- public function showEvents($id)
- {
- $instance = $this->instance->getByID($id);
- $formData = form::data($this->config->instance->form->events)->get();
- $data = $this->cne->getEvents($instance, $formData->component) ?? new stdClass();
- return print(json_encode($data));
- }
- }
|