コード例 #1
0
ファイル: farm.py プロジェクト: viahoon/openag_python
def create_farm(farm_name):
    """
    Create a farm. Creates a farm named FARM_NAME on the currently selected
    cloud server. You can use the `openag cloud select_farm` command to start
    mirroring data into it.
    """
    utils.check_for_cloud_server()
    utils.check_for_cloud_user()
    server = Server(config["cloud_server"]["url"])
    username = config["cloud_server"]["username"]
    password = config["cloud_server"]["password"]
    server.log_in(username, password)
    url = urljoin(server.resource.url, "_openag", "v0.1", "register_farm")
    status, _, content = server.resource.session.request(
        "POST",
        url,
        headers=server.resource.headers.copy(),
        body={
            "name": username,
            "farm_name": farm_name
        },
        credentials=(username, password))
    if status != 200:
        raise click.ClickException(
            "Failed to register farm with cloud server ({}): {}".format(
                status, content))
コード例 #2
0
ファイル: db_init.py プロジェクト: zulinjenrn/openag_brain
def db_init(db_url, api_url):
    """
    Initialize the database server. Sets some configuration parameters on the
    server, creates the necessary databases for this project, pushes design
    documents into those databases.
    """
    db_config = generate_config(api_url)
    server = BootstrapServer(db_url)

    # Configure the CouchDB instance itself
    config_items = []
    for section, values in db_config.items():
        for param, value in values.items():
            config_items.append((section, param, value))

    print "Applying CouchDB configuration"
    for section, param, value in config_items:
        url = urljoin(server.resource.url, "_config", section, param)
        try:
            current_val = server.resource.session.request(
                "GET",
                url,
                body=None,
                headers=None,
                credentials=server.resource.credentials)[2].read().strip()
        except ResourceNotFound:
            current_val = None
        desired_val = '"{}"'.format(value.replace('"', '\\"'))
        if current_val != desired_val:
            status = server.resource.session.request(
                "PUT",
                url,
                body=desired_val,
                headers=None,
                credentials=server.resource.credentials)[0]
            # Unless there is some delay between requests, CouchDB gets
            # sad for some reason
            if status != 200:
                raise Exception(
                    'Failed to set configuration parameter "{}": {}'.format(
                        param, res.content))
            time.sleep(1)

    # Create all dbs on the server
    print "Creating databases"
    for db_name in all_dbs:
        server.get_or_create(db_name)

    # Push design documents
    print "Pushing design documents"
    design_path = os.path.dirname(_design.__file__)
    server.push_design_documents(design_path)

    # Save the local server
    config["local_server"]["url"] = db_url
コード例 #3
0
ファイル: mailbox.py プロジェクト: ltucker/radarpost
def get_json_raw_url(mb, path):
    """
    little workaround for skirting overzealous 
    couchdb-python quoting behavior. 
    """
    from couchdb import http, json

    session = mb.resource.session
    creds = mb.resource.credentials
    method = "GET"
    path = [mb.resource.url] + path
    url = http.urljoin(*path)
    status, headers, data = session.request(method, url, credentials=creds)
    if "application/json" in headers.get("content-type"):
        data = json.decode(data.read())
    return status, headers, data
コード例 #4
0
def replicate_global_dbs(cloud_url=None, local_url=None):
    """
    Set up replication of the global databases from the cloud server to the
    local server.

    :param str cloud_url: Used to override the cloud url from the global
    configuration in case the calling function is in the process of
    initializing the cloud server
    :param str local_url: Used to override the local url from the global
    configuration in case the calling function is in the process of
    initializing the local server
    """
    local_url = local_url or config["local_server"]["url"]
    cloud_url = cloud_url or config["cloud_server"]["url"]
    server = Server(local_url)
    for db_name in global_dbs:
        server.replicate(
            db_name, urljoin(cloud_url, db_name), db_name, continuous=True,
        )
コード例 #5
0
def replicate_per_farm_dbs(cloud_url=None, local_url=None, farm_name=None):
    """
    Sete up replication of the per-farm databases from the local server to the
    cloud server.

    :param str cloud_url: Used to override the cloud url from the global
    configuration in case the calling function is in the process of
    initializing the cloud server
    :param str local_url: Used to override the local url from the global
    configuration in case the calling function is in the process of
    initializing the local server
    :param str farm_name: Used to override the farm name from the global
    configuratino in case the calling function is in the process of
    initializing the farm
    """
    cloud_url = cloud_url or config["cloud_server"]["url"]
    local_url = local_url or config["local_server"]["url"]
    farm_name = farm_name or config["cloud_server"]["farm_name"]
    username = config["cloud_server"]["username"]
    password = config["cloud_server"]["password"]

    # Add credentials to the cloud url
    parsed_cloud_url = urlparse(cloud_url)
    if not parsed_cloud_url.username:
        new_netloc = "{}:{}@{}".format(
            username, password, parsed_cloud_url.netloc
        )
    cloud_url = ParseResult(
        parsed_cloud_url.scheme, new_netloc, parsed_cloud_url.path,
        parsed_cloud_url.params, parsed_cloud_url.query,
        parsed_cloud_url.fragment
    ).geturl()

    server = Server(local_url)
    for db_name in per_farm_dbs:
        remote_db_name = "{}/{}/{}".format(username, farm_name, db_name)
        server.replicate(
            db_name, db_name, urljoin(cloud_url, remote_db_name),
            continuous=True
        )
コード例 #6
0
ファイル: utils.py プロジェクト: viahoon/openag_python
def replicate_global_dbs(cloud_url=None, local_url=None):
    """
    Set up replication of the global databases from the cloud server to the
    local server.

    :param str cloud_url: Used to override the cloud url from the global
    configuration in case the calling function is in the process of
    initializing the cloud server
    :param str local_url: Used to override the local url from the global
    configuration in case the calling function is in the process of
    initializing the local server
    """
    local_url = local_url or config["local_server"]["url"]
    cloud_url = cloud_url or config["cloud_server"]["url"]
    server = Server(local_url)
    for db_name in global_dbs:
        server.replicate(
            db_name,
            urljoin(cloud_url, db_name),
            db_name,
            continuous=True,
        )
コード例 #7
0
ファイル: utils.py プロジェクト: viahoon/openag_python
def replicate_per_farm_dbs(cloud_url=None, local_url=None, farm_name=None):
    """
    Sete up replication of the per-farm databases from the local server to the
    cloud server.

    :param str cloud_url: Used to override the cloud url from the global
    configuration in case the calling function is in the process of
    initializing the cloud server
    :param str local_url: Used to override the local url from the global
    configuration in case the calling function is in the process of
    initializing the local server
    :param str farm_name: Used to override the farm name from the global
    configuratino in case the calling function is in the process of
    initializing the farm
    """
    cloud_url = cloud_url or config["cloud_server"]["url"]
    local_url = local_url or config["local_server"]["url"]
    farm_name = farm_name or config["cloud_server"]["farm_name"]
    username = config["cloud_server"]["username"]
    password = config["cloud_server"]["password"]

    # Add credentials to the cloud url
    parsed_cloud_url = urlparse(cloud_url)
    if not parsed_cloud_url.username:
        new_netloc = "{}:{}@{}".format(username, password,
                                       parsed_cloud_url.netloc)
    cloud_url = ParseResult(parsed_cloud_url.scheme, new_netloc,
                            parsed_cloud_url.path, parsed_cloud_url.params,
                            parsed_cloud_url.query,
                            parsed_cloud_url.fragment).geturl()

    server = Server(local_url)
    for db_name in per_farm_dbs:
        remote_db_name = "{}/{}/{}".format(username, farm_name, db_name)
        server.replicate(db_name,
                         db_name,
                         urljoin(cloud_url, remote_db_name),
                         continuous=True)
コード例 #8
0
def create_farm(farm_name):
    """
    Create a farm. Creates a farm named FARM_NAME on the currently selected
    cloud server. You can use the `openag cloud select_farm` command to start
    mirroring data into it.
    """
    utils.check_for_cloud_server()
    utils.check_for_cloud_user()
    server = Server(config["cloud_server"]["url"])
    username = config["cloud_server"]["username"]
    password = config["cloud_server"]["password"]
    server.log_in(username, password)
    url = urljoin(server.resource.url, "_openag", "v0.1", "register_farm")
    status, _, content = server.resource.session.request(
        "POST", url, headers=server.resource.headers.copy(), body={
            "name": username, "farm_name": farm_name
        }, credentials=(username, password)
    )
    if status != 200:
        raise click.ClickException(
            "Failed to register farm with cloud server ({}): {}".format(
                status, content
            )
        )
コード例 #9
0
ファイル: __init__.py プロジェクト: viahoon/openag_python
def init(db_url, api_url):
    """
    Initialize the database server. Sets some configuration parameters on the
    server, creates the necessary databases for this project, pushes design
    documents into those databases, and sets up replication with the cloud
    server if one has already been selected.
    """
    old_db_url = config["local_server"]["url"]
    if old_db_url and old_db_url != db_url:
        raise click.ClickException(
            "Local database \"{}\" already initialized. Switching local "
            "databases is not currently supported".format(old_db_url)
        )

    db_config = generate_config(api_url)
    server = Server(db_url)

    # Configure the CouchDB instance itself
    config_items = []
    for section, values in db_config.items():
        for param, value in values.items():
            config_items.append((section, param, value))
    with click.progressbar(
        config_items, label="Applying CouchDB configuration",
        length=len(config_items)
    ) as _config_items:
        for section, param, value in _config_items:
            url = urljoin(server.resource.url, "_config", section, param)
            try:
                current_val = server.resource.session.request(
                    "GET", url
                )[2].read().strip()
            except ResourceNotFound:
                current_val = None
            desired_val = '"{}"'.format(value.replace('"', '\\"'))
            if current_val != desired_val:
                status = server.resource.session.request(
                    "PUT", url, body=desired_val
                )[0]
                # Unless there is some delay  between requests, CouchDB gets
                # sad for some reason
                if status != 200:
                    click.ClickException(
                        'Failed to set configuration parameter "{}": {}'.format(
                            param, res.content
                        )
                    )
                time.sleep(1)

    # Create all dbs on the server
    with click.progressbar(
        all_dbs, label="Creating databases", length=len(all_dbs)
    ) as _dbs:
        for db_name in _dbs:
            server.get_or_create(db_name)

    # Push design documents
    click.echo("Pushing design documents")
    design_path = os.path.dirname(_design.__file__)
    server.push_design_documents(design_path)

    # Set up replication
    if config["cloud_server"]["url"]:
        click.echo("Setting up replication with cloud server")
        utils.replicate_global_dbs(local_url=db_url)
        if config["cloud_server"]["farm_name"]:
            utils.replicate_per_farm_dbs(local_url=db_url)

    config["local_server"]["url"] = db_url