Cleanup aisle 10.

This commit is contained in:
Joshua Harlow 2012-02-26 17:59:52 -08:00
parent 900120df84
commit d5ddf5fc1b
6 changed files with 63 additions and 86 deletions

View File

@ -392,9 +392,6 @@ class ProgramRuntime(ComponentBase):
def start(self):
#select how we are going to start it
run_type = utils.fetch_run_type(self.cfg)
if run_type not in settings.RUN_TYPES_KNOWN:
msg = "Unknown run type %s found in configuration" % (run_type)
raise excp.ConfigException(msg)
startercls = self._getstartercls(run_type)
starter = startercls(self.cfg)
#start all apps
@ -403,28 +400,19 @@ class ProgramRuntime(ComponentBase):
apps = self._get_apps_to_start()
for app_info in apps:
#extract needed keys
app_name = app_info.get("name")
app_name = app_info["name"]
app_pth = app_info.get("path", app_name)
app_dir = app_info.get("app_dir", self.appdir)
#adjust the program options now that we have real locations
params = self._get_param_map(app_name)
program_opts = self._get_app_options(app_name)
if params and program_opts:
adjusted_opts = list()
for opt in program_opts:
adjusted_opts.append(utils.param_replace(str(opt), params))
program_opts = adjusted_opts
program_opts = utils.param_replace_list(self._get_app_options(app_name), self._get_param_map(app_name))
#start it with the given settings
LOG.debug("Starting [%s] with options [%s] with runner type [%s]" % (app_name, ", ".join(program_opts), run_type))
fn = starter.start(app_name, app_pth, *program_opts, app_dir=app_dir, \
trace_dir=self.tracedir)
if fn:
fns.append(fn)
LOG.debug("Started %s, details are in %s" % (app_name, fn))
#this trace is used to locate details about what to stop
self.tracewriter.started_info(app_name, fn)
else:
LOG.debug("Started %s" % (app_name))
runtime_info = (app_pth, app_dir, program_opts)
info_fn = starter.start(app_name, runtime_info, self.tracedir)
fns.append(info_fn)
LOG.debug("Started %s, details are in %s" % (app_name, info_fn))
#this trace is used to locate details about what to stop
self.tracewriter.started_info(app_name, info_fn)
return fns
def stop(self):
@ -433,11 +421,8 @@ class ProgramRuntime(ComponentBase):
killedam = 0
for mp in start_traces:
#extract the apps name and where its trace is
fn = mp.get('trace_fn')
name = mp.get('name')
#missing some key info, skip it
if fn is None or name is None:
continue
fn = mp['trace_fn']
name = mp['name']
#figure out which class will stop it
contents = tr.parse_fn(fn)
killcls = None
@ -453,11 +438,11 @@ class ProgramRuntime(ComponentBase):
LOG.debug("Stopping %s of run type %s" % (name, runtype))
#create an instance of the killer class and attempt to stop
killer = killcls(self.cfg)
killer.stop(name, trace_dir=self.tracedir)
killer.stop(name, self.tracedir)
killedam += 1
else:
#TODO raise error??
pass
msg = "Could not figure out which class to use to stop (%s, %s)" % (name, fn)
raise excp.StopException(msg)
#if we got rid of them all get rid of the trace
if killedam == len(start_traces):
fn = self.starttracereader.trace_fn

View File

@ -844,10 +844,7 @@ class NovaConf(object):
full_line = key_str
else:
key_str = self._form_key(key, True)
filled_opts = list()
for opt in opts:
if opt is not None:
filled_opts.append(utils.param_replace(str(opt), param_dict))
filled_opts = utils.param_replace_list(opts, param_dict)
full_line = key_str + ",".join(filled_opts)
gen_lines.append(full_line)
return gen_lines

View File

@ -49,6 +49,9 @@ ARGS = "ARGS"
NAME = "NAME"
FORK_TEMPL = "%s.fork"
#run fork cmds as root?
ROOT_GO = True
class ForkRunner(object):
def __init__(self, cfg):
@ -75,9 +78,8 @@ class ForkRunner(object):
time.sleep(SLEEP_TIME)
return (killed, attempts)
def stop(self, name, *args, **kargs):
with sh.Rooted(kargs.get("run_as_root", True)):
trace_dir = kargs["trace_dir"]
def stop(self, name, trace_dir):
with sh.Rooted(ROOT_GO):
if not trace_dir or not sh.isdir(trace_dir):
msg = "No trace directory found from which to stop %s" % (name)
raise excp.StopException(msg)
@ -161,19 +163,25 @@ class ForkRunner(object):
#be bad right now
os._exit(0)
def start(self, name, program, *program_args, **kargs):
tracedir = kargs["trace_dir"]
appdir = kargs["app_dir"]
fn_name = FORK_TEMPL % (name)
(pidfile, stderrfn, stdoutfn) = self._form_file_names(tracedir, fn_name)
tracefn = tr.touch_trace(tracedir, fn_name)
def _do_trace(self, fn, tracedir, kvs):
tracefn = tr.touch_trace(tracedir, fn)
runtrace = tr.Trace(tracefn)
runtrace.trace(TYPE, RUN_TYPE)
runtrace.trace(PID_FN, pidfile)
runtrace.trace(STDERR_FN, stderrfn)
runtrace.trace(STDOUT_FN, stdoutfn)
runtrace.trace(ARGS, json.dumps(program_args))
for (k, v) in kvs.items():
runtrace.trace(k, v)
return tracefn
def start(self, name, runtime_info, tracedir):
(program, appdir, program_args) = runtime_info
fn_name = FORK_TEMPL % (name)
(pidfile, stderrfn, stdoutfn) = self._form_file_names(tracedir, fn_name)
trace_info = dict()
trace_info[PID_FN] = pidfile
trace_info[STDERR_FN] = stderrfn
trace_info[STDOUT_FN] = stdoutfn
trace_info[ARGS] = json.dumps(program_args)
tracefn = self._do_trace(fn_name, tracedir, trace_info)
LOG.debug("Forking [%s] by running command [%s]" % (name, program))
with sh.Rooted(kargs.get("run_as_root", True)):
with sh.Rooted(ROOT_GO):
self._fork_start(program, appdir, pidfile, stdoutfn, stderrfn, *program_args)
return tracefn

View File

@ -74,8 +74,7 @@ class ScreenRunner(object):
def __init__(self, cfg):
self.cfg = cfg
def stop(self, name, *args, **kargs):
tracedir = kargs["trace_dir"]
def stop(self, name, tracedir):
fn_name = SCREEN_TEMPL % (name)
trace_fn = tr.trace_fn(tracedir, fn_name)
session_id = self._find_session(name, trace_fn)
@ -137,11 +136,7 @@ class ScreenRunner(object):
return env
def _gen_cmd(self, base_cmd, params=dict()):
full_cmd = base_cmd
actual_cmd = list()
for piece in full_cmd:
actual_cmd.append(utils.param_replace(piece, params))
return actual_cmd
return utils.param_replace_list(base_cmd, params)
def _active_sessions(self):
knowns = list()
@ -215,14 +210,11 @@ class ScreenRunner(object):
#we have really no way of knowing if it worked or not
#screen sux...
def _do_socketdir_init(self):
socketdir = SCREEN_SOCKET_DIR
def _do_socketdir_init(self, socketdir):
with sh.Rooted(ROOT_GO):
if not sh.isdir(socketdir):
dirs = sh.mkdirslist(socketdir)
for d in dirs:
sh.chmod(d, SCREEN_SOCKET_PERM)
return socketdir
dirs = sh.mkdirslist(socketdir)
for d in dirs:
sh.chmod(d, SCREEN_SOCKET_PERM)
def _begin_start(self, name, program, args, tracedir):
fn_name = SCREEN_TEMPL % (name)
@ -244,8 +236,8 @@ class ScreenRunner(object):
self._do_start(session_name, name, full_cmd)
return tracefn
def start(self, name, program, *program_args, **kargs):
self._do_socketdir_init()
tracedir = kargs["trace_dir"]
args = list(program_args)
return self._begin_start(name, program, args, tracedir)
def start(self, name, runtime_info, tracedir):
(program, _, program_args) = runtime_info
if not sh.isdir(SCREEN_SOCKET_DIR):
self._do_socketdir_init(SCREEN_SOCKET_DIR)
return self._begin_start(name, program, program_args, tracedir)

View File

@ -46,7 +46,7 @@ class UpstartRunner(object):
def __init__(self, cfg):
self.cfg = cfg
def stop(self, name, *args, **kargs):
def stop(self, name, trace_dir):
msg = "Not implemented yet"
raise NotImplementedError(msg)
@ -97,6 +97,7 @@ class UpstartRunner(object):
runtrace.trace(ARGS, json.dumps(program_args))
return tracefn
def start(self, name, program, *program_args, **kargs):
def start(self, name, runtime_info, tracedir):
#(program, appdir, program_args) = runtime_info
msg = "Not implemented yet"
raise NotImplementedError(msg)

View File

@ -56,28 +56,12 @@ def execute_template(*cmds, **kargs):
ignore_missing = kargs.pop('ignore_missing', False)
cmd_results = list()
for cmdinfo in cmds:
cmd_to_run_templ = cmdinfo.get("cmd")
if not cmd_to_run_templ:
continue
cmd_to_run = list()
if not params_replacements:
cmd_to_run = cmd_to_run_templ
else:
for piece in cmd_to_run_templ:
cmd_to_run.append(param_replace(str(piece),
params_replacements,
ignore_missing=ignore_missing))
cmd_to_run_templ = cmdinfo["cmd"]
cmd_to_run = param_replace_list(cmd_to_run_templ, params_replacements, ignore_missing)
stdin_templ = cmdinfo.get('stdin')
stdin = None
if stdin_templ:
stdin_full = list()
if not params_replacements:
stdin_full = stdin_templ
else:
for piece in stdin_templ:
stdin_full.append(param_replace(str(piece),
params_replacements,
ignore_missing=ignore_missing))
stdin_full = param_replace_list(stdin_templ, params_replacements, ignore_missing)
stdin = joinlinesep(*stdin_full)
exec_result = sh.execute(*cmd_to_run,
run_as_root=cmdinfo.get('run_as_root', False),
@ -312,6 +296,16 @@ def service_enabled(name, components, empty_true=True):
return False
def param_replace_list(values, replacements, ignore_missing=False):
new_values = list()
if not values:
return new_values
for v in values:
if v is not None:
new_values.append(param_replace(str(v), replacements, ignore_missing))
return new_values
def param_replace(text, replacements, ignore_missing=False):
if not replacements: