2013-07-17 20:37:04 +00:00
|
|
|
#!/usr/bin/python -tt
|
2013-03-30 14:38:42 +00: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/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: rax
|
2013-04-16 00:20:45 +00:00
|
|
|
short_description: create / delete an instance in Rackspace Public Cloud
|
2013-03-30 14:38:42 +00:00
|
|
|
description:
|
2013-06-14 09:53:43 +00:00
|
|
|
- creates / deletes a Rackspace Public Cloud instance and optionally waits for it to be 'running'.
|
2013-03-30 14:38:42 +00:00
|
|
|
version_added: "1.2"
|
|
|
|
options:
|
|
|
|
service:
|
|
|
|
description:
|
|
|
|
- Cloud service to interact with
|
2013-04-16 00:20:45 +00:00
|
|
|
choices: ['cloudservers']
|
2013-03-30 14:38:42 +00:00
|
|
|
default: cloudservers
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the resource
|
|
|
|
choices: ['present', 'active', 'absent', 'deleted']
|
|
|
|
default: present
|
2013-08-05 22:49:14 +00:00
|
|
|
credentials:
|
2013-03-30 14:38:42 +00:00
|
|
|
description:
|
2013-08-05 22:49:14 +00:00
|
|
|
- File to find the Rackspace credentials in (ignored if C(api_key) and
|
|
|
|
C(username) are provided)
|
2013-03-30 14:38:42 +00:00
|
|
|
default: null
|
2013-08-05 22:49:14 +00:00
|
|
|
aliases: ['creds_file']
|
|
|
|
api_key:
|
|
|
|
description:
|
|
|
|
- Rackspace API key (overrides C(credentials))
|
|
|
|
username:
|
|
|
|
description:
|
|
|
|
- Rackspace username (overrides C(credentials))
|
2013-03-30 14:38:42 +00:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name to give the instance
|
|
|
|
default: null
|
|
|
|
flavor:
|
|
|
|
description:
|
2013-03-31 07:05:14 +00:00
|
|
|
- flavor to use for the instance
|
2013-03-30 14:38:42 +00:00
|
|
|
default: null
|
|
|
|
image:
|
|
|
|
description:
|
2013-03-31 07:05:14 +00:00
|
|
|
- image to use for the instance
|
2013-03-30 14:38:42 +00:00
|
|
|
default: null
|
2013-03-31 07:05:14 +00:00
|
|
|
meta:
|
|
|
|
description:
|
|
|
|
- A hash of metadata to associate with the instance
|
|
|
|
default: null
|
2013-03-30 14:38:42 +00:00
|
|
|
key_name:
|
|
|
|
description:
|
2013-03-31 07:05:14 +00:00
|
|
|
- key pair to use on the instance
|
2013-03-30 14:38:42 +00:00
|
|
|
default: null
|
|
|
|
aliases: ['keypair']
|
2013-03-31 07:05:14 +00:00
|
|
|
files:
|
|
|
|
description:
|
2013-07-31 01:08:22 +00:00
|
|
|
- Files to insert into the instance. remotefilename:localcontent
|
2013-03-31 07:05:14 +00:00
|
|
|
default: null
|
2013-03-30 14:38:42 +00:00
|
|
|
region:
|
|
|
|
description:
|
2013-03-31 07:05:14 +00:00
|
|
|
- Region to create an instance in
|
2013-08-05 22:49:14 +00:00
|
|
|
default: DFW
|
2013-03-30 14:38:42 +00:00
|
|
|
wait:
|
|
|
|
description:
|
2013-03-31 07:05:14 +00:00
|
|
|
- wait for the instance to be in state 'running' before returning
|
2013-03-30 14:38:42 +00:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
wait_timeout:
|
|
|
|
description:
|
2013-03-31 07:05:14 +00:00
|
|
|
- how long before wait gives up, in seconds
|
2013-03-30 14:38:42 +00:00
|
|
|
default: 300
|
|
|
|
requirements: [ "pyrax" ]
|
|
|
|
author: Jesse Keating
|
|
|
|
notes:
|
2013-08-05 22:49:14 +00:00
|
|
|
- The following environment variables can be used, C(RAX_USERNAME),
|
|
|
|
C(RAX_API_KEY), C(RAX_CREDS), C(RAX_CREDENTIALS), C(RAX_REGION).
|
|
|
|
- C(RAX_CREDENTIALS) and C(RAX_CREDS) points to a credentials file
|
|
|
|
appropriate for pyrax
|
|
|
|
- C(RAX_USERNAME) and C(RAX_API_KEY) obviate the use of a credentials file
|
|
|
|
- C(RAX_REGION) defines a Rackspace Public Cloud region (DFW, ORD, LON, ...)
|
2013-03-30 14:38:42 +00:00
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
2013-08-13 13:46:18 +00:00
|
|
|
- name: Build a Cloud Server
|
|
|
|
gather_facts: False
|
|
|
|
|
|
|
|
tasks:
|
|
|
|
- name: Server build request
|
|
|
|
local_action:
|
|
|
|
module: rax
|
|
|
|
credentials: ~/.raxpub
|
|
|
|
service: cloudservers
|
|
|
|
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-06-14 09:53:43 +00:00
|
|
|
'''
|
|
|
|
|
2013-03-30 14:38:42 +00:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
try:
|
|
|
|
import pyrax
|
2013-09-20 13:53:54 +00:00
|
|
|
import pyrax.utils
|
2013-03-30 14:38:42 +00:00
|
|
|
except ImportError:
|
|
|
|
print("failed=True msg='pyrax required for this module'")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-04-16 00:22:42 +00:00
|
|
|
# These are possible services, but only cloudservers is supported at this time
|
|
|
|
#SUPPORTEDSERVICES = ['cloudservers', 'cloudfiles', 'cloud_blockstorage',
|
|
|
|
# 'cloud_databases', 'cloud_loadbalancers']
|
|
|
|
SUPPORTEDSERVICES = ['cloudservers']
|
2013-03-30 14:38:42 +00:00
|
|
|
|
2013-03-31 07:05:14 +00:00
|
|
|
def cloudservers(module, state, name, flavor, image, meta, key_name, files,
|
|
|
|
wait, wait_timeout):
|
2013-03-30 14:38:42 +00:00
|
|
|
# Check our args (this could be done better)
|
|
|
|
for arg in (state, name, flavor, image):
|
|
|
|
if not arg:
|
|
|
|
module.fail_json(msg='%s is required for cloudservers' % arg)
|
|
|
|
|
|
|
|
instances = []
|
|
|
|
changed = False
|
|
|
|
servers = []
|
|
|
|
# See if we can find servers that match our options
|
|
|
|
for server in pyrax.cloudservers.list():
|
|
|
|
if name != server.name:
|
|
|
|
continue
|
2013-04-27 22:13:01 +00:00
|
|
|
if int(flavor) != int(server.flavor['id']):
|
2013-03-30 14:38:42 +00:00
|
|
|
continue
|
|
|
|
if image != server.image['id']:
|
|
|
|
continue
|
|
|
|
if meta != server.metadata:
|
|
|
|
continue
|
|
|
|
# Nothing else ruled us not a match, so consider it a winner
|
|
|
|
servers.append(server)
|
|
|
|
|
|
|
|
# act on the state
|
|
|
|
if state in ('active', 'present'):
|
|
|
|
if not servers:
|
2013-03-31 07:05:14 +00:00
|
|
|
# Handle the file contents
|
|
|
|
for rpath in files.keys():
|
|
|
|
lpath = os.path.expanduser(files[rpath])
|
|
|
|
try:
|
|
|
|
fileobj = open(lpath, 'r')
|
|
|
|
files[rpath] = fileobj
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg = 'Failed to load %s' % lpath)
|
2013-03-30 14:38:42 +00:00
|
|
|
try:
|
|
|
|
servers = [pyrax.cloudservers.servers.create(name=name,
|
|
|
|
image=image,
|
|
|
|
flavor=flavor,
|
|
|
|
key_name=key_name,
|
2013-03-31 07:05:14 +00:00
|
|
|
meta=meta,
|
|
|
|
files=files)]
|
2013-03-30 14:38:42 +00:00
|
|
|
changed = True
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg = '%s' % e.message)
|
|
|
|
|
|
|
|
for server in servers:
|
2013-09-20 13:53:54 +00:00
|
|
|
# If requested, wait for server activation
|
|
|
|
if wait:
|
|
|
|
pyrax.utils.wait_until(server, 'status', ('ACTIVE', 'ERROR'),
|
|
|
|
interval=5, attempts=wait_timeout/5)
|
|
|
|
|
2013-03-30 14:38:42 +00:00
|
|
|
# Get a fresh copy of the server details
|
|
|
|
server.get()
|
2013-09-20 13:53:54 +00:00
|
|
|
if server.status == 'ACTIVE':
|
|
|
|
instance = {'id': server.id,
|
|
|
|
'accessIPv4': server.accessIPv4,
|
|
|
|
'name': server.name,
|
|
|
|
'status': server.status}
|
|
|
|
instances.append(instance)
|
|
|
|
elif server.status == 'ERROR':
|
2013-03-30 14:38:42 +00:00
|
|
|
module.fail_json(msg = '%s failed to build' % server.id)
|
2013-09-20 13:53:54 +00:00
|
|
|
elif wait:
|
|
|
|
# waiting took too long
|
|
|
|
module.fail_json(msg = 'Timeout waiting on %s' % server.id)
|
|
|
|
|
2013-03-30 14:38:42 +00:00
|
|
|
|
|
|
|
elif state in ('absent', 'deleted'):
|
|
|
|
# See if we can find a server that matches our credentials
|
|
|
|
for server in servers:
|
|
|
|
if server.name == name:
|
2013-08-13 13:46:18 +00:00
|
|
|
if int(server.flavor['id']) == int(flavor) and \
|
2013-03-30 14:38:42 +00:00
|
|
|
server.image['id'] == image and \
|
|
|
|
server.metadata == meta:
|
|
|
|
try:
|
|
|
|
server.delete()
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg = e.message)
|
2013-09-20 13:53:54 +00:00
|
|
|
|
2013-03-30 14:38:42 +00:00
|
|
|
instance = {'id': server.id,
|
|
|
|
'accessIPv4': server.accessIPv4,
|
|
|
|
'name': server.name,
|
|
|
|
'status': 'DELETING'}
|
2013-09-20 13:53:54 +00:00
|
|
|
|
|
|
|
# If requested, wait for server deletion
|
|
|
|
if wait:
|
|
|
|
try:
|
|
|
|
pyrax.utils.wait_until(server, 'status', '', interval=5,
|
|
|
|
attempts=wait_timeout/5)
|
|
|
|
# Get a fresh copy of the server details
|
|
|
|
server.get()
|
|
|
|
except Exception, e:
|
|
|
|
# In this case, an exception means the server is NotFound
|
|
|
|
instance['status'] = 'DELETED'
|
|
|
|
else:
|
|
|
|
# waiting took too long
|
|
|
|
module.fail_json(msg = 'Timeout waiting on delete %s (%s)' % (server.id, server.status))
|
|
|
|
|
2013-03-30 14:38:42 +00:00
|
|
|
instances.append(instance)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, instances=instances)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
service = dict(default='cloudservers', choices=SUPPORTEDSERVICES),
|
|
|
|
state = dict(default='present', choices=['active', 'present',
|
|
|
|
'deleted', 'absent']),
|
2013-08-05 22:49:14 +00:00
|
|
|
credentials = dict(aliases = ['creds_file']),
|
|
|
|
api_key=dict(),
|
|
|
|
username=dict(),
|
2013-03-30 14:38:42 +00:00
|
|
|
name = dict(),
|
|
|
|
flavor = dict(),
|
|
|
|
image = dict(),
|
|
|
|
meta = dict(type='dict', default={}),
|
2013-03-31 07:05:14 +00:00
|
|
|
key_name = dict(aliases = ['keypair']),
|
|
|
|
files = dict(type='dict', default={}),
|
2013-03-30 14:38:42 +00:00
|
|
|
region = dict(),
|
|
|
|
wait = dict(type='bool', choices=BOOLEANS),
|
|
|
|
wait_timeout = dict(default=300),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
service = module.params.get('service')
|
|
|
|
state = module.params.get('state')
|
2013-08-05 22:49:14 +00:00
|
|
|
credentials = module.params.get('credentials')
|
|
|
|
api_key = module.params.get('api_key')
|
|
|
|
username = module.params.get('username')
|
2013-03-30 14:38:42 +00:00
|
|
|
name = module.params.get('name')
|
|
|
|
flavor = module.params.get('flavor')
|
|
|
|
image = module.params.get('image')
|
|
|
|
meta = module.params.get('meta')
|
2013-03-31 07:05:14 +00:00
|
|
|
key_name = module.params.get('key_name')
|
|
|
|
files = module.params.get('files')
|
2013-03-30 14:38:42 +00:00
|
|
|
region = module.params.get('region')
|
|
|
|
wait = module.params.get('wait')
|
|
|
|
wait_timeout = int(module.params.get('wait_timeout'))
|
|
|
|
|
2013-08-05 22:49:14 +00:00
|
|
|
# Setup the credentials and region
|
|
|
|
try:
|
|
|
|
username = username or os.environ.get('RAX_USERNAME')
|
|
|
|
api_key = api_key or os.environ.get('RAX_API_KEY')
|
|
|
|
credentials = credentials or os.environ.get('RAX_CREDENTIALS') or \
|
|
|
|
os.environ.get('RAX_CREDS_FILE')
|
|
|
|
region = region or os.environ.get('RAX_REGION')
|
2013-03-30 14:38:42 +00:00
|
|
|
|
2013-08-05 22:49:14 +00:00
|
|
|
except KeyError, e:
|
|
|
|
module.fail_json(msg = 'Unable to load %s' % e.message)
|
2013-03-30 14:38:42 +00:00
|
|
|
|
|
|
|
# setup the auth
|
|
|
|
try:
|
2013-06-27 18:50:36 +00:00
|
|
|
pyrax.set_setting("identity_type", "rackspace")
|
2013-08-05 22:49:14 +00:00
|
|
|
if api_key and username:
|
|
|
|
pyrax.set_credentials(username, api_key=api_key, region=region)
|
|
|
|
elif credentials:
|
|
|
|
credentials = os.path.expanduser(credentials)
|
|
|
|
pyrax.set_credential_file(credentials, region=region)
|
|
|
|
else:
|
|
|
|
raise Exception('No credentials supplied!')
|
2013-03-30 14:38:42 +00:00
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg = '%s' % e.message)
|
|
|
|
|
|
|
|
# Act based on service
|
|
|
|
if service == 'cloudservers':
|
2013-03-31 07:05:14 +00:00
|
|
|
cloudservers(module, state, name, flavor, image, meta, key_name, files,
|
|
|
|
wait, wait_timeout)
|
2013-03-30 14:38:42 +00:00
|
|
|
elif service in ['cloudfiles', 'cloud_blockstorage',
|
|
|
|
'cloud_databases', 'cloud_loadbalancers']:
|
|
|
|
module.fail_json(msg = 'Service %s is not supported at this time' %
|
|
|
|
service)
|
|
|
|
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|