2014-02-13 04:51:36 +01:00
|
|
|
#!/usr/bin/python
|
2013-03-30 15:38:42 +01:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-02-25 03:48:15 +01:00
|
|
|
# This is a DOCUMENTATION stub specific to this module, it extends
|
|
|
|
# a documentation fragment located in ansible.utils.module_docs_fragments
|
2013-03-30 15:38:42 +01:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: rax
|
2013-04-16 02:20:45 +02:00
|
|
|
short_description: create / delete an instance in Rackspace Public Cloud
|
2013-03-30 15:38:42 +01:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- creates / deletes a Rackspace Public Cloud instance and optionally
|
|
|
|
waits for it to be 'running'.
|
2013-03-30 15:38:42 +01:00
|
|
|
version_added: "1.2"
|
|
|
|
options:
|
2014-01-07 16:10:31 +01:00
|
|
|
auto_increment:
|
|
|
|
description:
|
|
|
|
- Whether or not to increment a single number with the name of the
|
|
|
|
created servers. Only applicable when used with the I(group) attribute
|
|
|
|
or meta key.
|
|
|
|
default: yes
|
2014-05-21 14:16:10 +02:00
|
|
|
choices:
|
|
|
|
- "yes"
|
|
|
|
- "no"
|
2014-01-07 16:10:31 +01:00
|
|
|
version_added: 1.5
|
2014-05-21 14:16:10 +02:00
|
|
|
config_drive:
|
|
|
|
description:
|
|
|
|
- Attach read-only configuration drive to server as label config-2
|
|
|
|
default: no
|
|
|
|
choices:
|
|
|
|
- "yes"
|
|
|
|
- "no"
|
|
|
|
version_added: 1.7
|
2013-11-15 19:25:21 +01:00
|
|
|
count:
|
2013-03-30 15:38:42 +01:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- number of instances to launch
|
|
|
|
default: 1
|
|
|
|
version_added: 1.4
|
|
|
|
count_offset:
|
|
|
|
description:
|
|
|
|
- number count to start at
|
|
|
|
default: 1
|
|
|
|
version_added: 1.4
|
|
|
|
disk_config:
|
2013-08-06 00:49:14 +02:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- Disk partitioning strategy
|
2014-02-24 19:47:42 +01:00
|
|
|
choices:
|
|
|
|
- auto
|
|
|
|
- manual
|
2013-11-15 19:25:21 +01:00
|
|
|
version_added: '1.4'
|
|
|
|
default: auto
|
|
|
|
exact_count:
|
2013-08-06 00:49:14 +02:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- Explicitly ensure an exact count of instances, used with
|
|
|
|
state=active/present
|
|
|
|
default: no
|
2014-05-21 14:16:10 +02:00
|
|
|
choices:
|
|
|
|
- "yes"
|
|
|
|
- "no"
|
2013-11-15 19:25:21 +01:00
|
|
|
version_added: 1.4
|
2014-03-05 19:38:28 +01:00
|
|
|
extra_client_args:
|
|
|
|
description:
|
|
|
|
- A hash of key/value pairs to be used when creating the cloudservers
|
|
|
|
client. This is considered an advanced option, use it wisely and
|
|
|
|
with caution.
|
|
|
|
version_added: 1.6
|
|
|
|
extra_create_args:
|
|
|
|
description:
|
|
|
|
- A hash of key/value pairs to be used when creating a new server.
|
|
|
|
This is considered an advanced option, use it wisely and with caution.
|
|
|
|
version_added: 1.6
|
2013-11-15 19:25:21 +01:00
|
|
|
files:
|
2013-03-30 15:38:42 +01:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- Files to insert into the instance. remotefilename:localcontent
|
2013-03-30 15:38:42 +01:00
|
|
|
default: null
|
|
|
|
flavor:
|
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- flavor to use for the instance
|
2013-03-30 15:38:42 +01:00
|
|
|
default: null
|
2013-11-15 19:25:21 +01:00
|
|
|
group:
|
|
|
|
description:
|
|
|
|
- host group to assign to server, is also used for idempotent operations
|
|
|
|
to ensure a specific number of instances
|
|
|
|
version_added: 1.4
|
2013-03-30 15:38:42 +01:00
|
|
|
image:
|
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- image to use for the instance. Can be an C(id), C(human_id) or C(name)
|
2013-03-30 15:38:42 +01:00
|
|
|
default: null
|
2013-11-15 19:25:21 +01:00
|
|
|
instance_ids:
|
2013-03-31 09:05:14 +02:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- list of instance ids, currently only used when state='absent' to
|
|
|
|
remove instances
|
|
|
|
version_added: 1.4
|
2013-03-30 15:38:42 +01:00
|
|
|
key_name:
|
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- key pair to use on the instance
|
2013-03-30 15:38:42 +01:00
|
|
|
default: null
|
2014-02-24 19:47:42 +01:00
|
|
|
aliases:
|
|
|
|
- keypair
|
2013-11-15 19:25:21 +01:00
|
|
|
meta:
|
2013-03-31 09:05:14 +02:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- A hash of metadata to associate with the instance
|
2013-03-31 09:05:14 +02:00
|
|
|
default: null
|
2013-11-15 19:25:21 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name to give the instance
|
|
|
|
default: null
|
|
|
|
networks:
|
|
|
|
description:
|
|
|
|
- The network to attach to the instances. If specified, you must include
|
|
|
|
ALL networks including the public and private interfaces. Can be C(id)
|
|
|
|
or C(label).
|
2014-02-24 19:47:42 +01:00
|
|
|
default:
|
|
|
|
- public
|
|
|
|
- private
|
2013-11-15 19:25:21 +01:00
|
|
|
version_added: 1.4
|
|
|
|
state:
|
2013-08-23 18:57:53 +02:00
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- Indicate desired state of the resource
|
2014-02-24 19:47:42 +01:00
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
2013-11-15 19:25:21 +01:00
|
|
|
default: present
|
2014-05-21 14:16:10 +02:00
|
|
|
user_data:
|
|
|
|
description:
|
|
|
|
- Data to be uploaded to the servers config drive. This option implies
|
|
|
|
I(config_drive)
|
|
|
|
version_added: 1.7
|
2013-03-30 15:38:42 +01:00
|
|
|
wait:
|
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- wait for the instance to be in state 'running' before returning
|
2013-03-30 15:38:42 +01:00
|
|
|
default: "no"
|
2014-02-24 19:47:42 +01:00
|
|
|
choices:
|
|
|
|
- "yes"
|
|
|
|
- "no"
|
2013-03-30 15:38:42 +01:00
|
|
|
wait_timeout:
|
|
|
|
description:
|
2013-11-15 19:25:21 +01:00
|
|
|
- how long before wait gives up, in seconds
|
2013-03-30 15:38:42 +01:00
|
|
|
default: 300
|
2013-11-15 19:25:21 +01:00
|
|
|
author: Jesse Keating, Matt Martz
|
2014-02-25 03:48:15 +01:00
|
|
|
extends_documentation_fragment: rackspace.openstack
|
2013-03-30 15:38:42 +01:00
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
2013-08-13 15:46:18 +02:00
|
|
|
- name: Build a Cloud Server
|
|
|
|
gather_facts: False
|
|
|
|
tasks:
|
|
|
|
- name: Server build request
|
|
|
|
local_action:
|
|
|
|
module: rax
|
|
|
|
credentials: ~/.raxpub
|
|
|
|
name: rax-test1
|
|
|
|
flavor: 5
|
|
|
|
image: b11d9567-e412-4255-96b9-bd63ab23bcfe
|
|
|
|
files:
|
|
|
|
/root/.ssh/authorized_keys: /home/localuser/.ssh/id_rsa.pub
|
|
|
|
/root/test.txt: /home/localuser/test.txt
|
|
|
|
wait: yes
|
|
|
|
state: present
|
2013-11-15 19:25:21 +01:00
|
|
|
networks:
|
|
|
|
- private
|
|
|
|
- public
|
2014-01-07 16:10:31 +01:00
|
|
|
register: rax
|
|
|
|
|
|
|
|
- name: Build an exact count of cloud servers with incremented names
|
|
|
|
hosts: local
|
|
|
|
gather_facts: False
|
|
|
|
tasks:
|
|
|
|
- name: Server build requests
|
|
|
|
local_action:
|
|
|
|
module: rax
|
|
|
|
credentials: ~/.raxpub
|
|
|
|
name: test%03d.example.org
|
|
|
|
flavor: performance1-1
|
|
|
|
image: ubuntu-1204-lts-precise-pangolin
|
|
|
|
state: present
|
|
|
|
count: 10
|
|
|
|
count_offset: 10
|
|
|
|
exact_count: yes
|
|
|
|
group: test
|
|
|
|
wait: yes
|
|
|
|
register: rax
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2013-03-30 15:38:42 +01:00
|
|
|
import os
|
2013-11-15 19:25:21 +01:00
|
|
|
import re
|
2014-02-13 04:51:36 +01:00
|
|
|
import time
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
from uuid import UUID
|
|
|
|
from types import NoneType
|
2013-03-30 15:38:42 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import pyrax
|
2014-02-13 04:51:36 +01:00
|
|
|
HAS_PYRAX = True
|
2013-03-30 15:38:42 +01:00
|
|
|
except ImportError:
|
2014-02-13 04:51:36 +01:00
|
|
|
HAS_PYRAX = False
|
2013-03-30 15:38:42 +01:00
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
ACTIVE_STATUSES = ('ACTIVE', 'BUILD', 'HARD_REBOOT', 'MIGRATING', 'PASSWORD',
|
|
|
|
'REBOOT', 'REBUILD', 'RESCUE', 'RESIZE', 'REVERT_RESIZE')
|
|
|
|
FINAL_STATUSES = ('ACTIVE', 'ERROR')
|
|
|
|
NON_CALLABLES = (basestring, bool, dict, int, list, NoneType)
|
|
|
|
PUBLIC_NET_ID = "00000000-0000-0000-0000-000000000000"
|
|
|
|
SERVICE_NET_ID = "11111111-1111-1111-1111-111111111111"
|
2013-03-30 15:38:42 +01:00
|
|
|
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
def rax_slugify(value):
|
|
|
|
return 'rax_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_'))
|
|
|
|
|
|
|
|
|
2014-04-01 02:38:25 +02:00
|
|
|
def server_to_dict(obj):
|
2013-11-15 19:25:21 +01:00
|
|
|
instance = {}
|
|
|
|
for key in dir(obj):
|
|
|
|
value = getattr(obj, key)
|
|
|
|
if (isinstance(value, NON_CALLABLES) and not key.startswith('_')):
|
|
|
|
key = rax_slugify(key)
|
|
|
|
instance[key] = value
|
|
|
|
|
|
|
|
for attr in ['id', 'accessIPv4', 'name', 'status']:
|
|
|
|
instance[attr] = instance.get(rax_slugify(attr))
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2014-05-21 17:20:02 +02:00
|
|
|
def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
|
|
|
|
files={}, wait=True, wait_timeout=300, disk_config=None,
|
|
|
|
group=None, nics=[], extra_create_args={}, user_data=None,
|
|
|
|
config_drive=False, existing=[]):
|
2013-11-15 19:25:21 +01:00
|
|
|
cs = pyrax.cloudservers
|
|
|
|
changed = False
|
|
|
|
|
2014-05-21 14:16:10 +02:00
|
|
|
if user_data:
|
|
|
|
config_drive = True
|
|
|
|
|
|
|
|
if user_data and os.path.isfile(user_data):
|
|
|
|
try:
|
|
|
|
f = open(user_data)
|
|
|
|
user_data = f.read()
|
|
|
|
f.close()
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg='Failed to load %s' % user_data)
|
|
|
|
|
|
|
|
# Handle the file contents
|
2013-11-15 19:25:21 +01:00
|
|
|
for rpath in files.keys():
|
|
|
|
lpath = os.path.expanduser(files[rpath])
|
|
|
|
try:
|
|
|
|
fileobj = open(lpath, 'r')
|
2014-01-07 16:10:31 +01:00
|
|
|
files[rpath] = fileobj.read()
|
2014-05-21 14:16:10 +02:00
|
|
|
fileobj.close()
|
2013-11-15 19:25:21 +01:00
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg='Failed to load %s' % lpath)
|
|
|
|
try:
|
|
|
|
servers = []
|
|
|
|
for name in names:
|
|
|
|
servers.append(cs.servers.create(name=name, image=image,
|
|
|
|
flavor=flavor, meta=meta,
|
|
|
|
key_name=key_name,
|
|
|
|
files=files, nics=nics,
|
2014-03-05 19:38:28 +01:00
|
|
|
disk_config=disk_config,
|
2014-05-21 14:16:10 +02:00
|
|
|
config_drive=config_drive,
|
|
|
|
userdata=user_data,
|
2014-03-05 19:38:28 +01:00
|
|
|
**extra_create_args))
|
2013-11-15 19:25:21 +01:00
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg='%s' % e.message)
|
|
|
|
else:
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
end_time = time.time() + wait_timeout
|
|
|
|
infinite = wait_timeout == 0
|
|
|
|
while infinite or time.time() < end_time:
|
|
|
|
for server in servers:
|
|
|
|
try:
|
|
|
|
server.get()
|
|
|
|
except:
|
|
|
|
server.status == 'ERROR'
|
|
|
|
|
|
|
|
if not filter(lambda s: s.status not in FINAL_STATUSES,
|
|
|
|
servers):
|
|
|
|
break
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
success = []
|
|
|
|
error = []
|
|
|
|
timeout = []
|
|
|
|
for server in servers:
|
|
|
|
try:
|
|
|
|
server.get()
|
|
|
|
except:
|
|
|
|
server.status == 'ERROR'
|
2014-04-01 02:38:25 +02:00
|
|
|
instance = server_to_dict(server)
|
2013-11-15 19:25:21 +01:00
|
|
|
if server.status == 'ACTIVE' or not wait:
|
|
|
|
success.append(instance)
|
|
|
|
elif server.status == 'ERROR':
|
|
|
|
error.append(instance)
|
|
|
|
elif wait:
|
|
|
|
timeout.append(instance)
|
|
|
|
|
2014-04-01 02:38:25 +02:00
|
|
|
untouched = [server_to_dict(s) for s in existing]
|
|
|
|
instances = success + untouched
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
results = {
|
|
|
|
'changed': changed,
|
|
|
|
'action': 'create',
|
2014-04-01 02:38:25 +02:00
|
|
|
'instances': instances,
|
2013-11-15 19:25:21 +01:00
|
|
|
'success': success,
|
|
|
|
'error': error,
|
|
|
|
'timeout': timeout,
|
|
|
|
'instance_ids': {
|
2014-04-01 02:38:25 +02:00
|
|
|
'instances': [i['id'] for i in instances],
|
2013-11-15 19:25:21 +01:00
|
|
|
'success': [i['id'] for i in success],
|
|
|
|
'error': [i['id'] for i in error],
|
|
|
|
'timeout': [i['id'] for i in timeout]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout:
|
|
|
|
results['msg'] = 'Timeout waiting for all servers to build'
|
|
|
|
elif error:
|
|
|
|
results['msg'] = 'Failed to build all servers'
|
|
|
|
|
|
|
|
if 'msg' in results:
|
|
|
|
module.fail_json(**results)
|
|
|
|
else:
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
|
2014-05-21 17:20:02 +02:00
|
|
|
def delete(module, instance_ids=[], wait=True, wait_timeout=300, kept=[]):
|
2013-11-15 19:25:21 +01:00
|
|
|
cs = pyrax.cloudservers
|
|
|
|
|
2013-03-30 15:38:42 +01:00
|
|
|
changed = False
|
2013-11-15 19:25:21 +01:00
|
|
|
instances = {}
|
2013-03-30 15:38:42 +01:00
|
|
|
servers = []
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
for instance_id in instance_ids:
|
|
|
|
servers.append(cs.servers.get(instance_id))
|
|
|
|
|
|
|
|
for server in servers:
|
|
|
|
try:
|
|
|
|
server.delete()
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg=e.message)
|
|
|
|
else:
|
|
|
|
changed = True
|
|
|
|
|
2014-04-01 02:38:25 +02:00
|
|
|
instance = server_to_dict(server)
|
2013-11-15 19:25:21 +01:00
|
|
|
instances[instance['id']] = instance
|
|
|
|
|
|
|
|
# If requested, wait for server deletion
|
|
|
|
if wait:
|
|
|
|
end_time = time.time() + wait_timeout
|
|
|
|
infinite = wait_timeout == 0
|
|
|
|
while infinite or time.time() < end_time:
|
|
|
|
for server in servers:
|
|
|
|
instance_id = server.id
|
2013-03-31 09:05:14 +02:00
|
|
|
try:
|
2013-11-15 19:25:21 +01:00
|
|
|
server.get()
|
|
|
|
except:
|
|
|
|
instances[instance_id]['status'] = 'DELETED'
|
2014-04-01 02:38:25 +02:00
|
|
|
instances[instance_id]['rax_status'] = 'DELETED'
|
2013-11-15 19:25:21 +01:00
|
|
|
|
|
|
|
if not filter(lambda s: s['status'] not in ('', 'DELETED',
|
|
|
|
'ERROR'),
|
|
|
|
instances.values()):
|
|
|
|
break
|
|
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
timeout = filter(lambda s: s['status'] not in ('', 'DELETED', 'ERROR'),
|
|
|
|
instances.values())
|
|
|
|
error = filter(lambda s: s['status'] in ('ERROR'),
|
|
|
|
instances.values())
|
|
|
|
success = filter(lambda s: s['status'] in ('', 'DELETED'),
|
|
|
|
instances.values())
|
|
|
|
|
2014-04-01 02:38:25 +02:00
|
|
|
instances = [server_to_dict(s) for s in kept]
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
results = {
|
|
|
|
'changed': changed,
|
|
|
|
'action': 'delete',
|
2014-04-01 02:38:25 +02:00
|
|
|
'instances': instances,
|
2013-11-15 19:25:21 +01:00
|
|
|
'success': success,
|
|
|
|
'error': error,
|
|
|
|
'timeout': timeout,
|
|
|
|
'instance_ids': {
|
2014-04-01 02:38:25 +02:00
|
|
|
'instances': [i['id'] for i in instances],
|
2013-11-15 19:25:21 +01:00
|
|
|
'success': [i['id'] for i in success],
|
|
|
|
'error': [i['id'] for i in error],
|
|
|
|
'timeout': [i['id'] for i in timeout]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout:
|
|
|
|
results['msg'] = 'Timeout waiting for all servers to delete'
|
|
|
|
elif error:
|
|
|
|
results['msg'] = 'Failed to delete all servers'
|
|
|
|
|
|
|
|
if 'msg' in results:
|
|
|
|
module.fail_json(**results)
|
|
|
|
else:
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
|
2014-05-21 17:20:02 +02:00
|
|
|
def cloudservers(module, state=None, name=None, flavor=None, image=None,
|
|
|
|
meta={}, key_name=None, files={}, wait=True, wait_timeout=300,
|
|
|
|
disk_config=None, count=1, group=None, instance_ids=[],
|
|
|
|
exact_count=False, networks=[], count_offset=0,
|
|
|
|
auto_increment=False, extra_create_args={}, user_data=None,
|
|
|
|
config_drive=False):
|
2013-11-15 19:25:21 +01:00
|
|
|
cs = pyrax.cloudservers
|
|
|
|
cnw = pyrax.cloud_networks
|
2014-02-13 04:51:36 +01:00
|
|
|
if not cnw:
|
|
|
|
module.fail_json(msg='Failed to instantiate client. This '
|
|
|
|
'typically indicates an invalid region or an '
|
|
|
|
'incorrectly capitalized region name.')
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
servers = []
|
|
|
|
|
2014-02-13 04:51:36 +01:00
|
|
|
for key, value in meta.items():
|
|
|
|
meta[key] = repr(value)
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
# Add the group meta key
|
|
|
|
if group and 'group' not in meta:
|
|
|
|
meta['group'] = group
|
|
|
|
elif 'group' in meta and group is None:
|
|
|
|
group = meta['group']
|
|
|
|
|
2014-01-07 16:10:31 +01:00
|
|
|
# When using state=absent with group, the absent block won't match the
|
|
|
|
# names properly. Use the exact_count functionality to decrease the count
|
|
|
|
# to the desired level
|
|
|
|
was_absent = False
|
|
|
|
if group is not None and state == 'absent':
|
|
|
|
exact_count = True
|
|
|
|
state = 'present'
|
|
|
|
was_absent = True
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
# Check if the provided image is a UUID and if not, search for an
|
|
|
|
# appropriate image using human_id and name
|
|
|
|
if image:
|
|
|
|
try:
|
|
|
|
UUID(image)
|
|
|
|
except ValueError:
|
2013-03-30 15:38:42 +01:00
|
|
|
try:
|
2013-11-15 19:25:21 +01:00
|
|
|
image = cs.images.find(human_id=image)
|
2013-12-16 16:33:29 +01:00
|
|
|
except(cs.exceptions.NotFound,
|
|
|
|
cs.exceptions.NoUniqueMatch):
|
2013-11-15 19:25:21 +01:00
|
|
|
try:
|
|
|
|
image = cs.images.find(name=image)
|
2013-12-16 16:33:29 +01:00
|
|
|
except (cs.exceptions.NotFound,
|
|
|
|
cs.exceptions.NoUniqueMatch):
|
2013-11-15 19:25:21 +01:00
|
|
|
module.fail_json(msg='No matching image found (%s)' %
|
|
|
|
image)
|
|
|
|
|
|
|
|
image = pyrax.utils.get_id(image)
|
|
|
|
|
|
|
|
# Check if the provided network is a UUID and if not, search for an
|
|
|
|
# appropriate network using label
|
|
|
|
nics = []
|
|
|
|
if networks:
|
|
|
|
for network in networks:
|
|
|
|
try:
|
|
|
|
UUID(network)
|
|
|
|
except ValueError:
|
|
|
|
if network.lower() == 'public':
|
|
|
|
nics.extend(cnw.get_server_networks(PUBLIC_NET_ID))
|
|
|
|
elif network.lower() == 'private':
|
|
|
|
nics.extend(cnw.get_server_networks(SERVICE_NET_ID))
|
|
|
|
else:
|
2013-03-30 15:38:42 +01:00
|
|
|
try:
|
2013-11-15 19:25:21 +01:00
|
|
|
network_obj = cnw.find_network_by_label(network)
|
|
|
|
except (pyrax.exceptions.NetworkNotFound,
|
|
|
|
pyrax.exceptions.NetworkLabelNotUnique):
|
|
|
|
module.fail_json(msg='No matching network found (%s)' %
|
|
|
|
network)
|
|
|
|
else:
|
|
|
|
nics.extend(cnw.get_server_networks(network_obj))
|
|
|
|
else:
|
|
|
|
nics.extend(cnw.get_server_networks(network))
|
|
|
|
|
|
|
|
# act on the state
|
|
|
|
if state == 'present':
|
|
|
|
for arg, value in dict(name=name, flavor=flavor,
|
|
|
|
image=image).iteritems():
|
|
|
|
if not value:
|
|
|
|
module.fail_json(msg='%s is required for the "rax" module' %
|
|
|
|
arg)
|
|
|
|
|
|
|
|
# Idempotent ensurance of a specific count of servers
|
|
|
|
if exact_count is not False:
|
|
|
|
# See if we can find servers that match our options
|
|
|
|
if group is None:
|
|
|
|
module.fail_json(msg='"group" must be provided when using '
|
|
|
|
'"exact_count"')
|
|
|
|
else:
|
2014-01-07 16:10:31 +01:00
|
|
|
if auto_increment:
|
|
|
|
numbers = set()
|
2013-11-15 19:25:21 +01:00
|
|
|
|
2014-01-07 16:10:31 +01:00
|
|
|
try:
|
|
|
|
name % 0
|
|
|
|
except TypeError, e:
|
|
|
|
if e.message.startswith('not all'):
|
|
|
|
name = '%s%%d' % name
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=e.message)
|
|
|
|
|
2014-01-20 23:42:41 +01:00
|
|
|
pattern = re.sub(r'%\d*[sd]', r'(\d+)', name)
|
2014-01-07 16:10:31 +01:00
|
|
|
for server in cs.servers.list():
|
|
|
|
if server.metadata.get('group') == group:
|
|
|
|
servers.append(server)
|
|
|
|
match = re.search(pattern, server.name)
|
|
|
|
if match:
|
|
|
|
number = int(match.group(1))
|
|
|
|
numbers.add(number)
|
|
|
|
|
|
|
|
number_range = xrange(count_offset, count_offset + count)
|
|
|
|
available_numbers = list(set(number_range)
|
|
|
|
.difference(numbers))
|
|
|
|
else:
|
|
|
|
for server in cs.servers.list():
|
|
|
|
if server.metadata.get('group') == group:
|
|
|
|
servers.append(server)
|
|
|
|
|
|
|
|
# If state was absent but the count was changed,
|
|
|
|
# assume we only wanted to remove that number of instances
|
|
|
|
if was_absent:
|
|
|
|
diff = len(servers) - count
|
|
|
|
if diff < 0:
|
|
|
|
count = 0
|
2013-11-15 19:25:21 +01:00
|
|
|
else:
|
2014-01-07 16:10:31 +01:00
|
|
|
count = diff
|
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
if len(servers) > count:
|
|
|
|
state = 'absent'
|
2014-04-01 02:38:25 +02:00
|
|
|
kept = servers[:count]
|
2013-11-15 19:25:21 +01:00
|
|
|
del servers[:count]
|
|
|
|
instance_ids = []
|
|
|
|
for server in servers:
|
|
|
|
instance_ids.append(server.id)
|
2014-05-21 17:20:02 +02:00
|
|
|
delete(module, instance_ids=instance_ids, wait=wait,
|
|
|
|
wait_timeout=wait_timeout, kept=kept)
|
2013-11-15 19:25:21 +01:00
|
|
|
elif len(servers) < count:
|
2014-01-07 16:10:31 +01:00
|
|
|
if auto_increment:
|
|
|
|
names = []
|
|
|
|
name_slice = count - len(servers)
|
|
|
|
numbers_to_use = available_numbers[:name_slice]
|
|
|
|
for number in numbers_to_use:
|
|
|
|
names.append(name % number)
|
|
|
|
else:
|
|
|
|
names = [name] * (count - len(servers))
|
2013-11-15 19:25:21 +01:00
|
|
|
else:
|
2014-04-01 02:38:25 +02:00
|
|
|
instances = []
|
|
|
|
instance_ids = []
|
|
|
|
for server in servers:
|
|
|
|
instances.append(server_to_dict(server))
|
|
|
|
instance_ids.append(server.id)
|
|
|
|
module.exit_json(changed=False, action=None,
|
|
|
|
instances=instances,
|
2013-11-15 19:25:21 +01:00
|
|
|
success=[], error=[], timeout=[],
|
2014-04-01 02:38:25 +02:00
|
|
|
instance_ids={'instances': instance_ids,
|
2013-11-15 19:25:21 +01:00
|
|
|
'success': [], 'error': [],
|
|
|
|
'timeout': []})
|
|
|
|
else:
|
|
|
|
if group is not None:
|
2014-01-07 16:10:31 +01:00
|
|
|
if auto_increment:
|
|
|
|
numbers = set()
|
2013-11-15 19:25:21 +01:00
|
|
|
|
2014-01-07 16:10:31 +01:00
|
|
|
try:
|
|
|
|
name % 0
|
|
|
|
except TypeError, e:
|
|
|
|
if e.message.startswith('not all'):
|
|
|
|
name = '%s%%d' % name
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=e.message)
|
|
|
|
|
2014-01-20 23:42:41 +01:00
|
|
|
pattern = re.sub(r'%\d*[sd]', r'(\d+)', name)
|
2014-01-07 16:10:31 +01:00
|
|
|
for server in cs.servers.list():
|
|
|
|
if server.metadata.get('group') == group:
|
|
|
|
servers.append(server)
|
|
|
|
match = re.search(pattern, server.name)
|
|
|
|
if match:
|
|
|
|
number = int(match.group(1))
|
|
|
|
numbers.add(number)
|
|
|
|
|
|
|
|
number_range = xrange(count_offset,
|
|
|
|
count_offset + count + len(numbers))
|
|
|
|
available_numbers = list(set(number_range)
|
|
|
|
.difference(numbers))
|
|
|
|
names = []
|
|
|
|
numbers_to_use = available_numbers[:count]
|
|
|
|
for number in numbers_to_use:
|
|
|
|
names.append(name % number)
|
|
|
|
else:
|
|
|
|
names = [name] * count
|
2013-11-15 19:25:21 +01:00
|
|
|
else:
|
|
|
|
search_opts = {
|
2014-02-10 20:15:03 +01:00
|
|
|
'name': '^%s$' % name,
|
2013-11-15 19:25:21 +01:00
|
|
|
'image': image,
|
|
|
|
'flavor': flavor
|
|
|
|
}
|
|
|
|
servers = []
|
|
|
|
for server in cs.servers.list(search_opts=search_opts):
|
|
|
|
if server.metadata != meta:
|
|
|
|
continue
|
|
|
|
servers.append(server)
|
|
|
|
|
|
|
|
if len(servers) >= count:
|
|
|
|
instances = []
|
|
|
|
for server in servers:
|
2014-04-01 02:38:25 +02:00
|
|
|
instances.append(server_to_dict(server))
|
2013-11-15 19:25:21 +01:00
|
|
|
|
|
|
|
instance_ids = [i['id'] for i in instances]
|
|
|
|
module.exit_json(changed=False, action=None,
|
|
|
|
instances=instances, success=[], error=[],
|
|
|
|
timeout=[],
|
|
|
|
instance_ids={'instances': instance_ids,
|
|
|
|
'success': [], 'error': [],
|
|
|
|
'timeout': []})
|
|
|
|
|
|
|
|
names = [name] * (count - len(servers))
|
|
|
|
|
2014-05-21 17:20:02 +02:00
|
|
|
create(module, names=names, flavor=flavor, image=image,
|
|
|
|
meta=meta, key_name=key_name, files=files, wait=wait,
|
|
|
|
wait_timeout=wait_timeout, disk_config=disk_config, group=group,
|
|
|
|
nics=nics, extra_create_args=extra_create_args,
|
|
|
|
user_data=user_data, config_drive=config_drive, existing=servers)
|
2013-11-15 19:25:21 +01:00
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
if instance_ids is None:
|
|
|
|
for arg, value in dict(name=name, flavor=flavor,
|
|
|
|
image=image).iteritems():
|
|
|
|
if not value:
|
|
|
|
module.fail_json(msg='%s is required for the "rax" '
|
|
|
|
'module' % arg)
|
|
|
|
search_opts = {
|
2014-02-10 20:15:03 +01:00
|
|
|
'name': '^%s$' % name,
|
2013-11-15 19:25:21 +01:00
|
|
|
'image': image,
|
|
|
|
'flavor': flavor
|
|
|
|
}
|
|
|
|
for server in cs.servers.list(search_opts=search_opts):
|
|
|
|
if meta != server.metadata:
|
|
|
|
continue
|
|
|
|
servers.append(server)
|
|
|
|
|
|
|
|
instance_ids = []
|
|
|
|
for server in servers:
|
|
|
|
if len(instance_ids) < count:
|
|
|
|
instance_ids.append(server.id)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
if not instance_ids:
|
|
|
|
module.exit_json(changed=False, action=None, instances=[],
|
|
|
|
success=[], error=[], timeout=[],
|
|
|
|
instance_ids={'instances': [],
|
|
|
|
'success': [], 'error': [],
|
|
|
|
'timeout': []})
|
|
|
|
|
2014-05-21 17:20:02 +02:00
|
|
|
delete(module, instance_ids=instance_ids, wait=wait,
|
|
|
|
wait_timeout=wait_timeout)
|
2013-11-15 19:25:21 +01:00
|
|
|
|
2013-03-30 15:38:42 +01:00
|
|
|
|
|
|
|
def main():
|
2013-11-19 19:51:04 +01:00
|
|
|
argument_spec = rax_argument_spec()
|
|
|
|
argument_spec.update(
|
|
|
|
dict(
|
2014-03-12 04:46:53 +01:00
|
|
|
auto_increment=dict(default=True, type='bool'),
|
2014-05-21 14:16:10 +02:00
|
|
|
config_drive=dict(default=False, type='bool'),
|
2013-11-15 19:25:21 +01:00
|
|
|
count=dict(default=1, type='int'),
|
|
|
|
count_offset=dict(default=1, type='int'),
|
2014-02-10 23:12:32 +01:00
|
|
|
disk_config=dict(choices=['auto', 'manual']),
|
2014-03-12 04:46:53 +01:00
|
|
|
exact_count=dict(default=False, type='bool'),
|
2014-03-05 19:38:28 +01:00
|
|
|
extra_client_args=dict(type='dict', default={}),
|
|
|
|
extra_create_args=dict(type='dict', default={}),
|
2013-11-15 19:25:21 +01:00
|
|
|
files=dict(type='dict', default={}),
|
|
|
|
flavor=dict(),
|
|
|
|
group=dict(),
|
|
|
|
image=dict(),
|
|
|
|
instance_ids=dict(type='list'),
|
|
|
|
key_name=dict(aliases=['keypair']),
|
|
|
|
meta=dict(type='dict', default={}),
|
|
|
|
name=dict(),
|
|
|
|
networks=dict(type='list', default=['public', 'private']),
|
|
|
|
service=dict(),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
2014-05-21 14:16:10 +02:00
|
|
|
user_data=dict(no_log=True),
|
2014-03-12 04:46:53 +01:00
|
|
|
wait=dict(default=False, type='bool'),
|
2013-11-15 19:25:21 +01:00
|
|
|
wait_timeout=dict(default=300),
|
2013-11-19 19:51:04 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
required_together=rax_required_together(),
|
2013-03-30 15:38:42 +01:00
|
|
|
)
|
|
|
|
|
2014-02-13 04:51:36 +01:00
|
|
|
if not HAS_PYRAX:
|
|
|
|
module.fail_json(msg='pyrax is required for this module')
|
|
|
|
|
2013-03-30 15:38:42 +01:00
|
|
|
service = module.params.get('service')
|
2013-11-15 19:25:21 +01:00
|
|
|
|
|
|
|
if service is not None:
|
|
|
|
module.fail_json(msg='The "service" attribute has been deprecated, '
|
|
|
|
'please remove "service: cloudservers" from your '
|
|
|
|
'playbook pertaining to the "rax" module')
|
|
|
|
|
2014-01-07 16:10:31 +01:00
|
|
|
auto_increment = module.params.get('auto_increment')
|
2014-05-21 14:16:10 +02:00
|
|
|
config_drive = module.params.get('config_drive')
|
2013-11-15 19:25:21 +01:00
|
|
|
count = module.params.get('count')
|
|
|
|
count_offset = module.params.get('count_offset')
|
2014-02-10 23:12:32 +01:00
|
|
|
disk_config = module.params.get('disk_config')
|
|
|
|
if disk_config:
|
|
|
|
disk_config = disk_config.upper()
|
2013-11-15 19:25:21 +01:00
|
|
|
exact_count = module.params.get('exact_count', False)
|
2014-03-05 19:38:28 +01:00
|
|
|
extra_client_args = module.params.get('extra_client_args')
|
|
|
|
extra_create_args = module.params.get('extra_create_args')
|
2013-11-15 19:25:21 +01:00
|
|
|
files = module.params.get('files')
|
2013-03-30 15:38:42 +01:00
|
|
|
flavor = module.params.get('flavor')
|
2013-11-15 19:25:21 +01:00
|
|
|
group = module.params.get('group')
|
2013-03-30 15:38:42 +01:00
|
|
|
image = module.params.get('image')
|
2013-11-15 19:25:21 +01:00
|
|
|
instance_ids = module.params.get('instance_ids')
|
2013-03-31 09:05:14 +02:00
|
|
|
key_name = module.params.get('key_name')
|
2013-11-15 19:25:21 +01:00
|
|
|
meta = module.params.get('meta')
|
|
|
|
name = module.params.get('name')
|
|
|
|
networks = module.params.get('networks')
|
|
|
|
state = module.params.get('state')
|
2014-05-21 14:16:10 +02:00
|
|
|
user_data = module.params.get('user_data')
|
2013-03-30 15:38:42 +01:00
|
|
|
wait = module.params.get('wait')
|
|
|
|
wait_timeout = int(module.params.get('wait_timeout'))
|
|
|
|
|
2013-11-19 19:51:04 +01:00
|
|
|
setup_rax_module(module, pyrax)
|
2013-03-30 15:38:42 +01:00
|
|
|
|
2014-03-05 19:38:28 +01:00
|
|
|
if extra_client_args:
|
|
|
|
pyrax.cloudservers = pyrax.connect_to_cloudservers(
|
|
|
|
region=pyrax.cloudservers.client.region_name,
|
|
|
|
**extra_client_args)
|
|
|
|
client = pyrax.cloudservers.client
|
|
|
|
if 'bypass_url' in extra_client_args:
|
|
|
|
client.management_url = extra_client_args['bypass_url']
|
|
|
|
|
2014-02-13 04:51:36 +01:00
|
|
|
if pyrax.cloudservers is None:
|
|
|
|
module.fail_json(msg='Failed to instantiate client. This '
|
|
|
|
'typically indicates an invalid region or an '
|
|
|
|
'incorrectly capitalized region name.')
|
|
|
|
|
2014-05-21 17:20:02 +02:00
|
|
|
cloudservers(module, state=state, name=name, flavor=flavor,
|
|
|
|
image=image, meta=meta, key_name=key_name, files=files,
|
|
|
|
wait=wait, wait_timeout=wait_timeout, disk_config=disk_config,
|
|
|
|
count=count, group=group, instance_ids=instance_ids,
|
|
|
|
exact_count=exact_count, networks=networks,
|
|
|
|
count_offset=count_offset, auto_increment=auto_increment,
|
|
|
|
extra_create_args=extra_create_args, user_data=user_data,
|
|
|
|
config_drive=config_drive)
|
2013-03-30 15:38:42 +01:00
|
|
|
|
|
|
|
|
2013-11-19 19:51:04 +01:00
|
|
|
# import module snippets
|
2013-11-15 19:25:21 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-11-19 19:51:04 +01:00
|
|
|
from ansible.module_utils.rax import *
|
2013-03-30 15:38:42 +01:00
|
|
|
|
2013-11-15 19:25:21 +01:00
|
|
|
### invoke the module
|
2013-03-30 15:38:42 +01:00
|
|
|
main()
|