From abfdcaa08a3d2bbc1b23d4fb837e4ab93bddd522 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Tue, 27 Mar 2012 20:40:24 -0500 Subject: [PATCH] Adding root enabled. * Added the MVC for root enabled. * Fixed a small bug in instance/models.py. * Pep8 compliance --- bin/reddwarf-guestagent | 2 +- reddwarf/extensions/mysql.py | 6 ++++++ reddwarf/extensions/mysql/models.py | 20 ++++++++++++++++++-- reddwarf/extensions/mysql/service.py | 25 +++++++++++++++++++++++++ reddwarf/extensions/mysql/views.py | 19 +++++++++++++++++++ reddwarf/instance/models.py | 6 ++---- 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/bin/reddwarf-guestagent b/bin/reddwarf-guestagent index 2e8bff293b..72d90c7581 100755 --- a/bin/reddwarf-guestagent +++ b/bin/reddwarf-guestagent @@ -57,7 +57,7 @@ if __name__ == '__main__': conf_loc = '%s/%s' % (config.Config.get('here'), 'conf.d/guest_info') config.Config.append_to_config_values('reddwarf-guestagent', {'config_file': conf_loc}, None) - # Now do the same for the /etc/guest_info file + # Now do the same for the /etc/guest_info file # that is injected into the VM config.Config.append_to_config_values('reddwarf-guestagent', {'config_file': '/etc/guest_info'}, None) diff --git a/reddwarf/extensions/mysql.py b/reddwarf/extensions/mysql.py index 08a15871ed..dd1844c44d 100644 --- a/reddwarf/extensions/mysql.py +++ b/reddwarf/extensions/mysql.py @@ -55,5 +55,11 @@ class Mysql(extensions.ExtensionsDescriptor): parent={'member_name': 'instance', 'collection_name': '{tenant_id}/instances'}) resources.append(resource) + resource = extensions.ResourceExtension( + 'root', + service.RootController(), + parent={'member_name': 'instance', + 'collection_name': '{tenant_id}/instances'}) + resources.append(resource) return resources diff --git a/reddwarf/extensions/mysql/models.py b/reddwarf/extensions/mysql/models.py index aa3acfbee5..bfdf397614 100644 --- a/reddwarf/extensions/mysql/models.py +++ b/reddwarf/extensions/mysql/models.py @@ -30,17 +30,17 @@ from reddwarf.guestagent import api as guest_api CONFIG = config.Config LOG = logging.getLogger(__name__) + def load_and_verify(context, instance_id): # Load InstanceServiceStatus to verify if its running instance = base_models.Instance.load(context, instance_id) - LOG.info("found instance %s" % instance) - LOG.info("is sql running %s" % instance.is_sql_running) if not instance.is_sql_running: raise exception.UnprocessableEntity( "Instance %s is not ready." % instance.id) else: return instance + def populate_databases(dbs): """ Create a serializable request with user provided data @@ -98,6 +98,22 @@ class User(object): guest_api.API().delete_user(context, instance_id, username) +class Root(object): + + @classmethod + def load(cls, context, instance_id): + load_and_verify(context, instance_id) + return guest_api.API().is_root_enabled(context, instance_id) + + @classmethod + def create(cls, context, instance_id): + load_and_verify(context, instance_id) + root = guest_api.API().enable_root(context, instance_id) + root_user = guest_models.MySQLUser() + root_user.deserialize(root) + return root_user + + class Users(object): @classmethod diff --git a/reddwarf/extensions/mysql/service.py b/reddwarf/extensions/mysql/service.py index afd062bf3c..b394500b86 100644 --- a/reddwarf/extensions/mysql/service.py +++ b/reddwarf/extensions/mysql/service.py @@ -31,6 +31,31 @@ class BaseController(wsgi.Controller): """Base controller class.""" +class RootController(BaseController): + """Controller for instance functionality""" + + def index(self, req, tenant_id, instance_id): + """ Returns True if root is enabled for the given instance; + False otherwise. """ + LOG.info("Getting root enabled for instance '%s'" % instance_id) + LOG.info("req : '%s'\n\n" % req) + context = rd_context.ReddwarfContext( + auth_tok=req.headers["X-Auth-Token"], + tenant=tenant_id) + is_root_enabled = models.Root.load(context, instance_id) + return views.RootEnabledView(is_root_enabled).data() + + def create(self, req, body, tenant_id, instance_id): + """ Enable the root user for the db instance """ + LOG.info("Enabling root for instance '%s'" % instance_id) + LOG.info("req : '%s'\n\n" % req) + context = rd_context.ReddwarfContext( + auth_tok=req.headers["X-Auth-Token"], + tenant=tenant_id) + root = models.Root.create(context, instance_id) + return views.RootCreatedView(root).data() + + class UserController(BaseController): """Controller for instance functionality""" diff --git a/reddwarf/extensions/mysql/views.py b/reddwarf/extensions/mysql/views.py index 495e15c675..a76b5aaa25 100644 --- a/reddwarf/extensions/mysql/views.py +++ b/reddwarf/extensions/mysql/views.py @@ -43,6 +43,25 @@ class UsersView(object): return data +class RootCreatedView(UserView): + + def data(self): + user_dict = { + "name": self.user.name, + "password": self.user.password + } + return {"user": user_dict} + + +class RootEnabledView(object): + + def __init__(self, is_root_enabled): + self.is_root_enabled = is_root_enabled + + def data(self): + return {'rootEnabled': self.is_root_enabled} + + class SchemaView(object): def __init__(self, schema): diff --git a/reddwarf/instance/models.py b/reddwarf/instance/models.py index cc16d9d3f6..cdc2f02f2c 100644 --- a/reddwarf/instance/models.py +++ b/reddwarf/instance/models.py @@ -40,8 +40,6 @@ LOG = logging.getLogger(__name__) def load_server(client, instance_id, server_id): """Loads a server or raises an exception.""" - if uuid is None: - raise TypeError("Argument uuid not defined.") try: server = client.servers.get(server_id) except nova_exceptions.NotFound, e: @@ -125,8 +123,8 @@ class Instance(object): task_status=InstanceTasks.BUILDING) LOG.debug("Created new Reddwarf instance %s..." % db_info.id) client = create_nova_client(context) - server = client.servers.create(name, image_id, flavor_ref, - files={"/etc/guest_info":"guest_id=%s" % db_info.id}) + server = client.servers.create(name, image_id, flavor_ref, + files={"/etc/guest_info": "guest_id=%s" % db_info.id}) LOG.debug("Created new compute instance %s." % server.id) db_info.compute_instance_id = server.id db_info.save()