Exemple #1
0
 public function create($name, $region, $size, $image, SshPublicKey $key = null)
 {
     $hasSshKey = !is_null($key);
     $deleteSshKey = false;
     $attributes = ['name' => $name, 'region' => $region, 'size' => $size, 'image' => $image, 'ipv6' => true, 'private_networking' => true];
     if ($hasSshKey) {
         try {
             // Import the ssh key so we can attach it to the machine
             $sshKeyId = $this->addSshKey($key);
             $deleteSshKey = true;
         } catch (RequestException $e) {
             // The key has most likely already been uploaded to the account
             // so just get the id for it
             $response = $this->sendRequest('get', 'account/keys/' . $key->getFingerprint());
             $sshKeyId = $response['ssh_key']['id'];
         }
         $attributes['ssh_keys'] = [$sshKeyId];
     }
     $this->emit('machine.create');
     $apiMachine = $this->sendRequest('post', 'droplets', $attributes);
     // Wait until the machine is up and has ip addresses assigned to it
     Action::waitUntilActionCompletes($this, $apiMachine['links']['actions'][0]['id']);
     $this->emit('machine.up');
     // Get the machine so we can get an ip-address to connect to
     $machine = $this->getMachine($apiMachine['droplet']['id']);
     // Do not return until SSH is up and we can connect
     $this->emit('ssh.waiting');
     $defaultTimeout = ini_set('default_socket_timeout', 5);
     while (true) {
         try {
             if (fsockopen($machine->getPublicIpv4(), 22)) {
                 break;
             }
         } catch (\ErrorException $e) {
         }
         sleep(2);
     }
     ini_set('default_socket_timeout', $defaultTimeout);
     $this->emit('ssh.up');
     if ($hasSshKey && $deleteSshKey) {
         // Remove the ssh key since we dont need it anymore now that the
         // machine is created and the key has been added to it
         $this->removeSshKey($key);
     }
     return $machine;
 }
Exemple #2
0
 private function runAction($action)
 {
     $apiAction = $this->provider->sendRequest('post', "droplets/{$this->id}/actions", ['type' => $action]);
     Action::waitUntilActionCompletes($this->provider, $apiAction['action']['id']);
 }