Exemple #1
0
 def _pre_decide(self, hc_sc, cur_out):
     cur_pdc = hc_sc.data
     is_installed = False if cur_out.count("is not installed") else True
     self.log("installed flag from '{}': {}".format(
         cur_out,
         str(is_installed),
     ))
     pack_xml = cur_pdc[0]
     yum_com = {
         "install": "install",
         "upgrade": "update",
         "erase": "erase"
     }.get(cur_pdc.attrib["target_state"])
     options = {
         "install": "--nogpgcheck",
         "upgrade": "--nogpgcheck",
         "erase": "",
     }.get(cur_pdc.attrib["target_state"])
     package_name = self.package_name(pack_xml)
     always_latest = self.get_always_latest(pack_xml)
     if (is_installed and yum_com in ["install", "upgrade"]) or (
             not is_installed and yum_com in ["erase"]):
         self.log("doing nothing, running post_command")
         if is_installed:
             cur_pdc.append(
                 E.main_result(
                     E.stdout(
                         "package {} is installed".format(package_name))))
         else:
             cur_pdc.append(
                 E.main_result(
                     E.stdout("package {} is not installed".format(
                         package_name))))
         return True
     else:
         if not is_installed and yum_com == "update" and always_latest:
             yum_com = "install"
             self.log(
                 "changing yum_com to '{}' (always_latest flag)".format(
                     yum_com), logging_tools.LOG_LEVEL_WARN)
         self.log("starting action '{}'".format(yum_com))
         yum_com = "/usr/bin/yum -y {} {} {}".format(
             yum_com,
             package_name,
             options,
         )
         simple_command(yum_com,
                        short_info="package",
                        done_func=self._command_done,
                        command_stage="main",
                        log_com=self.log,
                        info="handle package",
                        data=cur_pdc)
Exemple #2
0
 def _pre_decide(self, hc_sc, cur_out):
     cur_pdc = hc_sc.data
     is_installed = False if cur_out.count("no packages found") else True
     self.log("installed flag from '{}': {}".format(
         cur_out,
         str(is_installed),
     ))
     pack_xml = cur_pdc[0]
     deb_com = {
         "install": "install",
         "upgrade": "install",
         "erase": "remove"
     }.get(cur_pdc.attrib["target_state"])
     _opts = "-o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold'" \
         " -o Aptitude::Cmdline::ignore-trust-violations=true -y --no-gui -q"
     options = {
         "install": _opts,
         "upgrade": _opts,
         "erase": _opts,
     }.get(cur_pdc.attrib["target_state"])
     package_name = self.package_name(pack_xml, is_debian=True)
     always_latest = self.get_always_latest(pack_xml)
     if (is_installed
             and deb_com in ["install"]) or (not is_installed
                                             and deb_com in ["remove"]):
         self.log("doing nothing, running post_command")
         if is_installed:
             cur_pdc.append(
                 E.main_result(
                     E.stdout(
                         "package {} is installed".format(package_name))))
         else:
             cur_pdc.append(
                 E.main_result(
                     E.stdout("package {} is not installed".format(
                         package_name))))
         return True
     else:
         self.log("starting action '{}'".format(deb_com))
         deb_com = "/usr/bin/aptitude {} {} {}".format(
             options,
             deb_com,
             package_name,
         )
         simple_command(deb_com,
                        short_info="package",
                        done_func=self._command_done,
                        command_stage="main",
                        log_com=self.log,
                        info="handle package",
                        data=cur_pdc)
Exemple #3
0
 def _process_commands(self):
     if simple_command.idle():
         self.register_timer(self._check_commands, 1)
     # check if any commands are pending
     not_init = [
         cur_com for cur_com in self.package_commands
         if not int(cur_com.get("init"))
     ]
     if not_init:
         cur_init = not_init[0]
         cur_init.attrib["init"] = "1"
         # delete pre/post command strings
         pp_attrs = ["pre_command", "post_command"]
         for pp_attr in pp_attrs:
             if pp_attr in cur_init.attrib:
                 del cur_init.attrib[pp_attr]
         self.build_command(cur_init)
         pc_set = False
         for pp_attr in pp_attrs:
             if pp_attr in cur_init.attrib:
                 pc_set = True
                 self.log(" {} is '{}'".format(
                     pp_attr, cur_init.attrib[pp_attr].replace("\n",
                                                               "\\n")))
         # only do something if a pre_command ist set
         if pc_set:
             simple_command(cur_init.attrib["pre_command"],
                            short_info="package",
                            done_func=self._command_done,
                            log_com=self.log,
                            info="install package",
                            command_stage="pre",
                            data=cur_init)
         else:
             cur_init.append(E.main_result(E.info("nothing to do")))
             self.pdc_done(cur_init)
     else:
         # check for pending commands
         self.handle_pending_commands()
Exemple #4
0
 def _pre_decide(self, hc_sc, cur_out):
     _stage = hc_sc.command_stage
     cur_pdc = hc_sc.data
     is_installed = False if cur_out.count("is not installed") else True
     cur_pdc.attrib["pre_installed"] = "1" if is_installed else "0"
     cur_pdc.attrib["pre_zypper_com"] = ""
     pack_xml = cur_pdc[0]
     package_name = self.package_name(pack_xml)
     always_latest = self.get_always_latest(pack_xml)
     self.log(
         "installed flag from '{}': {}, target state is '{}', always_latest is '{}'"
         .format(
             cur_out,
             str(is_installed),
             cur_pdc.attrib["target_state"],
             str(always_latest),
         ))
     zypper_com = {
         "install": "in",
         "upgrade": "up",
         "erase": "rm",
     }.get(cur_pdc.attrib["target_state"])
     # o already installed and cmd == in
     # o already installed and cmd == up and always_latest flag not set
     # o not installed and cmd == rm
     if (is_installed and zypper_com in ["in"]) or (
             is_installed and zypper_com in ["up"]
             and not always_latest) or (not is_installed
                                        and zypper_com in ["rm"]):
         self.log("doing nothing")
         if is_installed:
             cur_pdc.append(
                 E.main_result(
                     E.stdout(
                         "package {} is installed".format(package_name))))
         else:
             cur_pdc.append(
                 E.main_result(
                     E.stdout("package {} is not installed".format(
                         package_name))))
         return True
     else:
         if not is_installed and zypper_com == "up" and always_latest:
             zypper_com = "in"
             self.log(
                 "changing zypper_com to '{}' (always_latest flag)".format(
                     zypper_com), logging_tools.LOG_LEVEL_WARN)
         self.log("starting action '{}'".format(zypper_com))
         cur_pdc.attrib["pre_zypper_com"] = zypper_com
         if pack_xml.attrib["target_repo_name"]:
             _repo_filter = "-r '{}'".format(
                 pack_xml.attrib["target_repo_name"])
         else:
             _repo_filter = ""
         # flags: xml output, non-interactive
         zypper_com = "/usr/bin/zypper -x -n {} {} {} {}".format(
             zypper_com,
             _repo_filter,
             "-f" if (int(cur_pdc.attrib["force_flag"])
                      and zypper_com not in ["rm"]) else "",
             package_name,
         )
         simple_command(zypper_com,
                        short_info="package",
                        done_func=self._command_done,
                        command_stage="main",
                        log_com=self.log,
                        info="handle package",
                        data=cur_pdc)
         return False
Exemple #5
0
 def _command_done(self, hc_sc):
     cur_out = hc_sc.read()
     self.log("hc_com '{}' (stage {}) finished with stat {:d} ({:d} bytes)".
              format(hc_sc.com_str.replace("\n", "\\n"),
                     hc_sc.command_stage, hc_sc.result, len(cur_out)))
     for line_num, line in enumerate(cur_out.split("\n")):
         self.log(" {:3d} {}".format(line_num + 1, line))
     hc_sc.terminate()
     if cur_out.startswith("<?xml") and hc_sc.command_stage == "main":
         try:
             xml_out = etree.fromstring(cur_out)  # @UndefinedVariable
         except:
             self.log(
                 "error parsing XML string ({:d} bytes, first 100: '{}'): {}"
                 .format(len(cur_out), cur_out[:100],
                         process_tools.get_except_info()),
                 logging_tools.LOG_LEVEL_ERROR)
             xml_out = E.stdout(cur_out)
     else:
         # todo: transform output to XML for sending back to server
         # pre and post commands
         xml_out = E.stdout(cur_out)
     # store in xml
     hc_sc.data.append(
         getattr(E, "{}_result".format(hc_sc.command_stage))(xml_out))
     # print "***", hc_sc.command_stage
     if hc_sc.data.tag == "package_device_connection":
         # only send something back for package_device_connection commands
         if hc_sc.command_stage == "pre":
             self.log("pre-command finished, deciding what to do")
             post_present = "post_command" in hc_sc.data.attrib
             if post_present:
                 send_return = self._pre_decide(hc_sc, cur_out.strip())
             else:
                 self.log("nothing to do, sending return")
                 hc_sc.data.append(E.main_result())
                 send_return = True
         elif hc_sc.command_stage == "main":
             send_return = False
             post_present = "post_command" in hc_sc.data.attrib
             if post_present:
                 simple_command(hc_sc.data.attrib["post_command"],
                                short_info="package",
                                done_func=self._command_done,
                                log_com=self.log,
                                info="install package",
                                command_stage="post",
                                data=hc_sc.data)
         elif hc_sc.command_stage == "post":
             # self._post_decide(hc_sc, cur_out.strip())
             send_return = True
         if send_return:
             # remove from package_commands
             self.pdc_done(hc_sc.data)
     else:
         # remove other commands (for instance refresh)
         new_list = [
             cur_com for cur_com in self.package_commands
             if cur_com != hc_sc.data
         ]
         self.package_commands = new_list
         self._process_commands()
     del hc_sc