From 05e4fd208aa24ce34f35746ca2b2625e67393f2b Mon Sep 17 00:00:00 2001 From: gzzchh Date: Sun, 6 Oct 2019 22:47:28 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cmds/VM/Network/Network.php | 2 +- src/Cmds/VM/VM.php | 2 +- src/Outdated/CredentialHelper.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Cmds/VM/Network/Network.php b/src/Cmds/VM/Network/Network.php index b8218d2..8beb009 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 diff --git a/src/Cmds/VM/VM.php b/src/Cmds/VM/VM.php index 8c930e2..257a4cc 100644 --- a/src/Cmds/VM/VM.php +++ b/src/Cmds/VM/VM.php @@ -3,7 +3,7 @@ namespace MisakaCloud\GoVC\Cmds\VM; use MisakaCloud\GoVC\Cmds\VM\Network\Network; -use MisakaCloud\GoVc\Helper\ProcessHelper; +use MisakaCloud\GoVC\Helper\ProcessHelper; /** * Class VM diff --git a/src/Outdated/CredentialHelper.php b/src/Outdated/CredentialHelper.php index 130f26f..5c79534 100644 --- a/src/Outdated/CredentialHelper.php +++ b/src/Outdated/CredentialHelper.php @@ -1,12 +1,12 @@ Date: Sun, 6 Oct 2019 23:37:35 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20array=5Fmerge=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/symfony2.xml | 6 ++++++ src/Cmds/VM/VM.php | 42 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 .idea/symfony2.xml 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/src/Cmds/VM/VM.php b/src/Cmds/VM/VM.php index 257a4cc..896aa78 100644 --- a/src/Cmds/VM/VM.php +++ b/src/Cmds/VM/VM.php @@ -46,7 +46,7 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn { // 基础命令 // 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]; + $cmd = [$this->goVcBin, 'vm.clone', '-host', $host, '-net.address', $mac, '-vm', $vmTemplate, '-d', $dataStore,'-on',$powerOn]; $modeParameter = []; // 如果你不写快照 那么就禁止使用快照克隆 if ($vmSnapshot == null) { @@ -71,7 +71,7 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn } // 连接命令行 最后加入目标虚拟机名字 // new-vm - array_merge($cmd, $modeParameter, [$vmDestination]); + $cmd=array_merge($cmd, $modeParameter, [$vmDestination]); ProcessHelper::runAsync($cmd); } @@ -120,42 +120,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 +163,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,21 +206,21 @@ 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); } // 最后就是运行了哦 @@ -265,21 +265,21 @@ 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); + $cmd=array_merge($cmd, $waitForCompleteParameter); } if (!empty($force)) { // -force bool $forceParameter = ['-force', $force]; - array_merge($cmd, $forceParameter); + $cmd=array_merge($cmd, $forceParameter); } - array_merge($cmd, [$vm]); + $cmd=array_merge($cmd, [$vm]); ProcessHelper::runAsync($cmd); } From 72b87e1e6cf52ebbbc6556f65e4164013ab75862 Mon Sep 17 00:00:00 2001 From: gzzchh Date: Mon, 7 Oct 2019 00:06:01 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BB=8D=E9=9C=80=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cmds/VM/VM.php | 43 ++++++++++++++++++++++--------------------- test.php | 13 ------------- 2 files changed, 22 insertions(+), 34 deletions(-) delete mode 100644 test.php diff --git a/src/Cmds/VM/VM.php b/src/Cmds/VM/VM.php index 896aa78..286ddf2 100644 --- a/src/Cmds/VM/VM.php +++ b/src/Cmds/VM/VM.php @@ -41,12 +41,13 @@ 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]; + $cmd = [$this->goVcBin, 'vm.clone', '-vm', $vmTemplate, '-host', $host, '-net.address', $mac, '-ds', $dataStore, '-on', $powerOn]; $modeParameter = []; // 如果你不写快照 那么就禁止使用快照克隆 if ($vmSnapshot == null) { @@ -71,7 +72,7 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn } // 连接命令行 最后加入目标虚拟机名字 // new-vm - $cmd=array_merge($cmd, $modeParameter, [$vmDestination]); + $cmd = array_merge($cmd, $modeParameter, [$vmDestination]); ProcessHelper::runAsync($cmd); } @@ -120,42 +121,42 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC if (!empty($annotation)) { // -annotation string $annotationParameter = ['-annotation', $annotation]; - $cmd=array_merge($cmd, $annotationParameter); + $cmd = array_merge($cmd, $annotationParameter); } // CPU热添加 if (!empty($cpuHotAdd)) { // -cpu-hot-add-enabled bool $cpuHotAddParameter = ['-cpu-hot-add-enabled', $cpuHotAdd]; - $cmd=array_merge($cmd, $cpuHotAddParameter); + $cmd = array_merge($cmd, $cpuHotAddParameter); } // CPU限制方面不填写 则设置为 -1 代表不受限制 if (!empty($cpuLimit)) { // -cpu.limit int $cpuLimitParameter = ['-cpu.limit', $cpuLimit]; - $cmd=array_merge($cmd, $cpuLimitParameter); + $cmd = array_merge($cmd, $cpuLimitParameter); } // CPU虚拟化计数器 if (!empty($cpuPerformanceCounter)) { // -vpmc-enabled bool $cpuPerformanceCounterParameter = ['-vpmc-enabled', $cpuPerformanceCounter]; - $cmd=array_merge($cmd, $cpuPerformanceCounterParameter); + $cmd = array_merge($cmd, $cpuPerformanceCounterParameter); } // CPU保留 最少为 0 if (!empty($cpuReservation)) { // -cpu.reservation int $cpuReservationParameter = ['-cpu.reservation', $cpuReservation]; - $cmd=array_merge($cmd, $cpuReservationParameter); + $cmd = array_merge($cmd, $cpuReservationParameter); } // CPU核心数 if (!empty($cpus)) { // -c int $cpusParameter = ['-c', $cpus]; - $cmd=array_merge($cmd, $cpusParameter); + $cmd = array_merge($cmd, $cpusParameter); } // CPU份额 @@ -163,42 +164,42 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC // -cpu.shares {normal,high,low} // -cpu.shares int $cpuSharesParameter = ['-cpu.shares', $cpuShares]; - $cmd=array_merge($cmd, $cpuSharesParameter); + $cmd = array_merge($cmd, $cpuSharesParameter); } // GuestOS 客户机系统类型 if (!empty($guestOS)) { // -g string $guestOSParameter = ['-g', $guestOS]; - $cmd=array_merge($cmd, $guestOSParameter); + $cmd = array_merge($cmd, $guestOSParameter); } // 内存限制 同CPU if (!empty($memoryLimit)) { // -mem.limit int $memoryLimitParameter = ['-mem.limit', $memoryLimit]; - $cmd=array_merge($cmd, $memoryLimitParameter); + $cmd = array_merge($cmd, $memoryLimitParameter); } // 内存保留 同CPU if (!empty($memoryReservation)) { // -mem.reservation int $memoryReservationParameter = ['-mem.limit', $memoryReservation]; - $cmd=array_merge($cmd, $memoryReservationParameter); + $cmd = array_merge($cmd, $memoryReservationParameter); } // 内存大小 单位是MB if (!empty($memory)) { // -m int $memoryParameter = ['-m', $memory]; - $cmd=array_merge($cmd, $memoryParameter); + $cmd = array_merge($cmd, $memoryParameter); } // 内存热添加 if (!empty($memoryHotAdd)) { // -memory-hot-add-enabled bool $memoryHotAddParameter = ['-memory-hot-add-enabled', $memoryHotAdd]; - $cmd=array_merge($cmd, $memoryHotAddParameter); + $cmd = array_merge($cmd, $memoryHotAddParameter); } // 内存份额 同CPU @@ -206,21 +207,21 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC // -mem.shares {normal,high,low} // -mem.shares int $memoryShareParameter = ['-mem.shares', $memoryShare]; - $cmd=array_merge($cmd, $memoryShareParameter); + $cmd = array_merge($cmd, $memoryShareParameter); } // 嵌套虚拟化 if (!empty($nestedHvEnabled)) { // -nested-hv-enabled bool $nestedHvEnabledParameter = ['-nested-hv-enabled', $nestedHvEnabled]; - $cmd=array_merge($cmd, $nestedHvEnabledParameter); + $cmd = array_merge($cmd, $nestedHvEnabledParameter); } // 同步系统时间 if (!empty($syncTimeWithHost)) { // -sync-time-with-host bool $syncTimeWithHostParameter = ['-sync-time-with-host', $syncTimeWithHost]; - $cmd=array_merge($cmd, $syncTimeWithHostParameter); + $cmd = array_merge($cmd, $syncTimeWithHostParameter); } // 最后就是运行了哦 @@ -265,21 +266,21 @@ public function power($vm, $powerActionType, $force, $waitForComplete) break; } - $cmd=array_merge($cmd, $powerActionTypeParameter); + $cmd = array_merge($cmd, $powerActionTypeParameter); if (!empty($waitForComplete)) { // -wait bool $waitForCompleteParameter = ['-wait', $waitForComplete]; - $cmd=array_merge($cmd, $waitForCompleteParameter); + $cmd = array_merge($cmd, $waitForCompleteParameter); } if (!empty($force)) { // -force bool $forceParameter = ['-force', $force]; - $cmd=array_merge($cmd, $forceParameter); + $cmd = array_merge($cmd, $forceParameter); } - $cmd=array_merge($cmd, [$vm]); + $cmd = array_merge($cmd, [$vm]); ProcessHelper::runAsync($cmd); } diff --git a/test.php b/test.php deleted file mode 100644 index dd366dc..0000000 --- a/test.php +++ /dev/null @@ -1,13 +0,0 @@ -vm()->network()->change(); From d78e560ca291cec45fcb7e2317c8bd2ecdd6e7a3 Mon Sep 17 00:00:00 2001 From: gzzchh Date: Thu, 10 Oct 2019 23:01:33 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=AE=E5=89=8D?= =?UTF-8?q?=E5=8F=AF=E8=A7=81=E7=9A=84=E4=BB=BB=E4=BD=95=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E9=94=99=E8=AF=AF=20=E6=B7=BB=E5=8A=A0TypeHelper?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/deployment.xml | 13 ++++++++++++- .idea/webServers.xml | 15 +++++++++++++++ src/Cmds/VM/VM.php | 18 ++++++++++-------- src/Helper/ProcessHelper.php | 13 ++++++++----- src/Helper/TypeHelper.php | 28 ++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 .idea/webServers.xml create mode 100644 src/Helper/TypeHelper.php diff --git a/.idea/deployment.xml b/.idea/deployment.xml index 0c72006..8f772ca 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -1,7 +1,17 @@ - + + + + + + + + + + + @@ -17,5 +27,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/src/Cmds/VM/VM.php b/src/Cmds/VM/VM.php index 286ddf2..e667b2c 100644 --- a/src/Cmds/VM/VM.php +++ b/src/Cmds/VM/VM.php @@ -4,6 +4,7 @@ use MisakaCloud\GoVC\Cmds\VM\Network\Network; use MisakaCloud\GoVC\Helper\ProcessHelper; +use MisakaCloud\GoVC\Helper\TypeHelper; /** * Class VM @@ -47,7 +48,8 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn { // 基础命令 // govc vm.clone -host dstHost -net.address= MACAddr -vm template-vm - $cmd = [$this->goVcBin, 'vm.clone', '-vm', $vmTemplate, '-host', $host, '-net.address', $mac, '-ds', $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) { @@ -56,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 @@ -64,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) { // 普通克隆模式 不使用链接 // @@ -84,9 +86,9 @@ 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 @@ -270,13 +272,13 @@ public function power($vm, $powerActionType, $force, $waitForComplete) if (!empty($waitForComplete)) { // -wait bool - $waitForCompleteParameter = ['-wait', $waitForComplete]; + $waitForCompleteParameter = ['-wait', TypeHelper::boolToString($waitForComplete)]; $cmd = array_merge($cmd, $waitForCompleteParameter); } if (!empty($force)) { // -force bool - $forceParameter = ['-force', $force]; + $forceParameter = ['-force=', TypeHelper::boolToString($force)]; $cmd = array_merge($cmd, $forceParameter); } diff --git a/src/Helper/ProcessHelper.php b/src/Helper/ProcessHelper.php index cac71f8..10ff343 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,21 @@ 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); +// print_r($process->getEnv()); +// print_r($process->getCommandLine()); $process->run(); while ($process->isRunning()) { - echo "正在执行任务"; - sleep(4); + echo "正在运行"; + echo $process->getOutput(); } // 失败处理 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 @@ + Date: Tue, 15 Oct 2019 23:15:05 +0800 Subject: [PATCH 5/5] no message --- .gitignore | 1 + .idea/deployment.xml | 2 +- README.md | 3 +-- composer.json | 1 + src/Cmds/VM/Network/Network.php | 4 ++-- src/Cmds/VM/VM.php | 10 +++++----- src/Helper/ProcessHelper.php | 13 ++++++++++--- 7 files changed, 21 insertions(+), 13 deletions(-) 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 8f772ca..e4be839 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -19,7 +19,7 @@ - + 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 8beb009..78569e0 100644 --- a/src/Cmds/VM/Network/Network.php +++ b/src/Cmds/VM/Network/Network.php @@ -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 e667b2c..fc89d36 100644 --- a/src/Cmds/VM/VM.php +++ b/src/Cmds/VM/VM.php @@ -75,7 +75,7 @@ public function clone($vmTemplate, $vmSnapshot, $vmDestination, $useLink, $useSn // 连接命令行 最后加入目标虚拟机名字 // new-vm $cmd = array_merge($cmd, $modeParameter, [$vmDestination]); - ProcessHelper::runAsync($cmd); + return ProcessHelper::runAsync($cmd); } /** @@ -93,7 +93,7 @@ public function info($vm, $showExtra, $showResource, $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); } /** @@ -227,7 +227,7 @@ public function change($vm, $annotation, $cpuHotAdd, $cpuLimit, $cpuPerformanceC } // 最后就是运行了哦 - ProcessHelper::runAsync($cmd); + return ProcessHelper::runAsync($cmd); } /** @@ -283,7 +283,7 @@ public function power($vm, $powerActionType, $force, $waitForComplete) } $cmd = array_merge($cmd, [$vm]); - ProcessHelper::runAsync($cmd); + return ProcessHelper::runAsync($cmd); } /** @@ -293,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 10ff343..c94d0eb 100644 --- a/src/Helper/ProcessHelper.php +++ b/src/Helper/ProcessHelper.php @@ -17,13 +17,20 @@ public static function runAsync($cmd) $process = new Process($cmd, null, ['GOVC_URL' => $globalGoVcURL, 'GOVC_DATACENTER' => $globalGoVcDataCenter]); $process->enableOutput(); $process->setTimeout($globalProcessTimeout); -// print_r($process->getEnv()); -// print_r($process->getCommandLine()); + $process->run(); +// function ($type, $buffer) { +// if (Process::ERR === $type) { +// echo 'ERR > ' . $buffer; +// } else { +// echo 'OUT > ' . $buffer; +// } +// } + while ($process->isRunning()) { echo "正在运行"; - echo $process->getOutput(); + $process->setTimeout(10); } // 失败处理 if (!$process->isSuccessful()) {