From df85ab484da57496e4f6ab2a541973ab9c820459 Mon Sep 17 00:00:00 2001 From: jmlim Date: Thu, 13 Sep 2018 15:32:05 +0900 Subject: [PATCH] Implement functions for storing VM and hypervisor infos --- Stella-OpenStack.py | 225 +++++++++++++++++++++++++++++++++++++++++++ connect_openstack.py | 71 ++++++++++++++ 2 files changed, 296 insertions(+) create mode 100644 Stella-OpenStack.py create mode 100644 connect_openstack.py diff --git a/Stella-OpenStack.py b/Stella-OpenStack.py new file mode 100644 index 0000000..9cc74aa --- /dev/null +++ b/Stella-OpenStack.py @@ -0,0 +1,225 @@ +import argparse +import logging +import os + +# for API server +from flask import Flask +from flask_restful import Resource, Api + +import connect_openstack + +app = Flask(__name__) +api = Api(app) +# + +""" + +Stella-OpenStack Conment main file + +- by jmlim@os.korea.ac.kr + +""" + + +class VM_info: + _list_vms = {} + + # _vm_name = " " + # _instance_name = " " + # _project_id = " " + # _hypervisor_name = " " + # _hypervisor_ip = " " + + def print_all(self): + print(self._list_vms) + + def print_num(self): + return len(self._list_vms) + 1 + + def set_info(self, _vm_name, _instance_name, _project_id, _hypervisor_name, _hypervisor_ip): + # Key for VMs list + num = len(self._list_vms) + # Add to VMs list + # self._list_vms[num] = [_vm_name, _instance_name, _project_id, _hypervisor_name, _hypervisor_ip] + self._list_vms[num] = {'vm_name': _vm_name, 'instance_name': _instance_name, 'project_id': _project_id, + 'hypervisor_name': _hypervisor_name, 'hypervisor_ip': _hypervisor_ip} + # _tmp = [ _vm_name, _instance_name, _project_id, _hypervisor_name, _hypervisor_ip] + # self._vm_info.append(_tmp) + return len(self._list_vms) + + +# def get_info(self, _vm_name ): +# _tmp = + +class hypervisor_info: + _list_hypervisor = {} + + def set_data(self, _name, _ip): + self._list_hypervisor[_name] = _ip + + def get_data(self, _name): + return self._list_hypervisor[_name] + + +class Stella_OpenStack(Resource): + + def __init__(self, log_file=None): + logging.basicConfig(level=logging.INFO, format='%(message)s') + self.logger = logging.getLogger("Stella-OpenStack START") + self.log_file = log_file + + if log_file: + self.log_handler = logging.FileHandler(self.log_file) + self.logger.addHandler(self.log_handler) + + self.__stop = False + + # self.signal(signal.SIGINT, self.stop) + # self.signal(signal.SIGTERM, self.stop) + + def main(self): + self.logger.info("STELLA: PID {0}".format(os.getpid())) + + def stop(self, signum, frame): + self.__stop = True + self.logger.info("STELLA: Signal {0}".format(signum)) + self.logger.info("STELLA: STOP") + + # Stella-OpenStack API functions + def API_setSLA(self): + + +# Stella-OpenStack API list +api.add_resource() +api.add_resource() + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--log", help="log filename", default=None) + parser.add_argument("--pid", help="pid filename", default='/tmp/stella.pid') + args = parser.parse_args() + + # fork to make deamin process + # pid = os.fork() + # if pid > 0: + # for parent process + + # exit(0) + + # else: + # For children process + # os.chdir('/') + # os.setsid() + # os.umask(0) + + # pid = os.fork() + # if pid > 0: + # exit(0) + + # else: + # sys.stdout.flush() + # sys.stderr.flush() + + # si = open(os.devnull, 'r') + # so = open(os.devnull, 'a+') + # se = open(os.devnull, 'a+') + + # os.dup2(si.fileno(), sys.stdin.fileno()) + # os.dup2(so.fileno(), sys.stdout.fileno()) + # os.dup2(se.fileno(), sys.stderr.fileno()) + + # with open(args.pid, "w") as pid_file: + # pid_file.write(str(os.getpid())) + + # Stella = Stella_OpenStack(args.log) + # code = Stella.main() + # exit(code) + + Stella = Stella_OpenStack(args.log) + code = Stella.main() + + Stella.logger.info("STELLA: connect to Stella-cloud") + conn = connect_openstack.Opts.create_connection_from_config() + + Stella.logger.info("STELLA: listing hypervisor") + + list_hypervisor_name = [] + list_hypervisor_ip = [] + # list_hypervisor = {} + + for HYPERVISOR in conn.compute.hypervisors(): + list_hypervisor_name.append(HYPERVISOR.name) + + for HYPERVISOR in conn.compute.hypervisors(list_hypervisor_name): + list_hypervisor_ip.append(HYPERVISOR.host_ip) + + # Make key-value storage for hypervisor + # e.g. hypervisor name: hypervisor ip + hypervisors = hypervisor_info + # print(hypervisors.print_num(hypervisors)) + count = 0 + for index in list_hypervisor_name: + # print(index) + list_hypervisor = {list_hypervisor_name[count]: list_hypervisor_ip[count]} + hypervisors.set_data(hypervisors, list_hypervisor_name[count], list_hypervisor_ip[count]) + count = count + 1 + # for debugging + # hypervisors.print_all(hypervisors) + # print(hypervisors.print_num(hypervisors)) + + # Storing VM information + + vms = VM_info + + print("VM information") + for VM in conn.compute.servers(): + # print("VM_NAME: " + VM.name) + # print("INSTANCE_NAME: " + VM.instance_name) + # print("PROJECT_ID: " + VM.project_id) + # print("HYPERVISOR_HOST: " + VM.hypervisor_hostname) + # print("HOST_ID: " + VM.host_id) + # print(vms.print_num(vms)) + ip = hypervisors.get_data(hypervisors, VM.hypervisor_hostname) + vms.set_info(vms, VM.name, VM.instance_name, VM.project_id, VM.hypervisor_hostname, ip) + + # vms.print_all(vms) + + # print(VM) + + # hypervisor_list_name = " " + # hypervisor_list_ip = " " + + # Get hypervisor list AND ip address + # for HYPERVISOR in conn.compute.hypervisors(): + # tmp_hypervisor['name'] = HYPERVISOR.name + # hypervisor_list_name = + # + # for HYPERVISOR in conn.compute.hypervisors(hypervisor_list_name): + # hypervisor_list_ip = HYPERVISOR.host_ip + + # for Debugging + + # print(hypervisor_list_name) + # print(hypervisor_list_ip) + + # print("VM information") + # for VM in conn.compute.servers(): + # print("VM_NAME: " + VM.name) + # print("INSTANCE_NAME: " + VM.instance_name) + # print("PROJECT_ID: " + VM.project_id) + # print("HYPERVISOR_HOST: " + VM.hypervisor_hostname) + # print("HOST_ID: " + VM.host_id) + # print(VM) + + # for HYPERVISOR in conn.compute.hypervisors("DevStack"): + # print(HYPERVISOR) + + # print("tenant information: ") + # for HYPERVISOR in conn.identity.projects(): + # print(HYPERVISOR) + + # VM_info = VM_info.set_info() + # print(VM_info) + + # run API server + app.run(debug=True) diff --git a/connect_openstack.py b/connect_openstack.py new file mode 100644 index 0000000..3bc40b4 --- /dev/null +++ b/connect_openstack.py @@ -0,0 +1,71 @@ +""" +This file defines wrapper to connect OpenStack cloud +assume that we use DevStack for configuration file + +by jmlim@os.korea.ac.kr +""" + +import argparse +import os +import sys + +import openstack +from openstack import utils +from openstack.config import loader + +utils.enable_logging(True, stream=sys.stdout) + +#: Defines the OpenStack Config loud key in your config file, +#: typically in $HOME/.config/openstack/clouds.yaml. That configuration +#: will determine where the examples will be run and what resource defaults +#: will be used to run the examples. +STELLA_CLOUD = os.getenv('OS_CLOUD', 'Stella-DevStack') +config = loader.OpenStackConfig() +CLOUD = openstack.connect(cloud=STELLA_CLOUD) + + +class Opts(object): + def __init__(self, cloud_name='Stella-DevStack', debug=False): + # This function is for init OpenStack connection + # I use DevStack and the configureation file is automaticly generated in /etc/openstack/clouds.yaml + self.cloud = cloud_name + self.debug = debug + # use identity v3 API + self.identity_api_version = '3' + + def _get_resource_value(resource_key, default): + return config.get_extra_config('example').get(resource_key, default) + + SERVER_NAME = 'openstacksdk-example' + IMAGE_NAME = _get_resource_value('image_name', 'cirros-0.3.5-x86_64-disk') + FLAVOR_NAME = _get_resource_value('flavor_name', 'm1.small') + NETWORK_NAME = _get_resource_value('network_name', 'private') + KEYPAIR_NAME = _get_resource_value('keypair_name', 'openstacksdk-example') + SSH_DIR = _get_resource_value( + 'ssh_dir', '{home}/.ssh'.format(home=os.path.expanduser("~"))) + PRIVATE_KEYPAIR_FILE = _get_resource_value( + 'private_keypair_file', '{ssh_dir}/id_rsa.{key}'.format( + ssh_dir=SSH_DIR, key=KEYPAIR_NAME)) + + EXAMPLE_IMAGE_NAME = 'openstacksdk-example-public-image' + + def create_connection_from_config(): + return openstack.connect(cloud=STELLA_CLOUD) + + def create_connection_from_args(): + parser = argparse.ArgumentParser() + config = loader.OpenStackConfig() + config.register_argparse_arguments(parser, sys.argv[1:]) + args = parser.parse_args() + return openstack.connect(config=config.get_one(argparse=args)) + + def create_connection(auth_url, region, project_name, username, password): + return openstack.connect( + auth_url=auth_url, + project_name=project_name, + username=username, + password=password, + region_name=region, + app_name='Stella-OpenStack', + app_version='0.1', + )