Commit c2c0565d authored by Vladimir Kiryakov's avatar Vladimir Kiryakov
Browse files

cli scripts deploy, publish, mqtt connect with try

parent ec67ca65
......@@ -4,7 +4,7 @@ const fs = require('fs');
const prog = require('caporal');
const {
describeStacks, createStack, changeRegion, syncUI,
describeStacks, createOrUpdateStack, changeRegion, syncUI, deleteStack,
DEFAULT_INSTANCE_TYPE, DEFAULT_STACK_NAME
} = require('./s3-service');
......@@ -22,7 +22,7 @@ prog
changeRegion(options.region);
}
createStack({
createOrUpdateStack({
stackName: options.stackName,
key: options.key,
instanceType: options.instanceType,
......@@ -36,7 +36,7 @@ prog
});
prog
.command('sync-ui', 'Sync Web to s3')
.command('publish', 'Sync Web to s3')
.option('--bucket <bucket>', 'static website bucket', NO_VALIDATE, null, false)
.action((args, options) => {
syncUI(options.bucket).then(({stdout, stderr}) => {
......@@ -66,4 +66,23 @@ prog
});
});
prog
.command('delete', 'Delete CloudFormation template')
.option('--stackName <stackName>', 'Stack Name', NO_VALIDATE, DEFAULT_STACK_NAME)
.option('--region <region>', 'Stack Name', NO_VALIDATE, null, false)
.action((args, options) => {
if (options.region) {
changeRegion(options.region);
}
deleteStack(options.stackName)
.then((data) => {
console.log(JSON.stringify(data))
})
.catch((err) => {
console.error(err, err.message)
});
});
prog.parse(process.argv);
......@@ -60,6 +60,22 @@ const sync = (dirPath, bucket) => {
});
};
const createOrUpdateStack = ({stackName, key, instanceType, template}) => {
return new Promise((resolve, reject) => {
describeStacks(stackName)
.then((data) => {
return updateStack({stackName, key, instanceType, template});
})
.catch((err) => {
if (_.includes(err.message, 'does not exist')) {
createStack({stackName, key, instanceType, template});
} else {
return reject(err);
}
});
});
};
const createStack = ({stackName, key, instanceType, template}) => {
return new Promise((resolve, reject) => {
......@@ -82,6 +98,38 @@ const createStack = ({stackName, key, instanceType, template}) => {
});
};
const updateStack = ({stackName, key, instanceType, template}) => {
return new Promise((resolve, reject) => {
const params = {
StackName: stackName,
Parameters: [{
ParameterKey: 'KeyName',
ParameterValue: key
}, {
ParameterKey: 'InstanceType',
ParameterValue: instanceType
}],
TemplateBody: template,
};
cloudformation.updateStack(params, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
};
const deleteStack = (stackName) => {
return new Promise((resolve, reject) => {
const params = {
StackName: stackName,
};
cloudformation.deleteStack(params, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
};
module.exports = {
DEFAULT_STACK_NAME,
......@@ -90,7 +138,10 @@ module.exports = {
describeStacks,
syncUI,
sync,
createStack
createStack,
updateStack,
deleteStack,
createOrUpdateStack
};
......@@ -30,26 +30,29 @@ const mqttMiddleware = (function () {
url = action.payload.url;
topic = action.payload.topic;
reconnectPeriod = action.payload.reconnectPeriod;
try {
client = mqtt.connect(url, {reconnectPeriod});
client.on('connect', () => {
if (config.debug) console.log('MQTT: client connected');
store.dispatch(connected());
client.subscribe(topic);
});
client.on('close', () => {
if (config.debug) console.log('MQTT: client disconnected');
client.end();
store.dispatch(disconnected());
});
client.on('message', (msgTopic, msg) => {
client = mqtt.connect(url, {reconnectPeriod});
client.on('connect', () => {
if (config.debug) console.log('MQTT: client connected');
store.dispatch(connected());
client.subscribe(topic);
});
client.on('close', () => {
if (config.debug) console.log('MQTT: client disconnected');
client.end();
const message = msg.toString();
if (config.debug) console.info('MQTT: Message recieved.\nTopic: %s\nPayload: %s', topic, message);
if (_.isEqual(topic, msgTopic)) {
messages.push({topic, message: message, timestamp: new Date().getTime()});
}
});
} catch (e) {
store.dispatch(disconnected());
});
client.on('message', (msgTopic, msg) => {
const message =msg.toString();
if (config.debug) console.info('MQTT: Message recieved.\nTopic: %s\nPayload: %s', topic, message);
if (_.isEqual(topic, msgTopic)) {
messages.push({topic, message: message, timestamp: new Date().getTime()});
}
});
}
return next(action);
}, 100);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment