-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 'iidy demo' for running packaged demo scripts
- updated the hello-world example to follow this format - this required exporting some types from index.ts. Further refactoring of that file is coming. - documentation for this feature is coming
- Loading branch information
Showing
9 changed files
with
243 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,8 @@ | ||
.PHONY : test | ||
.PHONY : demo | ||
|
||
.DEFAULT_GOAL := test | ||
.DEFAULT_GOAL := demo | ||
|
||
test: | ||
@mkdir -p demo-tmp/ | ||
@iidy render stack-args.yaml > demo-tmp/stack-args.yaml | ||
@grep StackName demo-tmp/stack-args.yaml | cut -f 2 -d':' > demo-tmp/stack-name | ||
@cp cfn-template.yaml demo-tmp/ | ||
test: demo | ||
|
||
@./.wrap-bash iidy help | grep --color=always -E 'create-stack .*$$|$$' | ||
@sleep 2 | ||
@./.wrap-bash cat demo-tmp/stack-args.yaml | ||
|
||
@./.wrap-bash iidy create-stack demo-tmp/stack-args.yaml | ||
|
||
@./.wrap-bash 'iidy list-stacks --region us-west-2 --profile sandbox | grep $$(cat demo-tmp/stack-name)' | ||
|
||
@./.wrap-bash grep owner demo-tmp/stack-args.yaml | ||
@./.wrap-bash sed -i.bak s/your-name/Tavis/ demo-tmp/stack-args.yaml | ||
@./.wrap-bash grep owner demo-tmp/stack-args.yaml | ||
|
||
@./.wrap-bash iidy help | grep --color=always -E 'update-stack .*$$|$$' | ||
@./.wrap-bash iidy update-stack demo-tmp/stack-args.yaml | ||
|
||
@./.wrap-bash iidy help | grep --color=always -E 'delete-stack .*$$|$$' | ||
@./.wrap-bash iidy delete-stack --region us-west-2 --profile sandbox --yes $$(cat demo-tmp/stack-name) | ||
|
||
@rm -r demo-tmp | ||
@echo "\033[91m Thanks for watching! \033[0m" | ||
demo: | ||
iidy demo demo-script.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env iidy demo | ||
$imports: | ||
nameSuffix: random:dashed-name | ||
StackName: "literal:iidy-demo-{{ nameSuffix }}" | ||
|
||
files: | ||
cfn-template.yaml: |- | ||
Parameters: | ||
Name: | ||
Type: String | ||
Resources: | ||
HelloWorld: | ||
Type: AWS::SNS::Topic | ||
Properties: | ||
DisplayName: !Sub "Hello ${Name}" | ||
.broken-cfn-template.yaml: |- | ||
Parameters: | ||
Name: | ||
Type: String | ||
Resources: | ||
HelloWorld: | ||
Type: AWS::SNS::Topic | ||
Properties: | ||
DisplayName: !Sub "Hello ${Name}" | ||
Foo: 1234 | ||
stack-args.yaml: |- | ||
StackName: "{{ StackName }}" # auto generated name | ||
Template: ./cfn-template.yaml | ||
Profile: sandbox | ||
Region: us-west-2 | ||
Parameters: | ||
Name: world | ||
Tags: | ||
owner: your-name | ||
project: iidy-demo | ||
environment: development | ||
lifetime: short | ||
demo: | ||
- setenv: | ||
AWS_REGION: us-west-2 | ||
AWS_PROFILE: sandbox | ||
"BASH_FUNC_highlight%%": | | ||
() { | ||
GREP_COLOR="1;32" grep --color -E "$1|$"; | ||
} | ||
- banner: In this *live* demo we will create, update, and then delete a simple stack. | ||
- sleep: 3 | ||
- ls . | ||
- banner: "We'll use these 2 files throughout the demo." | ||
- sleep: 3 | ||
- banner: "'cfn-template.yaml' is a very simple CloudFormation Template. It creates an SNS Topic." | ||
- cat cfn-template.yaml | ||
- sleep: 3 | ||
|
||
- banner: |- | ||
'stack-args.yaml' specifies the arguments for CloudFormation operations, including 'Template: cfn-template.yaml'. | ||
- sleep: 1 | ||
- cat stack-args.yaml | ||
- sleep: 4 | ||
|
||
- banner: "Let's review the help docs for 'iidy create-stack' and then use it." | ||
- sleep: 1 | ||
- iidy help | highlight 'create-stack .*' | ||
- sleep: 2 | ||
- iidy create-stack stack-args.yaml | ||
- sleep: 1 | ||
|
||
- banner: "We can see it with `iidy list-stacks` or `iidy describe-stack {{ StackName }}`." | ||
- "iidy list-stacks | grep {{ StackName }}" | ||
- sleep: 1 | ||
|
||
|
||
- banner: "Let's change the 'owner' tag and then update the stack." | ||
- sleep: 2 | ||
|
||
- highlight 'owner.*' < stack-args.yaml | ||
- "sed -i.bak s/your-name/${USER:-demouser}/ stack-args.yaml" | ||
- highlight 'owner.*' < stack-args.yaml | ||
- sleep: 2 | ||
|
||
- banner: "Now, we update-stack." | ||
- sleep: 1 | ||
- iidy update-stack stack-args.yaml | ||
|
||
- banner: "Let's break the template on purpose to see what an error looks like." | ||
- sleep: 3 | ||
- mv cfn-template.yaml cfn-template.yaml.bak | ||
- mv .broken-cfn-template.yaml cfn-template.yaml | ||
- iidy update-stack stack-args.yaml || true | ||
|
||
- banner: "We can now see the full stack history via 'describe-stack'." | ||
- sleep: 2 | ||
- iidy describe-stack {{ StackName }} | ||
- sleep: 2 | ||
|
||
#- mv cfn-template.yaml.bak cfn-template.yaml | ||
|
||
- sleep: 2 | ||
- banner: "We've created and updated the stack. Time to delete it." | ||
- sleep: 1 | ||
- "iidy delete-stack --region us-west-2 --profile sandbox --yes {{ StackName }}" | ||
|
||
- banner: "That's all folks. Thanks for watching!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as fs from 'fs'; | ||
import * as pathmod from 'path'; | ||
import * as child_process from 'child_process'; | ||
|
||
import * as _ from 'lodash'; | ||
import {Arguments} from 'yargs'; | ||
import * as tmp from 'tmp'; | ||
import * as cli from 'cli-color'; | ||
|
||
|
||
import timeout from './timeout'; | ||
import { transform } from './index'; | ||
import * as yaml from './yaml'; | ||
|
||
export async function demoMain(argv: Arguments): Promise<number> { | ||
const demoFile = argv.demoscript; | ||
const script0 = yaml.loadString(fs.readFileSync(demoFile), demoFile); | ||
const script: any = await transform(script0, demoFile); | ||
let tmpdir = tmp.dirSync(); | ||
try { | ||
for (let fp in script.files) { | ||
if (pathmod.isAbsolute(fp)) { | ||
throw new Error(`Illegal path ${fp}. Must be relative.`); | ||
} | ||
const fullpath = pathmod.resolve(tmpdir.name, fp) | ||
if (fp.indexOf(pathmod.sep) !== -1) { | ||
fs.mkdirSync(pathmod.dirname(fullpath)) | ||
} | ||
fs.writeFileSync(fullpath, script.files[fp]); | ||
} | ||
let bashEnv = _.merge({}, process.env); | ||
const exec = (command: string, captureAs?: string) => { | ||
let res = child_process.spawnSync( | ||
command, | ||
{shell: '/bin/bash', | ||
cwd: tmpdir.name, | ||
env: bashEnv, | ||
stdio: [0,1,2] }) | ||
if (res.status !== 0) { | ||
// TODO improve this | ||
throw new Error(`command failed: ${command}. exitcode=${res.status}`) | ||
} | ||
console.log(); | ||
|
||
} | ||
async function printComm(command: string) { | ||
process.stdout.write(cli.red('Shell Prompt > ')); | ||
process.stdout.write('\x1b[37m') | ||
for (let char of command) { | ||
process.stdout.write(char); | ||
await timeout(50); | ||
} | ||
process.stdout.write('\x1b[0m') | ||
console.log(); | ||
} | ||
// prompt:, capture: | ||
for (let command of script.demo) { | ||
if (typeof command === 'string') { | ||
await printComm(command); | ||
exec(command) | ||
} else if (command.do) { | ||
await printComm(command.show); | ||
exec(command.do) | ||
} else if (command.setenv) { | ||
_.extend(bashEnv, command.setenv) | ||
} else if (command.sleep) { | ||
await timeout(command.sleep * 1000); | ||
} else if (command.silent) { | ||
// logger | ||
exec(command.silent) | ||
} else if (command.banner) { | ||
console.log() | ||
const tty: any = process.stdout; | ||
const bannerFormat = cli.bgXterm(236); | ||
console.log(bannerFormat(' '.repeat(tty.columns))); | ||
for (const ln of command.banner.split('\n')) { | ||
const pad = (tty.columns - ln.length); | ||
console.log(bannerFormat(cli.bold(cli.yellow(' '.repeat(2) + ln + ' '.repeat(pad-2))))); | ||
} | ||
console.log(bannerFormat(' '.repeat(tty.columns))); | ||
console.log() | ||
} else { | ||
console.log(command); | ||
} | ||
} | ||
|
||
} catch (e) { | ||
throw e; | ||
} finally { | ||
// TODO check result | ||
child_process.execSync(`rm -r ${tmpdir.name}`, {cwd: tmpdir.name }) | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const timeout = (ms:number): Promise<void> => new Promise(res => setTimeout(res, ms)); | ||
export default timeout; |