Created
November 10, 2015 12:43
-
-
Save mgmarino/9e77e4c57b8ec6133ba9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Setup replication for a CouchDB server | |
""" | |
import cloudant | |
import json | |
_replicate_source_server = { | |
"protocol" : "http", | |
"address" : "optimal.universe-cluster.de", | |
"port" : "15984", | |
"credentials" : ("nedm_user", "pw"), | |
} | |
_replicate_target_server = { | |
"protocol" : "http", | |
"address" : "cluster-server-default", | |
"port" : "5984", | |
"credentials" : ("admin", """admin_pw"""), | |
} | |
pull_from = True | |
bidirectional = False | |
base_doc = { | |
"user_ctx" : { | |
"name" : "admin", | |
"roles" : ["_admin"] | |
}, | |
"continuous" : True, | |
"create_target" : True, | |
} | |
def get_acct(serv): | |
acct = cloudant.Account(uri="{protocol}://{address}:{port}".format(**serv)) | |
res = acct.login(*serv["credentials"]) | |
assert res.status_code == 200 | |
return acct | |
def get_all_dbs(serv): | |
acct = get_acct(serv) | |
res = acct.all_dbs().json() | |
_nedm_pre = "nedm/" | |
return [k[len(_nedm_pre):] for k in res if k[:len(_nedm_pre)] == _nedm_pre] | |
def setup_replicate(dbs, from_serv, to_serv, pull): | |
temp_doc = base_doc.copy() | |
save_on_serv = from_serv | |
other_serv = to_serv | |
temp_doc["source"] = "nedm/{db_name}" | |
temp_doc["target"] = "{protocol}://{un}:{pw}@{address}:{port}/nedm%2F{db_name}" | |
if pull: | |
save_on_serv = to_serv | |
other_serv = from_serv | |
src = temp_doc["source"] | |
temp_doc["source"] = temp_doc["target"] | |
temp_doc["target"] = src | |
print dbs | |
acct = get_acct(save_on_serv) | |
print acct | |
repl = acct["_replicator"] | |
all_docs = repl.all_docs().get().json() | |
_des_prepend = "_design/" | |
print all_docs | |
all_docs = [k["id"] for k in all_docs["rows"] if k["id"][:len(_des_prepend)] != _des_prepend ] | |
use_other_serv = other_serv.copy() | |
use_other_serv["un"] = use_other_serv["credentials"][0] | |
use_other_serv["pw"] = use_other_serv["credentials"][1] | |
for db in dbs: | |
# Make new replication document | |
if db in all_docs: | |
print("{} is already being replicated, skipping".format(db)) | |
continue | |
adoc = temp_doc.copy() | |
adoc["_id"] = db | |
for k in adoc: | |
try: | |
adoc[k] = adoc[k].format(db_name=db, **use_other_serv) | |
except: pass | |
print repl.post(data=json.dumps(adoc)).json() | |
if __name__ == '__main__': | |
dbs_to_replicate = get_all_dbs(_replicate_source_server) | |
setup_replicate(dbs_to_replicate, _replicate_source_server, _replicate_target_server, True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment