Reputation: 21
I'm trying to create instance assign an IP and set a startup script, I managed to create the instance and assign IP to it but I have no idea how to add startup script.
Here is my Code Please help :
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();
I tried adding This :
$metaItems = (new Items())
->setKey('startup-script')
->setValue('#_some_cmnd_I_want_to_exec_#');
$metaData = (new Metadata())
->setItems([$metaItems]);
but it didn't work, I know its messy maybe even bad written but i'm new to coding.
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
Upvotes: 1
Views: 284
Reputation: 21
Alright I just figured it out i removed the "[" and "]"
from
$instance = (new Instance())
->setName($instanceName)
->setDisks([$disk])
->setMachineType($machineTypeFullName)
->setNetworkInterfaces([$network])
->setMetadata([$metaData]);
to
$instance = (new Instance())
->setName($instanceName)
->setDisks([$disk])
->setMachineType($machineTypeFullName)
->setNetworkInterfaces([$network])
->setMetadata($metaData);
Now it Works
the full code now :
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);
Upvotes: 1