Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 6a76266

Browse files
author
David Chung
authored
conditional execution in template pipeline; playbook example (#482)
Signed-off-by: David Chung <[email protected]>
1 parent a4210cd commit 6a76266

File tree

12 files changed

+259
-7
lines changed

12 files changed

+259
-7
lines changed

docs/playbooks/intro/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Intro to Infrakit
2+
=================
3+
4+
This playbook introduces the user to Infrakit and some basic operations.
5+
6+
+ Start up infrakit in Docker containers
7+
+ Stop infrakit
8+
+ Provision instances
9+
+ Metadata
10+
+ Events
11+
12+
For more information, see [infrakit website](https://github.com/docker/infrakit)

docs/playbooks/intro/aws/index.ikb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# Create a single instance
3+
provision-instance : provision-instance.ikt
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Input to create instance using the AWS instance plugin
2+
{{/* =% sh %= */}}
3+
4+
{{ $user := flag "user" "string" "username" | prompt "Please enter your user name:" "string" (env "USER")}}
5+
{{ $name := flag "name" "string" "name" | prompt "Name?" "string"}}
6+
{{ $imageId := flag "ami" "string" "ami" | prompt "AMI?" "string"}}
7+
{{ $instanceType := flag "instance-type" "string" "instance type" | prompt "Instance type?" "string"}}
8+
{{ $keyName := flag "key" "string" "ssh key name" | prompt "SSH key?" "string"}}
9+
{{ $az := flag "az" "string" "availability zone" | prompt "Availability zone?" "string"}}
10+
{{ $subnetId := flag "subnet" "string" "subnet id" | prompt "Subnet ID?" "string"}}
11+
{{ $securityGroupId := flag "security-group-id" "string" "security group id" | prompt "Security group ID?" "string" }}
12+
13+
infrakit --log 3 --log-stack --name instance-aws/ec2-instance instance provision -y - <<EOF
14+
15+
Tags:
16+
infrakit.name: {{ $name }}
17+
infrakit.created: {{ now | htmlDate }}
18+
infrakit.user: {{ $user }}
19+
20+
Init: |
21+
#!/bin/bash
22+
sudo apt-get update -y
23+
sudo apt-get install wget curl
24+
25+
Properties:
26+
RunInstancesInput:
27+
BlockDeviceMappings: null
28+
DisableApiTermination: null
29+
EbsOptimized: null
30+
IamInstanceProfile: null
31+
ImageId: {{ $imageId }}
32+
InstanceInitiatedShutdownBehavior: null
33+
InstanceType: {{ $instanceType }}
34+
KeyName: {{ $keyName }}
35+
NetworkInterfaces:
36+
- AssociatePublicIpAddress: true
37+
DeleteOnTermination: true
38+
DeviceIndex: 0
39+
Groups:
40+
- {{ $securityGroupId }}
41+
NetworkInterfaceId: null
42+
SubnetId: {{ $subnetId }}
43+
Placement:
44+
Affinity: null
45+
AvailabilityZone: {{ $az }}
46+
Tenancy: null
47+
RamdiskId: null
48+
SubnetId: null
49+
UserData: null
50+
Tags:
51+
infrakit.name: {{ $name }}
52+
53+
EOF
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{/* =% sh %= */}}
2+
3+
{{ $aws := flag "start-aws" "bool" "Start AWS plugin" | prompt "Start AWS plugin?" "bool" "no" }}
4+
5+
{{ $profile := flag "aws-cred-profile" "string" "Profile name for credentials" | cond $aws | prompt "What's the profile for your .aws/credentials?" "string" "default" }}
6+
{{ $project := flag "project" "string" "Project name" | cond $aws | prompt "What's the name of the project?" "string" "testproject"}}
7+
{{ $region := flag "region" "string" "aws region" | cond $aws | prompt "What's the region?" "string" "us-west-1"}}
8+
9+
{{ $awsImage := flag "aws-plugin" "string" "Image of the plugin" | cond $aws | prompt "What's the AWS plugin image?" "string" "infrakit/aws:dev" }}
10+
11+
12+
{{ if $aws }}
13+
14+
echo "Starting AWS plugin"
15+
16+
{{/* Pick a credential from the local user's ~/.aws folder. You should have this if you use awscli. */}}
17+
{{ $creds := (source (cat "file://" (env "HOME") "/.aws/credentials" | nospace) | iniDecode | k $profile ) }}
18+
19+
20+
{{ $infrakit := (cat (env "HOME") "/.infrakit/" | nospace) }}
21+
{{ $dockerEnvs := "-e INFRAKIT_HOME=/infrakit -e INFRAKIT_PLUGINS_DIR=/infrakit/plugins" }}
22+
23+
24+
{{/* Show helpful message to user if we can find the credentials */}}
25+
{{ if not (empty $creds) }}{{ echo "Found your credential for profile" $profile }}
26+
{{ end }}
27+
28+
{{ if not (empty $creds) }}
29+
docker run -d --volumes-from infrakit --name instance-aws {{ $awsImage }} \
30+
infrakit-instance-aws --name instance-aws \
31+
--namespace-tags {{cat "infrakit.scope=" $project | nospace}} \
32+
--access-key-id {{ $creds.aws_access_key_id }} \
33+
--secret-access-key {{ $creds.aws_secret_access_key }} \
34+
--region {{ $region }} --log 5
35+
{{ end }}
36+
37+
{{ end }}

docs/playbooks/intro/events/index.ikb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Another index file that tells infrakit what other commands are here.
2+
3+
timer : tail.ikt # TODO - maybe add ability to pass in args?
4+
ls : ls.ikt

docs/playbooks/intro/events/ls.ikt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
{{/* =% sh %= */}}
3+
4+
5+
infrakit -H http://localhost:4358 event ls -al

docs/playbooks/intro/events/tail.ikt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
{{/* =% sh %= */}}
3+
4+
5+
{{ $topic := flag "topic" "string" "Topic to watch" | prompt "What topic?" "string" "event-time/timer/msec/100" }}
6+
7+
infrakit -H http://localhost:4358 event tail {{ $topic }}

docs/playbooks/intro/index.ikb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# An index file. This file can be of any extension but the convention is to use .ikb (for InfraKit playBook)
3+
4+
# Starts the infrakit daemons using a common data volume for sockets and files
5+
start-infrakit : start-infrakit.ikt
6+
7+
# Stops all the infrakit daemons
8+
stop-infrakit : stop-infrakit.ikt
9+
10+
# Here is a submodule. We point to a index.ikb (you can call whatever but nice to have convention) in a sub folder:
11+
events : events/index.ikb
12+
13+
aws : aws/index.ikb
14+
gcp : gcp/index.ikb
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Starts all infrakit containers
2+
3+
{{/* =% sh %= */}}
4+
5+
{{ $image := flag "infrakit-image" "string" "Infrakit image" | prompt "Infrakit image?" "string" "infrakit/devbundle:dev" }}
6+
{{ $port := flag "infrakit-port" "int" "Infrakit mux port" | prompt "Infrakit port for remote access?" "int" 24864 }}
7+
8+
{{/* optional plugins */}}
9+
10+
11+
echo "Starting up infrakit base... You can connect to it at infrakit -H localhost:{{$port}}"
12+
13+
docker run -d --name infrakit \
14+
-v /var/run/docker.sock:/var/run/docker.sock \
15+
-v `pwd`:/project \
16+
-p {{$port}}:24864 \
17+
{{ $image }} infrakit util mux --log 5
18+
19+
# Start a development mode fileserver
20+
# Start a network for connecting to it
21+
docker network create --attachable infrakit
22+
docker run -d --volumes-from infrakit --name playbooks.com \
23+
--network infrakit \
24+
--expose 80 \
25+
{{ $image }} infrakit util fileserver --listen :80 /project
26+
27+
docker run -d --volumes-from infrakit --name time {{ $image }} infrakit-event-time
28+
docker run -d --volumes-from infrakit --name vanilla {{ $image }} infrakit-flavor-vanilla
29+
docker run -d --volumes-from infrakit --name group-stateless {{ $image }} infrakit-group-default --name group-stateless
30+
31+
# The leader file -- only required for local store
32+
docker run --rm --volumes-from infrakit {{ $image }} /bin/sh -c "echo group > /infrakit/leader"
33+
docker run -d --volumes-from infrakit --name manager \
34+
{{ $image }} infrakit-manager --proxy-for-group group-stateless os
35+
36+
37+
{{/* Here we just source a file in the same folder for more reuse and modularity */}}
38+
{{ source "aws/start-plugin.ikt" }}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Stops all infrakit containers
2+
3+
{{/* This is a directive that says run this template as a sh script */}}
4+
{{/* =% sh %= */}}
5+
6+
{{/* We use a prompt to ask the user if we really want to stop. Note the nil at the end, it's required. */}}
7+
{{ $ok := prompt "Are you really sure you want to stop infrakit?" "bool" "no" nil }}
8+
9+
{{ if $ok }}
10+
11+
echo "Stopping Infrakit"
12+
13+
docker ps -f ancestor=infrakit/devbundle:dev -qa | xargs docker stop
14+
docker ps -f ancestor=infrakit/devbundle:dev -qa | xargs docker rm
15+
16+
docker ps -f ancestor=infrakit/aws:dev -qa | xargs docker stop
17+
docker ps -f ancestor=infrakit/aws:dev -qa | xargs docker rm
18+
19+
docker network rm infrakit
20+
21+
{{ else }}
22+
23+
echo "Not stopping Infrakit"
24+
25+
{{ end }}

0 commit comments

Comments
 (0)