* Refactor zanodeModel::createDefaultSnapshot.
This commit is contained in:
+27
-47
@@ -126,7 +126,7 @@ class zanodemodel extends model
|
||||
* @param object $node
|
||||
* @param object $snapshot
|
||||
* @access public
|
||||
* @return false|bool
|
||||
* @return false|int
|
||||
*/
|
||||
public function createSnapshot(object $node, object $snapshot): false|int
|
||||
{
|
||||
@@ -157,57 +157,37 @@ class zanodemodel extends model
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createDefaultSnapshot($zanodeID = 0)
|
||||
/**
|
||||
* 创建默认的快照。
|
||||
* Create default snapshot.
|
||||
*
|
||||
* @param int $zanodeID
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function createDefaultSnapshot(int $zanodeID = 0): bool
|
||||
{
|
||||
$node = $this->getNodeByID($zanodeID);
|
||||
if($node->status != 'running') dao::$errors['name'] = $this->lang->zanode->apiError['notRunning'];
|
||||
if(dao::isError()) return false;
|
||||
|
||||
$newSnapshot = new stdClass();
|
||||
$newSnapshot->host = $node->id;
|
||||
$newSnapshot->name = 'defaultSnap';
|
||||
$newSnapshot->desc = '';
|
||||
$newSnapshot->status = 'creating';
|
||||
$newSnapshot->osName = $node->osName;
|
||||
$newSnapshot->memory = 0;
|
||||
$newSnapshot->disk = 0;
|
||||
$newSnapshot->fileSize = 0;
|
||||
$newSnapshot->from = 'snapshot';
|
||||
$newSnapshot->createdBy = 'system';
|
||||
$newSnapshot->createdDate = helper::now();
|
||||
|
||||
$this->dao->insert(TABLE_IMAGE)
|
||||
->data($newSnapshot)
|
||||
->autoCheck()
|
||||
->exec();
|
||||
|
||||
if(dao::isError()) return false;
|
||||
|
||||
$newID = $this->dao->lastInsertID();
|
||||
|
||||
/* Prepare create params. */
|
||||
$agnetUrl = 'http://' . $node->ip . ':' . $node->hzap;
|
||||
$param = array(array(
|
||||
'name' => $newSnapshot->name,
|
||||
'task' => $newID,
|
||||
'type' => 'createSnap',
|
||||
'vm' => $node->name
|
||||
));
|
||||
|
||||
$result = json_decode(commonModel::http($agnetUrl . static::SNAPSHOT_CREATE_PATH, json_encode($param,JSON_NUMERIC_CHECK), null, array("Authorization:$node->tokenSN"), 'data', 'POST', 10));
|
||||
|
||||
|
||||
if(!empty($result) and $result->code == 'success')
|
||||
$node = $this->getNodeByID($zanodeID);
|
||||
if($node->status != 'running')
|
||||
{
|
||||
$this->dao->update(TABLE_ZAHOST)->set('status')->eq(static::STATUS_CREATING_SNAP)->where('id')->eq($node->id)->exec();
|
||||
|
||||
return $newID;
|
||||
dao::$errors['name'] = $this->lang->zanode->apiError['notRunning'];
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->dao->delete()->from(TABLE_IMAGE)->where('id')->eq($newID)->exec();
|
||||
dao::$errors[] = (!empty($result) and !empty($result->msg)) ? $result->msg : $this->app->lang->fail;
|
||||
$defaultSnapshot = new stdClass();
|
||||
$defaultSnapshot->host = $node->id;
|
||||
$defaultSnapshot->name = 'defaultSnap';
|
||||
$defaultSnapshot->desc = '';
|
||||
$defaultSnapshot->status = 'creating';
|
||||
$defaultSnapshot->osName = $node->osName;
|
||||
$defaultSnapshot->memory = 0;
|
||||
$defaultSnapshot->disk = 0;
|
||||
$defaultSnapshot->fileSize = 0;
|
||||
$defaultSnapshot->from = 'snapshot';
|
||||
$defaultSnapshot->createdBy = 'system';
|
||||
$defaultSnapshot->createdDate = helper::now();
|
||||
|
||||
return false;
|
||||
$this->createSnapshot($node, $defaultSnapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
|
||||
title=测试 zanodeModel->createDefaultSnapshot().
|
||||
cid=1
|
||||
|
||||
- 测试不是运行中的执行节点创建默认快照属性name @请检查执行节点状态
|
||||
|
||||
*/
|
||||
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
include dirname(__FILE__, 2) . '/zanode.class.php';
|
||||
|
||||
zdTable('host')->config('host')->gen(5);
|
||||
zdTable('user')->gen(5);
|
||||
su('admin');
|
||||
|
||||
$zanode = new zanodeTest();
|
||||
r($zanode->createDefaultSnapshotTest(2)) && p('name') && e('请检查执行节点状态'); //测试不是运行中的执行节点创建默认快照
|
||||
@@ -0,0 +1,31 @@
|
||||
title: zt_host
|
||||
author: Wang XuePeng
|
||||
version: "1.0"
|
||||
fields:
|
||||
- field: id
|
||||
range: 1-1000
|
||||
- field: name
|
||||
fields:
|
||||
- field: field1
|
||||
range: node{5},physics{5}
|
||||
- field: field2
|
||||
range: 1-4{2},aa{2}
|
||||
- field: image
|
||||
range: 1-10
|
||||
- field: mac
|
||||
prefix: mac
|
||||
range: 1-10
|
||||
- field: type
|
||||
range: node
|
||||
- field: parent
|
||||
range: 0{3},1-3{3},100
|
||||
- field: hostType
|
||||
range: virtual{3},``{10}
|
||||
- field: heartbeat
|
||||
range: "(-10D)-(+10D):-70S"
|
||||
type: timestamp
|
||||
format: "YYYY-MM-DD hh:mm:ss"
|
||||
- field: status
|
||||
range: running,ready,wait,running{3},wait{3},ready{3},running
|
||||
- field: extranet
|
||||
range: '`127.0.0.1`'
|
||||
@@ -138,4 +138,20 @@ class zanodeTest
|
||||
|
||||
return $this->objectModel->dao->select('*')->from(TABLE_IMAGE)->where('id')->eq($snapshotID)->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试创建默认的快照。
|
||||
* Test create default snapshot.
|
||||
*
|
||||
* @param int $nodeID
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function createDefaultSnapshotTest(int $nodeID): object|array
|
||||
{
|
||||
$snapshotID = $this->createDefaultSnapshot($nodeID);
|
||||
if(dao::isError()) return dao::getError();
|
||||
|
||||
return $this->objectModel->dao->select('*')->from(TABLE_IMAGE)->where('id')->eq($snapshotID)->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user