Esempio n. 1
0
class SyncClientBase(object):

    def __init__(self, logger, server_ip):
        self.server_ip = server_ip
        self.logger = logger
        self.rpc = AES_RPC_Client(conf.KEY, logger)

    def connect(self, timeout=None):
        addr = (self.server_ip, conf.INF_PORT)
        if timeout:
            self.rpc.set_timeout(timeout)
        return self.rpc.connect(addr)

    def close(self):
        self.rpc.close()

    def rsync(self, source, dest=None, speed=None, use_zip=False, log_file=None):
        """ speed is in Mbit/s """
        options = ("-avW", "--inplace", )
        if os.path.isdir(source):
            if source[-1] != "/":
                source += "/"
            if dest and dest[-1] != "/":
                dest += "/"
            if dest:
                options += ("--delete", )
        if log_file:
            options += ('--log-file', log_file)
        if not dest:
            dest = ""
        if speed:
            assert isinstance(speed, (int, float))
            options += ("--bwlimit", str(int(speed * 1000 / 8)), )
            use_zip = True
        if use_zip:
            options += ("-z", )

        cmd = ("rsync", ) + options + \
            (source,
             "rsync://%s:%s/%s/%s" % (self.server_ip,
                                      conf.RSYNC_PORT, RSYNC_SERVER_NAME, dest)
             )
        print cmd
        p = subprocess.Popen(cmd, stderr=subprocess.PIPE, close_fds=True)
        retcode = p.wait()
        if retcode:
            stderr = "\n".join(p.stderr.readlines())
        else:
            stderr = None
        p.stderr.close()
        return retcode, stderr
def main():

    key = "sddjou34324523432gh45t452354"
    server = AES_RPC_Server( 
            SERVER_ADDR,
            client_keys={'127.0.0.1': key},
            logger=Log("server", config=conf),
            )
    server.add_view(View())
    server.start(5)
    print "server started"
    def __run_server():
        print "run server"
        server.loop()
        return
    th = threading.Thread(target=__run_server)
    th.setDaemon(1)
    th.start()
    time.sleep(1)
    print "starting client"
    client = AES_RPC_Client(key, Log("client", config=conf))
    client.set_timeout(5)
    client.connect(SERVER_ADDR)
    print "connected"
    ret = client.call("foo", "aaa", arg2="bbb")
    assert ret == ("aaa", "bbb", {'dd': {'ee': 1}})
    val = AttrWrapper.wrap(ret)
    print "ret[2]['dd']['ee']", val[2].dd.ee
    print "foo => ret"
    try:
        client.call("bar")
    except Exception, e:
        print "bar", e
Esempio n. 3
0
 def __init__(self):
     self.logger = Log("proxy", config=config)
     self.logger_rpc = Log("proxy_rpc", config=config)
     self.output_path = config.NGINX_CONF_PATH
     self.nginx_reload_cmd = config.NGINX_RELOAD_CMD
     self.rpc = AES_RPC_Client(_private.KEY, self.logger_rpc)
Esempio n. 4
0
class VpsProxy(object):

    NGINX_TEMPLATE = """
server {
    listen      80;
    server_name %(host)s *.%(host)s;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header HOST $host.%(suffix)s;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://%(ip)s;
    }
}
"""

    
    def __init__(self):
        self.logger = Log("proxy", config=config)
        self.logger_rpc = Log("proxy_rpc", config=config)
        self.output_path = config.NGINX_CONF_PATH
        self.nginx_reload_cmd = config.NGINX_RELOAD_CMD
        self.rpc = AES_RPC_Client(_private.KEY, self.logger_rpc)

    def start(self):
        self.is_running = True

    def stop(self):
        self.is_running = False

    def loop(self):
        while self.is_running:
            self.gen_config()
            time.sleep(conf.RUN_INV)

    def reload_nginx(self):
        subprocess.check_output(self.nginx_reload_cmd)
        self.logger.info("nginx reloaded")
        print "nginx reloaded"

    def gen_config(self, force=False):
        domain_list = None
        try:
            self.rpc.connect(_private.SAAS_ADDR)
            try:
                domain_list = self.rpc.call("proxy_domain_list")
            finally:
                self.rpc.close()
        except Exception, e:
            self.logger.exception(e)
            print traceback.print_exc()
            return
        conf = []
        for i in domain_list:
            conf.append(self.NGINX_TEMPLATE % {'host':i["domain"], 'ip':i["ip"], 'suffix': _private.PROXY_DOMAIN_SUFFIX})
        content = "".join(conf)
        try:
            if not force and os.path.exists(self.output_path) and _md5_file(self.output_path) == _md5(content):
                print "skip the same"
                return
            f = open(self.output_path, "w")
            try:
                f.write(content)
            finally:
                f.close()
            self.logger.info("conf generated")
            self.reload_nginx()
        except Exception, e:
            self.logger.exception(e)
            print traceback.print_exc()
Esempio n. 5
0
 def __init__(self, logger, server_ip):
     self.server_ip = server_ip
     self.logger = logger
     self.rpc = AES_RPC_Client(conf.KEY, logger)
Esempio n. 6
0
File: saas_rpc.py Progetto: 42qu/vps
 def __init__(self, host_id, logger=None):
     self.rpc = AES_RPC_Client(conf.KEY, logger)
     self.host_id = int(host_id)
Esempio n. 7
0
File: saas_rpc.py Progetto: 42qu/vps
class SAAS_Client(object):

    def __init__(self, host_id, logger=None):
        self.rpc = AES_RPC_Client(conf.KEY, logger)
        self.host_id = int(host_id)

    def connect(self):
        return self.rpc.connect((conf.SAAS_HOST, conf.SAAS_PORT + 1))

    def close(self):
        self.rpc.close()

    def doing(self, cmd, vm_id):
        return self.rpc.call("doing", self.host_id, cmd, vm_id)

    def todo(self, cmd):
        return self.rpc.call("todo", self.host_id, cmd)

    def alarm(self, msg):
        self.rpc.call('alarm', msg)

    def host_list(self):
        return AttrWrapper.wrap(self.rpc.call("host_list"))

    def done(self, cmd, vm_id, state, message):
        return self.rpc.call("done", self.host_id, cmd, vm_id, int(state), str(message))

    def host_refresh(self, hd_remain, ram_remain, hd_total=0, ram_total=0):
        return self.rpc.call("host_refresh", self.host_id, int(hd_remain), int(ram_remain), int(hd_total), int(ram_total))

    def vps(self, vm_id):
        return AttrWrapper(self.rpc.call("vps", vm_id))

    def migrate_task(self, vm_id):
        return AttrWrapper(self.rpc.call("migrate_task", vm_id))
Esempio n. 8
0
 def __init__(self):
     self.logger = Log("proxy", config=config)
     self.logger_rpc = Log("proxy_rpc", config=config)
     self.output_path = config.NGINX_CONF_PATH
     self.nginx_reload_cmd = config.NGINX_RELOAD_CMD
     self.rpc = AES_RPC_Client(_private.KEY, self.logger_rpc)
Esempio n. 9
0
class VpsProxy(object):

    NGINX_TEMPLATE = """
server {
    listen      80;
    server_name %(host)s *.%(host)s;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header HOST $host.%(suffix)s;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://%(ip)s;
    }
}
"""

    def __init__(self):
        self.logger = Log("proxy", config=config)
        self.logger_rpc = Log("proxy_rpc", config=config)
        self.output_path = config.NGINX_CONF_PATH
        self.nginx_reload_cmd = config.NGINX_RELOAD_CMD
        self.rpc = AES_RPC_Client(_private.KEY, self.logger_rpc)

    def start(self):
        self.is_running = True

    def stop(self):
        self.is_running = False

    def loop(self):
        while self.is_running:
            self.gen_config()
            time.sleep(conf.RUN_INV)

    def reload_nginx(self):
        subprocess.check_output(self.nginx_reload_cmd)
        self.logger.info("nginx reloaded")
        print "nginx reloaded"

    def gen_config(self, force=False):
        domain_list = None
        try:
            self.rpc.connect(_private.SAAS_ADDR)
            try:
                domain_list = self.rpc.call("proxy_domain_list")
            finally:
                self.rpc.close()
        except Exception, e:
            self.logger.exception(e)
            print traceback.print_exc()
            return
        conf = []
        for i in domain_list:
            conf.append(
                self.NGINX_TEMPLATE % {
                    'host': i["domain"],
                    'ip': i["ip"],
                    'suffix': _private.PROXY_DOMAIN_SUFFIX
                })
        content = "".join(conf)
        try:
            if not force and os.path.exists(self.output_path) and _md5_file(
                    self.output_path) == _md5(content):
                print "skip the same"
                return
            f = open(self.output_path, "w")
            try:
                f.write(content)
            finally:
                f.close()
            self.logger.info("conf generated")
            self.reload_nginx()
        except Exception, e:
            self.logger.exception(e)
            print traceback.print_exc()
Esempio n. 10
0
 def __init__(self, logger, server_ip):
     self.server_ip = server_ip
     self.logger = logger
     self.rpc = AES_RPC_Client(conf.KEY, logger)
Esempio n. 11
0
class SyncClientBase(object):
    def __init__(self, logger, server_ip):
        self.server_ip = server_ip
        self.logger = logger
        self.rpc = AES_RPC_Client(conf.KEY, logger)

    def connect(self, timeout=None):
        addr = (self.server_ip, conf.INF_PORT)
        if timeout:
            self.rpc.set_timeout(timeout)
        return self.rpc.connect(addr)

    def close(self):
        self.rpc.close()

    def rsync(self,
              source,
              dest=None,
              speed=None,
              use_zip=False,
              log_file=None):
        """ speed is in Mbit/s """
        options = (
            "-avW",
            "--inplace",
        )
        if os.path.isdir(source):
            if source[-1] != "/":
                source += "/"
            if dest and dest[-1] != "/":
                dest += "/"
            if dest:
                options += ("--delete", )
        if log_file:
            options += ('--log-file', log_file)
        if not dest:
            dest = ""
        if speed:
            assert isinstance(speed, (int, float))
            options += (
                "--bwlimit",
                str(int(speed * 1000 / 8)),
            )
            use_zip = True
        if use_zip:
            options += ("-z", )

        cmd = ("rsync", ) + options + \
            (source,
             "rsync://%s:%s/%s/%s" % (self.server_ip,
                                      conf.RSYNC_PORT, RSYNC_SERVER_NAME, dest)
             )
        print cmd
        p = subprocess.Popen(cmd, stderr=subprocess.PIPE, close_fds=True)
        retcode = p.wait()
        if retcode:
            stderr = "\n".join(p.stderr.readlines())
        else:
            stderr = None
        p.stderr.close()
        return retcode, stderr