首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建实例时的GCP setMetadata启动脚本

创建实例时的GCP setMetadata启动脚本
EN

Stack Overflow用户
提问于 2021-09-07 07:28:02
回答 1查看 72关注 0票数 1

我正在尝试创建实例,分配IP并设置启动脚本,我设法创建实例并为其分配IP,但我不知道如何添加启动脚本。

以下是我的代码请帮助:

代码语言:javascript
复制
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();

我尝试添加以下内容:

代码语言:javascript
复制
        $metaItems = (new Items())
            ->setKey('startup-script')
            ->setValue('#_some_cmnd_I_want_to_exec_#');
        $metaData = (new Metadata())
            ->setItems([$metaItems]);

但它没有工作,我知道它很混乱,甚至可能写得很糟糕,但我是编程新手。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-07 10:12:47

好了,我刚刚弄明白了我去掉了"“和"”

从…

代码语言:javascript
复制
$instance = (new Instance())
    ->setName($instanceName)
    ->setDisks([$disk])
    ->setMachineType($machineTypeFullName)
    ->setNetworkInterfaces([$network])
    ->setMetadata([$metaData]);

代码语言:javascript
复制
$instance = (new Instance())
    ->setName($instanceName)
    ->setDisks([$disk])
    ->setMachineType($machineTypeFullName)
    ->setNetworkInterfaces([$network])
    ->setMetadata($metaData);

现在它可以工作了

下面是完整的代码:

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69084006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档