Пример #1
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");
 }
function ssh_ids_to_names($addon_settings, $sshKeys)
{
    $DOToken = $addon_settings["digitalocean_token"];
    if (empty($DOToken)) {
        return false;
    }
    $adapter = new BuzzAdapter($DOToken);
    $do = new DigitalOceanV2($adapter);
    $theKey = $do->key();
    $allKeys = $theKey->getAll();
    $matchKeys = array();
    foreach ($allKeys as $key) {
        foreach ($sshKeys as $refKey) {
            if ($refKey == $key->id) {
                $matchKeys[] = $key->name;
            }
        }
    }
    $matchKeys = implode(', ', $matchKeys);
    return (string) $matchKeys;
}
Пример #3
0
 public function doit(Request $request)
 {
     if ($request->session()->has('digitalocean') === false) {
         return redirect(url('/?loginToDigitalOceanFirstSilly'));
     }
     $errors = [];
     if (empty($request->input('size'))) {
         $errors['size'] = true;
     }
     if (empty($request->input('distro'))) {
         $errors['distro'] = true;
     }
     if (empty($request->input('name'))) {
         $errors['name'] = true;
     }
     if (empty($request->input('id'))) {
         $errors['id'] = true;
     }
     if (!empty($errors) || empty($request->input('uuid')) || empty($request->input('repo')) || empty($request->input('region'))) {
         return redirect(url('/?invalidSizeOrDistroOrNameOrRegion2'));
     }
     $inputs = $request->input('inputs', []);
     $id = $request->input('id');
     $uuid = $request->input('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');
     }
     $provisionid = $id;
     $name = $request->input('name');
     $repo = $request->input('repo');
     $size = $request->input('size');
     $distro = $request->input('distro');
     $region = $request->input('region');
     $keys = $request->input('keys', []);
     $keys = array_keys($keys);
     $request->session()->set("inputs.{$uuid}", $inputs);
     if (array_key_exists($size, config('digitalocean.sizes')) === false) {
         // Invalid size
         return redirect(url('/?sizeNotInConfigMustBeInvalidOrIAmOutOfDateLikeSausagesUsually'));
     }
     if (array_key_exists($region, config('digitalocean.regions')) === false) {
         // Invalid region
         return redirect(url('/?sizeNotInConfigMustBeInvalidOrIAmOutOfDateLikeSausagesUsuallyREGION'));
     }
     if (in_array($distro, config('digitalocean.distros')) === false) {
         // Invalid region
         return redirect(url('/?distro invalid'));
     }
     // For now do it the absolutely dreadful way
     // TODO: Tidy up with service provider/facade/something
     // If we did have a users table it could be stored in there
     $adapter = new GuzzleHttpAdapter($request->session()->get('digitalocean')['token']);
     $digitalocean = new DigitalOceanV2($adapter);
     $publicKey = (new \App\Fodor\Ssh\Keys($provision->uuid))->getPublic();
     $droplet = $digitalocean->droplet();
     $key = $digitalocean->key();
     try {
         $keyCreated = $key->create('fodor-' . $provision->uuid, $publicKey);
         // TODO: Check result
     } catch (\Exception $e) {
         $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'Could not add SSH key to your DigitalOcean account: ' . $e->getMessage()]);
         return redirect('/provision/start/' . $repo);
     }
     $keys[] = $keyCreated->id;
     try {
         $hostname = 'fodor-' . $name . '-' . $provision->uuid;
         $created = $droplet->create($hostname, $region, $size, $distro, false, false, false, $keys);
     } catch (\Exception $e) {
         $request->session()->flash(str_random(4), ['type' => 'danger', 'message' => 'Could not create DigitalOcean droplet: ' . $e->getMessage()]);
         return redirect('/provision/start/' . $repo);
     }
     if (empty($created)) {
         return redirect(url('/?createdDroplet=false'));
     }
     $dropletId = $created->id;
     $provision->region = $region;
     $provision->size = $size;
     $provision->dropletid = $dropletId;
     $provision->save();
     // It doesn't have a network straight away - we need to wait for it to be created
     return redirect(url('/provision/waiting/' . $provisionid . '/' . $provision->uuid));
 }