-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup_couchdb.py
executable file
·118 lines (105 loc) · 3.38 KB
/
setup_couchdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#! /usr/bin/env python
# Standard library
from __future__ import print_function
import getpass
import json
import sys
# App modules
from treq import content, json_content
from treq.client import HTTPClient
# External modules
from twisted.internet.task import react
from twisted.web.client import Agent
def main():
is_https = ""
while is_https.strip().lower() not in ('y', 'n'):
is_https = raw_input("Use HTTPS [Yn]? ")
if is_https.strip() == "":
is_https = "y"
is_https = (is_https.lower() == "y")
host = raw_input("CouchDB Server: ")
port = ""
while True:
port = raw_input("CouchDB Port: ")
try:
port = int(port)
except ValueError:
continue
break
db = ""
while db.strip() == "":
db = raw_input("Database Name: ")
admin = ""
while admin.strip() == "":
admin = raw_input("Admin User: ")
passwd = ""
confirm = None
while passwd != confirm:
passwd = getpass.getpass("Password: ")
confirm = getpass.getpass("Confirm Password: ")
print("Create Database")
print("Server: %s:%d" % (host, port))
print("Database: '%s'" % db)
yesno = raw_input("Continue [yN]? ")
if yesno.strip().lower() != "y":
sys.exit(1)
if is_https:
scheme = "https"
else:
scheme = "http"
url = "%s://%s:%d/%s" % (scheme, host, port, db)
def check_created(resp):
def report_error(resp_text):
raise Exception("Could not create database.\n%s" % resp_text)
if resp.code not in (201, 412):
return content(resp).adCallback(report_error)
return resp
def create_design_doc(_, http, scheme, host, port, db, admin, passwd):
url = "%s://%s:%d/%s/_design/views" % (scheme, host, port, db)
doc = {
'language': 'javascript',
'views': {
"get_ticket": {
"map": "function(doc) {\n emit(doc['ticket_id'], doc);\n}"
},
"get_by_expires": {
"map": """function(doc) {\n emit(doc['expires'], doc['ticket_id']);\n}"""
},
},
}
doc = json.dumps(doc)
d = http.put(url, auth=(admin, passwd), data=doc)
return d
#201 - create ddoc, 409 - exists
def report_status(resp):
if resp.code == 409:
print("Design document 'views' already exists.")
elif resp.code != 201:
print("Could not create design document 'views'.")
return resp
def print_result(result):
print(result)
return result
def stop(_, reactor):
print("Stopping ...")
reactor.stop()
def log_error(err):
print(err)
return err
print("URL => {0}".format(url))
def perform_task(reactor):
agent = Agent(reactor)
http = HTTPClient(agent)
d = http.put(url, auth=(admin, passwd))
d.addCallback(check_created)
d.addCallback(json_content)
d.addCallback(create_design_doc, http, scheme, host, port, db, admin, passwd)
d.addCallback(report_status)
d.addCallback(json_content)
#d.addCallback(print_result)
d.addErrback(log_error)
d.addBoth(stop, reactor)
return d
react(perform_task)
if __name__ == "__main__":
main()