Added edge config and also updated the deploy script

This commit is contained in:
Kasra Bigdeli
2019-01-13 17:44:35 -08:00
parent 104362dca6
commit f86a1e4f52
5 changed files with 117 additions and 55 deletions

View File

@@ -1,47 +1,87 @@
#!/usr/bin/env node #!/usr/bin/env node
const exec = require('child_process').exec; const execOriginal = require('child_process').exec;
const request = require('request'); const requestOriginal = require('request');
const version = require('./src/utils/CaptainConstants').version; function exec(command) {
const publishedNameOnDockerHub = require('./src/utils/CaptainConstants').publishedNameOnDockerHub; return new Promise(function (resolve, reject) {
execOriginal(command, function (err, stdout, stderr) {
console.log(version); if (stderr) {
console.log(' '); console.log('stderr');
console.log(stderr);
}
if (err) {
reject(err)
return
}
resolve(stdout)
})
})
}
exec('git status', function (err, stdout, stderr) { function request(url) {
return new Promise(function (resolve, reject) {
requestOriginal(url, function (error, response, body) {
if (body) {
body = JSON.parse(body);
}
if (err) { if (error || !body) {
console.log(err); console.log('Error while fetching tags from Docker Hub!');
return; reject(error);
} return;
}
var l1 = 'On branch master';
var l2 = 'Your branch is up to date with \'origin/master\'';
var l3 = 'nothing to commit, working tree clean';
if (stdout.indexOf(l1) < 0 || stdout.indexOf(l2) < 0 || stdout.indexOf(l3) < 0) { resolve(body)
console.log('Make sure you are on master branch, in sync with remote, and your working directory is clean'); })
return; })
} }
fetchTagsFromHub();
});
function fetchTagsFromHub() {
var URL = 'https://hub.docker.com/v2/repositories/' + publishedNameOnDockerHub + '/tags';
request(URL, function (error, response, body) { const publishedNameOnDockerHub = 'caprover/caprover';
let version = ''
if (body) { exec('npm run build')
body = JSON.parse(body); .then(function (data) {
data = (data + '').trim()
console.log('----------')
console.log(data)
console.log('----------')
if (!data.startsWith('> caprover@0.0.0') || !data.endsWith('rm -rf ./built && npx tsc')) {
console.log('Unexpected output:')
throw new Error(data)
} }
if (error || !body || !body.results || !body.results.length) { version = require('./built/utils/CaptainConstants').configs.version;
return exec('git status')
})
.then(function (data) {
var l1 = 'On branch master';
var l2 = 'Your branch is up to date with \'origin/master\'';
var l3 = 'nothing to commit, working tree clean';
if (data.indexOf(l1) < 0 || data.indexOf(l2) < 0 || data.indexOf(l3) < 0) {
console.log('Make sure you are on master branch, in sync with remote, and your working directory is clean');
throw new Error(data)
}
var URL = 'https://hub.docker.com/v2/repositories/' + publishedNameOnDockerHub + '/tags';
return request(URL)
})
.then(function (body) {
if (!body.results || !body.results.length) {
console.log('Error while fetching tags from Docker Hub!'); console.log('Error while fetching tags from Docker Hub!');
console.log(error); throw error;
return;
} }
var highestTag = ''; var highestTag = '';
@@ -77,40 +117,26 @@ function fetchTagsFromHub() {
} }
} }
if (!isVersionValid || !highestTagValue) { if (!isVersionValid || !highestTagValue || !version) {
console.log('The version you are pushing is not valid!'); throw new Error('The version you are pushing is not valid! ' + version);
return;
} }
console.log('Pushing ' + version); console.log('Pushing ' + version);
buildAndPush(); var t1 = publishedNameOnDockerHub + ':' + 'latest';
var t2 = publishedNameOnDockerHub + ':' + version;
}); return exec(`docker build -t ${t1} -t ${t2} -f dockerfile-captain.release . && docker push ${t1} && docker push ${t2}`)
} })
.then(function (stdout) {
function buildAndPush() {
var t1 = publishedNameOnDockerHub + ':' + 'latest';
var t2 = publishedNameOnDockerHub + ':' + version;
let command = 'docker build -t ' + t1 + ' -t ' + t2 + ' -f dockerfile-captain.release . && docker push ' + t1 + ' && docker push ' + t2;
exec(command, function (err, stdout, stderr) {
if (err) {
console.log('ERROR');
console.log(err);
}
if (stdout) { if (stdout) {
console.log('stdout'); console.log('stdout');
console.log(stdout); console.log(stdout);
} }
if (stderr) {
console.log('stderr');
console.log(stderr);
}
}); })
.catch(function (err) {
} console.error(err)
process.exit(1)
})

View File

@@ -0,0 +1,4 @@
#!/bin/sh
docker build -t caprover/caprover-edge:latest -f dockerfile-captain.edge .
docker push caprover/caprover-edge:latest

28
dockerfile-captain.edge Normal file
View File

@@ -0,0 +1,28 @@
FROM node:10
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Build backend code
COPY package.json /usr/src/app/
RUN npm install && npm cache clean --force
COPY . /usr/src/app
RUN npm run build
RUN mv edge-overrride.json overrride.json
# Build frontend code
RUN git clone https://github.com/githubsaturn/caprover-frontend.git && \
cd caprover-frontend && \
npm install && npm cache clean --force && \
npm run build &&
mv ./build ../dist-frontend
ENV NODE_ENV production
ENV PORT 3000
EXPOSE 3000
CMD ["npm" , "start"]

4
edge-override.json Normal file
View File

@@ -0,0 +1,4 @@
{
"publishedNameOnDockerHub": "caprover/caprover-edge",
"version": "0.0.1"
}

View File

@@ -7,7 +7,7 @@
"dev": "echo 'RECOMPILING' && npx madge --circular --extensions ts ./ && rm -rf ./built && npx tsc && sudo ./dev-scripts/dev-reset-service.sh", "dev": "echo 'RECOMPILING' && npx madge --circular --extensions ts ./ && rm -rf ./built && npx tsc && sudo ./dev-scripts/dev-reset-service.sh",
"clean": "sudo ./dev-scripts/dev-clean-run-as-dev.sh", "clean": "sudo ./dev-scripts/dev-clean-run-as-dev.sh",
"tslint": "tslint -c tslint.json -p tsconfig.json", "tslint": "tslint -c tslint.json -p tsconfig.json",
"build": "npx tsc", "build": "rm -rf ./built && npx tsc",
"test": "jest" "test": "jest"
}, },
"dependencies": { "dependencies": {