Skip to content

Commit

Permalink
Merge pull request #1 from gzzchh/1.0.0
Browse files Browse the repository at this point in the history
1.0.0
  • Loading branch information
chisaato authored Oct 15, 2019
2 parents 2ab387f + 570d458 commit e4890c8
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.phar
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
t2.php
15 changes: 13 additions & 2 deletions .idea/deployment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/symfony2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/webServers.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# StuInfoMgr
## 允许的变量表
* GOVC_DATACENTER
* GOVC_URL
此软件包已废弃
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "library",
"license": "GPL-3.0-or-later",
"minimum-stability": "dev",
"prefer-stable" : true,
"require": {
"symfony/process": "^4.3"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Cmds/VM/Network/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MisakaCloud\GoVC\Cmds\VM\Network;

use MisakaCloud\GoVc\Helper\ProcessHelper;
use MisakaCloud\GoVC\Helper\ProcessHelper;

/**
* Class Network
Expand Down Expand Up @@ -39,7 +39,7 @@ public function change($vm, $portGroup, $networkAdapter, $macAddr, $device)
// 注意 即使不修改虚拟机网络 '-net' 和 '-net.address' 也必须一起使用
// vm.network.change -vm string -net string -net.adapter string -net.address string string
$cmd = [$this->goVcBin, 'vm.network.change', '-vm', $vm, '-net', $portGroup, '-net.adapter', $networkAdapter, '-net.address', $macAddr, $device];
ProcessHelper::runAsync($cmd);
return ProcessHelper::runAsync($cmd);
}

/**
Expand All @@ -52,6 +52,6 @@ public function add($vm, $portGroup, $networkAdapter, $macAddr)
{
// vm.network.add -vm string -net string -net.adapter string -net.address string
$cmd = [$this->goVcBin, 'vm.network.change', '-vm', $vm, '-net', $portGroup, '-net.adapter', $networkAdapter, '-net.address', $macAddr];
ProcessHelper::runAsync($cmd);
return ProcessHelper::runAsync($cmd);
}
}
71 changes: 37 additions & 34 deletions src/Cmds/VM/VM.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace MisakaCloud\GoVC\Cmds\VM;

use MisakaCloud\GoVC\Cmds\VM\Network\Network;
use MisakaCloud\GoVc\Helper\ProcessHelper;
use MisakaCloud\GoVC\Helper\ProcessHelper;
use MisakaCloud\GoVC\Helper\TypeHelper;

/**
* Class VM
Expand Down Expand Up @@ -41,12 +42,14 @@ public function network()
* @param $host
* @param $mac
* @param $dataStore
* @param $powerOn
*/
public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSnapshot, $host, $mac, $dataStore, $powerOn)
{
// 基础命令
// govc vm.clone -host dstHost -net.address= MACAddr -vm template-vm
$cmd = [$this->goVcBin, 'vm.clone', '-host=', $host, '-net.address=', $mac, '-vm', $vmTemplate, '-d', $dataStore,'-on',$powerOn];
$vmPowerParameter = '-on=' . TypeHelper::boolToString($powerOn);
$cmd = [$this->goVcBin, 'vm.clone', '-vm', $vmTemplate, '-host', $host, '-net.address=' . $mac, '-ds', $dataStore, $vmPowerParameter, '-dump=true'];
$modeParameter = [];
// 如果你不写快照 那么就禁止使用快照克隆
if ($vmSnapshot == null) {
Expand All @@ -55,24 +58,24 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn
if ($useSnapshot == true & $useLink == true) {
// 快照克隆模式 使用链接
// -link -snapshot s-name
$modeParameter = ['-link', '-snapshot', $vmSnapshot,];
$modeParameter = ['-link=' . TypeHelper::boolToString($useLink), '-snapshot', $vmSnapshot];
} elseif ($useSnapshot == true & $useLink == false) {
// 快照克隆模式 不使用链接
// -snapshot s-name
$modeParameter = ['-snapshot', $vmSnapshot];
} elseif ($useSnapshot == false & $useLink == true) {
// 普通克隆模式 使用链接
// -link
$modeParameter = ['-link'];
$modeParameter = ['-link=' . $useLink];
} elseif ($useSnapshot == false & $useLink == false) {
// 普通克隆模式 不使用链接
//
$modeParameter = [];
}
// 连接命令行 最后加入目标虚拟机名字
// new-vm
array_merge($cmd, $modeParameter, [$vmDestination]);
ProcessHelper::runAsync($cmd);
$cmd = array_merge($cmd, $modeParameter, [$vmDestination]);
return ProcessHelper::runAsync($cmd);
}

/**
Expand All @@ -83,14 +86,14 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn
*/
public function info($vm, $showExtra, $showResource, $showToolsConfigInfo)
{
$showExtraParameter = '-e=' . $showExtra;
$showResourceParameter = '-r=' . $showResource;
$showToolsConfigInfoParameter = '-t=' . $showToolsConfigInfo;
$showExtraParameter = '-e=' . TypeHelper::boolToString($showExtra);
$showResourceParameter = '-r=' . TypeHelper::boolToString($showResource);
$showToolsConfigInfoParameter = '-t=' . TypeHelper::boolToString($showToolsConfigInfo);
// 查询需虚拟机信息
// govc vm.info -e=false -g=true -r=false -t=false

$cmd = [$this->goVcBin, 'vm.info', '-json', $showExtraParameter, $showResourceParameter, $showToolsConfigInfoParameter, $vm];
ProcessHelper::runAsync($cmd);
return ProcessHelper::runAsync($cmd);
}

/**
Expand Down Expand Up @@ -120,111 +123,111 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC
if (!empty($annotation)) {
// -annotation string
$annotationParameter = ['-annotation', $annotation];
array_merge($cmd, $annotationParameter);
$cmd = array_merge($cmd, $annotationParameter);
}

// CPU热添加
if (!empty($cpuHotAdd)) {
// -cpu-hot-add-enabled bool
$cpuHotAddParameter = ['-cpu-hot-add-enabled', $cpuHotAdd];
array_merge($cmd, $cpuHotAddParameter);
$cmd = array_merge($cmd, $cpuHotAddParameter);
}

// CPU限制方面不填写 则设置为 -1 代表不受限制
if (!empty($cpuLimit)) {
// -cpu.limit int
$cpuLimitParameter = ['-cpu.limit', $cpuLimit];
array_merge($cmd, $cpuLimitParameter);
$cmd = array_merge($cmd, $cpuLimitParameter);
}

// CPU虚拟化计数器
if (!empty($cpuPerformanceCounter)) {
// -vpmc-enabled bool
$cpuPerformanceCounterParameter = ['-vpmc-enabled', $cpuPerformanceCounter];
array_merge($cmd, $cpuPerformanceCounterParameter);
$cmd = array_merge($cmd, $cpuPerformanceCounterParameter);
}

// CPU保留 最少为 0
if (!empty($cpuReservation)) {
// -cpu.reservation int
$cpuReservationParameter = ['-cpu.reservation', $cpuReservation];
array_merge($cmd, $cpuReservationParameter);
$cmd = array_merge($cmd, $cpuReservationParameter);
}

// CPU核心数
if (!empty($cpus)) {
// -c int
$cpusParameter = ['-c', $cpus];
array_merge($cmd, $cpusParameter);
$cmd = array_merge($cmd, $cpusParameter);
}

// CPU份额
if (!empty($cpuShares)) {
// -cpu.shares {normal,high,low}
// -cpu.shares int
$cpuSharesParameter = ['-cpu.shares', $cpuShares];
array_merge($cmd, $cpuSharesParameter);
$cmd = array_merge($cmd, $cpuSharesParameter);
}

// GuestOS 客户机系统类型
if (!empty($guestOS)) {
// -g string
$guestOSParameter = ['-g', $guestOS];
array_merge($cmd, $guestOSParameter);
$cmd = array_merge($cmd, $guestOSParameter);
}

// 内存限制 同CPU
if (!empty($memoryLimit)) {
// -mem.limit int
$memoryLimitParameter = ['-mem.limit', $memoryLimit];
array_merge($cmd, $memoryLimitParameter);
$cmd = array_merge($cmd, $memoryLimitParameter);
}

// 内存保留 同CPU
if (!empty($memoryReservation)) {
// -mem.reservation int
$memoryReservationParameter = ['-mem.limit', $memoryReservation];
array_merge($cmd, $memoryReservationParameter);
$cmd = array_merge($cmd, $memoryReservationParameter);
}

// 内存大小 单位是MB
if (!empty($memory)) {
// -m int
$memoryParameter = ['-m', $memory];
array_merge($cmd, $memoryParameter);
$cmd = array_merge($cmd, $memoryParameter);
}

// 内存热添加
if (!empty($memoryHotAdd)) {
// -memory-hot-add-enabled bool
$memoryHotAddParameter = ['-memory-hot-add-enabled', $memoryHotAdd];
array_merge($cmd, $memoryHotAddParameter);
$cmd = array_merge($cmd, $memoryHotAddParameter);
}

// 内存份额 同CPU
if (!empty($memoryShare)) {
// -mem.shares {normal,high,low}
// -mem.shares int
$memoryShareParameter = ['-mem.shares', $memoryShare];
array_merge($cmd, $memoryShareParameter);
$cmd = array_merge($cmd, $memoryShareParameter);
}

// 嵌套虚拟化
if (!empty($nestedHvEnabled)) {
// -nested-hv-enabled bool
$nestedHvEnabledParameter = ['-nested-hv-enabled', $nestedHvEnabled];
array_merge($cmd, $nestedHvEnabledParameter);
$cmd = array_merge($cmd, $nestedHvEnabledParameter);
}

// 同步系统时间
if (!empty($syncTimeWithHost)) {
// -sync-time-with-host bool
$syncTimeWithHostParameter = ['-sync-time-with-host', $syncTimeWithHost];
array_merge($cmd, $syncTimeWithHostParameter);
$cmd = array_merge($cmd, $syncTimeWithHostParameter);
}

// 最后就是运行了哦
ProcessHelper::runAsync($cmd);
return ProcessHelper::runAsync($cmd);
}

/**
Expand Down Expand Up @@ -265,22 +268,22 @@ public function power($vm, $powerActionType, $force, $waitForComplete)
break;
}

array_merge($cmd, $powerActionTypeParameter);
$cmd = array_merge($cmd, $powerActionTypeParameter);

if (!empty($waitForComplete)) {
// -wait bool
$waitForCompleteParameter = ['-wait', $waitForComplete];
array_merge($cmd, $waitForCompleteParameter);
$waitForCompleteParameter = ['-wait', TypeHelper::boolToString($waitForComplete)];
$cmd = array_merge($cmd, $waitForCompleteParameter);
}

if (!empty($force)) {
// -force bool
$forceParameter = ['-force', $force];
array_merge($cmd, $forceParameter);
$forceParameter = ['-force=', TypeHelper::boolToString($force)];
$cmd = array_merge($cmd, $forceParameter);
}

array_merge($cmd, [$vm]);
ProcessHelper::runAsync($cmd);
$cmd = array_merge($cmd, [$vm]);
return ProcessHelper::runAsync($cmd);
}

/**
Expand All @@ -290,6 +293,6 @@ public function destroy($vm)
{
// vm.destroy
$cmd = [$this->goVcBin, 'vm.destroy', $vm];
ProcessHelper::runAsync($cmd);
return ProcessHelper::runAsync($cmd);
}
}
20 changes: 15 additions & 5 deletions src/Helper/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MisakaCloud\GoVC\Helper;


use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class ProcessHelper
Expand All @@ -14,19 +15,28 @@ public static function runAsync($cmd)
global $globalGoVcDataCenter;
global $globalProcessTimeout;
$process = new Process($cmd, null, ['GOVC_URL' => $globalGoVcURL, 'GOVC_DATACENTER' => $globalGoVcDataCenter]);
$process->enableOutput();
$process->setTimeout($globalProcessTimeout);

$process->run();
// function ($type, $buffer) {
// if (Process::ERR === $type) {
// echo 'ERR > ' . $buffer;
// } else {
// echo 'OUT > ' . $buffer;
// }
// }


while ($process->isRunning()) {
echo "正在执行任务";
sleep(4);
echo "正在运行";
$process->setTimeout(10);
}
// 失败处理
if (!$process->isSuccessful()) {
// throw new ProcessFailedException($process);
return $process->getErrorOutput();
throw new ProcessFailedException($process->getOutput());
// return $process->getOutput();
}

return $process->getOutput();
}
}
Loading

0 comments on commit e4890c8

Please sign in to comment.