Esempio n. 1
0
def _run_action(args):
    defaulted_components = False
    components = settings.parse_components(args.pop("components"))
    if not components:
        defaulted_components = True
        components = _get_def_components()
    action = _clean_action(args.pop("action"))
    if not action:
        cprint("No valid action specified!", "red")
        return False
    rootdir = args.pop("dir")
    if rootdir is None:
        cprint("No root directory specified!", "red")
        return False
    #ensure os/distro is known
    (distro, platform) = utils.determine_distro()
    if distro is None:
        print("Unsupported platform " + colored(platform, "red") + "!")
        return False
    #start it
    (rep, maxlen) = utils.welcome(_WELCOME_MAP.get(action))
    header = utils.center_text("Action Runner", rep, maxlen)
    print(header)
    #need to figure out dependencies for components (if any)
    ignore_deps = args.pop('ignore_deps', False)
    if not defaulted_components:
        LOG.info("Activating components [%s]" % (", ".join(sorted(components.keys()))))
    else:
        LOG.info("Activating default components [%s]" % (", ".join(sorted(components.keys()))))
    if not ignore_deps:
        new_components = settings.resolve_dependencies(components.keys())
        component_diff = new_components.difference(components.keys())
        if component_diff:
            LOG.info("Having to activate dependent components: [%s]" % (", ".join(sorted(component_diff))))
            for new_component in component_diff:
                components[new_component] = list()
    component_skips = _check_roots(action, rootdir, components.keys())
    for c in component_skips:
        components.pop(c)
    if not components:
        LOG.error("After checking the various components roots, no components ended up being specified!")
        return False
    #get the right component order (by priority)
    component_order = settings.prioritize_components(components.keys())
    if action in _REVERSE_ACTIONS:
        #reverse them so that we stop in the reverse order
        #and that we uninstall in the reverse order which seems to make sense
        component_order.reverse()
    #add in any that will just be referenced but which will not actually do anything (ie the action will not be applied to these)
    ref_components = settings.parse_components(args.pop("ref_components"))
    for c in ref_components.keys():
        if c not in components:
            components[c] = ref_components.get(c)
    #now do it!
    LOG.info("Starting action [%s] on %s for distro [%s]" % (action, date.rcf8222date(), distro))
    results = _run_components(action, component_order, components, distro, rootdir, args)
    LOG.info("Finished action [%s] on %s" % (action, date.rcf8222date()))
    if results:
        LOG.info('Check [%s] for traces of what happened.' % ", ".join(results))
    return True
def _run_action(args):
    defaulted_components = False
    components = utils.parse_components(args.pop("components"))
    if not components:
        defaulted_components = True
        components = common.get_default_components()
    action = _clean_action(args.pop("action"))
    if not action:
        print(utils.color_text("No valid action specified!", "red"))
        return False
    rootdir = args.pop("dir")
    if rootdir is None:
        print(utils.color_text("No root directory specified!", "red"))
        return False
    #ensure os/distro is known
    (distro, platform) = utils.determine_distro()
    if distro is None:
        print("Unsupported platform " + utils.color_text(platform, "red") + "!")
        return False
    #start it
    (rep, maxlen) = utils.welcome(_WELCOME_MAP.get(action))
    header = utils.center_text("Action Runner", rep, maxlen)
    print(header)
    #here on out should be using the logger
    if not defaulted_components:
        LOG.info("Activating components [%s]" % (", ".join(sorted(components.keys()))))
    else:
        LOG.info("Activating default components [%s]" % (", ".join(sorted(components.keys()))))
    #need to figure out dependencies for components (if any)
    ignore_deps = args.pop('ignore_deps', False)
    component_order = None
    if not ignore_deps:
        all_components_deps = common.get_components_deps(action, components)
        component_diff = set(all_components_deps.keys()).difference(components.keys())
        if component_diff:
            LOG.info("Having to activate dependent components: [%s]" \
                         % (", ".join(sorted(component_diff))))
            for new_component in component_diff:
                components[new_component] = list()
        component_order = utils.get_components_order(all_components_deps)
    else:
        component_order = components.keys()
    #reverse them so that we stop in the reverse order
    #and that we uninstall in the reverse order which seems to make sense
    if action in _REVERSE_ACTIONS:
        component_order.reverse()
    #add in any that will just be referenced but which will not actually do anything (ie the action will not be applied to these)
    ref_components = utils.parse_components(args.pop("ref_components"))
    for c in ref_components.keys():
        if c not in components:
            components[c] = ref_components.get(c)
    #now do it!
    LOG.info("Starting action [%s] on %s for distro [%s]" % (action, date.rcf8222date(), distro))
    _run_components(action, component_order, components, distro, rootdir, args)
    LOG.info("Finished action [%s] on %s" % (action, date.rcf8222date()))
    return True
 def _get_upstart_conf_params(self, app_pth, program_name, *program_args):
     params = dict()
     if self.cfg.getboolean('upstart', 'respawn'):
         params['RESPAWN'] = "respawn"
     else:
         params['RESPAWN'] = ""
     params['SHORT_NAME'] = program_name
     params['MADE_DATE'] = date.rcf8222date()
     params['START_EVENT'] = self.cfg.getdefaulted('upstart', 'start_event',
                                                   'all_os_start')
     params['STOP_EVENT'] = self.cfg.getdefaulted('upstart', 'stop_event',
                                                  'all_os_stop')
     params[
         'COMPONENT_START_EVENT'] = self.component_name + START_EVENT_SUFFIX
     params[
         'COMPONENT_STOP_EVENT'] = self.component_name + STOP_EVENT_SUFFIX
     params['PROGRAM_NAME'] = app_pth
     params['AUTHOR'] = settings.PROG_NICE_NAME
     if program_args:
         escaped_args = list()
         for opt in program_args:
             LOG.debug("Current opt: %s" % (opt))
             escaped_args.append(sh.shellquote(opt))
         params['PROGRAM_OPTIONS'] = " ".join(escaped_args)
     else:
         params['PROGRAM_OPTIONS'] = ''
     return params
Esempio n. 4
0
 def _generate_lines(self, session_name, env_exports):
     lines = list()
     lines.append("# RC file generated on %s" % (date.rcf8222date()))
     lines.append("")
     if env_exports:
         lines.append(
             "# Environment settings (these will need to be exported)")
         for (k, v) in env_exports.items():
             lines.append("# export %s=%s" % (k, sh.shellquote(v)))
         lines.append("")
     if ROOT_GO:
         lines.append(
             "# Screen sockets & programs were created/ran as the root user"
         )
         lines.append(
             "# So you will need to run as user root (or sudo) to enter the following sessions"
         )
         lines.append("")
     lines.append("# Session settings")
     lines.append("sessionname %s" % (session_name))
     lines.append(STATUS_BAR_CMD)
     lines.append("screen -t %s bash" % (SESSION_DEF_TITLE))
     lines.append("")
     lines.extend(self._generate_help(session_name, env_exports))
     return lines
Esempio n. 5
0
 def update(self, fn):
     current_vars = RcReader().extract(fn)
     possible_vars = dict()
     possible_vars.update(self._get_general_envs())
     possible_vars.update(self._get_ec2_envs())
     possible_vars.update(self._get_password_envs())
     possible_vars.update(self._get_os_envs())
     possible_vars.update(self._get_euca_envs())
     possible_vars.update(self._get_nova_envs())
     new_vars = dict()
     updated_vars = dict()
     for (key, value) in possible_vars.items():
         if value is not None:
             if key in current_vars and (current_vars.get(key) != value):
                 updated_vars[key] = value
             elif key not in current_vars:
                 new_vars[key] = value
     if new_vars or updated_vars:
         lines = list()
         lines.append("")
         lines.append('# Updated on %s' % (date.rcf8222date()))
         lines.append("")
         if new_vars:
             lines.append('# New stuff')
             lines.extend(self._make_dict_export(new_vars))
             lines.append("")
         if updated_vars:
             lines.append('# Updated stuff')
             lines.extend(self._make_dict_export(updated_vars))
             lines.append("")
         append_contents = utils.joinlinesep(*lines)
         sh.append_file(fn, append_contents)
         return len(new_vars) + len(updated_vars)
     else:
         return 0
Esempio n. 6
0
 def update(self, fn):
     current_vars = RcReader().extract(fn)
     possible_vars = dict()
     possible_vars.update(self._get_general_envs())
     possible_vars.update(self._get_ec2_envs())
     possible_vars.update(self._get_password_envs())
     possible_vars.update(self._get_os_envs())
     possible_vars.update(self._get_euca_envs())
     possible_vars.update(self._get_nova_envs())
     new_vars = dict()
     updated_vars = dict()
     for (key, value) in possible_vars.items():
         if value is not None:
             if key in current_vars and (current_vars.get(key) != value):
                 updated_vars[key] = value
             elif key not in current_vars:
                 new_vars[key] = value
     if new_vars or updated_vars:
         lines = list()
         lines.append("")
         lines.append('# Updated on %s' % (date.rcf8222date()))
         lines.append("")
         if new_vars:
             lines.append('# New stuff')
             lines.extend(self._make_dict_export(new_vars))
             lines.append("")
         if updated_vars:
             lines.append('# Updated stuff')
             lines.extend(self._make_dict_export(updated_vars))
             lines.append("")
         append_contents = utils.joinlinesep(*lines)
         sh.append_file(fn, append_contents)
         return len(new_vars) + len(updated_vars)
     else:
         return 0
Esempio n. 7
0
 def _generate_header(self):
     lines = list()
     lines.append("# Generated on %s by (%s)" % (date.rcf8222date(), sh.getuser()))
     lines.append("")
     lines.append(NV_CONF_DEF_SECTION)
     lines.append("")
     return lines
Esempio n. 8
0
 def _generate_header(self):
     lines = list()
     lines.append("# Generated on %s by (%s)" %
                  (date.rcf8222date(), sh.getuser()))
     lines.append("")
     lines.append(NV_CONF_DEF_SECTION)
     lines.append("")
     return lines
Esempio n. 9
0
def mark_unexecute_file(fn, kvs, comment_start='#'):
    add_lines = list()
    add_lines.append('')
    add_lines.append(comment_start + ' Ran on %s by %s' % (date.rcf8222date(), sh.getuser()))
    add_lines.append(comment_start + ' With data:')
    for (k, v) in kvs.items():
        add_lines.append(comment_start + ' %s => %s' % (k, v))
    sh.append_file(fn, joinlinesep(*add_lines))
    sh.chmod(fn, 0644)
Esempio n. 10
0
def add_header(fn, contents):
    lines = list()
    lines.append('# Adjusted source file %s' % (fn.strip()))
    lines.append("# On %s" % (date.rcf8222date()))
    lines.append("# By user %s, group %s" % (sh.getuser(), sh.getgroupname()))
    lines.append("# Comments may have been removed (TODO: darn python config writer)")
    lines.append("")
    if contents:
        lines.append(contents)
    return utils.joinlinesep(*lines)
Esempio n. 11
0
def mark_unexecute_file(fn, kvs, comment_start='#'):
    add_lines = list()
    add_lines.append('')
    add_lines.append(comment_start + ' Ran on %s by %s' %
                     (date.rcf8222date(), sh.getuser()))
    add_lines.append(comment_start + ' With data:')
    for (k, v) in kvs.items():
        add_lines.append(comment_start + ' %s => %s' % (k, v))
    sh.append_file(fn, joinlinesep(*add_lines))
    sh.chmod(fn, 0644)
Esempio n. 12
0
def add_header(fn, contents):
    lines = list()
    lines.append('# Adjusted source file %s' % (fn.strip()))
    lines.append("# On %s" % (date.rcf8222date()))
    lines.append("# By user %s, group %s" % (sh.getuser(), sh.getgroupname()))
    lines.append("# Comments may have been removed (TODO: darn python config writer)")
    # TODO Maybe use https://code.google.com/p/iniparse/ which seems to preserve comments!
    lines.append("")
    if contents:
        lines.append(contents)
    return utils.joinlinesep(*lines)
Esempio n. 13
0
def add_header(fn, contents):
    lines = list()
    if not fn:
        fn = "???"
    lines.append("# Adjusted source file %s" % (fn.strip()))
    lines.append("# On %s" % (date.rcf8222date()))
    lines.append("# By user %s, group %s" % (sh.getuser(), sh.getgroupname()))
    lines.append("")
    if contents:
        lines.append(contents)
    return utils.joinlinesep(*lines)
Esempio n. 14
0
 def _get_source_config(self, config_fn):
     if config_fn == API_CONF:
         (fn, top) = utils.load_template(self.component_name, API_CONF)
         (_, bottom) = self._get_source_config(API_PASTE_CONF)
         combined = [top, "### Joined here on %s with file %s" % (date.rcf8222date(), API_PASTE_CONF), bottom]
         return (fn, utils.joinlinesep(*combined))
     elif config_fn == REG_CONF:
         (fn, top) = utils.load_template(self.component_name, REG_CONF)
         (_, bottom) = self._get_source_config(REG_PASTE_CONF)
         combined = [top, "### Joined here on %s with file %s" % (date.rcf8222date(), REG_PASTE_CONF), bottom]
         return (fn, utils.joinlinesep(*combined))
     elif config_fn == POLICY_JSON:
         fn = sh.joinpths(self.cfgdir, POLICY_JSON)
         contents = sh.load_file(fn)
         return (fn, contents)
     elif config_fn == LOGGING_CONF:
         fn = sh.joinpths(self.cfgdir, LOGGING_SOURCE_FN)
         contents = sh.load_file(fn)
         return (fn, contents)
     return comp.PythonInstallComponent._get_source_config(self, config_fn)
Esempio n. 15
0
def add_header(fn, contents):
    lines = list()
    lines.append('# Adjusted source file %s' % (fn.strip()))
    lines.append("# On %s" % (date.rcf8222date()))
    lines.append("# By user %s, group %s" % (sh.getuser(), sh.getgroupname()))
    lines.append("# Comments may have been removed (TODO: darn python config writer)")
    # TODO Maybe use https://code.google.com/p/iniparse/ which seems to preserve comments!
    lines.append("")
    if contents:
        lines.append(contents)
    return utils.joinlinesep(*lines)
Esempio n. 16
0
def add_header(fn, contents):
    lines = list()
    if not fn:
        fn = "???"
    lines.append('# Adjusted source file %s' % (fn.strip()))
    lines.append("# On %s" % (date.rcf8222date()))
    lines.append("# By user %s, group %s" % (sh.getuser(), sh.getgroupname()))
    lines.append("")
    if contents:
        lines.append(contents)
    return utils.joinlinesep(*lines)
Esempio n. 17
0
 def _generate_lines(self):
     lines = list()
     lines.append('# Generated on %s' % (date.rcf8222date()))
     lines.append("")
     lines.extend(self._generate_general())
     lines.extend(self._generate_ec2_env())
     lines.extend(self._generate_nova_env())
     lines.extend(self._generate_os_env())
     lines.extend(self._generate_euca_env())
     lines.extend(self._generate_extern_inc())
     lines.extend(self._generate_aliases())
     return lines
Esempio n. 18
0
 def _generate_lines(self):
     lines = list()
     lines.append('# Generated on %s' % (date.rcf8222date()))
     lines.append("")
     lines.extend(self._generate_general())
     lines.extend(self._generate_passwords())
     lines.extend(self._generate_ec2_env())
     lines.extend(self._generate_nova_env())
     lines.extend(self._generate_os_env())
     lines.extend(self._generate_euca_env())
     lines.extend(self._generate_extern_inc())
     lines.extend(self._generate_aliases())
     return lines
Esempio n. 19
0
 def _generate_lines(self, session_name, env_exports):
     lines = list()
     lines.append("# RC file generated on %s" % (date.rcf8222date()))
     lines.append("")
     if env_exports:
         lines.append("# Environment settings (these will need to be exported)")
         for (k, v) in env_exports.items():
             lines.append("# export %s=%s" % (k, sh.shellquote(v)))
         lines.append("")
     if ROOT_GO:
         lines.append("# Screen sockets & programs were created/ran as the root user")
         lines.append("# So you will need to run as user root (or sudo) to enter the following sessions")
         lines.append("")
     lines.append("# Session settings")
     lines.append("sessionname %s" % (session_name))
     lines.append(STATUS_BAR_CMD)
     lines.append("screen -t %s bash" % (SESSION_DEF_TITLE))
     lines.append("")
     lines.extend(self._generate_help(session_name, env_exports))
     return lines
Esempio n. 20
0
 def _get_upstart_conf_params(self, app_pth, program_name, *program_args):
     params = dict()
     if self.cfg.getboolean('upstart', 'respawn'):
         params['RESPAWN'] = "respawn"
     else:
         params['RESPAWN'] = ""
     params['SHORT_NAME'] = program_name
     params['MADE_DATE'] = date.rcf8222date()
     params['START_EVENT'] = self.cfg.get('upstart', 'start_event')
     params['STOP_EVENT'] = self.cfg.get('upstart', 'stop_event')
     params['COMPONENT_START_EVENT'] = self.component_name + START_EVENT_SUFFIX
     params['COMPONENT_STOP_EVENT'] = self.component_name + STOP_EVENT_SUFFIX
     params['PROGRAM_NAME'] = app_pth
     params['AUTHOR'] = settings.PROG_NICE_NAME
     if program_args:
         escaped_args = list()
         for opt in program_args:
             LOG.debug("Current opt: %s" % (opt))
             escaped_args.append(sh.shellquote(opt))
         params['PROGRAM_OPTIONS'] = " ".join(escaped_args)
     else:
         params['PROGRAM_OPTIONS'] = ''
     return params
Esempio n. 21
0
 def generate(self, param_dict=None):
     conf_lines = sorted(self._generate_lines(param_dict))
     full_lines = list()
     full_lines.append("# Generated on %s" % (date.rcf8222date()))
     full_lines.extend(conf_lines)
     return utils.joinlinesep(*full_lines)
Esempio n. 22
0
 def trace(self, cmd, action=None):
     if action is None:
         action = date.rcf8222date()
     line = TRACE_FMT % (cmd, action)
     sh.append_file(self.tracefn, line)
Esempio n. 23
0
 def trace(self, cmd, action=None):
     if action is None:
         action = date.rcf8222date()
     if cmd is not None:
         sh.append_file(self.trace_fn, TRACE_FMT % (cmd, action))
Esempio n. 24
0
 def trace(self, cmd, action=None):
     if action is None:
         action = date.rcf8222date()
     if cmd is not None:
         sh.append_file(self.trace_fn, TRACE_FMT % (cmd, action))
Esempio n. 25
0
 def _generate_header(self):
     lines = list()
     lines.append('# Generated on %s' % (date.rcf8222date()))
     lines.append("")
     return lines