2015-07-06 22:25:15 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# 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: profitbricks
|
|
|
|
short_description: Create, destroy, start, stop, and reboot a ProfitBricks virtual machine.
|
|
|
|
description:
|
|
|
|
- Create, destroy, update, start, stop, and reboot a ProfitBricks virtual machine. When the virtual machine is created it can optionally wait for it to be 'running' before returning. This module has a dependency on profitbricks >= 1.0.0
|
2015-07-07 23:00:28 +02:00
|
|
|
version_added: "2.0"
|
2015-07-06 22:25:15 +02:00
|
|
|
options:
|
|
|
|
auto_increment:
|
|
|
|
description:
|
|
|
|
- Whether or not to increment a single number in the name for created virtual machines.
|
|
|
|
default: yes
|
|
|
|
choices: ["yes", "no"]
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the virtual machine.
|
|
|
|
required: true
|
2016-07-29 06:37:42 +02:00
|
|
|
image:
|
2015-07-06 22:25:15 +02:00
|
|
|
description:
|
|
|
|
- The system image ID for creating the virtual machine, e.g. a3eae284-a2fe-11e4-b187-5f1f641608c8.
|
|
|
|
required: true
|
2016-07-29 06:37:42 +02:00
|
|
|
image_password:
|
|
|
|
description:
|
|
|
|
- Password set for the administrative user.
|
|
|
|
required: false
|
2016-08-23 10:00:41 +02:00
|
|
|
version_added: '2.2'
|
2016-07-29 06:37:42 +02:00
|
|
|
ssh_keys:
|
|
|
|
description:
|
|
|
|
- Public SSH keys allowing access to the virtual machine.
|
|
|
|
required: false
|
2016-08-23 10:00:41 +02:00
|
|
|
version_added: '2.2'
|
2015-07-06 22:25:15 +02:00
|
|
|
datacenter:
|
|
|
|
description:
|
2016-07-29 06:37:42 +02:00
|
|
|
- The datacenter to provision this virtual machine.
|
2015-07-06 22:25:15 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
cores:
|
|
|
|
description:
|
|
|
|
- The number of CPU cores to allocate to the virtual machine.
|
|
|
|
required: false
|
|
|
|
default: 2
|
|
|
|
ram:
|
|
|
|
description:
|
|
|
|
- The amount of memory to allocate to the virtual machine.
|
|
|
|
required: false
|
|
|
|
default: 2048
|
2016-07-29 06:37:42 +02:00
|
|
|
cpu_family:
|
|
|
|
description:
|
|
|
|
- The CPU family type to allocate to the virtual machine.
|
|
|
|
required: false
|
|
|
|
default: AMD_OPTERON
|
|
|
|
choices: [ "AMD_OPTERON", "INTEL_XEON" ]
|
2016-08-23 10:00:41 +02:00
|
|
|
version_added: '2.2'
|
2015-07-06 22:25:15 +02:00
|
|
|
volume_size:
|
|
|
|
description:
|
|
|
|
- The size in GB of the boot volume.
|
|
|
|
required: false
|
|
|
|
default: 10
|
|
|
|
bus:
|
|
|
|
description:
|
|
|
|
- The bus type for the volume.
|
|
|
|
required: false
|
|
|
|
default: VIRTIO
|
|
|
|
choices: [ "IDE", "VIRTIO"]
|
|
|
|
instance_ids:
|
|
|
|
description:
|
|
|
|
- list of instance ids, currently only used when state='absent' to remove instances.
|
|
|
|
required: false
|
|
|
|
count:
|
|
|
|
description:
|
|
|
|
- The number of virtual machines to create.
|
|
|
|
required: false
|
|
|
|
default: 1
|
|
|
|
location:
|
|
|
|
description:
|
2016-07-29 06:37:42 +02:00
|
|
|
- The datacenter location. Use only if you want to create the Datacenter or else this value is ignored.
|
2015-07-06 22:25:15 +02:00
|
|
|
required: false
|
|
|
|
default: us/las
|
2016-08-23 09:52:39 +02:00
|
|
|
choices: [ "us/las", "de/fra", "de/fkb" ]
|
2015-07-06 22:25:15 +02:00
|
|
|
assign_public_ip:
|
|
|
|
description:
|
|
|
|
- This will assign the machine to the public LAN. If no LAN exists with public Internet access it is created.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
lan:
|
|
|
|
description:
|
|
|
|
- The ID of the LAN you wish to add the servers to.
|
|
|
|
required: false
|
|
|
|
default: 1
|
|
|
|
subscription_user:
|
|
|
|
description:
|
|
|
|
- The ProfitBricks username. Overrides the PB_SUBSCRIPTION_ID environement variable.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
subscription_password:
|
|
|
|
description:
|
2016-08-29 21:45:57 +02:00
|
|
|
- THe ProfitBricks password. Overrides the PB_PASSWORD environement variable.
|
2015-07-06 22:25:15 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
wait:
|
|
|
|
description:
|
|
|
|
- wait for the instance to be in state 'running' before returning
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
wait_timeout:
|
|
|
|
description:
|
|
|
|
- how long before wait gives up, in seconds
|
|
|
|
default: 600
|
|
|
|
remove_boot_volume:
|
|
|
|
description:
|
|
|
|
- remove the bootVolume of the virtual machine you're destroying.
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: ["yes", "no"]
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- create or terminate instances
|
|
|
|
required: false
|
|
|
|
default: 'present'
|
2015-07-07 23:00:28 +02:00
|
|
|
choices: [ "running", "stopped", "absent", "present" ]
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2015-07-20 02:11:23 +02:00
|
|
|
requirements:
|
|
|
|
- "profitbricks"
|
|
|
|
- "python >= 2.6"
|
2015-07-06 22:25:15 +02:00
|
|
|
author: Matt Baldwin (baldwin@stackpointcloud.com)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
|
|
|
|
# Note: These examples do not set authentication details, see the AWS Guide for details.
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
# Provisioning example. This will create three servers and enumerate their names.
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
- profitbricks:
|
|
|
|
datacenter: Tardis One
|
|
|
|
name: web%02d.stackpointcloud.com
|
|
|
|
cores: 4
|
|
|
|
ram: 2048
|
|
|
|
volume_size: 50
|
2016-07-29 06:37:42 +02:00
|
|
|
cpu_family: INTEL_XEON
|
2015-07-06 22:25:15 +02:00
|
|
|
image: a3eae284-a2fe-11e4-b187-5f1f641608c8
|
|
|
|
location: us/las
|
|
|
|
count: 3
|
|
|
|
assign_public_ip: true
|
|
|
|
|
|
|
|
# Removing Virtual machines
|
|
|
|
|
|
|
|
- profitbricks:
|
|
|
|
datacenter: Tardis One
|
|
|
|
instance_ids:
|
|
|
|
- 'web001.stackpointcloud.com'
|
|
|
|
- 'web002.stackpointcloud.com'
|
|
|
|
- 'web003.stackpointcloud.com'
|
|
|
|
wait_timeout: 500
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
# Starting Virtual Machines.
|
|
|
|
|
|
|
|
- profitbricks:
|
|
|
|
datacenter: Tardis One
|
|
|
|
instance_ids:
|
|
|
|
- 'web001.stackpointcloud.com'
|
|
|
|
- 'web002.stackpointcloud.com'
|
|
|
|
- 'web003.stackpointcloud.com'
|
|
|
|
wait_timeout: 500
|
|
|
|
state: running
|
|
|
|
|
|
|
|
# Stopping Virtual Machines
|
|
|
|
|
|
|
|
- profitbricks:
|
|
|
|
datacenter: Tardis One
|
|
|
|
instance_ids:
|
|
|
|
- 'web001.stackpointcloud.com'
|
|
|
|
- 'web002.stackpointcloud.com'
|
|
|
|
- 'web003.stackpointcloud.com'
|
|
|
|
wait_timeout: 500
|
|
|
|
state: stopped
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2016-08-29 21:45:57 +02:00
|
|
|
import re
|
2015-07-06 22:25:15 +02:00
|
|
|
import uuid
|
|
|
|
import time
|
|
|
|
|
2015-07-07 23:00:28 +02:00
|
|
|
HAS_PB_SDK = True
|
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
try:
|
2016-08-29 21:45:57 +02:00
|
|
|
from profitbricks.client import ProfitBricksService, Volume, Server, Datacenter, NIC, LAN
|
2015-07-06 22:25:15 +02:00
|
|
|
except ImportError:
|
2015-07-07 23:00:28 +02:00
|
|
|
HAS_PB_SDK = False
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
LOCATIONS = ['us/las',
|
|
|
|
'de/fra',
|
2016-08-23 09:52:39 +02:00
|
|
|
'de/fkb']
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-08-29 21:45:57 +02:00
|
|
|
uuid_match = re.compile(
|
|
|
|
'[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}', re.I)
|
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
def _wait_for_completion(profitbricks, promise, wait_timeout, msg):
|
|
|
|
if not promise: return
|
|
|
|
wait_timeout = time.time() + wait_timeout
|
|
|
|
while wait_timeout > time.time():
|
|
|
|
time.sleep(5)
|
|
|
|
operation_result = profitbricks.get_request(
|
|
|
|
request_id=promise['requestId'],
|
|
|
|
status=True)
|
|
|
|
|
2016-08-29 21:45:57 +02:00
|
|
|
if operation_result['metadata']['status'] == "DONE":
|
2015-07-06 22:25:15 +02:00
|
|
|
return
|
2016-08-29 21:45:57 +02:00
|
|
|
elif operation_result['metadata']['status'] == "FAILED":
|
2015-07-06 22:25:15 +02:00
|
|
|
raise Exception(
|
|
|
|
'Request failed to complete ' + msg + ' "' + str(
|
|
|
|
promise['requestId']) + '" to complete.')
|
|
|
|
|
|
|
|
raise Exception(
|
|
|
|
'Timed out waiting for async operation ' + msg + ' "' + str(
|
|
|
|
promise['requestId']
|
|
|
|
) + '" to complete.')
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
def _create_machine(module, profitbricks, datacenter, name):
|
|
|
|
cores = module.params.get('cores')
|
|
|
|
ram = module.params.get('ram')
|
2016-07-29 06:37:42 +02:00
|
|
|
cpu_family = module.params.get('cpu_family')
|
2015-07-06 22:25:15 +02:00
|
|
|
volume_size = module.params.get('volume_size')
|
2016-07-29 06:37:42 +02:00
|
|
|
disk_type = module.params.get('disk_type')
|
|
|
|
image_password = module.params.get('image_password')
|
|
|
|
ssh_keys = module.params.get('ssh_keys')
|
2015-07-06 22:25:15 +02:00
|
|
|
bus = module.params.get('bus')
|
|
|
|
lan = module.params.get('lan')
|
|
|
|
assign_public_ip = module.params.get('assign_public_ip')
|
|
|
|
subscription_user = module.params.get('subscription_user')
|
|
|
|
subscription_password = module.params.get('subscription_password')
|
|
|
|
location = module.params.get('location')
|
|
|
|
image = module.params.get('image')
|
|
|
|
assign_public_ip = module.boolean(module.params.get('assign_public_ip'))
|
|
|
|
wait = module.params.get('wait')
|
2015-07-20 02:11:23 +02:00
|
|
|
wait_timeout = module.params.get('wait_timeout')
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
if assign_public_ip:
|
|
|
|
public_found = False
|
|
|
|
|
|
|
|
lans = profitbricks.list_lans(datacenter)
|
|
|
|
for lan in lans['items']:
|
|
|
|
if lan['properties']['public']:
|
|
|
|
public_found = True
|
|
|
|
lan = lan['id']
|
|
|
|
|
|
|
|
if not public_found:
|
|
|
|
i = LAN(
|
|
|
|
name='public',
|
|
|
|
public=True)
|
|
|
|
|
|
|
|
lan_response = profitbricks.create_lan(datacenter, i)
|
|
|
|
_wait_for_completion(profitbricks, lan_response,
|
|
|
|
wait_timeout, "_create_machine")
|
2016-07-29 06:37:42 +02:00
|
|
|
lan = lan_response['id']
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
v = Volume(
|
|
|
|
name=str(uuid.uuid4()).replace('-', '')[:10],
|
|
|
|
size=volume_size,
|
|
|
|
image=image,
|
|
|
|
image_password=image_password,
|
|
|
|
ssh_keys=ssh_keys,
|
|
|
|
disk_type=disk_type,
|
|
|
|
bus=bus)
|
|
|
|
|
|
|
|
n = NIC(
|
|
|
|
lan=int(lan)
|
|
|
|
)
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
s = Server(
|
|
|
|
name=name,
|
|
|
|
ram=ram,
|
|
|
|
cores=cores,
|
|
|
|
cpu_family=cpu_family,
|
|
|
|
create_volumes=[v],
|
|
|
|
nics=[n],
|
|
|
|
)
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
try:
|
|
|
|
create_server_response = profitbricks.create_server(
|
2015-07-06 22:25:15 +02:00
|
|
|
datacenter_id=datacenter, server=s)
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
_wait_for_completion(profitbricks, create_server_response,
|
|
|
|
wait_timeout, "create_virtual_machine")
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
server_response = profitbricks.get_server(
|
|
|
|
datacenter_id=datacenter,
|
|
|
|
server_id=create_server_response['id'],
|
|
|
|
depth=3
|
|
|
|
)
|
2015-07-06 22:25:15 +02:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to create the new server: %s" % str(e))
|
2016-07-29 06:37:42 +02:00
|
|
|
else:
|
|
|
|
return server_response
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
def _startstop_machine(module, profitbricks, datacenter_id, server_id):
|
2015-07-06 22:25:15 +02:00
|
|
|
state = module.params.get('state')
|
|
|
|
|
|
|
|
try:
|
|
|
|
if state == 'running':
|
2016-07-29 06:37:42 +02:00
|
|
|
profitbricks.start_server(datacenter_id, server_id)
|
2015-07-06 22:25:15 +02:00
|
|
|
else:
|
2016-07-29 06:37:42 +02:00
|
|
|
profitbricks.stop_server(datacenter_id, server_id)
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to start or stop the virtual machine %s: %s" % (name, str(e)))
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
def _create_datacenter(module, profitbricks):
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
location = module.params.get('location')
|
2015-07-20 02:11:23 +02:00
|
|
|
wait_timeout = module.params.get('wait_timeout')
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
i = Datacenter(
|
|
|
|
name=datacenter,
|
|
|
|
location=location
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
datacenter_response = profitbricks.create_datacenter(datacenter=i)
|
|
|
|
|
|
|
|
_wait_for_completion(profitbricks, datacenter_response,
|
|
|
|
wait_timeout, "_create_datacenter")
|
|
|
|
|
|
|
|
return datacenter_response
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to create the new server(s): %s" % str(e))
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
def create_virtual_machine(module, profitbricks):
|
|
|
|
"""
|
|
|
|
Create new virtual machine
|
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if a new virtual machine was created, false otherwise
|
|
|
|
"""
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
name = module.params.get('name')
|
|
|
|
auto_increment = module.params.get('auto_increment')
|
|
|
|
count = module.params.get('count')
|
|
|
|
lan = module.params.get('lan')
|
2015-07-20 02:11:23 +02:00
|
|
|
wait_timeout = module.params.get('wait_timeout')
|
2015-07-06 22:25:15 +02:00
|
|
|
failed = True
|
|
|
|
datacenter_found = False
|
|
|
|
|
|
|
|
virtual_machines = []
|
|
|
|
virtual_machine_ids = []
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
# Locate UUID for datacenter if referenced by name.
|
|
|
|
datacenter_list = profitbricks.list_datacenters()
|
2016-08-29 21:45:57 +02:00
|
|
|
datacenter_id = _get_datacenter_id(datacenter_list, datacenter)
|
2016-07-29 06:37:42 +02:00
|
|
|
if datacenter_id:
|
|
|
|
datacenter_found = True
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
if not datacenter_found:
|
|
|
|
datacenter_response = _create_datacenter(module, profitbricks)
|
2016-07-29 06:37:42 +02:00
|
|
|
datacenter_id = datacenter_response['id']
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
_wait_for_completion(profitbricks, datacenter_response,
|
|
|
|
wait_timeout, "create_virtual_machine")
|
|
|
|
|
|
|
|
if auto_increment:
|
|
|
|
numbers = set()
|
|
|
|
count_offset = 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
name % 0
|
|
|
|
except TypeError, e:
|
|
|
|
if e.message.startswith('not all'):
|
|
|
|
name = '%s%%d' % name
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=e.message)
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
number_range = xrange(count_offset, count_offset + count + len(numbers))
|
2015-07-06 22:25:15 +02:00
|
|
|
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:
|
2016-07-29 06:37:42 +02:00
|
|
|
names = [name]
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
# Prefetch a list of servers for later comparison.
|
|
|
|
server_list = profitbricks.list_servers(datacenter_id)
|
|
|
|
for name in names:
|
|
|
|
# Skip server creation if the server already exists.
|
2016-08-29 21:45:57 +02:00
|
|
|
if _get_server_id(server_list, name):
|
2016-07-29 06:37:42 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
create_response = _create_machine(module, profitbricks, str(datacenter_id), name)
|
2016-08-29 21:45:57 +02:00
|
|
|
nics = profitbricks.list_nics(datacenter_id, create_response['id'])
|
|
|
|
for n in nics['items']:
|
|
|
|
if lan == n['properties']['lan']:
|
|
|
|
create_response.update({'public_ip': n['properties']['ips'][0]})
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
virtual_machines.append(create_response)
|
2016-07-29 06:37:42 +02:00
|
|
|
|
|
|
|
failed = False
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
results = {
|
|
|
|
'failed': failed,
|
|
|
|
'machines': virtual_machines,
|
|
|
|
'action': 'create',
|
|
|
|
'instance_ids': {
|
|
|
|
'instances': [i['id'] for i in virtual_machines],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
def remove_virtual_machine(module, profitbricks):
|
|
|
|
"""
|
2016-07-29 06:37:42 +02:00
|
|
|
Removes a virtual machine.
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
This will remove the virtual machine along with the bootVolume.
|
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object.
|
|
|
|
|
|
|
|
Not yet supported: handle deletion of attached data disks.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if a new virtual server was deleted, false otherwise
|
|
|
|
"""
|
2016-07-29 06:37:42 +02:00
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
instance_ids = module.params.get('instance_ids')
|
|
|
|
remove_boot_volume = module.params.get('remove_boot_volume')
|
|
|
|
changed = False
|
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
if not isinstance(module.params.get('instance_ids'), list) or len(module.params.get('instance_ids')) < 1:
|
|
|
|
module.fail_json(msg='instance_ids should be a list of virtual machine ids or names, aborting')
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
# Locate UUID for datacenter if referenced by name.
|
|
|
|
datacenter_list = profitbricks.list_datacenters()
|
2016-08-29 21:45:57 +02:00
|
|
|
datacenter_id = _get_datacenter_id(datacenter_list, datacenter)
|
2016-07-29 06:37:42 +02:00
|
|
|
if not datacenter_id:
|
|
|
|
module.fail_json(msg='Virtual data center \'%s\' not found.' % str(datacenter))
|
|
|
|
|
|
|
|
# Prefetch server list for later comparison.
|
|
|
|
server_list = profitbricks.list_servers(datacenter_id)
|
|
|
|
for instance in instance_ids:
|
|
|
|
# Locate UUID for server if referenced by name.
|
2016-08-29 21:45:57 +02:00
|
|
|
server_id = _get_server_id(server_list, instance)
|
2016-07-29 06:37:42 +02:00
|
|
|
if server_id:
|
|
|
|
# Remove the server's boot volume
|
|
|
|
if remove_boot_volume:
|
|
|
|
_remove_boot_volume(module, profitbricks, datacenter_id, server_id)
|
|
|
|
|
|
|
|
# Remove the server
|
|
|
|
try:
|
|
|
|
server_response = profitbricks.delete_server(datacenter_id, server_id)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to terminate the virtual server: %s" % str(e))
|
|
|
|
else:
|
|
|
|
changed = True
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
return changed
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
def _remove_boot_volume(module, profitbricks, datacenter_id, server_id):
|
|
|
|
"""
|
|
|
|
Remove the boot volume from the server
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
server = profitbricks.get_server(datacenter_id, server_id)
|
|
|
|
volume_id = server['properties']['bootVolume']['id']
|
|
|
|
volume_response = profitbricks.delete_volume(datacenter_id, volume_id)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to remove the server's boot volume: %s" % str(e))
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def startstop_machine(module, profitbricks, state):
|
|
|
|
"""
|
2016-07-29 06:37:42 +02:00
|
|
|
Starts or Stops a virtual machine.
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True when the servers process the action successfully, false otherwise.
|
|
|
|
"""
|
|
|
|
if not isinstance(module.params.get('instance_ids'), list) or len(module.params.get('instance_ids')) < 1:
|
|
|
|
module.fail_json(msg='instance_ids should be a list of virtual machine ids or names, aborting')
|
|
|
|
|
|
|
|
wait = module.params.get('wait')
|
2015-07-20 02:11:23 +02:00
|
|
|
wait_timeout = module.params.get('wait_timeout')
|
2015-07-06 22:25:15 +02:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
instance_ids = module.params.get('instance_ids')
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
# Locate UUID for datacenter if referenced by name.
|
|
|
|
datacenter_list = profitbricks.list_datacenters()
|
2016-08-29 21:45:57 +02:00
|
|
|
datacenter_id = _get_datacenter_id(datacenter_list, datacenter)
|
2016-07-29 06:37:42 +02:00
|
|
|
if not datacenter_id:
|
|
|
|
module.fail_json(msg='Virtual data center \'%s\' not found.' % str(datacenter))
|
|
|
|
|
|
|
|
# Prefetch server list for later comparison.
|
|
|
|
server_list = profitbricks.list_servers(datacenter_id)
|
|
|
|
for instance in instance_ids:
|
|
|
|
# Locate UUID of server if referenced by name.
|
2016-08-29 21:45:57 +02:00
|
|
|
server_id = _get_server_id(server_list, instance)
|
2016-07-29 06:37:42 +02:00
|
|
|
if server_id:
|
|
|
|
_startstop_machine(module, profitbricks, datacenter_id, server_id)
|
2015-07-06 22:25:15 +02:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
wait_timeout = time.time() + wait_timeout
|
|
|
|
while wait_timeout > time.time():
|
|
|
|
matched_instances = []
|
2016-07-29 06:37:42 +02:00
|
|
|
for res in profitbricks.list_servers(datacenter_id)['items']:
|
2015-07-06 22:25:15 +02:00
|
|
|
if state == 'running':
|
|
|
|
if res['properties']['vmState'].lower() == state:
|
|
|
|
matched_instances.append(res)
|
|
|
|
elif state == 'stopped':
|
|
|
|
if res['properties']['vmState'].lower() == 'shutoff':
|
2016-07-29 06:37:42 +02:00
|
|
|
matched_instances.append(res)
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
if len(matched_instances) < len(instance_ids):
|
|
|
|
time.sleep(5)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
if wait_timeout <= time.time():
|
|
|
|
# waiting took too long
|
2016-07-29 06:37:42 +02:00
|
|
|
module.fail_json(msg="wait for virtual machine state timeout on %s" % time.asctime())
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
return (changed)
|
|
|
|
|
2016-07-29 06:37:42 +02:00
|
|
|
|
2016-08-29 21:45:57 +02:00
|
|
|
def _get_datacenter_id(datacenters, identity):
|
|
|
|
"""
|
|
|
|
Fetch and return datacenter UUID by datacenter name if found.
|
|
|
|
"""
|
|
|
|
for datacenter in datacenters['items']:
|
|
|
|
if identity in (datacenter['properties']['name'], datacenter['id']):
|
|
|
|
return datacenter['id']
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _get_server_id(servers, identity):
|
2016-07-29 06:37:42 +02:00
|
|
|
"""
|
2016-08-29 21:45:57 +02:00
|
|
|
Fetch and return server UUID by server name if found.
|
2016-07-29 06:37:42 +02:00
|
|
|
"""
|
2016-08-29 21:45:57 +02:00
|
|
|
for server in servers['items']:
|
|
|
|
if identity in (server['properties']['name'], server['id']):
|
|
|
|
return server['id']
|
2016-07-29 06:37:42 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
datacenter=dict(),
|
|
|
|
name=dict(),
|
|
|
|
image=dict(),
|
2016-08-31 08:24:55 +02:00
|
|
|
cores=dict(type='int', default=2),
|
|
|
|
ram=dict(type='int', default=2048),
|
|
|
|
cpu_family=dict(choices=['AMD_OPTERON', 'INTEL_XEON'],
|
|
|
|
default='AMD_OPTERON'),
|
|
|
|
volume_size=dict(type='int', default=10),
|
|
|
|
disk_type=dict(choices=['HDD', 'SSD'], default='HDD'),
|
2016-07-29 06:37:42 +02:00
|
|
|
image_password=dict(default=None),
|
|
|
|
ssh_keys=dict(type='list', default=[]),
|
2016-08-31 09:46:12 +02:00
|
|
|
bus=dict(choices=['VIRTIO', 'IDE'], default='VIRTIO'),
|
2016-08-31 08:24:55 +02:00
|
|
|
lan=dict(type='int', default=1),
|
2016-07-29 06:37:42 +02:00
|
|
|
count=dict(type='int', default=1),
|
2015-07-06 22:25:15 +02:00
|
|
|
auto_increment=dict(type='bool', default=True),
|
2016-07-29 06:37:42 +02:00
|
|
|
instance_ids=dict(type='list', default=[]),
|
2015-07-06 22:25:15 +02:00
|
|
|
subscription_user=dict(),
|
|
|
|
subscription_password=dict(),
|
|
|
|
location=dict(choices=LOCATIONS, default='us/las'),
|
|
|
|
assign_public_ip=dict(type='bool', default=False),
|
|
|
|
wait=dict(type='bool', default=True),
|
2015-07-20 02:11:23 +02:00
|
|
|
wait_timeout=dict(type='int', default=600),
|
2015-07-06 22:25:15 +02:00
|
|
|
remove_boot_volume=dict(type='bool', default=True),
|
|
|
|
state=dict(default='present'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-07-07 23:00:28 +02:00
|
|
|
if not HAS_PB_SDK:
|
|
|
|
module.fail_json(msg='profitbricks required for this module')
|
|
|
|
|
2015-07-06 22:25:15 +02:00
|
|
|
subscription_user = module.params.get('subscription_user')
|
|
|
|
subscription_password = module.params.get('subscription_password')
|
|
|
|
wait = module.params.get('wait')
|
2015-07-20 02:11:23 +02:00
|
|
|
wait_timeout = module.params.get('wait_timeout')
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
profitbricks = ProfitBricksService(
|
|
|
|
username=subscription_user,
|
|
|
|
password=subscription_password)
|
|
|
|
|
|
|
|
state = module.params.get('state')
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if not module.params.get('datacenter'):
|
2016-07-29 06:37:42 +02:00
|
|
|
module.fail_json(msg='datacenter parameter is required ' +
|
2015-07-07 23:00:28 +02:00
|
|
|
'for running or stopping machines.')
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2015-07-07 23:00:28 +02:00
|
|
|
try:
|
|
|
|
(changed) = remove_virtual_machine(module, profitbricks)
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set instance state: %s' % str(e))
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
elif state in ('running', 'stopped'):
|
|
|
|
if not module.params.get('datacenter'):
|
2016-07-29 06:37:42 +02:00
|
|
|
module.fail_json(msg='datacenter parameter is required for ' +
|
2015-07-07 23:00:28 +02:00
|
|
|
'running or stopping machines.')
|
|
|
|
try:
|
|
|
|
(changed) = startstop_machine(module, profitbricks, state)
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set instance state: %s' % str(e))
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
elif state == 'present':
|
|
|
|
if not module.params.get('name'):
|
|
|
|
module.fail_json(msg='name parameter is required for new instance')
|
|
|
|
if not module.params.get('image'):
|
|
|
|
module.fail_json(msg='image parameter is required for new instance')
|
|
|
|
if not module.params.get('subscription_user'):
|
2016-07-29 06:37:42 +02:00
|
|
|
module.fail_json(msg='subscription_user parameter is ' +
|
2015-07-07 23:00:28 +02:00
|
|
|
'required for new instance')
|
2015-07-06 22:25:15 +02:00
|
|
|
if not module.params.get('subscription_password'):
|
2016-07-29 06:37:42 +02:00
|
|
|
module.fail_json(msg='subscription_password parameter is ' +
|
2015-07-07 23:00:28 +02:00
|
|
|
'required for new instance')
|
2015-07-06 22:25:15 +02:00
|
|
|
|
2015-07-07 23:00:28 +02:00
|
|
|
try:
|
|
|
|
(machine_dict_array) = create_virtual_machine(module, profitbricks)
|
|
|
|
module.exit_json(**machine_dict_array)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set instance state: %s' % str(e))
|
2015-07-06 22:25:15 +02:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
main()
|