source: Dev/trunk/src/server/config/config-couchdb.js @ 464

Last change on this file since 464 was 464, checked in by hendrikvanantwerpen, 12 years ago

Working deployment to Heroku.

File size: 4.1 KB
RevLine 
[455]1var q = require('q');
[464]2var HTTP = require('q-io/http');
3var URL = require('url');
[455]4var _ = require('underscore');
[464]5var util = require('util');
[455]6
7var designDocs = require('./couchdb-design-docs');
8
[464]9module.exports = function(couchDbURL) {
10
11    function stringifyFunctions(value) {
12        if ( value === null ) {
13            return null;
14        } else if ( _.isArray(value) ) {
15            return value;
16        } else if ( _.isFunction(value) ) {
17            return value.toString();
18        } else if ( _.isObject(value) ) {
19            value = _.clone(value);
20            _.each(value, function(propValue, propName){
21                value[propName] = stringifyFunctions(propValue);
22            });
23            return value;
24        } else {
25            return value;
26        }
[455]27    }
28
[464]29    function request(method,path,content) {
30        var url = couchDbURL+path;
31        var parsedURL = URL.parse(url);
32        var options = {
33            url: url,
34            method: method,
35            headers: {
36                'content-type': 'application/json; charset=utf-8',
37                'accept': 'application/json'
38            },
39            body: {
40                forEach: function(callback) {
41                    callback(JSON.stringify(content || {}));
42                }
[455]43            }
[464]44        };
45        // because q-io doesn't support auth properly, we have to
46        // build the f*ing wheel again.
47        if ( parsedURL.auth ) {
48            var auth = new Buffer(parsedURL.auth).toString("base64");
49            options.headers.authorization = 'Basic '+auth;
[455]50        }
[464]51        return HTTP.request(options)
52        .then(function(res){
53            return res.body.read().then(function(content){
54                return JSON.parse(content.toString() || "{}");
55            });
56        },function(res){
57            return res.body.read().then(function(error){
58                console.warn(error); // q.all doesn't do errors, so let's show them here
59                return JSON.parse(error.toString() || "{}");
60            });
[455]61        });
[464]62    }
[455]63
[464]64    console.log("Configuring CouchDB for QED");
65    console.log("Checking CouchDB version");
66    return request('GET','')
[455]67    .then(function(res){
[464]68        if (res.version !== "1.2.0" ) {
69            console.log("Found "+res.version+", only tested with CouchDB 1.2.0");
70        } else {
71            console.log("CouchDB 1.2.0 found");
72        }
73    }).then(function(res){
74        console.log("Checking database 'qed'");
75        return request('GET','qed')
76        .then(function(res){
77            console.log("Database 'qed' found.");
78        },function(err){
79            console.log("Creating database 'qed'");
80            return request('PUT','qed');
81        });
82    }).then(function(){
83        console.log("Putting documents in database.");
84        designDocs = stringifyFunctions(designDocs);
85        return q.all(_.map(designDocs, function(doc,docUrl){
86            var configAction = doc.__configAction;
87            delete doc.__configAction;
88            switch (configAction) {
89                case "ignore":
90                    console.log(docUrl+" ignored.");
91                break;
92                case "update":
93                    console.log(docUrl+" updating.");
94                    return request('GET',docUrl)
95                    .then(function(oldDoc){
96                        _.extend(oldDoc,doc);
97                        return request('PUT',docUrl,oldDoc);
98                    },function(){
99                        return request('PUT',docUrl,doc);
100                    });
101                break;
102                case "replace":
103                default:
104                    console.log(docUrl+" replacing.");
105                    return request('GET',docUrl)
106                    .then(function(oldDoc){
107                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
108                        return request('PUT',docUrl,doc);
109                    },function(){
110                        return request('PUT',docUrl,doc);
111                    });
112                break;
113            }
114        }));
115    }).then(function(){
116        console.log("Done!");
[455]117    },function(err){
[464]118        console.error("ERROR",err,err.stack);
[455]119    });
[464]120
121};
Note: See TracBrowser for help on using the repository browser.