-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.coffee
executable file
·71 lines (57 loc) · 2.07 KB
/
boot.coffee
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
#!./node_modules/.bin/iced
url = require "url"
Cradle = require "cradle"
errify = require "errify"
CloudantUser = require "cloudant-user"
CouchApp = require "couchapp"
_security = require "./_security-docs/_security"
## Couch server admin credentials
username = ""
password = ""
## URL, Database for blog
host = ""
port = 443
database = "blog"
## New admin user for blog
bloguser = ""
email = ""
blogpass = ""
## design doc for blog app
ddoc_path = "./_design-docs/app.coffee"
## Boot
{log, error} = console
log "setting up couch connection to #{host}"
log "using database #{database}"
secure = !!/https/.exec host
auth = {username, password}
conn = new Cradle.Connection {host, port, secure, auth}
db = conn.database database
ideally = errify (err) ->
error err
process.exit 1
await db.exists ideally defer exists
unless exists
log "creating database #{database}"
await db.create ideally defer res
log "adding _security document to database with admins", _security.admins
await db.save "_security", _security, ideally defer res
log "creating blog admin user #{bloguser}"
cloudantUser = new CloudantUser {host, port, secure, auth: {username, password}}
await cloudantUser.createWithMeta bloguser, blogpass, "blog", "admin", "_reader", "_writer", {email}, ideally defer res
log "testing blog database with user #{bloguser}"
testconn = new Cradle.Connection {host, port, secure, auth: username: bloguser, password: blogpass}
testdb = testconn.database database
testname = (new Buffer "#{Math.random()}").toString "base64"
log "creating test document #{testname}"
await testdb.save testname, {testing: true}, ideally defer res
log "removing test document #{testname}"
await testdb.remove testname, ideally defer res
fullurl = "#{host}:#{port}/#{database}"
log "pushing #{ddoc_path} to #{fullurl}"
fullauthurl = url.parse fullurl
fullauthurl.auth = "#{username}:#{password}"
fullauthurl = url.format fullauthurl
ddoc = require ddoc_path
await CouchApp.createApp ddoc, fullauthurl, defer couchapp
await couchapp.push ideally defer res
log "database ready"