Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cascade-examples
rigado-node-hello-world-frontend
Commits
c2c0565d
Commit
c2c0565d
authored
Apr 11, 2018
by
Vladimir Kiryakov
Browse files
cli scripts deploy, publish, mqtt connect with try
parent
ec67ca65
Changes
3
Hide whitespace changes
Inline
Side-by-side
cli/app.js
View file @
c2c0565d
...
...
@@ -4,7 +4,7 @@ const fs = require('fs');
const
prog
=
require
(
'
caporal
'
);
const
{
describeStacks
,
createStack
,
changeRegion
,
syncUI
,
describeStacks
,
create
OrUpdate
Stack
,
changeRegion
,
syncUI
,
deleteStack
,
DEFAULT_INSTANCE_TYPE
,
DEFAULT_STACK_NAME
}
=
require
(
'
./s3-service
'
);
...
...
@@ -22,7 +22,7 @@ prog
changeRegion
(
options
.
region
);
}
createStack
({
create
OrUpdate
Stack
({
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
);
cli/s3-service.js
View file @
c2c0565d
...
...
@@ -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
};
src/middlewares/mqtt.js
View file @
c2c0565d
...
...
@@ -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.
\n
Topic: %s
\n
Payload: %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.
\n
Topic: %s
\n
Payload: %s
'
,
topic
,
message
);
if
(
_
.
isEqual
(
topic
,
msgTopic
))
{
messages
.
push
({
topic
,
message
:
message
,
timestamp
:
new
Date
().
getTime
()});
}
});
}
return
next
(
action
);
},
100
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment