tripleo-ansible/playbooks/library/nova_powercontrol
Julia Kreger 640202df2e Addition of a nova power controlling module
While it appears Helion RC6 does not automatically resume virtual
machine states, this untested and proof of concept module will allow
us to stop/start and pause/unpause instances, as well as return them
to a previously known state if recorded.

Change-Id: If822de1c57bf6f6101ba8816d69c250c77488203
2014-11-18 21:52:33 -05:00

351 lines
11 KiB
Python

#!/usr/bin/env python
# This code is part of Ansible, but is an independent component. This
# particular file snippet, and this file snippet only, is BSD
# licensed. Modules you write using this snippet, which is embedded
# dynamically by Ansible still belong to the author of the module, and
# may assign their own license to the complete work.
#
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
try:
from novaclient.v1_1 import client as nova_client
from novaclient import exceptions
import time
except ImportError:
print("failed=True msg='novaclient is required for this module'")
DOCUMENTATION = '''
---
module: nova_powercontrol
short_description: Stops/Starts virtual machines via nova.
description:
- Controls the power state of virtual machines via nova.
options:
login_username:
description:
- login username to authenticate to keystone
required: true
default: admin
login_password:
description:
- Password of login user
required: true
default: 'yes'
login_tenant_name:
description:
- The tenant name of the login user
required: true
default: 'yes'
auth_url:
description:
- The keystone url for authentication
required: false
default: 'http://127.0.0.1:35357/v2.0/'
region_name:
description:
- Name of the region
required: false
default: None
state:
description:
- The desired state for the instance to be set to.
required: true
default: None
instance_id:
description:
- Instance ID of a single instance to control.
default: None
hypervisor:
description:
- Hypervisor ID of the instances to control.
default: None
zone:
description:
- zone name of the instances to control.
default: None
all_instances:
description:
- Any value to affirm decision to act upon all instances.
default: None
requirements: ["novaclient"]
'''
EXAMPLES = '''
# Changes the power state via nova
- nova_power_control:
login_username: admin
login_password: admin
login_tenant_name: admin
instance_id: 4f905f38-e52a-43d2-b6ec-754a13ffb529
state: stopped
'''
# The following two openstack_ are copy pasted from an upcoming
# core module "lib/ansible/module_utils/openstack.py" Once that's landed,
# these should be replaced with a line at the bottom of the file:
# from ansible.module_utils.openstack import *
def openstack_argument_spec():
# Consume standard OpenStack environment variables.
# This is mainly only useful for ad-hoc command line operation as
# in playbooks one would assume variables would be used appropriately
OS_AUTH_URL = os.environ.get('OS_AUTH_URL', 'http://127.0.0.1:35357/v2.0/')
OS_PASSWORD = os.environ.get('OS_PASSWORD', None)
OS_REGION_NAME = os.environ.get('OS_REGION_NAME', None)
OS_USERNAME = os.environ.get('OS_USERNAME', 'admin')
OS_TENANT_NAME = os.environ.get('OS_TENANT_NAME', OS_USERNAME)
spec = dict(
login_username=dict(default=OS_USERNAME),
auth_url=dict(default=OS_AUTH_URL),
region_name=dict(default=OS_REGION_NAME),
availability_zone=dict(default=None),
)
if OS_PASSWORD:
spec['login_password'] = dict(default=OS_PASSWORD)
else:
spec['login_password'] = dict(required=True)
if OS_TENANT_NAME:
spec['login_tenant_name'] = dict(default=OS_TENANT_NAME)
else:
spec['login_tenant_name'] = dict(required=True)
return spec
def _get_server(nova, instance_id):
try:
return nova.servers.get(instance_id)
except Exception, e:
module.fail_json(
msg="Error accessing instance %s: %s" % (instance_id, e.message)
)
def _write_metadata(nova, server):
nova.servers.set_meta(
server.id,
{'ansible_previous_state': server.status}
)
def _suspend_instance(nova, instance_id):
try:
server = None
server = _get_server(nova, instance_id)
if server.status != "ACTIVE":
if server.status != "PAUSED":
return False
_write_metadata(nova, server)
nova.servers.suspend(server.id)
return (True, nova.servers.suspend(server.id))
except Exception, e:
return (False, e.message)
def _resume_instance(nova, instance_id):
try:
server = None
server = _get_server(nova, instance_id)
if server.status != "SUSPENDED":
return False
_write_metadata(nova, server)
return (True, nova.servers.resume(server.id))
except Exception, e:
return (False, e.message)
def _stop_instance(nova, instance_id):
try:
server = None
server = _get_server(nova, instance_id)
if server.status != "ACTIVE":
if server.status != "PAUSED":
return False
_write_metadata(nova, server)
return (True, nova.servers.stop(server.id))
except Exception, e:
return (False, e.message)
def _start_instance(nova, instance_id):
try:
server = None
server = _get_server(nova, instance_id)
if server.status != "STOPPED":
if server.status != "SUSPENDED":
if server.status != "PAUSED":
return False
_write_metadata(nova, server)
return (True, nova.servers.start(server.id))
except Exception, e:
return (False, e.message)
def _previous_state(nova, instance_id):
"""
Attempts to return instances to their previous state if they were
stopped or suspended.
"""
try:
server = _get_server(nova, instance_id)
if server.metadata["ansible_previous_state"] == "ACTIVE":
if server.status != "ACTIVE":
if server.status == "STOPPED":
return _start_instance(nova, instance_id)
if server.status == "SUSPENDED":
return _resume_instance(nova, instance_id)
return (False, None)
except Exception, e:
return (False, e.message)
def _determine_task(nova, instance_id, action):
choice = {
'running': _start_instance,
'start': _start_instance,
'on': _start_instance,
'stopped': _stop_instance,
'stop': _stop_instance,
'off': _stop_instance,
'resume': _resume_instance,
'previous': _previous_state
}
return choice[action](nova, instance_id)
def _many_servers(module, nova, servers):
"""
Enumerates through the list of services and calls
_determine_task.
"""
results = []
for server in servers:
result = _determine_task(nova, server.id, module.params['state'])
results.append(results)
module.exit_json(changed=True, output=results)
def _all_instances(module, nova):
"""
Retrieves the entire list of servers and changes and applies the
desired state.
"""
results = []
for server in nova.servers.list():
_determine_task(nova, server.id, module.params['state'])
results.append(results)
module.exit_json(changed=True, output=results)
def _nova_powercontrol(module, nova):
state = module.params['state']
if module.params['instance_id'] is not None:
(status, message) = _determine_task(
nova,
module.params['instance_id'],
state
)
if status:
module.exit_json(changed=True, output=message)
else:
module.fail_json(
msg="Instance %s failed to update with error %s" % (
module.params['instance_id'],
message
)
)
elif module.params['hypervisor'] is not None:
try:
hypervisor = nova.hypervisors.search(
True,
module.params['hypervisor'],
servers=True
)
_many_servers(module, nova, hypervisor.servers)
except:
module.fail_json(
msg="Unable to find zone %s" % module.params['hypervisor']
)
elif module.params['zone'] is not None:
try:
servers = nova.servers.list(
True,
{'OS-EXT-AZ:availability_zone': module.params['zone']}
)
_many_servers(module, nova, servers)
except:
module.fail_json(
msg="Unable to find zone %s" % module.params['zone']
)
elif module.params['all_instances'] is not None:
_all_instances(module, nova)
else:
module.fail_json(
msg="Unable to proceed: Requires instance_id, hypervisor, "
"zone, or special flag all_instances."
)
def main():
argument_spec = openstack_argument_spec()
argument_spec.update(dict(
state=dict(required=True),
instance_id=dict(),
hypervisor=dict(),
zone=dict(),
all_instances=dict()
))
module = AnsibleModule(argument_spec=argument_spec)
nova = nova_client.Client(module.params['login_username'],
module.params['login_password'],
module.params['login_tenant_name'],
module.params['auth_url'],
region_name=module.params['region_name'],
service_type='compute')
try:
nova.authenticate()
except exceptions.Unauthorized, e:
module.fail_json(
msg="Invalid OpenStack Nova credentials.: %s" % e.message
)
except exceptions.AuthorizationFailure, e:
module.fail_json(
msg="Unable to authorize user: %s" % e.message
)
_nova_powercontrol(module, nova)
# this is magic, see lib/ansible/module_common.py
from ansible.module_utils.basic import *
main()