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

Working #5

Open
wants to merge 5 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
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js
node_js:
- "10"
- "8"
- "17"
services: mongodb
cache:
directories:
- "node_modules"
sudo: false
install:
- npm install
script:
- npm test
deploy:
- provider: script
skip_cleanup: true
script: chmod +x ./deploy.sh && ./deploy.sh
on:
branch: master
27 changes: 27 additions & 0 deletions automation-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAi24xB1lk97SthmntOaq+VDcPEOFcRyHOgjVLJAokV5LPTpxW
LaeNxaXF+YG0vHiEj7MuAMQ4RWH3f0Sd9uE/dZrHzbkN2XWeTNrpOU5eQ7SZDs4L
RRqOzWZV6r2IWJc9EWTiXaW9WMt1xXwnxpkEWoUfUcZTZi320an11a8AMawcGIH0
5OR4PFqCKjGj8ahW1pmX6d6RjIvDPZL1H1tvibuTPdtMvdfsefxMLkWUvsOaVEZL
RDx2x3Olz2wBCVbHkKp8TU4fZXG097s+TVW+ZAVtGyu2wdnfvj7cKy5BmN8iSrj6
6GPgI/q+hMT2jM8t0KoguCG0rrVe7hZcYERo0wIDAQABAoIBAEM1PEDUoEyeNL6D
tKuFV/T0DTkPyBEphIdS9QNMODRWOcMkrI2ZJEdPHtyj3uCyHwO9VMFd09nt3QTl
qFbmQFaJ4yGQv9JAVauqIZ1T3zA3j0eJnfkTUh5D48ogzeYWEIOf41VMnLZ+bChj
LjEWdPjFDlG2jWDFayG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N+yAyBCW
-----END RSA PRIVATE KEY-----
1 change: 0 additions & 1 deletion client/tag-todo.jade
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ todo
li(each="{items}")
span \#{id + 1}
span(class="{completed: done}") {title}
 
a(if="{!done}", onclick="{ parent.done }") done
a(if="{done}", onclick="{ parent.undone }") undone

Expand Down
148 changes: 148 additions & 0 deletions deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
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.');
sshConnect();
}

// installs PM2
function installPM2() {
return ssh.execCommand(
'sudo npm install pm2 -g', {
cwd: '/home/ubuntu'
});
}

// transfers local project to the remote server
function transferProjectToRemote(failed, successful) {
return ssh.putDirectory(
'../riot',
'/home/ubuntu/riot-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-temp && mkdir riot-temp', {
cwd: '/home/ubuntu'
});
}

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

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

// restart mongodb and node services on the remote server
function restartRemoteServices() {
return ssh.execCommand(
'cd riot && sudo service mongod start && pm2 start app.js', {
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: '34.201.37.248',
username: 'ubuntu',
privateKey: 'automation-key.pem'
})
.then(function() {
console.log('SSH Connection established.');
console.log('Installing PM2...');
return installPM2();
})
.then(function() {
console.log('Creating `riot-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);
}
console.log('Transferring files to remote server...');
return transferProjectToRemote(failed, successful);
})
.then(function(status) {
if (status) {
console.log('Stopping remote services.');
return stopRemoteServices();
} else {
return Promise.reject(failed.join(', '));
}
})
.then(function(status) {
if (status) {
console.log('Updating remote app.');
return updateRemoteApp();
} else {
return Promise.reject(failed.join(', '));
}
})
.then(function(status) {
if (status) {
console.log('Restarting remote services...');
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
Loading