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

Commit 2779452

Browse files
author
David Chung
authored
Allow playbook templates to call instance plugins directly (#487)
Signed-off-by: David Chung <[email protected]>
1 parent 4725f4b commit 2779452

File tree

10 files changed

+157
-34
lines changed

10 files changed

+157
-34
lines changed

cmd/cli/manager/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
5656
return err
5757
}
5858

59-
log.Info("Found manager", "name", name, "leader", isLeader)
59+
log.Debug("Found manager", "name", name, "leader", isLeader)
6060
if isLeader {
6161

6262
groupPlugin = group_plugin.Adapt(rpcClient)
6363
groupPluginName = name
6464

65-
log.Info("Found manager", "name", name, "addr", endpoint.Address)
65+
log.Debug("Found manager", "name", name, "addr", endpoint.Address)
6666

6767
break
6868
}

docs/playbooks/intro/aws/provision-instance.ikt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# Input to create instance using the AWS instance plugin
2-
{{/* =% sh %= */}}
1+
{{/* Input to create instance using the AWS instance plugin */}}
2+
{{/* =% instanceProvision "instance-aws/ec2-instance" true %= */}}
33

44
{{ $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"}}
5+
{{ $imageId := flag "image-id" "string" "Image ID" | prompt "AMI?" "string"}}
76
{{ $instanceType := flag "instance-type" "string" "instance type" | prompt "Instance type?" "string"}}
7+
{{ $privateIP := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "" nil }}
8+
9+
810
{{ $keyName := flag "key" "string" "ssh key name" | prompt "SSH key?" "string"}}
911
{{ $az := flag "az" "string" "availability zone" | prompt "Availability zone?" "string"}}
1012
{{ $subnetId := flag "subnet" "string" "subnet id" | prompt "Subnet ID?" "string"}}
1113
{{ $securityGroupId := flag "security-group-id" "string" "security group id" | prompt "Security group ID?" "string" }}
1214

13-
infrakit --log 3 --log-stack --name instance-aws/ec2-instance instance provision -y - <<EOF
14-
1515
Tags:
16-
infrakit.name: {{ $name }}
1716
infrakit.created: {{ now | htmlDate }}
1817
infrakit.user: {{ $user }}
1918

2019
Init: |
2120
#!/bin/bash
2221
sudo apt-get update -y
2322
sudo apt-get install wget curl
23+
wget -q0- https://get.docker.com | sh
2424

2525
Properties:
2626
RunInstancesInput:
@@ -34,6 +34,7 @@ Properties:
3434
KeyName: {{ $keyName }}
3535
NetworkInterfaces:
3636
- AssociatePublicIpAddress: true
37+
PrivateIpAddress: {{ $privateIP }}
3738
DeleteOnTermination: true
3839
DeviceIndex: 0
3940
Groups:
@@ -48,6 +49,4 @@ Properties:
4849
SubnetId: null
4950
UserData: null
5051
Tags:
51-
infrakit.name: {{ $name }}
52-
53-
EOF
52+
infrakit.user: {{ $user }}

docs/playbooks/intro/aws/start-plugin.ikt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
{{ $aws := flag "start-aws" "bool" "Start AWS plugin" | prompt "Start AWS plugin?" "bool" "no" }}
44

55
{{ $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"}}
76
{{ $region := flag "region" "string" "aws region" | cond $aws | prompt "What's the region?" "string" "us-west-1"}}
87

98
{{ $awsImage := flag "aws-plugin" "string" "Image of the plugin" | cond $aws | prompt "What's the AWS plugin image?" "string" "infrakit/aws:dev" }}
109

10+
{{ $project := var "/project" }}
1111

1212
{{ if $aws }}
1313

@@ -17,10 +17,6 @@ echo "Starting AWS plugin"
1717
{{ $creds := (source (cat "file://" (env "HOME") "/.aws/credentials" | nospace) | iniDecode | k $profile ) }}
1818

1919

20-
{{ $infrakit := (cat (env "HOME") "/.infrakit/" | nospace) }}
21-
{{ $dockerEnvs := "-e INFRAKIT_HOME=/infrakit -e INFRAKIT_PLUGINS_DIR=/infrakit/plugins" }}
22-
23-
2420
{{/* Show helpful message to user if we can find the credentials */}}
2521
{{ if not (empty $creds) }}{{ echo "Found your credential for profile" $profile }}
2622
{{ end }}

docs/playbooks/intro/gcp/index.ikb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11

22
# Create a single instance
33
provision-instance : provision-instance.ikt
4+
5+
# Starts the plugin as a daemon (not container)
6+
start-daemon : start-plugin-daemon.ikt
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
# Input to create instance using the GCP instance plugin
2-
{{/* =% sh %= */}}
1+
{{/* Input to create instance using the GCP instance plugin */}}
2+
{{/* =% instanceProvision "instance-gcp" true %= */}}
33

44
{{ $user := flag "user" "string" "owner" | prompt "Owner?" "string" (env "USER") nil }}
5-
{{ $prefix := flag "prefix" "string" "Prefix to use" | prompt "Prefix for hostname:" "string" (env "USER") }}
6-
{{ $diskSize := flag "disk-size" "int" "Disk size in mb" | prompt "Disk size in MB?" "int" 100 }}
7-
{{ $machineType := flag "machine-type" "string" "Machine type" | prompt "Machine type?" "string" "n1-standard-1"}}
5+
{{ $image := flag "image-id" "string" "Image ID" | prompt "Image to boot?" "string" "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20161205" }}
6+
{{ $instanceType := flag "instance-type" "string" "Machine type" | prompt "Machine type?" "string" "n1-standard-1"}}
87
{{ $privateIP := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "10.128.0.10" nil }}
9-
{{ $image := flag "image" "string" "Image" | prompt "Image to boot?" "string" "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20161205" }}
108

11-
echo "===> Running with {{$user}}, {{$image}}, {{$privateIP}}:"
9+
{{ $diskSize := flag "disk-size" "int" "Disk size in mb" | prompt "Disk size in MB?" "int" 100 }}
1210

13-
infrakit --log 3 --log-stack --name instance-gcp instance provision -y - <<EOF
1411

1512
LogicalID : {{ $privateIP }}
1613
Tags:
17-
infrakit-user: {{ $user }}
18-
infrakit-created: {{ now | htmlDate }}
14+
infrakit.created: {{ now | htmlDate }}
15+
infrakit.user: {{ $user }}
1916

2017
Init: |
2118
#!/bin/bash
@@ -24,17 +21,15 @@ Init: |
2421
wget -q0- https://get.docker.com | sh
2522

2623
Properties:
27-
NamePrefix: {{ $prefix }}
24+
NamePrefix: {{ $user }}
2825
PrivateIP: {{ $privateIP }}
2926
Description: Some description
3027
Network: default
31-
MachineType: {{ $machineType }}
28+
MachineType: {{ $instanceType }}
3229
DiskSizeMb: {{ $diskSize }}
3330
DiskImage: {{ $image }}
3431
Scopes:
3532
- https://www.googleapis.com/auth/cloudruntimeconfig
3633
- https://www.googleapis.com/auth/logging.write
3734
Tags:
3835
- {{ $user }}
39-
40-
EOF
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
{{/* =% sh %= */}}
4+
5+
{{ $defaultCred := cat (env "HOME") "/.config/gcloud/application_default_credentials.json" | nospace }}
6+
{{ $credentials := flag "credential-path" "string" "Path to credentials.json" | prompt "Credentials JSON path?" "string" $defaultCred }}
7+
{{ $zone := flag "zone" "string" "GCP zone" | prompt "What's the zone?" "string" }}
8+
{{ $project := flag "project" "string" "Project name" | prompt "What's the name of the project?" "string" }}
9+
{{ $instanceImage := "infrakit/gcp:dev" }}
10+
{{ $gcpCredentials := (cat $credentials ":/infrakit/platforms/gcp/credentials.json" | nospace) }}
11+
12+
echo "Starting daemon"
13+
infrakit-instance-gcp \
14+
--namespace-tags {{cat "infrakit.scope=" $project | nospace}} \
15+
--zone {{ $zone }} --log 5 --project {{ $project }} \
16+
> {{env "INFRAKIT_HOME"}}/logs/instance-gcp.log 2>&1 &
17+
18+
echo "Tailing log"
19+
tail -f {{env "INFRAKIT_HOME"}}/logs/*.log

docs/playbooks/intro/gcp/start-plugin.ikt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
{{ $gcp := flag "start-gcp" "bool" "Start GCP plugin" | prompt "Start GCP plugin?" "bool" "no" }}
66
{{ $defaultCred := cat (env "HOME") "/.config/gcloud/application_default_credentials.json" | nospace }}
77
{{ $credentials := flag "credential-path" "string" "Path to credentials.json" | cond $gcp | prompt "Credentials JSON path?" "string" $defaultCred }}
8-
{{ $zone := flag "zone" "string" "GCP zone" | cond $gcp | prompt "What's the zone?" "string" "us-centra1-a"}}
8+
{{ $zone := flag "zone" "string" "GCP zone" | cond $gcp | prompt "What's the zone?" "string" "us-central1-f"}}
99

1010
{{ $gcpImage := flag "gcp-plugin" "string" "Image of the plugin" | cond $gcp | prompt "What's the GCP plugin image?" "string" "infrakit/gcp:dev" }}
1111

12+
{{ $project := var "/project" }}
1213

1314
{{ if $gcp }}
1415

15-
{{ $project := flag "project" "string" "Project name" | cond $gcp | prompt "What's the name of the project?" "string" }}
16+
echo "Starting GCP plugin"
1617

1718
{{ $gcpCredentials := (cat $credentials ":/infrakit/platforms/gcp/credentials.json" | nospace) }}
1819

1920

2021
# Starting docker container for instance plugin
21-
docker run -d --volumes-from infrakit --name instance-plugin \
22+
docker run -d --volumes-from infrakit --name instance-gcp \
2223
-v {{$gcpCredentials}} {{$gcpImage}} infrakit-instance-gcp \
2324
--namespace-tags {{cat "infrakit.scope=" $project | nospace}} \
2425
--zone {{ $zone }} --log 5 --project {{ $project }}

docs/playbooks/intro/start-infrakit.ikt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
{{ $image := flag "infrakit-image" "string" "Infrakit image" | prompt "Infrakit image?" "string" "infrakit/devbundle:dev" }}
66
{{ $port := flag "infrakit-port" "int" "Infrakit mux port" | prompt "Infrakit port for remote access?" "int" 24864 }}
77

8+
{{ $project := flag "project" "string" "Project name" | prompt "What's the name of the project?" "string" "testproject"}}
9+
10+
811
{{/* optional plugins */}}
912

1013

@@ -34,6 +37,8 @@ docker run -d --volumes-from infrakit --name manager \
3437
{{ $image }} infrakit-manager --proxy-for-group group-stateless os
3538

3639

40+
{{ var "/project" $project }}
41+
3742
{{/* Here we just source a file in the same folder for more reuse and modularity */}}
3843
{{ source "aws/start-plugin.ikt" }}
3944

docs/playbooks/intro/stop-infrakit.ikt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ docker ps -f ancestor=infrakit/devbundle:dev -qa | xargs docker rm
1616
docker ps -f ancestor=infrakit/aws:dev -qa | xargs docker stop
1717
docker ps -f ancestor=infrakit/aws:dev -qa | xargs docker rm
1818

19+
docker ps -f ancestor=infrakit/gcp:dev -qa | xargs docker stop
20+
docker ps -f ancestor=infrakit/gcp:dev -qa | xargs docker rm
21+
1922
docker network rm infrakit
2023

2124
{{ else }}

pkg/cli/context.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import (
99
"strings"
1010

1111
"github.com/docker/infrakit/pkg/discovery"
12+
"github.com/docker/infrakit/pkg/plugin"
13+
group_plugin "github.com/docker/infrakit/pkg/rpc/group"
14+
instance_plugin "github.com/docker/infrakit/pkg/rpc/instance"
15+
"github.com/docker/infrakit/pkg/spi/group"
16+
"github.com/docker/infrakit/pkg/spi/instance"
1217
"github.com/docker/infrakit/pkg/template"
18+
"github.com/docker/infrakit/pkg/types"
1319
"github.com/docker/infrakit/pkg/util/exec"
1420
"github.com/spf13/cobra"
1521
)
@@ -338,6 +344,102 @@ func (c *Context) loadBackends() error {
338344
}
339345
return ""
340346
})
347+
t.AddFunc("instanceProvision",
348+
func(name string, isYAML bool) string {
349+
c.run = func(script string) error {
350+
351+
// locate the plugin
352+
endpoint, err := c.plugins().Find(plugin.Name(name))
353+
if err != nil {
354+
return err
355+
}
356+
357+
plugin, err := instance_plugin.NewClient(plugin.Name(name), endpoint.Address)
358+
if err != nil {
359+
return err
360+
}
361+
362+
spec := instance.Spec{}
363+
if isYAML {
364+
y, err := types.AnyYAML([]byte(script))
365+
if err != nil {
366+
return err
367+
}
368+
if err := y.Decode(&spec); err != nil {
369+
return err
370+
}
371+
} else {
372+
if err := types.AnyString(script).Decode(&spec); err != nil {
373+
return err
374+
}
375+
}
376+
377+
id, err := plugin.Provision(spec)
378+
if err != nil {
379+
return err
380+
}
381+
fmt.Println(id)
382+
return nil
383+
}
384+
return ""
385+
})
386+
387+
t.AddFunc("managerCommit",
388+
func(isYAML, pretend bool) string {
389+
c.run = func(script string) error {
390+
391+
groups := []plugin.Spec{}
392+
if isYAML {
393+
y, err := types.AnyYAML([]byte(script))
394+
if err != nil {
395+
return err
396+
}
397+
if err := y.Decode(&groups); err != nil {
398+
return err
399+
}
400+
} else {
401+
if err := types.AnyString(script).Decode(&groups); err != nil {
402+
return err
403+
}
404+
}
405+
406+
// Check the list of plugins
407+
for _, gp := range groups {
408+
409+
endpoint, err := c.plugins().Find(gp.Plugin)
410+
if err != nil {
411+
return err
412+
}
413+
414+
// unmarshal the group spec
415+
spec := group.Spec{}
416+
if gp.Properties != nil {
417+
err = gp.Properties.Decode(&spec)
418+
if err != nil {
419+
return err
420+
}
421+
}
422+
423+
target, err := group_plugin.NewClient(endpoint.Address)
424+
425+
log.Debug("commit", "plugin", gp.Plugin, "address", endpoint.Address, "err", err, "spec", spec)
426+
427+
if err != nil {
428+
return err
429+
}
430+
431+
plan, err := target.CommitGroup(spec, pretend)
432+
if err != nil {
433+
return err
434+
}
435+
436+
fmt.Println("Group", spec.ID, "with plugin", gp.Plugin, "plan:", plan)
437+
}
438+
return nil
439+
}
440+
return ""
441+
})
442+
341443
t.AddFunc("sh",
342444
func(opts ...string) string {
343445
c.run = func(script string) error {

0 commit comments

Comments
 (0)