|
|
|
@ -36,70 +36,56 @@ options:
|
|
|
|
|
state:
|
|
|
|
|
description:
|
|
|
|
|
- State of the server
|
|
|
|
|
required: False
|
|
|
|
|
default: running
|
|
|
|
|
choices: ['running', 'stopped', 'absent']
|
|
|
|
|
choices: [ running, stopped, absent ]
|
|
|
|
|
name:
|
|
|
|
|
description:
|
|
|
|
|
- Name of the Server
|
|
|
|
|
- Name of the Server.
|
|
|
|
|
- Either C(name) or C(uuid) are required. These options are mutually exclusive.
|
|
|
|
|
required: False
|
|
|
|
|
uuid:
|
|
|
|
|
description:
|
|
|
|
|
- UUID of the server
|
|
|
|
|
- UUID of the server.
|
|
|
|
|
- Either C(name) or C(uuid) are required. These options are mutually exclusive.
|
|
|
|
|
required: False
|
|
|
|
|
flavor:
|
|
|
|
|
description:
|
|
|
|
|
- Flavor of the server
|
|
|
|
|
required: False
|
|
|
|
|
- Flavor of the server.
|
|
|
|
|
image:
|
|
|
|
|
description:
|
|
|
|
|
- Image used to create the server
|
|
|
|
|
required: False
|
|
|
|
|
- Image used to create the server.
|
|
|
|
|
volume_size_gb:
|
|
|
|
|
description:
|
|
|
|
|
- Size of the root volume in GB
|
|
|
|
|
required: False
|
|
|
|
|
- Size of the root volume in GB.
|
|
|
|
|
default: 10
|
|
|
|
|
bulk_volume_size_gb:
|
|
|
|
|
description:
|
|
|
|
|
- Size of the bulk storage volume in GB
|
|
|
|
|
required: False
|
|
|
|
|
default: null (no bulk storage volume)
|
|
|
|
|
- Size of the bulk storage volume in GB.
|
|
|
|
|
- No bulk storage volume if not set.
|
|
|
|
|
ssh_keys:
|
|
|
|
|
description:
|
|
|
|
|
- List of SSH public keys
|
|
|
|
|
- List of SSH public keys.
|
|
|
|
|
- Use the full content of your .pub file here.
|
|
|
|
|
required: False
|
|
|
|
|
use_public_network:
|
|
|
|
|
description:
|
|
|
|
|
- Attach a public network interface to the server
|
|
|
|
|
required: False
|
|
|
|
|
- Attach a public network interface to the server.
|
|
|
|
|
default: True
|
|
|
|
|
use_private_network:
|
|
|
|
|
description:
|
|
|
|
|
- Attach a private network interface to the server
|
|
|
|
|
required: False
|
|
|
|
|
- Attach a private network interface to the server.
|
|
|
|
|
default: False
|
|
|
|
|
use_ipv6:
|
|
|
|
|
description:
|
|
|
|
|
- Enable IPv6 on the public network interface
|
|
|
|
|
required: False
|
|
|
|
|
- Enable IPv6 on the public network interface.
|
|
|
|
|
default: True
|
|
|
|
|
anti_affinity_with:
|
|
|
|
|
description:
|
|
|
|
|
- UUID of another server to create an anti-affinity group with
|
|
|
|
|
required: False
|
|
|
|
|
- UUID of another server to create an anti-affinity group with.
|
|
|
|
|
user_data:
|
|
|
|
|
description:
|
|
|
|
|
- Cloud-init configuration (cloud-config) data to use for the server.
|
|
|
|
|
required: False
|
|
|
|
|
api_token:
|
|
|
|
|
description:
|
|
|
|
|
- cloudscale.ch API token.
|
|
|
|
|
- This can also be passed in the CLOUDSCALE_API_TOKEN environment variable.
|
|
|
|
|
required: False
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
@ -221,7 +207,7 @@ from ansible.module_utils.six.moves.urllib.parse import urlencode
|
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
API_URL = 'https://api.cloudscale.ch/v1/'
|
|
|
|
|
API_URL = 'https://api.cloudscale.ch/v1/'
|
|
|
|
|
TIMEOUT_WAIT = 30
|
|
|
|
|
ALLOWED_STATES = ('running',
|
|
|
|
|
'stopped',
|
|
|
|
@ -248,7 +234,7 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
if uuid:
|
|
|
|
|
# Look for server by UUID if given
|
|
|
|
|
if s['uuid'] == uuid:
|
|
|
|
|
self.info = self._transform_state(s)
|
|
|
|
|
self.info = self._transform_state(s)
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
# Look for server by name
|
|
|
|
@ -261,9 +247,8 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self._module.fail_json(msg="More than one server with name '%s' exists. "
|
|
|
|
|
"Use the 'uuid' parameter to identify the server" % name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get(self, api_call):
|
|
|
|
|
resp, info = fetch_url(self._module, API_URL+api_call, headers=self._auth_header)
|
|
|
|
|
resp, info = fetch_url(self._module, API_URL + api_call, headers=self._auth_header)
|
|
|
|
|
|
|
|
|
|
if info['status'] == 200:
|
|
|
|
|
return json.loads(resp.read())
|
|
|
|
@ -271,14 +256,13 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self._module.fail_json(msg='Failure while calling the cloudscale.ch API with GET for '
|
|
|
|
|
'"%s": %s' % (api_call, info['body']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _post(self, api_call, data=None):
|
|
|
|
|
if data is not None:
|
|
|
|
|
data = urlencode(data)
|
|
|
|
|
|
|
|
|
|
resp, info = fetch_url(self._module,
|
|
|
|
|
API_URL+api_call,
|
|
|
|
|
headers = self._auth_header,
|
|
|
|
|
API_URL + api_call,
|
|
|
|
|
headers=self._auth_header,
|
|
|
|
|
method='POST',
|
|
|
|
|
data=data)
|
|
|
|
|
|
|
|
|
@ -290,11 +274,10 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self._module.fail_json(msg='Failure while calling the cloudscale.ch API with POST for '
|
|
|
|
|
'"%s": %s' % (api_call, info['body']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _delete(self, api_call):
|
|
|
|
|
resp, info = fetch_url(self._module,
|
|
|
|
|
API_URL+api_call,
|
|
|
|
|
headers = self._auth_header,
|
|
|
|
|
API_URL + api_call,
|
|
|
|
|
headers=self._auth_header,
|
|
|
|
|
method='DELETE')
|
|
|
|
|
|
|
|
|
|
if info['status'] == 204:
|
|
|
|
@ -303,7 +286,6 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self._module.fail_json(msg='Failure while calling the cloudscale.ch API with DELETE for '
|
|
|
|
|
'"%s": %s' % (api_call, info['body']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _transform_state(server):
|
|
|
|
|
if 'status' in server:
|
|
|
|
@ -313,16 +295,15 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
server['state'] = 'absent'
|
|
|
|
|
return server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_info(self):
|
|
|
|
|
|
|
|
|
|
# If we don't have a UUID (yet) there is nothing to update
|
|
|
|
|
if not 'uuid' in self.info:
|
|
|
|
|
if 'uuid' not in self.info:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Can't use _get here because we want to handle 404
|
|
|
|
|
resp, info = fetch_url(self._module,
|
|
|
|
|
API_URL+'servers/'+self.info['uuid'],
|
|
|
|
|
API_URL + 'servers/' + self.info['uuid'],
|
|
|
|
|
headers=self._auth_header)
|
|
|
|
|
if info['status'] == 200:
|
|
|
|
|
self.info = self._transform_state(json.loads(resp.read()))
|
|
|
|
@ -334,7 +315,6 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self._module.fail_json(msg='Failure while calling the cloudscale.ch API for '
|
|
|
|
|
'update_info: %s' % info['body'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_state(self, states):
|
|
|
|
|
start = datetime.now()
|
|
|
|
|
while datetime.now() - start < timedelta(seconds=TIMEOUT_WAIT):
|
|
|
|
@ -346,14 +326,13 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self._module.fail_json(msg='Timeout while waiting for a state change on server %s to states %s. Current state is %s'
|
|
|
|
|
% (self.info['name'], states, self.info['state']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_server(self):
|
|
|
|
|
data = self._module.params.copy()
|
|
|
|
|
|
|
|
|
|
# check for required parameters to create a server
|
|
|
|
|
missing_parameters = []
|
|
|
|
|
for p in ('name', 'ssh_keys', 'image', 'flavor'):
|
|
|
|
|
if not p in data or not data[p]:
|
|
|
|
|
if p not in data or not data[p]:
|
|
|
|
|
missing_parameters.append(p)
|
|
|
|
|
|
|
|
|
|
if len(missing_parameters) > 0:
|
|
|
|
@ -361,7 +340,7 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
' '.join(missing_parameters))
|
|
|
|
|
|
|
|
|
|
# Sanitize data dictionary
|
|
|
|
|
for k,v in data.items():
|
|
|
|
|
for k, v in data.items():
|
|
|
|
|
|
|
|
|
|
# Remove items not relevant to the create server call
|
|
|
|
|
if k in ('api_token', 'uuid', 'state'):
|
|
|
|
@ -376,44 +355,39 @@ class AnsibleCloudscaleServer(object):
|
|
|
|
|
self.info = self._transform_state(self._post('servers', data))
|
|
|
|
|
self.wait_for_state(('running', ))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_server(self):
|
|
|
|
|
self._delete('servers/%s' % self.info['uuid'])
|
|
|
|
|
self.wait_for_state(('absent', ))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_server(self):
|
|
|
|
|
self._post('servers/%s/start' % self.info['uuid'])
|
|
|
|
|
self.wait_for_state(('running', ))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop_server(self):
|
|
|
|
|
self._post('servers/%s/stop' % self.info['uuid'])
|
|
|
|
|
self.wait_for_state(('stopped', ))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_servers(self):
|
|
|
|
|
return self._get('servers')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
|
argument_spec = dict(
|
|
|
|
|
state = dict(default='running',
|
|
|
|
|
choices=ALLOWED_STATES),
|
|
|
|
|
name = dict(),
|
|
|
|
|
uuid = dict(),
|
|
|
|
|
flavor = dict(),
|
|
|
|
|
image = dict(),
|
|
|
|
|
volume_size_gb = dict(type='int', default=10),
|
|
|
|
|
bulk_volume_size_gb = dict(type='int'),
|
|
|
|
|
ssh_keys = dict(type='list'),
|
|
|
|
|
use_public_network = dict(type='bool', default=True),
|
|
|
|
|
use_private_network = dict(type='bool', default=False),
|
|
|
|
|
use_ipv6 = dict(type='bool', default=True),
|
|
|
|
|
anti_affinity_with = dict(),
|
|
|
|
|
user_data = dict(),
|
|
|
|
|
api_token = dict(no_log=True),
|
|
|
|
|
argument_spec=dict(
|
|
|
|
|
state=dict(default='running', choices=ALLOWED_STATES),
|
|
|
|
|
name=dict(),
|
|
|
|
|
uuid=dict(),
|
|
|
|
|
flavor=dict(),
|
|
|
|
|
image=dict(),
|
|
|
|
|
volume_size_gb=dict(type='int', default=10),
|
|
|
|
|
bulk_volume_size_gb=dict(type='int'),
|
|
|
|
|
ssh_keys=dict(type='list'),
|
|
|
|
|
use_public_network=dict(type='bool', default=True),
|
|
|
|
|
use_private_network=dict(type='bool', default=False),
|
|
|
|
|
use_ipv6=dict(type='bool', default=True),
|
|
|
|
|
anti_affinity_with=dict(),
|
|
|
|
|
user_data=dict(),
|
|
|
|
|
api_token=dict(no_log=True),
|
|
|
|
|
),
|
|
|
|
|
required_one_of=(('name', 'uuid'),),
|
|
|
|
|
mutually_exclusive=(('name', 'uuid'),),
|
|
|
|
|