Exemple #1
0
    def _execSupervisorServiceAction(self, service_name, action):
        """Perform the action with the run-supervisorctl command.

        :return: tuple (exit code, std-output, std-error)
        """
        env = get_env_with_bytes_locale()
        cmd = os.path.join(snappy.get_snap_path(), "bin", "run-supervisorctl")
        cmd = (cmd, action, service_name)
        return self._execCmd(cmd, env)
Exemple #2
0
 def test__sets_LANG_and_LC_ALL(self):
     self.assertThat(
         get_env_with_bytes_locale({}),
         Equals({
             b"LANG": b"C.UTF-8",
             b"LANGUAGE": b"C.UTF-8",
             b"LC_ALL": b"C.UTF-8",
         }),
     )
Exemple #3
0
 def test__defaults_to_process_environment(self):
     name = factory.make_name("name")
     value = factory.make_name("value")
     with EnvironmentVariable(name, value):
         self.assertThat(
             get_env_with_bytes_locale(),
             ContainsDict(
                 {name.encode("ascii"): Equals(value.encode("ascii"))}),
         )
Exemple #4
0
 def test__overwrites_LANGUAGE(self):
     self.assertThat(
         get_env_with_bytes_locale(
             {b"LANGUAGE": factory.make_name("LANGUAGE").encode("ascii")}),
         Equals({
             b"LANG": b"C.UTF-8",
             b"LANGUAGE": b"C.UTF-8",
             b"LC_ALL": b"C.UTF-8",
         }),
     )
Exemple #5
0
    def _execSystemDServiceAction(self, service_name, action, extra_opts=None):
        """Perform the action with the systemctl command.

        :return: tuple (exit code, std-output, std-error)
        """
        env = get_env_with_bytes_locale()
        cmd = ["sudo", "--non-interactive", "systemctl", action]
        if extra_opts is not None:
            cmd.extend(extra_opts)
        cmd.append(service_name)
        return self._execCmd(cmd, env)
Exemple #6
0
 def test__passes_other_variables_through(self):
     basis = {
         factory.make_name("name").encode("ascii"):
         (factory.make_name("value").encode("ascii"))
         for _ in range(5)
     }
     expected = basis.copy()
     expected[b"LANG"] = expected[b"LC_ALL"] = expected[b"LANGUAGE"] = (
         b"C.UTF-8")
     observed = get_env_with_bytes_locale(basis)
     self.assertThat(observed, Equals(expected))
Exemple #7
0
 def test__removes_other_LC_variables(self):
     self.assertThat(
         get_env_with_bytes_locale({
             name.encode("ascii"): factory.make_name(name).encode("ascii")
             for name in LC_VAR_NAMES
         }),
         Equals({
             b"LANG": b"C.UTF-8",
             b"LANGUAGE": b"C.UTF-8",
             b"LC_ALL": b"C.UTF-8",
         }),
     )
Exemple #8
0
 def test___execSystemDServiceAction_calls_systemctl(self):
     service_monitor = self.make_service_monitor()
     service_name = factory.make_name("service")
     action = factory.make_name("action")
     mock_getProcessOutputAndValue = self.patch(
         service_monitor_module, "getProcessOutputAndValue")
     mock_getProcessOutputAndValue.return_value = succeed((b"", b"", 0))
     yield service_monitor._execSystemDServiceAction(service_name, action)
     cmd = ["sudo", "--non-interactive", "systemctl", action, service_name]
     self.assertThat(
         mock_getProcessOutputAndValue, MockCalledOnceWith(
             # The environment contains LC_ALL and LANG too.
             cmd[0], cmd[1:], env=get_env_with_bytes_locale()))
Exemple #9
0
    def _execSupervisorServiceAction(self, service_name, action):
        """Perform the action with the run-supervisorctl command.

        :return: tuple (exit code, std-output, std-error)
        """
        env = get_env_with_bytes_locale()
        cmd = os.path.join(snappy.get_snap_path(), "bin", "run-supervisorctl")
        cmd = (cmd, action, service_name)

        def decode(result):
            out, err, code = result
            return code, out.decode("utf-8"), err.decode("utf-8")

        d = getProcessOutputAndValue(cmd[0], cmd[1:], env=env)
        return d.addCallback(decode)
Exemple #10
0
    def _execSystemDServiceAction(self, service_name, action, extra_opts=None):
        """Perform the action with the systemctl command.

        :return: tuple (exit code, std-output, std-error)
        """
        env = get_env_with_bytes_locale()
        cmd = ["sudo", "--non-interactive", "systemctl", action]
        if extra_opts is not None:
            cmd.extend(extra_opts)
        cmd.append(service_name)

        def decode(result):
            out, err, code = result
            return code, out.decode("utf-8"), err.decode("utf-8")

        d = getProcessOutputAndValue(cmd[0], cmd[1:], env=env)
        return d.addCallback(decode)
Exemple #11
0
 def test___execSupervisorServiceAction_calls_supervisorctl(self):
     snap_path = factory.make_name("path")
     self.patch(snappy, "get_snap_path").return_value = snap_path
     service_monitor = self.make_service_monitor()
     service_name = factory.make_name("service")
     action = factory.make_name("action")
     mock_getProcessOutputAndValue = self.patch(
         service_monitor_module, "getProcessOutputAndValue")
     mock_getProcessOutputAndValue.return_value = succeed((b"", b"", 0))
     yield service_monitor._execSupervisorServiceAction(
         service_name, action)
     cmd = os.path.join(snap_path, 'bin', 'run-supervisorctl')
     cmd = cmd, action, service_name
     self.assertThat(
         mock_getProcessOutputAndValue, MockCalledOnceWith(
             # The environment contains LC_ALL and LANG too.
             cmd[0], cmd[1:], env=get_env_with_bytes_locale()))
Exemple #12
0
    def _execSupervisorServiceAction(self,
                                     service_name,
                                     action,
                                     extra_opts=None):
        """Perform the action with the run-supervisorctl command.

        :return: tuple (exit code, std-output, std-error)
        """
        env = get_env_with_bytes_locale()

        cmd = os.path.join(snap.SnapPaths.from_environ().snap, "bin",
                           "run-supervisorctl")

        # supervisord doesn't support native kill like systemd. Emulate this
        # behaviour by getting the PID of the process and then killing the PID.
        if action == "kill":

            def _kill_pid(result):
                exit_code, stdout, _ = result
                if exit_code != 0:
                    return result
                try:
                    pid = int(stdout.strip())
                except ValueError:
                    pid = 0
                if pid == 0:
                    # supervisorctl returns 0 when the process is already dead
                    # or we where not able to get the actual pid. Nothing to
                    # do, as its already dead.
                    return 0, "", ""
                cmd = ("kill", )
                if extra_opts:
                    cmd += extra_opts
                cmd += ("%s" % pid, )
                return self._execCmd(cmd, env)

            d = self._execCmd((cmd, "pid", service_name), env)
            d.addCallback(_kill_pid)
            return d

        cmd = (cmd, action)
        if extra_opts is not None:
            cmd += extra_opts
        cmd += (service_name, )
        return self._execCmd(cmd, env)