Commit 68d7c238 authored by Thiago Paiva's avatar Thiago Paiva
Browse files

Adding actuator initial script

parents
[DEFAULT]
use_stderr=True
debug=True
# Rpc all
transport_url = rabbit://nova:5bf959e557aee5f340379ba68e72fe85f5fb18f4f9e523f73c9ed9a470@172.29.237.59:5671//nova
executor_thread_pool_size = 64
rpc_response_timeout = 60
[oslo_messaging_notifications]
notification_topics = notifications
driver = messagingv2
transport_url = rabbit://nova:5bf959e557aee5f340379ba68e72fe85f5fb18f4f9e523f73c9ed9a470@172.29.237.59:5671//nova
[oslo_messaging_rabbit]
rabbit_use_ssl = True
rpc_conn_pool_size = 30
[keystone_authtoken]
insecure=True
auth_url = http://172.29.236.100:35357
auth_uri = http://172.29.236.100:5000
project_domain_name = Default
user_domain_name = Default
project_name = admin
username = admin
password = f6eff7a5a5472a25ffee2d409a0ee6ccac
memcached_servers = 172.29.238.111:11211
token_cache_time = 300
# if your memcached server is shared, use these settings to avoid cache poisoning
memcache_security_strategy = ENCRYPT
memcache_secret_key = 93d4605951d0e0e7765e08cdf95106ed
[thresholds]
vcpu=80
free_ram_mb=1024
free_disk_gb=20
import json
import logging
import os
from novaclient import client as nova_client
from oslo_config import cfg
import oslo_messaging
LOG = logging.getLogger(__name__)
AUTH_OPTS = [
cfg.StrOpt('auth_url'),
cfg.StrOpt('username'),
cfg.StrOpt('password'),
cfg.StrOpt('project_name'),
cfg.StrOpt('project_domain_name'),
cfg.StrOpt('user_domain_name'),
cfg.BoolOpt('insecure'),
]
THRESH_OPTS = [
cfg.IntOpt("vcpus"),
cfg.IntOpt("free_ram_mb"),
cfg.IntOpt("free_disk_gb"),
]
CONF = cfg.CONF
CONF.register_opts(AUTH_OPTS, group="keystone_authtoken")
CONF.register_opts(THRESH_OPTS, group="thresholds")
# Define a default handler at INFO logging level
logging.basicConfig(level=logging.DEBUG)
class NotificationEndpoint(object):
def __init__(self):
super(NotificationEndpoint, self).__init__()
self.nova_client = nova_client.Client(
'2',
auth_url=CONF.keystone_authtoken.auth_url ,
username=CONF.keystone_authtoken.username,
password=CONF.keystone_authtoken.password,
project_name=CONF.keystone_authtoken.project_name,
project_domain_name=CONF.keystone_authtoken.project_domain_name,
user_domain_name=CONF.keystone_authtoken.user_domain_name,
insecure=CONF.keystone_authtoken.insecure
)
def warn(self, ctxt, publisher_id, event_type, payload, metadata):
LOG.warn("\n\nWARNNING")
LOG.warn(publisher_id)
LOG.warn(event_type)
LOG.warn(payload)
def error(self, ctxt, publisher_id, event_type, payload, metadata):
LOG.error("\n\nERROR")
LOG.error(publisher_id)
LOG.error(event_type)
LOG.error(payload)
def cloud_over_thresholds(self):
stats = self.nova_client.hypervisors.statistics()
available_vcpu = stats.vcpus - stats.vcpus_used
if available_vcpu < CONF.thresholds.vcpus:
LOG.info("Cloud over the VCPU threshold")
return False
if stats.free_ram_mb < CONF.thresholds.free_ram_mb:
LOG.info("Cloud over the RAM threshold")
return False
if stats.free_disk_gb < CONF.thresholds.free_disk_gb:
LOG.info("Cloud over the Disk threshold")
return False
else:
True
class Waker(NotificationEndpoint):
filter_rule = oslo_messaging.NotificationFilter(
# publisher_id='^compute.*',
event_type='^compute\.instance\.create\.end$',
)
def info(self, ctxt, publisher_id, event_type, payload, metadata):
LOG.debug("\n\nINFO")
LOG.debug(publisher_id)
LOG.debug(event_type)
LOG.debug(payload)
if self.cloud_over_thresholds():
hypervisors = self.nova_client.hypervisors.list()
empty_servers = [hyp for hyp in hypervisors if hyp.running_vms == 0 and
hyp.state == "up" and
hyp.status == "enabled"]
for server in empty_servers:
stats = self.nova_client.hypervisors.statistics()
new_vcpus = stats.vcpus - server.vcpus
new_free_ram_mb = stats.free_ram_mb - server.free_ram_mb
new_free_disk_gb = stats.free_disk_gb - server.free_disk_gb
# TODO(thiagop): what is disk_available_least?
if new_vcpus > CONF.thresholds.vcpus:
continue
elif new_free_ram_mb > CONF.thresholds.free_ram_mb:
continue
elif new_free_disk_gb > CONF.thresholds.free_disk_gb:
continue
else:
self.wake_up_server()
def wake_up_server(self):
raise NotImplementedError("DO SOMETHING")
class Morpheus(NotificationEndpoint):
filter_rule = oslo_messaging.NotificationFilter(
# publisher_id='^compute.*',
event_type='^compute\.instance\.delete\.end$',
)
def info(self, ctxt, publisher_id, event_type, payload, metadata):
LOG.debug("\n\nINFO")
LOG.debug(publisher_id)
LOG.debug(event_type)
LOG.debug(payload)
if not self.cloud_over_thresholds():
hypervisors = self.nova_client.hypervisors.list()
empty_servers = [hyp for hyp in hypervisors if hyp.running_vms == 0 and
hyp.state == "up" and
hyp.status == "enabled"]
for server in empty_servers:
stats = self.nova_client.hypervisors.statistics()
new_vcpus = stats.vcpus - server.vcpus
new_free_ram_mb = stats.free_ram_mb - server.free_ram_mb
new_free_disk_gb = stats.free_disk_gb - server.free_disk_gb
# TODO(thiagop): what is disk_available_least?
if new_vcpus < CONF.thresholds.vcpus:
continue
elif new_free_ram_mb < CONF.thresholds.free_ram_mb:
continue
elif new_free_disk_gb < CONF.thresholds.free_disk_gb:
continue
else:
self.sleep_server(server)
def sleep_server(self, server):
LOG.info("Suspending %s [ip: %s]" %(server.hypervisor_hostname, server.host_ip))
os.system("systemctl --host=root@%s suspend" %server.host_ip)
class OpenStackClient:
def __init__(self, os_auth_url, os_username, os_password, os_tenant_name):
self.session = None
self.os_auth_url = os_auth_url
self.os_username = os_username
self.os_password = os_password
self.os_tenant_name = os_tenant_name
def get_session(self):
if self.session is None:
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options()
self.session = session.Session(auth=auth, verify=False)
return self.session
def nova(self):
sess = self.get_session()
return nova
if __name__ == "__main__":
CONF(default_config_files=['actuator.conf'])
transport = oslo_messaging.get_notification_transport(CONF)
targets = [
oslo_messaging.Target(topic='notifications', exchange="/nova"),
oslo_messaging.Target(topic='versioned_notifications', exchange="/nova"),
]
endpoints = [
Morpheus(),
Waker(),
]
pool = "autonomic-listener"
server = oslo_messaging.get_notification_listener(
transport,
targets,
endpoints,
executor='threading',
# pool=pool
)
server.start()
server.wait()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment