def probe_sf(self, serverfarm, probe, type_of_operation):
     haproxy_serverfarm = HaproxyBackend()
     haproxy_serverfarm.name = serverfarm['name']
     self.del_lines = ['option httpchk', 'option ssl-hello-chk',
                                                     'http-check expect']
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                     self.configfilename))
     remote = RemoteConfig(self.device_ref, self.localpath,
                           self.remotepath, self.configfilename)
     remote.get_config()
     if type_of_operation == 'del':
         config_file.del_lines_from_backend_block(haproxy_serverfarm,
                                                             self.del_lines)
     elif type_of_operation == 'add':
         pr_type = probe['type'].lower()
         if pr_type not in ('http', 'https', 'tcp'):
             logger.debug('[HAPROXY] unsupported probe type %s, exit',
                                                                 pr_type)
             return
         if pr_type == "http":
             haproxy_serverfarm = HaproxyBackend()
             haproxy_serverfarm.name = serverfarm['name']
             self.option_httpchk = "option httpchk"
             requestMethodType = probe.get('requestMethodType', '')
             requestHTTPurl = probe.get('requestHTTPurl', '')
             expectRegExp = probe.get('expectRegExp', '')
             minExpectStatus = probe.get('minExpectStatus',  '')
             if requestMethodType != "":
                 self.option_httpchk = "%s %s " % (self.option_httpchk,
                                                         requestMethodType)
             else:
                 self.option_httpchk = "%s GET" % self.option_httpchk
             if requestHTTPurl != "":
                 self.option_httpchk = "%s %s" % (self.option_httpchk,
                                                             requestHTTPurl)
             else:
                 self.option_httpchk = "%s /" % self.option_httpchk
             if expectRegExp != "":
                 self.http_check = "http-check expect rstring %s" % (
                                                             expectRegExp,)
             elif minExpectStatus != "":
                 self.http_check = "http-check expect status %s" % (
                                                         minExpectStatus,)
             self.new_lines = [self.option_httpchk, self.http_check]
         elif pr_type == "tcp":
             self.new_lines = ["option httpchk"]
         elif pr_type == "https":
             self.new_lines = ["option ssl-hello-chk"]
         config_file.del_lines_from_backend_block(haproxy_serverfarm,
                 self.del_lines)
         config_file.add_lines_to_backend_block(haproxy_serverfarm,
                 self.new_lines)
     remote.put_config()
 def delete_server_farm(self, serverfarm):
     if not bool(serverfarm['name']):
         logger.error('[HAPROXY] Serverfarm name is empty')
         return 'SERVER FARM NAME ERROR'
     haproxy_serverfarm = HaproxyBackend()
     haproxy_serverfarm.name = serverfarm['name']
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                             self.configfilename))
     remote = RemoteConfig(self.device_ref, self.localpath,
                           self.remotepath, self.configfilename)
     remote.get_config()
     config_file.delete_block(haproxy_serverfarm)
     remote.put_config()
class TestHaproxyDriverRemoteConfig(unittest.TestCase):
    def setUp(self):
        self.remote_config = RemoteConfig(device_fake, "/tmp", "/etc/haproxy", "haproxy.conf")
        self.remote_config.ssh = Mock()

    def test_get_config(self):
        self.assertTrue(self.remote_config.get_config())

    def test_put_config(self):
        self.assertTrue(self.remote_config.put_config())

    def test_validate_config_bad(self):
        file_channel = MagicMock(spec=file)
        self.remote_config.ssh.exec_command.return_value = [file_channel, file_channel, file_channel]
        self.assertFalse(self.remote_config.validate_config())
 def delete_real_server_from_server_farm(self, serverfarm, rserver):
     haproxy_serverfarm = HaproxyBackend()
     haproxy_serverfarm.name = serverfarm['name']
     haproxy_rserver = HaproxyRserver()
     haproxy_rserver.name = rserver['name']
     #Modify remote config file, check and restart remote haproxy
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                             self.configfilename))
     remote = RemoteConfig(self.device_ref, self.localpath,
                           self.remotepath, self.configfilename)
     logger.debug('[HAPROXY] Deleting rserver %s in the'
                  'backend block %s' %
                  (haproxy_rserver.name, haproxy_serverfarm.name))
     remote.get_config()
     config_file.del_rserver_from_backend_block(haproxy_serverfarm,
                                            haproxy_rserver)
     remote.put_config()
 def add_real_server_to_server_farm(self, serverfarm, rserver):
     haproxy_serverfarm = HaproxyBackend()
     haproxy_serverfarm.name = serverfarm['name']
     haproxy_rserver = HaproxyRserver()
     haproxy_rserver.name = rserver['name']
     haproxy_rserver.weight = rserver['weight']
     haproxy_rserver.address = rserver['address']
     haproxy_rserver.port = rserver['port']
     haproxy_rserver.maxconn = rserver['maxCon']
     #Modify remote config file, check and restart remote haproxy
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                     self.configfilename))
     remote = RemoteConfig(self.device_ref, self.localpath,
                           self.remotepath, self.configfilename)
     logger.debug('[HAPROXY] Creating rserver %s in the'
                  'backend block %s' %
                  (haproxy_rserver.name, haproxy_serverfarm.name))
     remote.get_config()
     config_file.add_rserver_to_backend_block(haproxy_serverfarm,
                                          haproxy_rserver)
     remote.put_config()
    def create_server_farm(self, serverfarm, predictor):
        if not bool(serverfarm['name']):
            logger.error('[HAPROXY] Serverfarm name is empty')
            return 'SERVERFARM FARM NAME ERROR'
        haproxy_serverfarm = HaproxyBackend()
        haproxy_serverfarm.name = serverfarm['name']

        if predictor['type'] == 'RoundRobin':
            haproxy_serverfarm.balance = 'roundrobin'
        elif predictor['type'] == 'LeastConnections':
            haproxy_serverfarm.balance = 'leastconn'
        elif predictor['type'] == 'HashAddrPredictor':
            haproxy_serverfarm.balance = 'source'
        elif predictor['type'] == 'HashURL':
            haproxy_serverfarm.balance = 'uri'

        config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                                self.configfilename))
        remote = RemoteConfig(self.device_ref, self.localpath,
                              self.remotepath, self.configfilename)
        remote.get_config()
        config_file.add_backend(haproxy_serverfarm)
        remote.put_config()
 def operationWithRServer(self, serverfarm, rserver,
                          type_of_operation):
     haproxy_rserver = HaproxyRserver()
     haproxy_rserver.name = rserver['name']
     haproxy_serverfarm = HaproxyBackend()
     haproxy_serverfarm.name = serverfarm['name']
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                     self.configfilename))
     remote_config = RemoteConfig(self.device_ref, self.localpath,
                                  self.remotepath, self.configfilename)
     remote_socket = RemoteSocketOperation(self.device_ref,
                                     haproxy_serverfarm, haproxy_rserver,
                                     self.interface, self.haproxy_socket)
     remote_config.get_config()
     if type_of_operation == 'suspend':
         config_file.enable_disable_reserver_in_backend_block(
                          haproxy_serverfarm, haproxy_rserver, 'disable')
         remote_socket.suspend_server()
     elif type_of_operation == 'activate':
         config_file.enable_disable_reserver_in_backend_block(
                          haproxy_serverfarm, haproxy_rserver, 'enable')
         remote_socket.activate_server()
     remote_config.put_config()
 def delete_virtual_ip(self, virtualserver):
     logger.debug('[HAPROXY] delete VIP')
     if not bool(virtualserver['name']):
         logger.error('[HAPROXY] Virtualserver name is empty')
         return 'VIRTUALSERVER NAME ERROR'
     haproxy_virtualserver = HaproxyFronted()
     haproxy_virtualserver.name = virtualserver['name']
     haproxy_virtualserver.bind_address = virtualserver['address']
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                     self.configfilename))
     remote = RemoteConfig(self.device_ref, self.localpath,
                           self.remotepath, self.configfilename)
     #Check ip for using in the another frontend
     if not config_file.find_string_in_the_block('frontend',
         haproxy_virtualserver.bind_address):
         logger.debug('[HAPROXY] ip %s does not using in the other '
                              'frontend, delete it from remote interface' %
                               haproxy_virtualserver.bind_address)
         remote_interface = RemoteInterface(self.device_ref,
                                            haproxy_virtualserver)
         remote_interface.del_ip()
     remote.get_config()
     config_file.delete_block(haproxy_virtualserver)
     remote.put_config()
 def create_virtual_ip(self, virtualserver, serverfarm):
     if not bool(virtualserver['name']):
         logger.error('[HAPROXY] Virtualserver name is empty')
         return 'VIRTUALSERVER NAME ERROR'
     haproxy_virtualserver = HaproxyFronted()
     haproxy_virtualserver.name = virtualserver['name']
     haproxy_virtualserver.bind_address = virtualserver['address']
     haproxy_virtualserver.bind_port = virtualserver['port']
     haproxy_serverfarm = HaproxyBackend()
     haproxy_serverfarm.name = serverfarm['name']
     logger.debug('[HAPROXY] create VIP %s' % haproxy_serverfarm.name)
     #Add new IP address
     remote_interface = RemoteInterface(self.device_ref,
                                        haproxy_virtualserver)
     remote_interface.add_ip()
     #Modify remote config file, check and restart remote haproxy
     config_file = HaproxyConfigFile('%s/%s' % (self.localpath,
                                     self.configfilename))
     remote = RemoteConfig(self.device_ref, self.localpath,
                           self.remotepath, self.configfilename)
     remote.get_config()
     config_file.add_frontend(haproxy_virtualserver, haproxy_serverfarm)
     remote.put_config()
     remote.validate_config()
 def setUp(self):
     self.remote_config = RemoteConfig(device_fake, "/tmp", "/etc/haproxy", "haproxy.conf")
     self.remote_config.ssh = Mock()
 def setUp(self):
     self.remote_config = RemoteConfig(device_fake, '/tmp',
                     '/etc/haproxy', 'haproxy.conf')
     self.remote_config.ssh = Mock()