Ejemplo n.º 1
0
    def __init__(self, cluster_config, target, local_port, apk_path, activity, reinstall):

        self.target = target
        self.local_port = local_port

        self.url = ""
        self.install_and_launch_app(target, local_port, apk_path, activity, reinstall)

        if self.is_emulator(target):
            self.url = "http://{}:{}".format(self.get_host_ip(), local_port)
        else:
            self.url = "http://{}:{}".format(self.get_device_ip(target), 5984)

        # Wrap a Sync Gateway instance and use that as a client to hit the LiteServ listener
        # in Couchbase Lite
        fake_target = {"ip": None, "name": None}
        self.sg = SyncGateway(cluster_config, fake_target)
        self.sg.url = self.url

        log.info("Listener running at {} ...".format(self.url))
Ejemplo n.º 2
0
class Listener:
    def __init__(self, cluster_config, target, local_port, apk_path, activity, reinstall):

        self.target = target
        self.local_port = local_port

        self.url = ""
        self.install_and_launch_app(target, local_port, apk_path, activity, reinstall)

        if self.is_emulator(target):
            self.url = "http://{}:{}".format(self.get_host_ip(), local_port)
        else:
            self.url = "http://{}:{}".format(self.get_device_ip(target), 5984)

        # Wrap a Sync Gateway instance and use that as a client to hit the LiteServ listener
        # in Couchbase Lite
        fake_target = {"ip": None, "name": None}
        self.sg = SyncGateway(cluster_config, fake_target)
        self.sg.url = self.url

        log.info("Listener running at {} ...".format(self.url))

    def install_and_launch_app(self, target, local_port, apk_path, activity, reinstall):

        # Build monkeyrunner install cmd
        cmd_args = [
            "monkeyrunner",
            "libraries/utilities/monkeyrunner.py",
            "--target={}".format(target),
            "--apk-path={}".format(apk_path),
            "--activity={}".format(activity),
        ]

        if self.is_emulator(target):
            cmd_args.append("--local-port={}".format(local_port))

        if reinstall:
            cmd_args.append("--reinstall")

        # Execute monkeyrunner install
        monkey_output = subprocess.check_output(cmd_args)
        log.info("OUTPUT: {}".format(monkey_output))

    def is_emulator(self, target):
        return target.startswith("emulator") or target.startswith("192.168")

    def get_host_ip(self):
        cmd_output = subprocess.check_output("ifconfig")
        en0_section = cmd_output.split("\n")[11]
        full_ip = en0_section.split()[1]
        ip = full_ip.split("/")[0]
        return ip

    def get_device_ip(self, target):
        log.info("Getting Device ip ...")
        result = subprocess.check_output(["adb", "-s", "{}".format(target), "shell", "netcfg"])
        log.info("RESULT: {}".format(result))
        ip_line = result.split('\n')[0]
        ip = ip_line.split()[2]
        ip = ip.split("/")[0]
        return ip

    def kill_port_forwarding(self):
        log.info("Killing forwarding rule for {} on port: {}".format(self.target, self.local_port))
        subprocess.call(['adb', '-s', self.target, 'forward', '--remove', 'tcp:{}'.format(self.local_port)])

    def setup_port_forwarding(self):
        log.info("Setup forwarding rule for {} on port: {}".format(self.target, self.local_port))
        subprocess.call(['adb', '-s', self.target, 'forward', 'tcp:{}'.format(self.local_port), 'tcp:5984'])

    def verify_launched(self):
        self.sg.verify_launched()

    def create_db(self, name):
        return self.sg.create_db(name)

    def delete_db(self, name):
        return self.sg.delete_db(name)

    def get_dbs(self):
        self.sg.admin.get_dbs()

    def reset(self):
        self.sg.reset()

    def start_push_replication(self, target, source_db, target_db):
        self.sg.start_push_replication(target, source_db, target_db)

    def stop_push_replication(self, target, source_db, target_db):
        self.sg.stop_push_replication(target, source_db, target_db)

    def start_pull_replication(self, source_url, source_db, target_db):
        self.sg.start_pull_replication(source_url, source_db, target_db)

    def stop_pull_replication(self, source_url, source_db, target_db):
        self.sg.stop_pull_replication(source_url, source_db, target_db)

    def get_num_docs(self, db):
        return self.sg.get_num_docs(db)
Ejemplo n.º 3
0
    def __init__(self, config):

        self._cluster_config = config

        if not os.path.isfile(self._cluster_config):
            log_info(
                "Cluster config not found in 'resources/cluster_configs/'")
            raise IOError(
                "Cluster config not found in 'resources/cluster_configs/'")

        log_info(self._cluster_config)

        # Load resources/cluster_configs/<cluster_config>.json
        with open("{}.json".format(config)) as f:
            cluster = json.loads(f.read())

        # Get load balancer IP
        lb_ip = None
        if is_load_balancer_enabled(self._cluster_config):
            # If load balancer is defined,
            # Switch all SG URLs to that of load balancer
            lb_ip = get_load_balancer_ip(self._cluster_config)

            sgs = [{
                "name": sg["name"],
                "ip": lb_ip
            } for sg in cluster["sync_gateways"]]
            log_info("Using load balancer IP as the SG IP: {}".format(sgs))
        else:
            sgs = [{
                "name": sg["name"],
                "ip": sg["ip"]
            } for sg in cluster["sync_gateways"]]

        acs = [{
            "name": ac["name"],
            "ip": ac["ip"]
        } for ac in cluster["sg_accels"]]

        self.cbs_ssl = cluster["environment"]["cbs_ssl_enabled"]
        self.xattrs = cluster["environment"]["xattrs_enabled"]

        if self.cbs_ssl:
            cbs_urls = [
                "https://{}:18091".format(cbs["ip"])
                for cbs in cluster["couchbase_servers"]
            ]
        else:
            cbs_urls = [
                "http://{}:8091".format(cbs["ip"])
                for cbs in cluster["couchbase_servers"]
            ]

        log_info("cbs: {}".format(cbs_urls))
        log_info("sgs: {}".format(sgs))
        log_info("acs: {}".format(acs))
        log_info("ssl: {}".format(self.cbs_ssl))

        self.sync_gateways = [
            SyncGateway(cluster_config=self._cluster_config, target=sg)
            for sg in sgs
        ]
        self.sg_accels = [
            SgAccel(cluster_config=self._cluster_config, target=ac)
            for ac in acs
        ]
        self.servers = [CouchbaseServer(url=cb_url) for cb_url in cbs_urls]
        self.sync_gateway_config = None  # will be set to Config object when reset() called