我正在尝试创建实例,分配IP并设置启动脚本,我设法创建实例并为其分配IP,但我不知道如何添加启动脚本。
以下是我的代码请帮助:
namespace Google\Cloud\Samples\Compute;
require_once 'vendor/autoload.php';
use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\AttachedDisk;
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;
use Google\Cloud\Compute\V1\Operation;
use Google\Cloud\Compute\V1\ZoneOperationsClient;
use Google\Cloud\Compute\V1\AccessConfig;
use Google\Cloud\Compute\V1\Items;
use Google\Cloud\Compute\V1\Metadata;
function create_instance() {
$projectId = 'impactful-name-324714';
$zone = 'us-central1-c';
$instanceName = 'test1-micro';
$machineType = 'e2-micro';
$sourceImage = 'projects/centos-cloud/global/images/family/centos-7';
$networkName = 'global/networks/default';
$networkTier = 'PREMIUM';
// Set the machine type using the specified zone.
$machineTypeFullName = sprintf('zones/%s/machineTypes/%s', $zone, $machineType);
// Describe the source image of the boot disk to attach to the instance.
$diskInitializeParams = (new AttachedDiskInitializeParams())
->setSourceImage($sourceImage);
$disk = (new AttachedDisk())
->setBoot(true)
->setInitializeParams($diskInitializeParams);
// Use the network interface provided in the $networkName argument.
$accessConfig = (new AccessConfig())
->setName('PREMIUM');
$network = (new NetworkInterface())
->setAccessConfigs([$accessConfig]);
// Create the Instance object.
$instance = (new Instance())
->setName($instanceName)
->setDisks([$disk])
->setMachineType($machineTypeFullName)
->setNetworkInterfaces([$network])
->setMetadata([$metaData]);
// Insert the new Compute Engine instance using InstancesClient.
$instancesClient = new InstancesClient();
$operation = $instancesClient->insert($instance, $projectId, $zone);
// Wait for the create operation to complete.
if ($operation->getStatus() === Operation\Status::RUNNING) {
$operationClient = new ZoneOperationsClient();
$operationClient->wait($operation->getName(), $projectId, $zone);
}
printf('Created instance %s' . PHP_EOL, $instanceName);
}
putenv('GOOGLE_APPLICATION_CREDENTIALS=keyfile.json');
create_instance();我尝试添加以下内容:
$metaItems = (new Items())
->setKey('startup-script')
->setValue('#_some_cmnd_I_want_to_exec_#');
$metaData = (new Metadata())
->setItems([$metaItems]);但它没有工作,我知道它很混乱,甚至可能写得很糟糕,但我是编程新手。
Used resources :
http://googleapis.github.io/google-cloud-php/#/docs/cloud-compute/v0.3.1/compute/readme
https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute发布于 2021-09-07 10:12:47
好了,我刚刚弄明白了我去掉了"“和"”
从…
$instance = (new Instance())
->setName($instanceName)
->setDisks([$disk])
->setMachineType($machineTypeFullName)
->setNetworkInterfaces([$network])
->setMetadata([$metaData]);至
$instance = (new Instance())
->setName($instanceName)
->setDisks([$disk])
->setMachineType($machineTypeFullName)
->setNetworkInterfaces([$network])
->setMetadata($metaData);现在它可以工作了
下面是完整的代码:
namespace Google\Cloud\Samples\Compute;
require_once 'vendor/autoload.php';
use Google\Cloud\Compute\V1\InstancesClient;
use Google\Cloud\Compute\V1\AttachedDisk;
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\NetworkInterface;
use Google\Cloud\Compute\V1\Operation;
use Google\Cloud\Compute\V1\ZoneOperationsClient;
use Google\Cloud\Compute\V1\AccessConfig;
use Google\Cloud\Compute\V1\Items;
use Google\Cloud\Compute\V1\Metadata;
function create_instance() {
$projectId = 'impactful-name-324714';
$zone = 'us-central1-c';
$instanceName = 'test1-micro';
$machineType = 'e2-micro';
$sourceImage = 'projects/centos-cloud/global/images/family/centos-7';
$networkName = 'global/networks/default';
$networkTier = 'PREMIUM';
// Set the machine type using the specified zone.
$machineTypeFullName = sprintf('zones/%s/machineTypes/%s', $zone, $machineType);
// Describe the source image of the boot disk to attach to the instance.
$diskInitializeParams = (new AttachedDiskInitializeParams())
->setSourceImage($sourceImage);
$disk = (new AttachedDisk())
->setBoot(true)
->setInitializeParams($diskInitializeParams);
$metaItems = (new Items())
->setKey('startup-script')
->setValue('#_some_cmnd_I_want_to_exec_#');
$metaData = (new Metadata())
->setItems([$metaItems]);
// Use the network interface provided in the $networkName argument.
$accessConfig = (new AccessConfig())
->setName('PREMIUM');
$network = (new NetworkInterface())
->setAccessConfigs([$accessConfig]);
// Create the Instance object.
$instance = (new Instance())
->setName($instanceName)
->setDisks([$disk])
->setMachineType($machineTypeFullName)
->setNetworkInterfaces([$network])
->setMetadata($metaData);
// Insert the new Compute Engine instance using InstancesClient.
$instancesClient = new InstancesClient();
$operation = $instancesClient->insert($instance, $projectId, $zone);
// Wait for the create operation to complete.
if ($operation->getStatus() === Operation\Status::RUNNING) {
$operationClient = new ZoneOperationsClient();
$operationClient->wait($operation->getName(), $projectId, $zone);
}
printf('Created instance %s' . PHP_EOL, $instanceName);https://stackoverflow.com/questions/69084006
复制相似问题