Пример #1
0
 /**
  * Destroy a droplet.
  *
  * @param Host $host
  *
  * @return bool
  * @throws Exception
  */
 public function destroy(Host $host)
 {
     if (!$host instanceof Droplet) {
         throw new Exception(self::class . " can only destroy DigitalOcean droplets");
     }
     $this->api->droplet()->delete($host->getDroplet()->id);
     return true;
 }
Пример #2
0
 /**
  * Fetch fresh droplet information from the API.
  *
  * @return bool
  */
 public function fresh()
 {
     if (!$this->droplet instanceof ApiDroplet) {
         return false;
     }
     if ($droplet = $this->api->droplet()->getById($this->droplet->id)) {
         $this->setDroplet($droplet);
         return true;
     }
     return false;
 }
function get_all_droplet_names($addon_settings)
{
    $DOToken = $addon_settings["digitalocean_token"];
    if (empty($DOToken)) {
        return false;
    }
    $adapter = new BuzzAdapter($DOToken);
    $do = new DigitalOceanV2($adapter);
    $droplet = $do->droplet();
    $allDroplets = $droplet->getAll();
    if (empty($allDroplets)) {
        return false;
    }
    $theDroplets = array();
    foreach ($allDroplets as $droplet) {
        $theDroplets[] = $droplet->name;
    }
    return $theDroplets;
}
 function gfdo_paypal_successful_payment($entry, $config, $transaction_id, $amount)
 {
     //Gravity Forms + DigitalOcean object
     $GFDO = new GFDigitalOcean();
     //Log it up
     $GFDO->log_debug(__("GFDO - PAYPAL SUCCESFUL PAYMENT"));
     $GFDO->log_debug("ENTRY : " . print_r($entry, true));
     $GFDO->log_debug("CONFIG : " . print_r($config, true));
     //NULL the Droplet
     $newDroplet = null;
     //Get the form
     $form = GFFormsModel::get_form_meta($entry['form_id']);
     //Keep on loggin' in the free world
     $GFDO->log_debug("NEW FORM OBJECT : " . print_r($form, true));
     //Get form settings
     $form_settings = $GFDO->get_form_settings($form);
     //Get add-on settings
     $addon_settings = $GFDO->get_plugin_settings();
     //If it's not enabled, exit
     if (!$addon_settings || !$form_settings || !$form_settings["isEnabled"]) {
         return;
     }
     //API token
     $DOToken = $addon_settings["digitalocean_token"];
     //Return if token empty
     if (empty($DOToken)) {
         return;
     }
     //Get the chosen settings for the droplet
     $droplet_size = $form_settings["selectDropletSize"];
     $droplet_name = $entry[$form_settings["selectDropletName"]];
     $droplet_image = $form_settings["selectDropletImage"];
     $droplet_region = $form_settings["selectDropletRegion"];
     //Get all droplet names
     $allDropletNames = get_all_droplet_names($GFDO->get_plugin_settings());
     //If we don't have any existing droplets, don't bother continuing
     if ($allDropletNames != false) {
         //If the chosen droplet name already exists..
         if (in_array($droplet_name, $allDropletNames)) {
             //Append a '1' to the name if it exists
             $droplet_name = $droplet_name . "1";
         }
     }
     //Update the entry droplet name with our new one
     $entry["droplet_name"] = $droplet_name;
     //Create an API instance
     $adapter = new BuzzAdapter($DOToken);
     $do = new DigitalOceanV2($adapter);
     //Create a droplet object
     $droplet = $do->droplet();
     $form_settings['backupsEnabled'] == 1 ? $anyBackups = true : ($anyBackups = false);
     $form_settings['privateNetworkEnabled'] == 1 ? $anyPN = true : ($anyPN = false);
     //Any SSH keys?
     //Store them if so
     if ($form_settings['SSHKeysEnabled'] == 1) {
         $theSSHKeys = get_ssh_keys_as_array($form_settings);
     } else {
         $theSSHKeys = array();
     }
     //Create the droplet
     try {
         $newDroplet = $droplet->create($droplet_name, $droplet_region, $droplet_size, $droplet_image, $anyBackups, false, $anyPN, $theSSHKeys);
         $GFDO = new GFDigitalOcean();
         $GFDO->log_debug("DROPLET CREATED: " . print_r($newDroplet, true));
         $GFDO->log_debug("droplet_name: " . print_r($droplet_name, true));
         $GFDO->log_debug("droplet_region: " . print_r($droplet_region, true));
         $GFDO->log_debug("droplet_size: " . print_r($droplet_size, true));
         $GFDO->log_debug("droplet_image: " . print_r($droplet_image, true));
         $GFDO->log_debug("droplet_backups: " . print_r($anyBackups, true));
         $GFDO->log_debug("droplet_private_network: " . print_r($anyPN, true));
         $GFDO->log_debug("droplet_ssh_keys: " . print_r($theSSHKeys, true));
         //Get its status
         $dStatus = $newDroplet->status;
         //Get its ID
         $dId = $newDroplet->id;
         //Are domains enabled?
         if ($form_settings['domainEnabled'] == 1 && $form_settings['chooseDomain'] != "" && $dStatus == "new") {
             //2 mins from now
             $nowPlusTwo = time() + 120;
             //Schedule it for 2 mins from now as an event
             wp_schedule_single_event($nowPlusTwo, 'do_domain_for_droplet', array($DOToken, $dId, $entry[$form_settings['chooseDomain']]));
         }
         //Record the Droplet's ID
         $form_settings["droplet_id"] = trim($dId);
         $entry["droplet_id"] = trim($dId);
         //Record SSH Keys
         if (count($theSSHKeys > 0)) {
             $entry["ssh_keys"] = ssh_ids_to_names($addon_settings, $theSSHKeys);
         }
         //and the 'verdict'
         $form_settings["droplet_creation_success"] = trim($dStatus);
         //Save it all
         GFAPI::update_entry($entry);
         $this->save_form_settings($form, $form_settings);
         do_action('gfdo_after_droplet_creation', $newDroplet, $entry, $form);
     } catch (Exception $e) {
         $GFDO = new GFDigitalOcean();
         if ($e !== null) {
             $GFDO->log_debug("DROPLET NOT CREATED: " . $e);
         } else {
             $GFDO->log_debug("DROPLET NOT CREATED: ");
         }
         $GFDO->log_debug("droplet_name: " . print_r($droplet_name, true));
         $GFDO->log_debug("droplet_region: " . print_r($droplet_region, true));
         $GFDO->log_debug("droplet_size: " . print_r($droplet_size, true));
         $GFDO->log_debug("droplet_image: " . print_r($droplet_image, true));
         $GFDO->log_debug("droplet_backups: " . print_r($anyBackups, true));
         $GFDO->log_debug("droplet_private_network: " . print_r($anyPN, true));
         $GFDO->log_debug("droplet_ssh_keys: " . print_r($theSSHKeys, true));
         //Save it all
         GFAPI::update_entry($entry);
         $this->save_form_settings($form, $form_settings);
     }
 }
Пример #5
0
 public function tidyUp()
 {
     $redisKey = config('rediskeys.digitalocean_token') . $this->provision->uuid;
     $sshKeys = new \App\Fodor\Ssh\Keys($this->provision->uuid);
     $adapter = new GuzzleHttpAdapter(Redis::get($redisKey));
     $digitalocean = new DigitalOceanV2($adapter);
     // ## REMOVE SSH KEYS FROM DIGITALOCEAN AND OUR LOCAL FILESYSTEM ##
     $keysFromDo = $digitalocean->key()->getAll();
     if (!empty($keysFromDo)) {
         foreach ($keysFromDo as $key) {
             if ($key->name == 'fodor-' . $this->provision->uuid) {
                 $digitalocean->key()->delete($key->id);
                 // Remove our fodor SSH key from the users DigitalOcean account
                 $this->log->addInfo("Removed SSH key: {$key->name}: {$key->id} from DigitalOcean");
             }
         }
     }
     if (\Storage::exists($sshKeys->getPublicKeyPath())) {
         try {
             $this->log->addInfo("Removed local SSH Keys");
             $sshKeys->remove();
             // uuid is the name of the file
         } catch (Exception $e) {
             // TODO: Handle.  We should probably be alerted as we don't want these lying around
         }
     }
     Redis::del($redisKey);
     $this->provision->dateready = (new \DateTime('now', new \DateTimeZone('UTC')))->format('c');
     $this->provision->exitcode = $this->exitCode;
     $this->provision->status = $this->exitCode === 0 ? 'ready' : 'errored';
     //TODO: Other distros or shells?
     $this->provision->save();
     //TODO: If it errored, an alert should be sent out for investigation
     $this->log->addInfo("Set provision row's status to {$this->provision->status}, we're done here");
 }
Пример #6
0
 public function waiting(Request $request, $id, $uuid)
 {
     $provision = \App\Provision::where('id', $id)->where('uuid', $uuid)->first();
     // TODO: Check they own it
     if ($provision === null) {
         $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'Could not find id/uuid combo']);
         return redirect('/?ohno');
     }
     $adapter = new GuzzleHttpAdapter($request->session()->get('digitalocean')['token']);
     $digitalocean = new DigitalOceanV2($adapter);
     $droplet = $digitalocean->droplet();
     $newDroplet = $droplet->getById($provision->dropletid);
     $status = $newDroplet->status;
     if ($status == "active") {
         // when you create it, it's 'new', then it goes active
         // Now we can get the IP, and create the subdomain
         // Then SSH in and provision
         foreach ($newDroplet->networks as $network) {
             //TODO: Support IPv6, though causes issues with digitalocean apt-get timing out currently
             if ($network->version === 4) {
                 // we only support ipv4 for this hacked together version
                 $ip = $network->ipAddress;
                 $client = new \Cloudflare\Api(env('CLOUDFLARE_API_EMAIL'), env('CLOUDFLARE_API_KEY'));
                 $dns = new \Cloudflare\Zone\Dns($client);
                 $subdomain = new \App\Fodor\Subdomain($dns);
                 $subdomainName = $subdomain->generateName($provision->id);
                 $result = $subdomain->create($subdomainName, $ip);
                 if (empty($result)) {
                     $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'We failed to create the DNS needed for ' . $ip . ', really sorry about that.  You should probably delete this failed attemp :(']);
                     return redirect('/?ohno');
                 }
                 $provision->status = 'active';
                 // TODO: Provision class should handle this, and use constants - or just $provision->setActive() or $provision->active(true);
                 $provision->ipv4 = $ip;
                 $provision->subdomain = $subdomainName;
                 $provision->save();
                 return redirect(url('/provision/provision/' . $provision->id . '/' . $provision->uuid));
             }
         }
     }
     return view('provision.waiting', ['status' => $status, 'id' => $id, 'uuid' => $uuid, 'provision' => $provision]);
 }
Пример #7
0
    Route::get('/do/callback', function (Request $request) {
        if (empty($request->input('state'))) {
            $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'Invalid state, what are you up to?!']);
            return redirect('/?ohno');
        }
        /** @var \App\Providers\DigitalOceanOauthServiceProvider $provider */
        $provider = $this->app['DigitalOceanOauthServiceProvider'];
        try {
            // Try to get an access token using the authorization code grant.
            $accessToken = $provider->getAccessToken('authorization_code', ['code' => $request->input('code')]);
            if (!is_object($accessToken)) {
                $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'We couldn\'t get your details and log you in!']);
                return redirect('/?ohno');
            }
            $adapter = new GuzzleHttpAdapter($accessToken->getToken());
            $digitalocean = new DigitalOceanV2($adapter);
            $account = $digitalocean->account()->getUserInformation();
            $request->session()->set('digitalocean', ['token' => $accessToken->getToken(), 'refreshToken' => $accessToken->getToken(), 'expires' => $accessToken->getExpires(), 'hasExpired' => $accessToken->hasExpired(), 'email' => $account->email, 'uuid' => $account->uuid]);
        } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
            // Failed to get the access token or user details.
            $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'We couldn\'t get your details and log you in because of a pesky exception!']);
            return redirect('/?ohno');
        }
        if ($request->session()->has('intendedRepo')) {
            $intendedRepo = $request->session()->get('intendedRepo');
            $request->session()->forget('intendedRepo');
            return redirect(url('/provision/start/' . $intendedRepo));
        }
        return redirect(url('/'));
    });
});