GCP setMetadata 启动脚本到实例创建

GCP setMetadata startup script to instance on Creation

我正在尝试创建实例分配 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

好吧,我刚弄明白我删除了“[”和“]”

来自

$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);