Updated with runtime status function and impl.

This commit is contained in:
Joshua Harlow
2012-01-12 14:33:35 -08:00
parent 3647c1a015
commit f2b5ebf293
5 changed files with 72 additions and 20 deletions

View File

@@ -65,3 +65,9 @@ class RuntimeComponent():
def stop(self):
raise NotImplementedError()
#this should return a status string
#with "stop" in it if its stopped
#"start" in it if its started (not both)
def status(self):
raise NotImplementedError()

View File

@@ -24,6 +24,8 @@ import Util
from Util import (DB,
get_pkg_list,
execute_template)
import Exceptions
from Exceptions import StartException, StopException, StatusException
import Trace
from Trace import (TraceWriter, TraceReader)
import Shell
@@ -35,9 +37,11 @@ TYPE = DB
MYSQL = 'mysql'
DB_ACTIONS = {
MYSQL: {
#hopefully these are distro independent
'start': ["/etc/init.d/mysql", "start"],
'stop': ["/etc/init.d/mysql", "stop"],
#hopefully these are distro independent, these should be since they are invoking system init scripts
'start': ["service", "mysql", 'start'],
'stop': ["service", 'mysql', "stop"],
'status': ["service", 'mysql', "status"],
#
'create_db': ['mysql', '--user=%USER%', '--password=%PASSWORD%', '-e', 'CREATE DATABASE %DB%;'],
'drop_db': ['mysql', '--user=%USER%', '--password=%PASSWORD%', '-e', 'DROP DATABASE IF EXISTS %DB%;'],
'grant_all': [
@@ -81,6 +85,7 @@ class DBInstaller(ComponentBase, InstallComponent):
def __init__(self, *args, **kargs):
ComponentBase.__init__(self, TYPE, *args, **kargs)
self.tracewriter = TraceWriter(self.tracedir, Trace.IN_TRACE)
self.runtime = DBRuntime(*args, **kargs)
def download(self):
#nothing to download, we are just a pkg
@@ -141,8 +146,8 @@ class DBInstaller(ComponentBase, InstallComponent):
self.tracewriter.package_install(name, pkgs.get(name))
#run any post-installs cmds
self._post_install(pkgs)
#todo - stop it (since it usually autostarts)
#so that we control the start/stop, not the distro...
#it should be started now, if not start it
self.runtime.start()
return self.tracedir
@@ -158,14 +163,16 @@ class DBRuntime(ComponentBase, RuntimeComponent):
msg = "Can not start %s since it was not installed" % (TYPE)
raise StartException(msg)
#figure out how to start it
dbtype = cfg.get("db", "type")
dbtype = self.cfg.get("db", "type")
typeactions = DB_ACTIONS.get(dbtype)
if(typeactions == None or not typeactions.get('start')):
msg = BASE_ERROR % ('start', dbtype)
raise NotImplementedError(msg)
#run whatever the command is to get it going
startcmd = typeactions.get('start')
execute(*startcmd, run_as_root=True)
#check if already going
if(self.status().find('start') == -1):
#run whatever the command is to get it going
startcmd = typeactions.get('start')
execute(*startcmd, run_as_root=True)
return None
def stop(self):
@@ -175,15 +182,33 @@ class DBRuntime(ComponentBase, RuntimeComponent):
msg = "Can not stop %s since it was not installed" % (TYPE)
raise StopException(msg)
#figure out how to stop it
dbtype = cfg.get("db", "type")
dbtype = self.cfg.get("db", "type")
typeactions = DB_ACTIONS.get(dbtype)
if(typeactions == None or not typeactions.get('stop')):
msg = BASE_ERROR % ('stop', dbtype)
#run whatever the command is to get it stopped
stopcmd = typeactions.get('stop')
execute(*stopcmd, run_as_root=True)
raise NotImplementedError(msg)
#check if already stopped
if(self.status().find('stop') == -1):
#run whatever the command is to get it stopped
stopcmd = typeactions.get('stop')
execute(*stopcmd, run_as_root=True)
return None
def status(self):
#make sure it was actually installed
pkgsinstalled = self.tracereader.packages_installed()
if(len(pkgsinstalled) == 0):
msg = "Can not check the status of %s since it was not installed" % (TYPE)
raise StatusException(msg)
#figure out how to get the status of it
dbtype = self.cfg.get("db", "type")
if(typeactions == None or not typeactions.get('status')):
msg = BASE_ERROR % ('status', dbtype)
raise NotImplementedError(msg)
statuscmd = typeactions.get('status')
(sysout, stderr) = execute(*statuscmd, run_as_root=True)
return sysout.strip()
def drop_db(cfg, dbname):
dbtype = cfg.get("db", "type")

View File

@@ -38,6 +38,10 @@ class StopException(Exception):
pass
class StatusException(Exception):
pass
class FileException(Exception):
pass

View File

@@ -18,6 +18,8 @@ import Logger
import Component
from Component import (ComponentBase, RuntimeComponent,
UninstallComponent, InstallComponent)
import Exceptions
from Exceptions import StartException, StopException, StatusException
import Packager
import Util
from Util import (RABBIT,
@@ -29,9 +31,11 @@ from Shell import (mkdirslist, execute, deldir)
LOG = Logger.getLogger("install.rabbit")
TYPE = RABBIT
#hopefully these are distro independent..
START_CMD = ["/etc/init.d/rabbitmq-server", "start"]
STOP_CMD = ["/etc/init.d/rabbitmq-server", "stop"]
START_CMD = ['service', "rabbitmq-server", "start"]
STOP_CMD = ['service', "rabbitmq-server", "stop"]
STATUS_CMD = ['service', "rabbitmq-server", "status"]
PWD_CMD = ['rabbitmqctl', 'change_password', 'guest']
@@ -64,6 +68,7 @@ class RabbitInstaller(ComponentBase, InstallComponent):
def __init__(self, *args, **kargs):
ComponentBase.__init__(self, TYPE, *args, **kargs)
self.tracewriter = TraceWriter(self.tracedir, Trace.IN_TRACE)
self.runtime = RabbitRuntime(*args, **kargs)
def download(self):
#nothing to download, we are just a pkg
@@ -93,8 +98,8 @@ class RabbitInstaller(ComponentBase, InstallComponent):
#this trace is used to remove the dirs created
self.tracewriter.dir_made(*dirsmade)
self._setup_pw()
#TODO - stop it (since it usually autostarts)
#so that we control the start/stop, not it
#it should be started now, if not start it
self.runtime.start()
return self.tracedir
@@ -108,13 +113,25 @@ class RabbitRuntime(ComponentBase, RuntimeComponent):
if(len(pkgsinstalled) == 0):
msg = "Can not start %s since it was not installed" % (TYPE)
raise StartException(msg)
execute(*START_CMD, run_as_root=True)
#check if already going
if(self.status().find('start') == -1):
execute(*START_CMD, run_as_root=True)
return None
def status(self):
pkgsinstalled = self.tracereader.packages_installed()
if(len(pkgsinstalled) == 0):
msg = "Can not check the status of %s since it was not installed" % (TYPE)
raise StatusException(msg)
(sysout, stderr) = execute(*STATUS_CMD, run_as_root=True)
return sysout.strip().lower()
def stop(self):
pkgsinstalled = self.tracereader.packages_installed()
if(len(pkgsinstalled) == 0):
msg = "Can not stop %s since it was not installed" % (TYPE)
raise StopException(msg)
execute(*STOP_CMD, run_as_root=True)
#check if already stopped
if(self.status().find('stop') == -1):
execute(*STOP_CMD, run_as_root=True)
return None

View File

@@ -192,7 +192,7 @@ def execute_template(*cmds, **kargs):
stdin_full.append(piece)
stdin = joinlinesep(stdin_full)
root_run = cmdinfo.get('run_as_root', False)
execute(*cmd_to_run, process_input=stdin, **kargs)
execute(*cmd_to_run, run_as_root=root_run, process_input=stdin, **kargs)
def fetch_deps(component, add=False):