|
|
@ -194,44 +194,12 @@ EXAMPLES = '''
|
|
|
|
register: rax
|
|
|
|
register: rax
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from types import NoneType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import pyrax
|
|
|
|
import pyrax
|
|
|
|
HAS_PYRAX = True
|
|
|
|
HAS_PYRAX = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYRAX = False
|
|
|
|
HAS_PYRAX = False
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rax_slugify(value):
|
|
|
|
|
|
|
|
return 'rax_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def server_to_dict(obj):
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
|
|
|
|
def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
|
|
|
|
files={}, wait=True, wait_timeout=300, disk_config=None,
|
|
|
|
files={}, wait=True, wait_timeout=300, disk_config=None,
|
|
|
@ -299,7 +267,7 @@ def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
|
|
|
|
server.get()
|
|
|
|
server.get()
|
|
|
|
except:
|
|
|
|
except:
|
|
|
|
server.status == 'ERROR'
|
|
|
|
server.status == 'ERROR'
|
|
|
|
instance = server_to_dict(server)
|
|
|
|
instance = rax_to_dict(server, 'server')
|
|
|
|
if server.status == 'ACTIVE' or not wait:
|
|
|
|
if server.status == 'ACTIVE' or not wait:
|
|
|
|
success.append(instance)
|
|
|
|
success.append(instance)
|
|
|
|
elif server.status == 'ERROR':
|
|
|
|
elif server.status == 'ERROR':
|
|
|
@ -307,7 +275,7 @@ def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
|
|
|
|
elif wait:
|
|
|
|
elif wait:
|
|
|
|
timeout.append(instance)
|
|
|
|
timeout.append(instance)
|
|
|
|
|
|
|
|
|
|
|
|
untouched = [server_to_dict(s) for s in existing]
|
|
|
|
untouched = [rax_to_dict(s, 'server') for s in existing]
|
|
|
|
instances = success + untouched
|
|
|
|
instances = success + untouched
|
|
|
|
|
|
|
|
|
|
|
|
results = {
|
|
|
|
results = {
|
|
|
@ -354,7 +322,7 @@ def delete(module, instance_ids=[], wait=True, wait_timeout=300, kept=[]):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
|
|
instance = server_to_dict(server)
|
|
|
|
instance = rax_to_dict(server, 'server')
|
|
|
|
instances[instance['id']] = instance
|
|
|
|
instances[instance['id']] = instance
|
|
|
|
|
|
|
|
|
|
|
|
# If requested, wait for server deletion
|
|
|
|
# If requested, wait for server deletion
|
|
|
@ -384,7 +352,7 @@ def delete(module, instance_ids=[], wait=True, wait_timeout=300, kept=[]):
|
|
|
|
success = filter(lambda s: s['status'] in ('', 'DELETED'),
|
|
|
|
success = filter(lambda s: s['status'] in ('', 'DELETED'),
|
|
|
|
instances.values())
|
|
|
|
instances.values())
|
|
|
|
|
|
|
|
|
|
|
|
instances = [server_to_dict(s) for s in kept]
|
|
|
|
instances = [rax_to_dict(s, 'server') for s in kept]
|
|
|
|
|
|
|
|
|
|
|
|
results = {
|
|
|
|
results = {
|
|
|
|
'changed': changed,
|
|
|
|
'changed': changed,
|
|
|
@ -451,48 +419,13 @@ def cloudservers(module, state=None, name=None, flavor=None, image=None,
|
|
|
|
state = 'present'
|
|
|
|
state = 'present'
|
|
|
|
was_absent = True
|
|
|
|
was_absent = True
|
|
|
|
|
|
|
|
|
|
|
|
# Check if the provided image is a UUID and if not, search for an
|
|
|
|
|
|
|
|
# appropriate image using human_id and name
|
|
|
|
|
|
|
|
if image:
|
|
|
|
if image:
|
|
|
|
try:
|
|
|
|
image = rax_find_image(module, pyrax, image)
|
|
|
|
UUID(image)
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
image = cs.images.find(human_id=image)
|
|
|
|
|
|
|
|
except(cs.exceptions.NotFound,
|
|
|
|
|
|
|
|
cs.exceptions.NoUniqueMatch):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
image = cs.images.find(name=image)
|
|
|
|
|
|
|
|
except (cs.exceptions.NotFound,
|
|
|
|
|
|
|
|
cs.exceptions.NoUniqueMatch):
|
|
|
|
|
|
|
|
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 = []
|
|
|
|
nics = []
|
|
|
|
if networks:
|
|
|
|
if networks:
|
|
|
|
for network in networks:
|
|
|
|
for network in networks:
|
|
|
|
try:
|
|
|
|
nics.extend(rax_find_network(module, pyrax, network))
|
|
|
|
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:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
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
|
|
|
|
# act on the state
|
|
|
|
if state == 'present':
|
|
|
|
if state == 'present':
|
|
|
@ -568,7 +501,7 @@ def cloudservers(module, state=None, name=None, flavor=None, image=None,
|
|
|
|
instances = []
|
|
|
|
instances = []
|
|
|
|
instance_ids = []
|
|
|
|
instance_ids = []
|
|
|
|
for server in servers:
|
|
|
|
for server in servers:
|
|
|
|
instances.append(server_to_dict(server))
|
|
|
|
instances.append(rax_to_dict(server, 'server'))
|
|
|
|
instance_ids.append(server.id)
|
|
|
|
instance_ids.append(server.id)
|
|
|
|
module.exit_json(changed=False, action=None,
|
|
|
|
module.exit_json(changed=False, action=None,
|
|
|
|
instances=instances,
|
|
|
|
instances=instances,
|
|
|
@ -623,7 +556,7 @@ def cloudservers(module, state=None, name=None, flavor=None, image=None,
|
|
|
|
if len(servers) >= count:
|
|
|
|
if len(servers) >= count:
|
|
|
|
instances = []
|
|
|
|
instances = []
|
|
|
|
for server in servers:
|
|
|
|
for server in servers:
|
|
|
|
instances.append(server_to_dict(server))
|
|
|
|
instances.append(rax_to_dict(server, 'server'))
|
|
|
|
|
|
|
|
|
|
|
|
instance_ids = [i['id'] for i in instances]
|
|
|
|
instance_ids = [i['id'] for i in instances]
|
|
|
|
module.exit_json(changed=False, action=None,
|
|
|
|
module.exit_json(changed=False, action=None,
|
|
|
|