Separate out the class MoltenIron

Putting MoltenIron as a public class in the library allows
clients to programatically call the server and perform
operations.

Change-Id: I9fb03ee5de56c9196d6aaf983dfe22135f29c568
This commit is contained in:
Mark Hamzy 2016-11-30 12:12:50 -06:00
parent 06277f2de9
commit 6ac505d94c
2 changed files with 373 additions and 397 deletions

View File

@ -27,406 +27,14 @@ from __future__ import print_function
import argparse
import json
from molteniron import molteniron
import os
import sys
import yaml
if sys.version_info >= (3, 0):
import http.client # noqa
else:
import httplib # noqa
DEBUG = False
# Create a decorator pattern that maintains a registry
def makeRegistrar():
"""Decorator that keeps track of tagged functions."""
registry = {}
def registrar(func):
"""Store the function pointer."""
registry[func.__name__] = func
# normally a decorator returns a wrapped function,
# but here we return func unmodified, after registering it
return func
registrar.all = registry
return registrar
# Create the decorator
command = makeRegistrar()
class MoltenIron(object):
"""This is the MoltenIron client object."""
def __init__(self):
self.conf = None
self.argv = None
self.parser = None
self.request = None
self.response_str = None
self.response_json = None
def setup_conf(self, _conf):
"""Sets the class variable to what is passed in. """
self.conf = _conf
def setup_argv(self, _argv):
"""Sets the class variable to what is passed in. """
self.argv = _argv
def setup_parser(self, _parser):
"""Sets the class variable to what is passed in. """
self.parser = _parser
def _setup_response(self):
""" """
if self.request is not None:
return
# Call the function specified on the command line!
self.request = self.argv.func()
# Send the request and print the response
self.response_str = self.send(self.request)
self.response_json = json.loads(self.response_str)
def send(self, request):
"""Send the generated request """
ip = str(self.conf['serverIP'])
port = int(self.conf['mi_port'])
if sys.version_info > (3, 0):
connection = http.client.HTTPConnection(ip, port) # noqa
else:
connection = httplib.HTTPConnection(ip, port) # noqa
connection.request('POST', '/', json.dumps(request))
response = connection.getresponse()
data = response.read()
if sys.version_info > (3, 0):
# We actually receive bytes instead of a string!
data = data.decode("utf-8")
return data
def get_response(self):
"""Returns the response from the server """
self._setup_response()
return self.response_str
def get_response_map(self):
"""Returns the response from the server """
self._setup_response()
return self.response_json
@command
def add_baremetal(self, subparsers=None):
"""Generate a request to add a node to the MoltenIron database.
All parameters are manditory."""
if subparsers is not None:
sp = subparsers.add_parser("add_baremetal",
help="Add a node to the MoltenIron"
" database.")
sp.add_argument("name",
help="Name of the baremetal node")
sp.add_argument("ipmi_ip",
help="IP for issuing IPMI commands to"
" this node")
sp.add_argument("ipmi_user",
help="IPMI username used when issuing"
" IPMI commands to this node")
sp.add_argument("ipmi_password",
help="IPMI password used when"
" issuing IPMI commands to this node")
sp.add_argument("allocation_pool",
help="Comma separated list of"
" IPs to be used in deployment")
sp.add_argument("port_hwaddr",
help="MAC address of port on"
" machine to use during deployment")
sp.add_argument("cpu_arch",
help="Architecture of the node")
sp.add_argument("cpus",
type=int,
help="Number of CPUs on the node")
sp.add_argument("ram_mb",
type=int,
help="Amount of RAM (in MiB)"
" that the node has")
sp.add_argument("disk_gb",
type=int,
help="Amount of disk (in GiB)"
" that the node has")
sp.set_defaults(func=self.add_baremetal)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'add_baremetal'
return request
@command
def add_keyvalue_pairs(self, subparsers=None):
"""Generate a request to add a node to the MoltenIron database.
args are a list of key=value pairs."""
if subparsers is not None:
sp = subparsers.add_parser("add_keyvalue_pairs",
help="Add a node to the MoltenIron"
" database.")
sp.add_argument("name",
help="Name of the baremetal node")
sp.add_argument("ipmi_ip",
help="IP for issuing IPMI commands to"
" this node")
sp.add_argument("allocation_pool",
help="Comma separated list of"
" IPs to be used in deployment")
sp.add_argument("args",
nargs=argparse.REMAINDER,
help="Architecture of the node")
sp.set_defaults(func=self.add_keyvalue_pairs)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'add_keyvalue_pairs'
return request
@command
def add_json_blob(self, subparsers=None):
"""Generate a request to add a node to the MoltenIron database.
blob is a JSON encoded string."""
if subparsers is not None:
sp = subparsers.add_parser("add_json_blob",
help="Add a node to the MoltenIron"
" database.")
sp.add_argument("name",
help="Name of the baremetal node")
sp.add_argument("ipmi_ip",
help="IP for issuing IPMI commands to"
" this node")
sp.add_argument("allocation_pool",
help="Comma separated list of"
" IPs to be used in deployment")
sp.add_argument("blob",
help="JSON encoded string")
sp.set_defaults(func=self.add_json_blob)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'add_json_blob'
return request
@command
def allocate(self, subparsers=None):
"""Generate request to checkout a node from the MoltenIron database """
if subparsers is not None:
sp = subparsers.add_parser("allocate",
help="Checkout a node in MoltenIron."
" Returns the node's info.")
sp.add_argument("owner_name",
help="Name of the requester")
sp.add_argument("number_of_nodes",
type=int,
help="How many nodes to reserve")
sp.set_defaults(func=self.allocate)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'allocate'
return request
@command
def release(self, subparsers=None):
"""Generate a request to release an allocated node from the MoltenIron
database
"""
if subparsers is not None:
sp = subparsers.add_parser("release",
help="Given an owner name, release the"
" allocated node, returning it to"
" the available state.")
sp.add_argument("owner_name",
help="Name of the owner who"
" currently owns the nodes to be released")
sp.set_defaults(func=self.release)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'release'
return request
@command
def get_field(self, subparsers=None):
"""Generate a request to return a field of data from an owned node from
the MoltenIron database
"""
if subparsers is not None:
sp = subparsers.add_parser("get_field",
help="Given an owner name and the name"
" of a field, get the value of"
" the field.")
sp.add_argument("owner_name",
help="Name of the owner who currently"
" owns the nodes to get the field from")
sp.add_argument("field_name",
help="Name of the field to retrieve"
" the value from")
sp.set_defaults(func=self.get_field)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'get_field'
return request
@command
def set_field(self, subparsers=None):
"""Generate request to set a field of data from an id in the MoltenIron
database
"""
if subparsers is not None:
sp = subparsers.add_parser("set_field",
help="Given an id, set a field with a"
" value.")
sp.add_argument("id",
help="Id of the entry")
sp.add_argument("key",
help="Field name to set")
sp.add_argument("value",
help="Field value to set")
sp.add_argument("type",
help="Field Python type to set")
sp.set_defaults(func=self.set_field)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'set_field'
return request
@command
def status(self, subparsers=None):
"""Return status """
if subparsers is not None:
sp = subparsers.add_parser("status",
help="Return a list of current"
" MoltenIron Node database"
" entries.")
sp.add_argument("-t",
"--type",
action="store",
type=str,
default="human",
dest="type",
help="Either human (the default) or csv")
sp.set_defaults(func=self.status)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'status'
return request
@command
def status_baremetal(self, subparsers=None):
"""Return status """
if subparsers is not None:
sp = subparsers.add_parser("status_baremetal",
help="Return a list of current"
" MoltenIron Node database"
" entries.")
sp.add_argument("-t",
"--type",
action="store",
type=str,
default="human",
dest="type",
help="Either human (the default) or csv")
sp.set_defaults(func=self.status_baremetal)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'status_baremetal'
return request
@command
def delete_db(self, subparsers=None):
"""Delete all database entries"""
if subparsers is not None:
sp = subparsers.add_parser("delete_db",
help="Delete every entry in the"
" MoltenIron Node database.")
sp.set_defaults(func=self.delete_db)
return
# Make a map of our arguments
request = vars(self.argv)
# But delete the class function pointer. json can't serialize it!
del request['func']
request['method'] = 'delete_db'
return request
if __name__ == "__main__":
mi = MoltenIron()
mi = molteniron.MoltenIron()
parser = argparse.ArgumentParser(description="Molteniron CLI tool")
parser.add_argument("-c",
@ -449,9 +57,9 @@ if __name__ == "__main__":
# Register all decorated class functions by telling them argparse
# is running
for (cmd_name, cmd_func) in list(command.all.items()):
for (cmd_name, cmd_func) in list(molteniron.command.all.items()):
func = getattr(mi, cmd_name)
func(subparsers) # Tell the function to setup for argparse
func(subparsers=subparsers) # Tell the function to setup for argparse
args = parser.parse_args()
@ -477,10 +85,15 @@ if __name__ == "__main__":
conf = yaml.load(fobj)
mi.setup_conf(conf)
mi.setup_argv(args)
mi.setup_parser(parser)
# Make a map of our arguments
args_map = vars(args)
# And call the function
mi.call_function(args_map)
ret = mi.get_response()
json_ret = json.loads(ret)
if output == "json":

363
molteniron/molteniron.py Normal file
View File

@ -0,0 +1,363 @@
#! /usr/bin/env python
"""
This is the MoltenIron client class that speaks to a MoltenIron server.
"""
# Copyright (c) 2016 IBM Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable-msg=C0103
# pylint: disable=redefined-outer-name
from __future__ import print_function
import argparse
import json
import sys
if sys.version_info >= (3, 0):
import http.client # noqa
else:
import httplib # noqa
# Create a decorator pattern that maintains a registry
def makeRegistrar():
"""Decorator that keeps track of tagged functions."""
registry = {}
def registrar(func):
"""Store the function pointer."""
registry[func.__name__] = func
# normally a decorator returns a wrapped function,
# but here we return func unmodified, after registering it
return func
registrar.all = registry
return registrar
# Create the decorator
command = makeRegistrar()
class MoltenIron(object):
"""This is the MoltenIron client object."""
def __init__(self):
self.conf = None
self.parser = None
self.request = None
self.response_str = None
self.response_json = None
def setup_conf(self, _conf):
"""Sets the class variable to what is passed in."""
self.conf = _conf
def setup_parser(self, _parser):
"""Sets the class variable to what is passed in."""
self.parser = _parser
def call_function(self, args):
"""Calls the function supplied on the server."""
if self.request is not None:
return False
# Save the previously defined function pointer
func = args['func']
# But delete the class function pointer. json can't serialize it!
del args['func']
# Call the function specified on the command line!
self.request = func(args=args)
# Send the request and print the response
self.response_str = self.send(self.request)
self.response_json = json.loads(self.response_str)
return True
def send(self, request):
"""Send the generated request"""
ip = str(self.conf['serverIP'])
port = int(self.conf['mi_port'])
if sys.version_info > (3, 0):
connection = http.client.HTTPConnection(ip, port) # noqa
else:
connection = httplib.HTTPConnection(ip, port) # noqa
connection.request('POST', '/', json.dumps(request))
response = connection.getresponse()
data = response.read()
if sys.version_info > (3, 0):
# We actually receive bytes instead of a string!
data = data.decode("utf-8")
return data
def get_response(self):
"""Returns the response from the server"""
if self.request is None:
raise Exception("Call call_function first")
return self.response_str
def get_response_map(self):
"""Returns the response map from the server"""
if self.request is None:
raise Exception("Call call_function first")
return self.response_json
@command
def add_baremetal(self, args=None, subparsers=None):
"""Add a node to the MoltenIron database.
All parameters are manditory.
"""
if subparsers is not None:
sp = subparsers.add_parser("add_baremetal",
help="Add a node to the MoltenIron"
" database.")
sp.add_argument("name",
help="Name of the baremetal node")
sp.add_argument("ipmi_ip",
help="IP for issuing IPMI commands to"
" this node")
sp.add_argument("ipmi_user",
help="IPMI username used when issuing"
" IPMI commands to this node")
sp.add_argument("ipmi_password",
help="IPMI password used when"
" issuing IPMI commands to this node")
sp.add_argument("allocation_pool",
help="Comma separated list of"
" IPs to be used in deployment")
sp.add_argument("port_hwaddr",
help="MAC address of port on"
" machine to use during deployment")
sp.add_argument("cpu_arch",
help="Architecture of the node")
sp.add_argument("cpus",
type=int,
help="Number of CPUs on the node")
sp.add_argument("ram_mb",
type=int,
help="Amount of RAM (in MiB)"
" that the node has")
sp.add_argument("disk_gb",
type=int,
help="Amount of disk (in GiB)"
" that the node has")
sp.set_defaults(func=self.add_baremetal)
return
args['method'] = 'add_baremetal'
return args
@command
def add_keyvalue_pairs(self, args=None, subparsers=None):
"""Add a node to the MoltenIron database.
args are a list of key=value pairs.
"""
if subparsers is not None:
sp = subparsers.add_parser("add_keyvalue_pairs",
help="Add a node to the MoltenIron"
" database.")
sp.add_argument("name",
help="Name of the baremetal node")
sp.add_argument("ipmi_ip",
help="IP for issuing IPMI commands to"
" this node")
sp.add_argument("allocation_pool",
help="Comma separated list of"
" IPs to be used in deployment")
sp.add_argument("args",
nargs=argparse.REMAINDER,
help="Architecture of the node")
sp.set_defaults(func=self.add_keyvalue_pairs)
return
args['method'] = 'add_keyvalue_pairs'
return args
@command
def add_json_blob(self, args=None, subparsers=None):
"""Add a node to the MoltenIron database.
blob is a JSON encoded string.
"""
if subparsers is not None:
sp = subparsers.add_parser("add_json_blob",
help="Add a node to the MoltenIron"
" database.")
sp.add_argument("name",
help="Name of the baremetal node")
sp.add_argument("ipmi_ip",
help="IP for issuing IPMI commands to"
" this node")
sp.add_argument("allocation_pool",
help="Comma separated list of"
" IPs to be used in deployment")
sp.add_argument("blob",
help="JSON encoded string")
sp.set_defaults(func=self.add_json_blob)
return
args['method'] = 'add_json_blob'
return args
@command
def allocate(self, args=None, subparsers=None):
"""Checkout a node from the MoltenIron database"""
if subparsers is not None:
sp = subparsers.add_parser("allocate",
help="Checkout a node in MoltenIron."
" Returns the node's info.")
sp.add_argument("owner_name",
help="Name of the requester")
sp.add_argument("number_of_nodes",
type=int,
help="How many nodes to reserve")
sp.set_defaults(func=self.allocate)
return
args['method'] = 'allocate'
return args
@command
def release(self, args=None, subparsers=None):
"""Release an allocated node from the MoltenIron database."""
if subparsers is not None:
sp = subparsers.add_parser("release",
help="Given an owner name, release the"
" allocated node, returning it to"
" the available state.")
sp.add_argument("owner_name",
help="Name of the owner who"
" currently owns the nodes to be released")
sp.set_defaults(func=self.release)
return
args['method'] = 'release'
return args
@command
def get_field(self, args=None, subparsers=None):
"""Return a field of data from an owned node from the MoltenIron db."""
if subparsers is not None:
sp = subparsers.add_parser("get_field",
help="Given an owner name and the name"
" of a field, get the value of"
" the field.")
sp.add_argument("owner_name",
help="Name of the owner who currently"
" owns the nodes to get the field from")
sp.add_argument("field_name",
help="Name of the field to retrieve"
" the value from")
sp.set_defaults(func=self.get_field)
return
args['method'] = 'get_field'
return args
@command
def set_field(self, args=None, subparsers=None):
"""Set a field of data from an id in the MoltenIron database."""
if subparsers is not None:
sp = subparsers.add_parser("set_field",
help="Given an id, set a field with a"
" value.")
sp.add_argument("id",
help="Id of the entry")
sp.add_argument("key",
help="Field name to set")
sp.add_argument("value",
help="Field value to set")
sp.add_argument("type",
help="Field Python type to set")
sp.set_defaults(func=self.set_field)
return
args['method'] = 'set_field'
return args
@command
def status(self, args=None, subparsers=None):
"""Return the nodes as a status"""
if subparsers is not None:
sp = subparsers.add_parser("status",
help="Return a list of current"
" MoltenIron Node database"
" entries.")
sp.add_argument("-t",
"--type",
action="store",
type=str,
default="human",
dest="type",
help="Either human (the default) or csv")
sp.set_defaults(func=self.status)
return
args['method'] = 'status'
return args
@command
def status_baremetal(self, args=None, subparsers=None):
"""Return the nodes as a status"""
if subparsers is not None:
sp = subparsers.add_parser("status_baremetal",
help="Return a list of current"
" MoltenIron Node database"
" entries.")
sp.add_argument("-t",
"--type",
action="store",
type=str,
default="human",
dest="type",
help="Either human (the default) or csv")
sp.set_defaults(func=self.status_baremetal)
return
args['method'] = 'status_baremetal'
return args
@command
def delete_db(self, args=None, subparsers=None):
"""Delete all database entries"""
if subparsers is not None:
sp = subparsers.add_parser("delete_db",
help="Delete every entry in the"
" MoltenIron Node database.")
sp.set_defaults(func=self.delete_db)
return
args['method'] = 'delete_db'
return args