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

View File

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

View File

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

View File

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

View File

@ -46,7 +46,7 @@ class UpstartRunner(object):
def __init__(self, cfg): def __init__(self, cfg):
self.cfg = cfg self.cfg = cfg
def stop(self, name, *args, **kargs): def stop(self, name, trace_dir):
msg = "Not implemented yet" msg = "Not implemented yet"
raise NotImplementedError(msg) raise NotImplementedError(msg)
@ -97,6 +97,7 @@ class UpstartRunner(object):
runtrace.trace(ARGS, json.dumps(program_args)) runtrace.trace(ARGS, json.dumps(program_args))
return tracefn 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" msg = "Not implemented yet"
raise NotImplementedError(msg) raise NotImplementedError(msg)

View File

@ -56,28 +56,12 @@ def execute_template(*cmds, **kargs):
ignore_missing = kargs.pop('ignore_missing', False) ignore_missing = kargs.pop('ignore_missing', False)
cmd_results = list() cmd_results = list()
for cmdinfo in cmds: for cmdinfo in cmds:
cmd_to_run_templ = cmdinfo.get("cmd") cmd_to_run_templ = cmdinfo["cmd"]
if not cmd_to_run_templ: cmd_to_run = param_replace_list(cmd_to_run_templ, params_replacements, ignore_missing)
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))
stdin_templ = cmdinfo.get('stdin') stdin_templ = cmdinfo.get('stdin')
stdin = None stdin = None
if stdin_templ: if stdin_templ:
stdin_full = list() stdin_full = param_replace_list(stdin_templ, params_replacements, ignore_missing)
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 = joinlinesep(*stdin_full) stdin = joinlinesep(*stdin_full)
exec_result = sh.execute(*cmd_to_run, exec_result = sh.execute(*cmd_to_run,
run_as_root=cmdinfo.get('run_as_root', False), run_as_root=cmdinfo.get('run_as_root', False),
@ -312,6 +296,16 @@ def service_enabled(name, components, empty_true=True):
return False 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): def param_replace(text, replacements, ignore_missing=False):
if not replacements: if not replacements: