Skip to content

Commit cd2dc05

Browse files
committed
Merge pull request #1 from leftyhitchens/master
Merging this despite the issue with Scrutinizer.
2 parents f2b1e51 + f7e3735 commit cd2dc05

18 files changed

Lines changed: 418 additions & 136 deletions

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ ssh:
136136
keys:
137137
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
138138
```
139+
## Running
140+
141+
You can provision a server on digital ocean using the 'provision' command as below:
142+
143+
```
144+
robo provision my_host_name
145+
```
146+
147+
The above will use the information in your config file to create a droplet on digital
148+
ocean with the specified hostname as 'my_host_name'.
149+
150+
You can destroy the cloud server using the 'destroy' command as below:
151+
152+
```
153+
robo destroy 553f9e1334fa9
154+
```
155+
156+
Where the value '553f9e1334fa9' is the unique id of the cloud server to destroy.
139157

140158
## Testing
141159
``` bash

RoboFile.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Indatus\Assembler\Traits\StockerTrait;
1010
use Indatus\Assembler\Traits\ProvisionTrait;
1111
use Indatus\Assembler\Traits\PackagerTrait;
12+
use Indatus\Assembler\Traits\DestroyerTrait;
1213
use Robo\Result;
1314
use Robo\Tasks;
1415

@@ -26,6 +27,8 @@ class RoboFile extends Tasks
2627
use LoaderTrait;
2728
use ProvisionTrait;
2829
use PackagerTrait;
30+
use DestroyerTrait;
31+
2932

3033
/**
3134
* Retrieve, organize, and create instructions for materials related to a product line
@@ -222,15 +225,27 @@ public function assemblerMake(
222225
}
223226

224227
/**
225-
* Provisions a fresh server
226-
*
227-
* @param string $hostname hostname of the box that you would like provisioned
228-
* @param string $region region of where you would like the box provisioned
229-
* @param string $size size of the box that you would like provisioned
230-
* @param string $image name of the image that you would like provisioned
231-
* @param bool $backups turn on backups
232-
* @param bool $ipv6 turn on ipv6 features
233-
* @param bool $privateNetworking turn on private networking
228+
* Destroys the cloud server
229+
* @param $id the id of the cloud server being destroyed
230+
* @return Result
231+
*/
232+
public function destroy($id)
233+
{
234+
return $this->taskDestroyServer($id)
235+
->run();
236+
}
237+
238+
239+
/**
240+
* Provision a fresh server
241+
* @param $hostname
242+
* @param string $region
243+
* @param string $size
244+
* @param string $image
245+
* @param bool $backups
246+
* @param bool $ipv6
247+
* @param bool $privateNetworking
248+
* @return Result
234249
*/
235250
public function provision(
236251
$hostname,
@@ -241,16 +256,7 @@ public function provision(
241256
$ipv6 = false,
242257
$privateNetworking = false
243258
) {
244-
$configuration = new Configuration;
245-
try {
246-
$apiToken = $configuration->apiToken();
247-
} catch (NoProviderTokenException $exception) {
248-
$this->say("Couldn't find a well-formed provider token in the provisioning config file.");
249-
exit;
250-
}
251-
$this->taskProvisionServer(
252-
$apiToken,
253-
$configuration->sshKeys(),
259+
$result = $this->taskProvisionServer(
254260
$hostname,
255261
$region,
256262
$size,
@@ -259,5 +265,8 @@ public function provision(
259265
$ipv6,
260266
$privateNetworking
261267
)->run();
268+
$data = $result->getData();
269+
$this->say("Provisioned server with id of: $data->id");
270+
return $result;
262271
}
263272
}

src/AdapterFactory.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Indatus\Assembler;
3+
4+
use DigitalOceanV2\Adapter\Guzzle5Adapter;
5+
use DigitalOceanV2\DigitalOceanV2;
6+
use Indatus\Assembler\Adapters\DigitalOceanAdapter;
7+
use Indatus\Assembler\Exceptions\InvalidProviderException;
8+
9+
class AdapterFactory
10+
{
11+
public static function make(Configuration $config)
12+
{
13+
$provider = $config->provider();
14+
$adapter = null;
15+
switch($provider) {
16+
case "digitalocean":
17+
$digitalOcean = new DigitalOceanV2(new Guzzle5Adapter($config->apiToken()));
18+
$adapter = new DigitalOceanAdapter($digitalOcean);
19+
break;
20+
default:
21+
throw new InvalidProviderException("$provider is not a valid cloud provider");
22+
break;
23+
}
24+
return $adapter;
25+
}
26+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace Indatus\Assembler\Adapters;
3+
4+
use DigitalOceanV2\DigitalOceanV2;
5+
use Indatus\Assembler\Contracts\CloudAdapterInterface;
6+
7+
class DigitalOceanAdapter implements CloudAdapterInterface
8+
{
9+
protected $digitalOceanV2;
10+
11+
public function __construct(DigitalOceanV2 $digitalOceanV2)
12+
{
13+
$this->digitalOceanV2 = $digitalOceanV2;
14+
}
15+
16+
/**
17+
* @param string $hostName the hostname of the droplet being created
18+
* @param string $region the region where the droplet should be provisioned
19+
* @param string $size the size of the droplet being provisioned
20+
* @param string $image the image being used for the doplet
21+
* @param bool $backups true if you want backups to be created
22+
* @param bool $ipv6 true if you want to use ipv6 networking
23+
* @param bool $privateNetworking true if you want the droplet on a private network
24+
* @param array $sshKeys an array of keys to be used on the newly created droplet
25+
* @return \DigitalOceanV2\Api\Droplet
26+
*/
27+
public function create(
28+
$hostName,
29+
$region,
30+
$size,
31+
$image,
32+
$backups,
33+
$ipv6,
34+
$privateNetworking,
35+
array $sshKeys = array()
36+
) {
37+
$droplet = $this->digitalOceanV2->droplet();
38+
return $droplet->create(
39+
$hostName,
40+
$region,
41+
$size,
42+
$image,
43+
$backups,
44+
$ipv6,
45+
$privateNetworking,
46+
$sshKeys
47+
);
48+
}
49+
50+
/**
51+
* Drops the specified droplet
52+
* @param $id
53+
*/
54+
public function drop($id)
55+
{
56+
return $this->digitalOceanV2->droplet()->delete($id);
57+
}
58+
}

src/Configuration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public function __construct($directory = './config', $name = 'provisioning.yaml'
2121
$this->values = Yaml::parse(file_get_contents($file));
2222
}
2323

24+
/**
25+
* Returns the provider configured in the provisioning.yaml file
26+
*/
27+
public function provider()
28+
{
29+
return $this->values['provider'];
30+
}
31+
2432
/**
2533
* @return mixed
2634
* @throws NoProviderTokenException
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Indatus\Assembler\Contracts;
3+
4+
interface CloudAdapterInterface
5+
{
6+
public function create($hostname, $region, $size, $image, $backups, $ipv6, $privateNetworking, array $sshKeys);
7+
8+
public function drop($id);
9+
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Indatus\Assembler\Exceptions;
3+
4+
use Exception;
5+
6+
class InvalidProviderException extends Exception
7+
{
8+
9+
}

src/Provisioner.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Tasks/DestroyTask.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Indatus\Assembler\Tasks;
3+
4+
5+
use Indatus\Assembler\Contracts\CloudAdapterInterface;
6+
use Robo\Contract\TaskInterface;
7+
use Robo\Result;
8+
9+
class DestroyTask implements TaskInterface
10+
{
11+
protected $cloudAdapter;
12+
13+
protected $id;
14+
15+
public function __construct($id, CloudAdapterInterface $cloudAdapter)
16+
{
17+
$this->id = $id;
18+
$this->cloudAdapter = $cloudAdapter;
19+
}
20+
21+
public function run()
22+
{
23+
$this->cloudAdapter->drop($this->id);
24+
return new Result($this,0);
25+
}
26+
}

0 commit comments

Comments
 (0)