Skip to content

Commit

Permalink
set up cloudant locally with one script
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Nov 18, 2019
1 parent a0e3699 commit ef41ae7
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 186 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ matrix:
- TASK=test-repository-cloudant
script:
# set up docker image and create admin for cloudant.
- npm run docker:setup
- npm run postinstall -- --scope "@loopback/test-repository-cloudant" --include-dependencies
- npm run build -- --scope "@loopback/test-repository-cloudant" --include-dependencies
- source cloudant-config.sh && cd acceptance/repository-cloudant && npm run mocha
- cd acceptance/repository-cloudant && run docker:setup
- source cloudant-config.sh && npm run mocha

branches:
only:
Expand Down
29 changes: 20 additions & 9 deletions docker.setup.js → ...tance/repository-cloudant/docker.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@

const _ = require('lodash');
const async = require('async');
const spawn = require('child_process').spawn;
const docker = new require('dockerode')();
const fmt = require('util').format;
const http = require('http');
const ms = require('ms');
const fs = require('fs-extra');

// // we don't pass any node flags, so we can call _mocha instead the wrapper
// const mochaBin = require.resolve('mocha/bin/_mocha');
/**
* This script creates a new couchDB3 container locally. And it also creates the admin for
* the container. Type in bash command:
* ```bash
* CLOUDANT_PASSWORD=myadmin CLOUDANT_PASSWORD=mypass CLOUDANT_DATABASE=mydb node docker.setup.sh
* ```
* to customize the names.
*/

process.env.CLOUDANT_DATABASE = 'testdb';
process.env.CLOUDANT_PASSWORD = 'pass';
process.env.CLOUDANT_USERNAME = 'admin';
process.env.CLOUDANT_DATABASE = process.env.CLOUDANT_DATABASE || 'testdb';
process.env.CLOUDANT_PASSWORD = process.env.CLOUDANT_PASSWORD || 'pass';
process.env.CLOUDANT_USERNAME = process.env.CLOUDANT_USERNAME || 'admin';

// these are placeholders. They get set dynamically based on what IP and port
// get assigned by docker.
Expand All @@ -39,7 +43,7 @@ async.waterfall(
setCloudantEnv,
waitFor('/_all_dbs'),
createAdmin(),
createDB('testdb'),
createDB(process.env.CLOUDANT_DATABASE),
listUser(),
exportENV(),
],
Expand All @@ -53,12 +57,14 @@ async.waterfall(

function sleep(n) {
return function delayedPassThrough() {
// eslint-disable-next-line prefer-rest-params
const args = [].slice.call(arguments);
// last argument is the callback
const next = args.pop();
// prepend `null` to indicate no error
args.unshift(null);
setTimeout(function() {
// eslint-disable-next-line prefer-spread
next.apply(null, args);
}, n);
};
Expand All @@ -68,6 +74,7 @@ function dockerStart(imgName) {
return function pullAndStart(next) {
console.log('pulling image: %s', imgName);
docker.pull(imgName, function(err, stream) {
// eslint-disable-next-line no-shadow
docker.modem.followProgress(stream, function(err, output) {
if (err) {
return next(err);
Expand All @@ -80,6 +87,7 @@ function dockerStart(imgName) {
PublishAllPorts: true,
},
},
// eslint-disable-next-line no-shadow
function(err, container) {
console.log(
'recording container for later cleanup: ',
Expand All @@ -89,6 +97,7 @@ function dockerStart(imgName) {
if (err) {
return next(err);
}
// eslint-disable-next-line no-shadow
container.start(function(err, data) {
next(err, container);
});
Expand Down Expand Up @@ -164,6 +173,7 @@ function waitFor(path) {
});
})
.on('error', tryAgain);
// eslint-disable-next-line no-shadow
function tryAgain(err) {
setTimeout(ping, CONNECT_DELAY, err, tries - 1);
}
Expand All @@ -173,7 +183,7 @@ function waitFor(path) {

function createAdmin() {
return function createAdminUser(container, next) {
const data = '"pass"';
const data = `"${process.env.CLOUDANT_PASSWORD}"`;
const uri =
'/_node/[email protected]/_config/admins/' +
process.env.CLOUDANT_USERNAME;
Expand Down Expand Up @@ -288,6 +298,7 @@ function exportENV() {
}

// clean up any previous containers
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function dockerCleanup(next) {
if (containerToDelete) {
console.log('cleaning up container: %s', containerToDelete.id);
Expand Down
Loading

0 comments on commit ef41ae7

Please sign in to comment.