diff --git a/.gitignore b/.gitignore index a67d42b..0366bde 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.idea/deployment.xml b/.idea/deployment.xml index 0c72006..e4be839 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -1,7 +1,17 @@ - + + + + + + + + + + + @@ -9,7 +19,7 @@ - + @@ -17,5 +27,6 @@ + \ No newline at end of file diff --git a/.idea/symfony2.xml b/.idea/symfony2.xml new file mode 100644 index 0000000..bd98e40 --- /dev/null +++ b/.idea/symfony2.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/webServers.xml b/.idea/webServers.xml new file mode 100644 index 0000000..2d5cb26 --- /dev/null +++ b/.idea/webServers.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 2645268..922dc4b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ # StuInfoMgr ## 允许的变量表 -* GOVC_DATACENTER -* GOVC_URL +此软件包已废弃 diff --git a/composer.json b/composer.json index 57c75e6..4e574f0 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "type": "library", "license": "GPL-3.0-or-later", "minimum-stability": "dev", + "prefer-stable" : true, "require": { "symfony/process": "^4.3" }, diff --git a/src/Cmds/VM/Network/Network.php b/src/Cmds/VM/Network/Network.php index b8218d2..78569e0 100644 --- a/src/Cmds/VM/Network/Network.php +++ b/src/Cmds/VM/Network/Network.php @@ -2,7 +2,7 @@ namespace MisakaCloud\GoVC\Cmds\VM\Network; -use MisakaCloud\GoVc\Helper\ProcessHelper; +use MisakaCloud\GoVC\Helper\ProcessHelper; /** * Class Network @@ -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); } /** @@ -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); } } diff --git a/src/Cmds/VM/VM.php b/src/Cmds/VM/VM.php index 8c930e2..fc89d36 100644 --- a/src/Cmds/VM/VM.php +++ b/src/Cmds/VM/VM.php @@ -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 @@ -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) { @@ -55,7 +58,7 @@ 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 @@ -63,7 +66,7 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn } elseif ($useSnapshot == false & $useLink == true) { // 普通克隆模式 使用链接 // -link - $modeParameter = ['-link']; + $modeParameter = ['-link=' . $useLink]; } elseif ($useSnapshot == false & $useLink == false) { // 普通克隆模式 不使用链接 // @@ -71,8 +74,8 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn } // 连接命令行 最后加入目标虚拟机名字 // new-vm - array_merge($cmd, $modeParameter, [$vmDestination]); - ProcessHelper::runAsync($cmd); + $cmd = array_merge($cmd, $modeParameter, [$vmDestination]); + return ProcessHelper::runAsync($cmd); } /** @@ -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); } /** @@ -120,42 +123,42 @@ 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份额 @@ -163,42 +166,42 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC // -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 @@ -206,25 +209,25 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC // -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); } /** @@ -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); } /** @@ -290,6 +293,6 @@ public function destroy($vm) { // vm.destroy $cmd = [$this->goVcBin, 'vm.destroy', $vm]; - ProcessHelper::runAsync($cmd); + return ProcessHelper::runAsync($cmd); } } diff --git a/src/Helper/ProcessHelper.php b/src/Helper/ProcessHelper.php index cac71f8..c94d0eb 100644 --- a/src/Helper/ProcessHelper.php +++ b/src/Helper/ProcessHelper.php @@ -4,6 +4,7 @@ namespace MisakaCloud\GoVC\Helper; +use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; class ProcessHelper @@ -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(); } } diff --git a/src/Helper/TypeHelper.php b/src/Helper/TypeHelper.php new file mode 100644 index 0000000..ec64cab --- /dev/null +++ b/src/Helper/TypeHelper.php @@ -0,0 +1,28 @@ +vm()->network()->change();