Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- "7"
services:
- mongodb
install:
- npm install
deploy:
provider: script
skip_cleanup: true
script: /bin/sh deploy.sh
on:
branch: master
script:
- npm test

157 changes: 157 additions & 0 deletions deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// deploy.js

var cmd = require('node-cmd');
var path, node_ssh, ssh, fs;
fs = require('fs');
path = require('path');
node_ssh = require('node-ssh');
ssh = new node_ssh();

// the method that starts the deployment process
function main() {
console.log('Deployment started.');
cloneRepo();
}

// responsible for cloning the repo
function cloneRepo() {
console.log('Cloning repo...');
// delete old copy of repo. Then, clone a fresh copy of repo from GitHub
cmd.get(
'rm -rf riot-express-todo-list && git clone https://github.com/Gwenvel/riot-express-todo-list',
function(err, data, stderr) {
console.log(
'cloneRepo callback\n\t err: ' +
err +
'\n\t data: ' +
data +
'\n\t stderr: ' +
stderr
);
if (err == null) {
sshConnect();
}
}
);
}

// transfers local project to the remote server
function transferProjectToRemote(failed, successful) {
return ssh.putDirectory(
__dirname + '/riot-express-todo-list',
'/home/ubuntu/riot-express-todo-list-temp',
{
recursive: true,
concurrency: 1,
validate: function(itemPath) {
const baseName = path.basename(itemPath);
return (
baseName.substr(0, 1) !== '.' && baseName !== 'node_modules' // do not allow dot files
); // do not allow node_modules
},
tick: function(localPath, remotePath, error) {
if (error) {
failed.push(localPath);
console.log('failed.push: ' + localPath);
} else {
successful.push(localPath);
console.log('successful.push: ' + localPath);
}
}
}
);
}

// creates a temporary folder on the remote server
function createRemoteTempFolder() {
return ssh.execCommand(
'rm -rf riot-express-todo-list-temp && mkdir riot-express-todo-list-temp',
{ cwd: '/home/ubuntu' }
);
}

// stops mongodb and node services on the remote server
function stopRemoteServices() {
return ssh.execCommand('npm stop && sudo service mongod stop', {
cwd: '/home/ubuntu'
});
}

// updates the project on the server
function updateRemoteApp() {
return ssh.execCommand(
'cp -r riot-express-todo-list-temp/* riot-express-todo-list/ && rm -rf riot-express-todo-list-temp/*',
{ cwd: '/home/ubuntu' }
);
}

// restart mongodb and node services on the remote server
function restartRemoteServices() {
return ssh.execCommand('npm start && sudo service mongod start', {
cwd: '/home/ubuntu'
});
}

// connect to the remote server
function sshConnect() {
console.log('Connecting to the server...');

ssh
.connect({
// TODO: ADD YOUR IP ADDRESS BELOW (e.g. '12.34.5.67')
host: '18.220.167.239',
username: 'ubuntu',
privateKey: 'hs-key.pem'
})
.then(function() {
console.log('SSH Connection established.');

// Create "hackathon-starter-temp" directory on remote server
console.log('Creating `riot-express-todo-list-temp` folder.');

return createRemoteTempFolder();
})
.then(function(result) {
const failed = [];
const successful = [];
if (result.stdout) {
console.log('STDOUT: ' + result.stdout);
}
if (result.stderr) {
console.log('STDERR: ' + result.stderr);
return Promise.reject(result.stderr);
}
return transferProjectToRemote(failed, successful);
})
.then(function(status) {
if (status) {
return stopRemoteServices();
} else {
return Promise.reject(failed.join(', '));
}
})
.then(function(status) {
if (status) {
return updateRemoteApp();
} else {
return Promise.reject(failed.join(', '));
}
})
.then(function(status) {
if (status) {
return restartRemoteServices();
} else {
return Promise.reject(failed.join(', '));
}
})
.then(function() {
console.log('Deployment complete.');
process.exit(0);
})
.catch(e => {
console.error(e);
process.exit(1);
});
}

main();
6 changes: 6 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "Installing 'node-cmd'"
npm install node-cmd
echo "Installing 'node-ssh'"
npm install node-ssh
echo "Running 'deploy.js'"
node deploy.js
23 changes: 23 additions & 0 deletions hs-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAntppABKX8xaYz9VT8v8TXxoBTbpIVIP77UNXilKLk2U10fTjdC3XH9qKoAuI
pWMYTa45KazcUviPzys2QwApbhSN089QzricW5fBIP1MmcJsS6wEnaKPV6CFmZ8gIGSruzP5qRfN
tiyLDTJLdfRGVf4e80ynjIU7mu2lpKhjJlLhUfyzt5mzBKUXvQ71+IXpAczhF4KUDMzhJ2Xb+9rS
DLnW98N/wM1VifDatvuIZUBLjYvwoYMUQ9QID/FgYBzMMYbmOeik8PwvSJiB5mPssVvKhdJR5V0o
OaVJqVLckI5eLZ2t56hQK4RVu00u2hFtgBTPNnZlHYuCFZbSG6xYSQIDAQABAoIBAEVIDrl4Yosj
1Nv4N5foheNZbS4gS8GImsydLUXnwHY1kyaR/s3B3GxJHWK2BQ/Uy1m93bJ+cM1DGT2KpCzOmkMn
db/mUGEIhTExW4ruzoTVFfRe/thdeNUH7pzUmOdn4LpR7dZAynhkrvS/dcfpFPAWWBtW2FBiH1/v
u+m3gx7IHBR0cXsKzhoUZNpsyTQPZVe9ha+yTyspvcok58DT/1IfkbLR9fB0Z64o+CaWF31Sdn81
qetUIJ+MEQdPgEJJJeNLSvm+AqjbJRtogDceHGVExoCkRgsmampK8pJINyjLkPkNEZHjkG8M3Lzl
afAckZEv4q8hyfDNgFQrtmKiDB0CgYEA/81w3nRU/UkCQh9SUCP6G2NSaMgXwnCacWhPF4jEiEHD
wO0n/aj8W4Q+DWre6q+h40vh8oqiixtBX/wPUu5zb1PwiXRFQN85bnFesnEe2zlXVyO5LMjwQG5S
V1BiIl00xdtC8t9ng+zzvgFTstAPv8+3g5BNa8oIunmlvxyThlMCgYEAnvnOrM+6h5mGmHj9/OM4
YUN958d+0c/ZZtFmA6g1pKd1jgbG9St0XdYCRyuwAql3JOYHqoR8GXUgM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