def run(self):
        # make sure no other haproxy process running
        if self.__status == HaproxyProcess.RUNNING or self.check_haproxy_process(
        ) == 0:
            raise ServoError("haproxy already running")

        haproxy_cmd = '%s -f %s -p %s -V -C %s -D' % (
            self.__haproxy_bin, self.__conf_file, self.__pid_path, RUN_ROOT)

        if servo.run_as_sudo(haproxy_cmd) != 0:
            raise ServoError("failed to launch haproxy process")

        self.__status = HaproxyProcess.RUNNING
 def __init__(self,
              haproxy_bin='/usr/local/sbin/haproxy',
              conf_file=None,
              pid_path=None):
     self.__conf_file = conf_file
     self.__pid_path = pid_path
     self.__haproxy_bin = haproxy_bin
     if not os.path.exists(haproxy_bin):
         raise ServoError("%s not found in the system" % haproxy_bin)
     if not os.path.exists(conf_file):
         raise ServoError("%s not found in the system" % conf_file)
     if self.check_haproxy_process() == 0:
         self.__status = HaproxyProcess.RUNNING
     else:
         self.__status = HaproxyProcess.TERMINATED
 def get_pids(self):
     proc = subprocess.Popen(['ps', '-C', 'haproxy'],
                             stdout=subprocess.PIPE)
     pids = []
     if not (proc and proc.stdout):
         raise ServoError("Failed to obtain haproxy's process id using ps")
     else:
         while True:
             line = proc.stdout.readline()
             if not line or len(line) <= 0:
                 break
             elif line.find('haproxy') >= 0:
                 tokens = line.split()
                 pids.append(tokens[0])
     if not pids:
         raise ServoError("No pid is found for haproxy process")
     return pids
Exemple #4
0
 def __init__(self,
              haproxy_bin='/usr/local/sbin/haproxy',
              conf_file=None,
              pid_path=None,
              use_sudo=False):
     self.__conf_file = conf_file
     self.__pid_path = pid_path
     self.__haproxy_bin = haproxy_bin
     self.__use_sudo = use_sudo
     if not os.path.exists(haproxy_bin):
         raise ServoError("%s not found in the system" % haproxy_bin)
     if not os.path.exists(conf_file):
         raise ServoError("%s not found in the system" % conf_file)
     if subprocess.call('ps ax | grep haproxy | grep -v grep',
                        shell=True) == 0:
         self.__status = HaproxyProcess.RUNNING
     else:
         self.__status = HaproxyProcess.TERMINATED
    def terminate(self):
        kill_cmd = 'kill -9 %s' % self.get_pid()

        if os.path.isfile(self.__pid_path):
            servo.run_as_sudo(kill_cmd)

        if self.check_haproxy_process() == 0:
            raise ServoError("haproxy still running")
        self.__status = HaproxyProcess.TERMINATED
    def terminate(self):
        pids = self.get_pids()

        for pid in pids:
            kill_cmd = 'kill -9 %s' % pid
            servo.run_as_sudo(kill_cmd)

        if self.check_haproxy_process() == 0:
            raise ServoError("haproxy still running")

        self.__status = HaproxyProcess.TERMINATED
Exemple #7
0
    def run(self):
        # make sure no other haproxy process running
        if self.__status == HaproxyProcess.RUNNING or subprocess.call(
                'ps ax | grep haproxy | grep -v grep', shell=True) == 0:
            raise ServoError("haproxy already running")

        haproxy_cmd = '%s -f %s -p %s -V -C %s -D'
        haproxy_args = [
            self.__haproxy_bin, self.__conf_file, self.__pid_path, RUN_ROOT
        ]

        # Insert sudo command if we are using sudo.  The '-n' argument is
        # included so if we are prompted for a password, the command will
        # return an error code instead of prompting us.
        if self.__use_sudo:
            haproxy_args.insert(0, SUDO_BIN)
            haproxy_cmd = '%s -n -- ' + haproxy_cmd

        if subprocess.call(haproxy_cmd % tuple(haproxy_args), shell=True) != 0:
            raise ServoError("failed to launch haproxy process")
        self.__status = HaproxyProcess.RUNNING
    def restart(self):
        if self.check_haproxy_process() != 0:
            servo.log.warning('on restart, no running haproxy process found')

        haproxy_cmd = '%s -f %s -p %s -V -C %s -D -sf %s)' % (
            self.__haproxy_bin, self.__conf_file, self.__pid_path, RUN_ROOT,
            self.get_pid())

        if servo.run_as_sudo(haproxy_cmd) != 0:
            raise ServoError("failed to restart haproxy process")

        self.__status = HaproxyProcess.RUNNING
Exemple #9
0
    def terminate(self):
        kill_args = [self.__pid_path]
        kill_cmd = 'kill -9 $(<%s)'

        if self.__use_sudo:
            kill_args.insert(0, SUDO_BIN)
            kill_cmd = '%s -n -- ' + kill_cmd

        if os.path.isfile(self.__pid_path):
            subprocess.call(kill_cmd % tuple(kill_args), shell=True)
        if subprocess.call('ps ax | grep haproxy | grep -v grep',
                           shell=True) == 0:
            raise ServoError("haproxy still running")
        self.__status = HaproxyProcess.TERMINATED
Exemple #10
0
    def restart(self):
        if subprocess.call('ps ax | grep haproxy | grep -v grep',
                           shell=True) != 0:
            servo.log.warning('on restart, no running haproxy process found')

        haproxy_cmd = '%s -f %s -p %s -V -C %s -D -sf $(<%s)'
        haproxy_args = [
            self.__haproxy_bin, self.__conf_file, self.__pid_path, RUN_ROOT,
            self.__pid_path
        ]

        # Insert sudo command if we are using sudo.  The '-n' argument is
        # included so if we are prompted for a password, the command will
        # return an error code instead of prompting us.
        if self.__use_sudo:
            haproxy_args.insert(0, SUDO_BIN)
            haproxy_cmd = '%s -n -- ' + haproxy_cmd

        if subprocess.call(haproxy_cmd % tuple(haproxy_args), shell=True) != 0:
            raise ServoError("failed to restart haproxy process")
        self.__status = HaproxyProcess.RUNNING