Example #1
0
    def _is_orphaned(self):
        parent_pid = os.getppid()
        if parent_pid in (1, None):
            return True

        if not os.path.isfile(conf.get_agent_pid_file_path()):
            return True

        return fileutil.read_file(conf.get_agent_pid_file_path()) != ustr(parent_pid)
Example #2
0
    def _is_orphaned(self):
        parent_pid = os.getppid()
        if parent_pid in (1, None):
            return True

        if not os.path.isfile(conf.get_agent_pid_file_path()):
            return True

        return fileutil.read_file(conf.get_agent_pid_file_path()) != ustr(parent_pid)
Example #3
0
    def _foreach_legacy_cgroup(operation):
        """
        Previous versions of the daemon (2.2.31-2.2.40) wrote their PID to /sys/fs/cgroup/{cpu,memory}/WALinuxAgent/WALinuxAgent;
        starting from version 2.2.41 we track the agent service in walinuxagent.service instead of WALinuxAgent/WALinuxAgent. Also,
        when running under systemd, the PIDs should not be explicitly moved to the cgroup filesystem. The older daemons would
        incorrectly do that under certain conditions.

        This method checks for the existence of the legacy cgroups and, if the daemon's PID has been added to them, executes the
        given operation on the cgroups. After this check, the method attempts to remove the legacy cgroups.

        :param operation:
            The function to execute on each legacy cgroup. It must take 2 arguments: the controller and the daemon's PID
        """
        legacy_cgroups = []
        for controller in ['cpu', 'memory']:
            cgroup = os.path.join(CGROUPS_FILE_SYSTEM_ROOT, controller, "WALinuxAgent", "WALinuxAgent")
            if os.path.exists(cgroup):
                logger.info('Found legacy cgroup {0}', cgroup)
                legacy_cgroups.append((controller, cgroup))

        try:
            for controller, cgroup in legacy_cgroups:
                procs_file = os.path.join(cgroup, "cgroup.procs")

                if os.path.exists(procs_file):
                    procs_file_contents = fileutil.read_file(procs_file).strip()
                    daemon_pid = fileutil.read_file(get_agent_pid_file_path()).strip()

                    if daemon_pid in procs_file_contents:
                        operation(controller, daemon_pid)
        finally:
            for _, cgroup in legacy_cgroups:
                logger.info('Removing {0}', cgroup)
                shutil.rmtree(cgroup, ignore_errors=True)
        return len(legacy_cgroups)
Example #4
0
    def check_pid(self):
        """Check whether daemon is already running"""
        pid = None
        pid_file = conf.get_agent_pid_file_path()
        if os.path.isfile(pid_file):
            pid = fileutil.read_file(pid_file)

        if self.osutil.check_pid_alive(pid):
            logger.info("Daemon is already running: {0}", pid)
            sys.exit(0)

        fileutil.write_file(pid_file, ustr(os.getpid()))
Example #5
0
    def check_pid(self):
        """Check whether daemon is already running"""
        pid = None
        pid_file = conf.get_agent_pid_file_path()
        if os.path.isfile(pid_file):
            pid = fileutil.read_file(pid_file)

        if self.osutil.check_pid_alive(pid):
            logger.info("Daemon is already running: {0}", pid)
            sys.exit(0)

        fileutil.write_file(pid_file, ustr(os.getpid()))
Example #6
0
    def _get_pid_files(self):
        pid_file = conf.get_agent_pid_file_path()
        
        pid_dir = os.path.dirname(pid_file)
        pid_name = os.path.basename(pid_file)
        
        pid_re = re.compile("(\d+)_{0}".format(re.escape(pid_name)))
        pid_files = [int(pid_re.match(f).group(1)) for f in os.listdir(pid_dir) if pid_re.match(f)]
        pid_files.sort()

        pid_index = -1 if len(pid_files) <= 0 else pid_files[-1]
        previous_pid_file = None \
                        if pid_index < 0 \
                        else os.path.join(pid_dir, "{0}_{1}".format(pid_index, pid_name))
        pid_file = os.path.join(pid_dir, "{0}_{1}".format(pid_index+1, pid_name))
        return previous_pid_file, pid_file
Example #7
0
    def _get_pid_files(self):
        pid_file = conf.get_agent_pid_file_path()
        
        pid_dir = os.path.dirname(pid_file)
        pid_name = os.path.basename(pid_file)
        
        pid_re = re.compile("(\d+)_{0}".format(re.escape(pid_name)))
        pid_files = [int(pid_re.match(f).group(1)) for f in os.listdir(pid_dir) if pid_re.match(f)]
        pid_files.sort()

        pid_index = -1 if len(pid_files) <= 0 else pid_files[-1]
        previous_pid_file = None \
                        if pid_index < 0 \
                        else os.path.join(pid_dir, "{0}_{1}".format(pid_index, pid_name))
        pid_file = os.path.join(pid_dir, "{0}_{1}".format(pid_index+1, pid_name))
        return previous_pid_file, pid_file
Example #8
0
 def get_daemon_pid():
     return int(fileutil.read_file(get_agent_pid_file_path()).strip())
Example #9
0
 def _get_pid_parts(self):
     pid_file = conf.get_agent_pid_file_path()
     pid_dir = os.path.dirname(pid_file)
     pid_name = os.path.basename(pid_file)
     pid_re = re.compile("(\d+)_{0}".format(re.escape(pid_name)))
     return pid_dir, pid_name, pid_re
Example #10
0
 def _get_pid_parts(self):
     pid_file = conf.get_agent_pid_file_path()
     pid_dir = os.path.dirname(pid_file)
     pid_name = os.path.basename(pid_file)
     pid_re = re.compile("(\d+)_{0}".format(re.escape(pid_name)))
     return pid_dir, pid_name, pid_re