/** * {@inheritdoc} */ public function provisionWith(InstanceAccessInterface $access, array $scripts) { $access->connect($this->getHost()); foreach ($scripts as $script) { $access->exec($script); } }
/** * {@inheritdoc} */ public function launchInstances(ImageInterface $image, $instanceCount = 1, array $instanceConfig = [], InstanceAccessInterface $instanceAccess = null, array $userDataConfig = []) { $ec2Client = $this->getEc2Client(); $config = array_merge($instanceConfig, ['ImageId' => $image->getId(), 'MinCount' => $instanceCount, 'MaxCount' => $instanceCount]); if (count($userDataConfig)) { $yamlDumper = new Dumper(); $config['UserData'] = base64_encode("#cloud-config\n" . $yamlDumper->dump($userDataConfig)); } if ($instanceAccess instanceof InstanceAccess) { $keyName = $instanceAccess->getKeyName(); $this->info(sprintf('Using provided aws instance access (as KeyName = \'%s\' for launching instance(s) ...', $keyName)); if (!$instanceAccess->hasKey()) { $this->info('Provided aws instance access, does not have a key!'); $response = $ec2Client->deleteKeyPair(array('KeyName' => $keyName)); $this->info(sprintf('Deleted \'%s\' key pair in aws', $keyName), ['response' => $response->toArray()]); $response = $ec2Client->createKeyPair(array('KeyName' => $keyName)); $this->info(sprintf('Created new \'%s\' key pair in aws', $keyName), ['response' => $response->toArray()]); $instanceAccess->setPrivateKey($response->getPath('KeyMaterial')); } $config['KeyName'] = $keyName; } $this->info(sprintf('Launching %s instance(s) ...', $instanceCount), ['request' => $config]); $responses = $ec2Client->runInstances($config); $instanceIds = $responses->getPath('Instances/*/InstanceId'); $this->info('Waiting for instance(s) to be ready ...', ['instance.ids' => $instanceIds]); $ec2Client->waitUntilInstanceRunning(array('InstanceIds' => $instanceIds)); return $this->convertToInstances($instanceIds); }