实现从主机创建到远程部署服务的自动化流程
主机部分使用了aws的sdk
创建实例
1 2 3 4 5 6 |
$result = $LightsailClient->createInstances([ 'availabilityZone' => $availabilityZone, 'blueprintId' => $blueprintId, 'bundleId' => $bundleId, 'instanceNames' => [$instanceName], ]); |
打开端口
1 2 3 4 5 6 7 8 9 10 11 |
$result = $LightsailClient->openInstancePublicPorts([ 'instanceName' => $instanceName, // REQUIRED 'portInfo' => [ // REQUIRED 'cidrListAliases' => [], 'cidrs' => ['0.0.0.0/0'], 'ipv6Cidrs' => ['::/0'], 'fromPort' => 0, 'toPort' => 65535, 'protocol' => 'all', ], ]); |
注入服务部分使用了Spatie\Ssh
使用本地证书连接主机并执行预设命令
1 2 3 4 |
$process = Ssh::create('admin', $data['publicIpAddress']) ->usePrivateKey('/.aws/LightsailDefaultKey-ap-southeast-1.pem') ->disableStrictHostKeyChecking() ->execute(['sudo whoami']); |
获取输出结果
1 |
$data['getOutput'] = preg_split("/\r\n|\n|\r/", $process->getOutput()); |
不过在执行批量命令的时候遇到了超时,需要手动设置超时。
There are no comments yet