Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add digitalocean user data #9

Merged
merged 2 commits into from
May 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ config/provisioning.yaml
repos/*
.vagrant
.machine*
config/userdata.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are we planning on getting the userdata to the jenkins server when we run this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a private repo that jenkins pulls from.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ raw_goods:
suppliers:
- [email protected]:saltstack-formulas/mysql.git
- [email protected]:saltstack-formulas/php.git
userData: config/provision.sh # optional
```

The above yaml represents a product line that tells assembler you want a container with mysql that has a root
Expand Down Expand Up @@ -149,7 +150,10 @@ tokens:
ssh:
keys:
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
userData: config/provision.sh # optional
```
`userData` key provides a path to a [DigitalOcean cloud config](http://cloudinit.readthedocs.org/en/latest/topics/examples.html)
script. This will be executed during your droplet's creation and can be used for provisioning.
## Running

You can provision a server on digital ocean using the 'provision' command as below:
Expand Down
3 changes: 2 additions & 1 deletion config/provisioning.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ tokens:
digitalocean: yoursuperlongapitoken111
ssh:
keys:
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
userData: config/userdata.sh.example
4 changes: 4 additions & 0 deletions config/userdata.sh.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#cloud-config

runcmd:
- ls
7 changes: 5 additions & 2 deletions src/Adapters/DigitalOceanAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(DigitalOceanV2 $digitalOceanV2)
* @param bool $ipv6 true if you want to use ipv6 networking
* @param bool $privateNetworking true if you want the droplet on a private network
* @param array $sshKeys an array of keys to be used on the newly created droplet
* @param string $userData path to a cloud-config script for provisioning
* @return MachineObject
*/
public function create(
Expand All @@ -32,7 +33,8 @@ public function create(
$backups,
$ipv6,
$privateNetworking,
array $sshKeys = array()
array $sshKeys = array(),
$userData = ""
) {
$dropletApi = $this->digitalOceanV2->droplet();
$droplet = $dropletApi->create(
Expand All @@ -43,7 +45,8 @@ public function create(
$backups,
$ipv6,
$privateNetworking,
$sshKeys
$sshKeys,
$userData
);
$machine = new MachineObject();
$machine->hostname = $droplet->name;
Expand Down
16 changes: 16 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
namespace Indatus\Assembler;

use Indatus\Assembler\Exceptions\MalformedSSHKeysException;
use Indatus\Assembler\Exceptions\MissingUserDataFileException;
use Indatus\Assembler\Exceptions\NoProviderTokenException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;

class Configuration
Expand Down Expand Up @@ -61,4 +63,18 @@ public function sshKeys()

throw new MalformedSSHKeysException('Missing ssh section of configuration.');
}

public function userData()
{
if (array_key_exists('userData', $this->values)) {
$userDataPath = $this->values['userData'];
if ((new Filesystem())->exists($userDataPath)) {
return file_get_contents($userDataPath);
}

throw new MissingUserDataFileException;
}

return "";
}
}
4 changes: 3 additions & 1 deletion src/Contracts/CloudAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface CloudAdapterInterface
* @param bool $ipv6 true if you want to use ipv6 networking
* @param bool $privateNetworking true if you want the droplet on a private network
* @param array $sshKeys an array of keys to be used on the newly created droplet
* @param string $userData path to a cloud-config script for provisioning
*
* @return \Indatus\Assembler\Adapters\MachineObject
*/
Expand All @@ -23,7 +24,8 @@ public function create(
$backups,
$ipv6,
$privateNetworking,
array $sshKeys
array $sshKeys,
$userData
);

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/MissingUserDataFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Indatus\Assembler\Exceptions;

use Exception;

class MissingUserDataFileException extends Exception
{
//
}
11 changes: 9 additions & 2 deletions src/Tasks/ProvisionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ProvisionTask implements TaskInterface
*/
protected $sshKeys;

/** @var string */
protected $userData;

/**
* @param array $sshKeys
* @param string $hostname
Expand All @@ -34,6 +37,7 @@ class ProvisionTask implements TaskInterface
* @param bool $backups
* @param bool $ipv6
* @param bool $privateNetworking
* @param string $userData
*/
public function __construct(
$hostname,
Expand All @@ -44,7 +48,8 @@ public function __construct(
$ipv6,
$privateNetworking,
array $sshKeys = [],
CloudAdapterInterface $cloudAdapter
CloudAdapterInterface $cloudAdapter,
$userData
) {
$this->cloudAdapter = $cloudAdapter;
$this->hostname = $hostname;
Expand All @@ -55,6 +60,7 @@ public function __construct(
$this->ipv6 = $ipv6;
$this->privateNetworking = $privateNetworking;
$this->sshKeys = $sshKeys;
$this->userData = $userData;
}

/**
Expand All @@ -70,7 +76,8 @@ public function run()
$this->backups,
$this->ipv6,
$this->privateNetworking,
$this->sshKeys
$this->sshKeys,
$this->userData
);

return new Result($this, 0, '', $droplet);
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/ProvisionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function taskProvisionServer(
) {
$configuration = new Configuration();
$sshKeys = $configuration->sshKeys();
$userData = $configuration->userData();
return new ProvisionTask(
$hostname,
$region,
Expand All @@ -27,7 +28,8 @@ public function taskProvisionServer(
$ipv6,
$privateNetworking,
$sshKeys,
AdapterFactory::make($configuration)
AdapterFactory::make($configuration),
$userData
);
}
}
7 changes: 5 additions & 2 deletions tests/Adapters/DigitalOceanAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testCreateWhenIPV4True()
$ipv6 = false;
$privateNetworking = false;
$sshKeys = ['somekey'];
$userData = "provision.sh";

$digitalOceanV2 = m::mock('DigitalOceanV2\DigitalOceanV2');
$dropletEntityMock = m::mock("DigitalOceanV2\Entity\Droplet");
Expand Down Expand Up @@ -66,7 +67,8 @@ public function testCreateWhenIPV4True()
$backups,
$ipv6,
$privateNetworking,
$sshKeys
$sshKeys,
$userData
)->andReturn($dropletEntityMock);
$dropletMock->shouldReceive('getById')
->once()
Expand All @@ -81,7 +83,8 @@ public function testCreateWhenIPV4True()
$backups,
$ipv6,
$privateNetworking,
$sshKeys
$sshKeys,
$userData
);
$this->assertInstanceOf(
'\Indatus\Assembler\Adapters\MachineObject',
Expand Down
24 changes: 24 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,28 @@ public function testComplainsWhenSshKeysAreMalformed()
{
$sshKeys = (new Configuration(realpath('tests'), 'bad-configuration-ssh.yaml'))->sshKeys();
}

public function testUserDataFileIsLocated()
{
$userData = (new Configuration(realpath('tests'), 'configuration.yaml'))->userData();
$expectedUserData = file_get_contents('tests/provision.sh.example');

$this->assertEquals($expectedUserData, $userData);
}

/**
* @expectedException \Indatus\Assembler\Exceptions\MissingUserDataFileException
*/
public function testUserDataFileCanBeMissing()
{
$userData = (new Configuration(realpath('tests'), 'bad-configuration-userdata.yaml'))->userData();
}

public function testUserDataReturnsBlankWhenOmitted()
{
$userData = (new Configuration(realpath('tests'), 'configuration-ommitted-userdata.yaml'))->userData();
$expectedUserData = "";

$this->assertEquals($expectedUserData, $userData);
}
}
7 changes: 5 additions & 2 deletions tests/Tasks/ProvisionTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testRun()
$privateNetworking = true;
$sshKeys = ['keys'];
$droplet = m::mock('\DigitalOceanV2\Api\Droplet');
$userData = "";
/**
* @var $cloudAdapter \Indatus\Assembler\Contracts\CloudAdapterInterface|\Mocker\MockInterface
*/
Expand All @@ -35,7 +36,8 @@ public function testRun()
$ipv6,
$privateNetworking,
$sshKeys,
$cloudAdapter
$cloudAdapter,
$userData
);
$cloudAdapter->shouldReceive('create')
->once()
Expand All @@ -47,7 +49,8 @@ public function testRun()
$backups,
$ipv6,
$privateNetworking,
$sshKeys
$sshKeys,
$userData
)->andReturn($droplet);
$result = $provision_task->run();
$this->assertEquals($result->getExitCode(), 0);
Expand Down
8 changes: 8 additions & 0 deletions tests/bad-configuration-userdata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
provider: myprovider
tokens:
myprovider: yoursuperlongapitoken111
ssh:
keys:
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
- 11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11
userData: tests/missing.sh.example
7 changes: 7 additions & 0 deletions tests/configuration-ommitted-userdata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
provider: myprovider
tokens:
myprovider: yoursuperlongapitoken111
ssh:
keys:
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
- 11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11
3 changes: 2 additions & 1 deletion tests/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ tokens:
ssh:
keys:
- 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
- 11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11
- 11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11
userData: tests/provision.sh.example
4 changes: 4 additions & 0 deletions tests/provision.sh.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#cloud-config

runcmd:
- ls