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

Commit 319152e

Browse files
author
David Chung
authored
Image instance plugin + packetnet instance plugin (#531)
Signed-off-by: David Chung <david.chung@docker.com>
1 parent bcc5b42 commit 319152e

34 files changed

Lines changed: 81158 additions & 0 deletions

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ $(call define_binary_target,infrakit-flavor-swarm,github.com/docker/infrakit/exa
145145
$(call define_binary_target,infrakit-flavor-vanilla,github.com/docker/infrakit/examples/flavor/vanilla)
146146
$(call define_binary_target,infrakit-flavor-zookeeper,github.com/docker/infrakit/examples/flavor/zookeeper)
147147
$(call define_binary_target,infrakit-instance-libvirt,github.com/docker/infrakit/cmd/instance/libvirt)
148+
$(call define_binary_target,infrakit-instance-packet,github.com/docker/infrakit/cmd/instance/packet)
149+
$(call define_binary_target,infrakit-instance-image,github.com/docker/infrakit/cmd/instance/image)
148150
$(call define_binary_target,infrakit-instance-file,github.com/docker/infrakit/examples/instance/file)
149151
$(call define_binary_target,infrakit-instance-terraform,github.com/docker/infrakit/examples/instance/terraform)
150152
$(call define_binary_target,infrakit-instance-vagrant,github.com/docker/infrakit/examples/instance/vagrant)
@@ -163,6 +165,8 @@ build-binaries: build/infrakit \
163165
build/infrakit-flavor-vanilla \
164166
build/infrakit-flavor-zookeeper \
165167
build/infrakit-instance-libvirt \
168+
build/infrakit-instance-packet \
169+
build/infrakit-instance-image \
166170
build/infrakit-instance-file \
167171
build/infrakit-instance-terraform \
168172
build/infrakit-instance-vagrant \

cmd/instance/image/config.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
gcp:
2+
store: google
3+
container: infrakit
4+
config:
5+
project_id: docker4x
6+
json: {{ include (list `file://` (env `HOME`) `/.config/gcloud/DevServiceAccountKey.json` | join ``) }}
7+
8+
local:
9+
store: local
10+
container: {{ list (env `INFRAKIT_HOME`) `instance-image` | join `/` }}
11+
config:
12+
path: {{ list (env `INFRAKIT_HOME`) `instance-image` | join `/` }}
13+
14+
{{ $creds := (source (cat `file://` (env `HOME`) `/.aws/credentials` | nospace) | iniDecode | k `default` ) }}
15+
aws:
16+
store: s3
17+
container: infrakit
18+
config:
19+
region: us-west-1
20+
access_key_id: {{ $creds.aws_access_key_id }}
21+
secret_key: {{ $creds.aws_secret_access_key }}

cmd/instance/image/index.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
upload-gcp : provision-gcp.ikt
4+
5+
upload-local : provision-local.ikt
6+
7+
upload-s3: provision-s3.ikt

cmd/instance/image/main.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"strings"
7+
8+
"github.com/docker/infrakit/pkg/cli"
9+
discovery_local "github.com/docker/infrakit/pkg/discovery/local"
10+
logutil "github.com/docker/infrakit/pkg/log"
11+
"github.com/docker/infrakit/pkg/plugin/instance/image"
12+
"github.com/docker/infrakit/pkg/plugin/metadata"
13+
instance_plugin "github.com/docker/infrakit/pkg/rpc/instance"
14+
metadata_plugin "github.com/docker/infrakit/pkg/rpc/metadata"
15+
instance_spi "github.com/docker/infrakit/pkg/spi/instance"
16+
"github.com/spf13/cobra"
17+
)
18+
19+
var log = logutil.New("module", "instance/image")
20+
21+
func init() {
22+
logutil.Configure(&logutil.ProdDefaults)
23+
}
24+
25+
func main() {
26+
27+
// Log setup
28+
logOptions := &logutil.ProdDefaults
29+
30+
if err := discovery_local.Setup(); err != nil {
31+
panic(err)
32+
}
33+
34+
var namespaceTags []string
35+
36+
cmd := &cobra.Command{
37+
Use: os.Args[0],
38+
Short: "Image instance plugin <config-url>",
39+
}
40+
41+
name := cmd.PersistentFlags().String("name", "instance-image", "Plugin name to advertise for discovery")
42+
logLevel := cmd.PersistentFlags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
43+
44+
cmd.PersistentFlags().AddFlagSet(cli.Flags(logOptions))
45+
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
46+
cmd.Flags().StringSliceVar(
47+
&namespaceTags,
48+
"namespace-tags",
49+
[]string{},
50+
"A list of key=value resource tags to namespace all resources created")
51+
52+
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
53+
logutil.Configure(logOptions)
54+
return nil
55+
}
56+
57+
// RUN -------------------------------------
58+
run := &cobra.Command{
59+
Use: "run",
60+
Short: "Run the plugin",
61+
}
62+
run.RunE = func(c *cobra.Command, args []string) error {
63+
64+
if len(args) != 0 {
65+
cmd.Usage()
66+
os.Exit(-1)
67+
}
68+
69+
cli.SetLogLevel(*logLevel)
70+
71+
namespace := map[string]string{}
72+
for _, tagKV := range namespaceTags {
73+
keyAndValue := strings.Split(tagKV, "=")
74+
if len(keyAndValue) != 2 {
75+
log.Error("Namespace tags must be formatted as key=value")
76+
os.Exit(1)
77+
}
78+
79+
namespace[keyAndValue[0]] = keyAndValue[1]
80+
}
81+
82+
imagePlugin, err := image.NewPlugin(namespace)
83+
if err != nil {
84+
return err
85+
}
86+
87+
cli.RunPlugin(*name,
88+
instance_plugin.PluginServer(imagePlugin),
89+
metadata_plugin.PluginServer(metadata.NewPluginFromData(
90+
map[string]interface{}{
91+
"version": cli.Version,
92+
"revision": cli.Revision,
93+
"implements": instance_spi.InterfaceSpec,
94+
},
95+
)),
96+
)
97+
return nil
98+
}
99+
100+
cmd.AddCommand(cli.VersionCommand(), run)
101+
102+
if err := cmd.Execute(); err != nil {
103+
log.Crit("Error", "err", err)
104+
os.Exit(1)
105+
}
106+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{{/* Input to create instance using the image instance plugin */}}
2+
{{/* =% instanceProvision `instance-image` true %= */}}
3+
4+
{{ $dir := flag `linuxkit-dir` `string` `LinuxKit dir` | prompt `LinuxKit dir?` `string` (env `PWD`) }}
5+
{{ $bundle := flag `bundle-name` `string` `Bundle name` | prompt `Bundle name?` `string` `ssh` }}
6+
{{ $user := flag `user` `string` `owner` | prompt `Owner?` `string` (env `USER`) nil }}
7+
8+
{{ $defaultPath := (list `file://` (env `HOME`) `/.config/gcloud/DevServiceAccountKey.json` | join ``) }}
9+
{{ $serviceAccountKey := flag `service-account-key-json` `string` `ServiceAccountKey path` | prompt `ServiceAccountKey path?` `string` $defaultPath }}
10+
11+
Tags:
12+
infrakit.created: {{ now | htmlDate }}
13+
infrakit.user: {{ $user }}
14+
15+
Properties:
16+
sources:
17+
{{$bundle}}-kernel: {{ list `file://` $dir `/` $bundle `-kernel` | join `` }}
18+
{{$bundle}}-initrd: {{ list `file://` $dir `/` $bundle `-initrd.img` | join `` }}
19+
{{$bundle}}.img.tar.gz: {{ list `file://` $dir `/` $bundle `.img.tar.gz` | join `` }}
20+
options:
21+
store: google
22+
container: infrakit
23+
config:
24+
project_id: docker4x
25+
json: {{ include $serviceAccountKey }}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{/* Input to create instance using the image instance plugin */}}
2+
{{/* =% instanceProvision `instance-image` true %= */}}
3+
4+
{{ $dir := flag `linuxkit-dir` `string` `LinuxKit dir` | prompt `LinuxKit dir?` `string` (env `PWD`) }}
5+
{{ $bundle := flag `bundle-name` `string` `Bundle name` | prompt `Bundle name?` `string` `ssh` }}
6+
{{ $user := flag `user` `string` `owner` | prompt `Owner?` `string` (env `USER`) nil }}
7+
8+
Tags:
9+
infrakit.created: {{ now | htmlDate }}
10+
infrakit.user: {{ $user }}
11+
12+
Properties:
13+
sources:
14+
{{$bundle}}-kernel: {{ list `file://` $dir `/` $bundle `-kernel` | join `` }}
15+
{{$bundle}}-initrd: {{ list `file://` $dir `/` $bundle `-initrd.img` | join `` }}
16+
{{$bundle}}.img.tar.gz: {{ list `file://` $dir `/` $bundle `.img.tar.gz` | join `` }}
17+
options:
18+
store: local
19+
container: {{ list (env `INFRAKIT_HOME`) `instance-image` | join `/` }}
20+
config:
21+
path: {{ list (env `INFRAKIT_HOME`) `instance-image` | join `/` }}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{{/* Input to create instance using the image instance plugin */}}
2+
{{/* =% instanceProvision `instance-image` true %= */}}
3+
4+
{{ $dir := flag `linuxkit-dir` `string` `LinuxKit dir` | prompt `LinuxKit dir?` `string` (env `PWD`) }}
5+
{{ $bundle := flag `bundle-name` `string` `Bundle name` | prompt `Bundle name?` `string` `ssh` }}
6+
{{ $user := flag `user` `string` `owner` | prompt `Owner?` `string` (env `USER`) nil }}
7+
{{ $creds := (source (cat `file://` (env `HOME`) `/.aws/credentials` | nospace) | iniDecode | k `default` ) }}
8+
9+
Tags:
10+
infrakit.created: {{ now | htmlDate }}
11+
infrakit.user: {{ $user }}
12+
13+
Properties:
14+
sources:
15+
{{$bundle}}-kernel: {{ list `file://` $dir `/` $bundle `-kernel` | join `` }}
16+
{{$bundle}}-initrd: {{ list `file://` $dir `/` $bundle `-initrd.img` | join `` }}
17+
{{$bundle}}.img.tar.gz: {{ list `file://` $dir `/` $bundle `.img.tar.gz` | join `` }}
18+
options:
19+
store: s3
20+
container: infrakit
21+
config:
22+
region: us-west-1
23+
access_key_id: {{ $creds.aws_access_key_id }}
24+
secret_key: {{ $creds.aws_secret_access_key }}

cmd/instance/packet/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"strings"
6+
7+
"github.com/docker/infrakit/pkg/cli"
8+
logutil "github.com/docker/infrakit/pkg/log"
9+
"github.com/docker/infrakit/pkg/plugin/instance/packet"
10+
instance_plugin "github.com/docker/infrakit/pkg/rpc/instance"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var log = logutil.New("module", "plugin/instance/packetnet")
15+
16+
func main() {
17+
18+
var namespaceTags []string
19+
20+
cmd := &cobra.Command{
21+
Use: os.Args[0],
22+
Short: "Packetnet instance plugin",
23+
}
24+
name := cmd.Flags().String("name", "instance-packet", "Plugin name to advertise for discovery")
25+
logLevel := cmd.Flags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
26+
apiToken := cmd.Flags().String("access-token", "", "API token")
27+
projectID := cmd.Flags().String("project-id", "", "Project ID")
28+
29+
cmd.Flags().StringSliceVar(
30+
&namespaceTags,
31+
"namespace-tags",
32+
[]string{},
33+
"A list of key=value resource tags to namespace all resources created")
34+
35+
cmd.Run = func(c *cobra.Command, args []string) {
36+
cli.SetLogLevel(*logLevel)
37+
38+
namespace := map[string]string{}
39+
for _, tagKV := range namespaceTags {
40+
keyAndValue := strings.Split(tagKV, "=")
41+
if len(keyAndValue) != 2 {
42+
log.Error("Namespace tags must be formatted as key=value")
43+
os.Exit(1)
44+
}
45+
46+
namespace[keyAndValue[0]] = keyAndValue[1]
47+
}
48+
49+
cli.RunPlugin(*name, instance_plugin.PluginServer(packet.NewPlugin(*projectID, *apiToken, namespace)))
50+
}
51+
52+
cmd.AddCommand(cli.VersionCommand())
53+
54+
err := cmd.Execute()
55+
if err != nil {
56+
log.Crit("error", "err", err)
57+
os.Exit(1)
58+
}
59+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{/* Input to create instance using the Packet instance plugin */}}
2+
{{/* =% instanceProvision "instance-packet" true %= */}}
3+
4+
{{ $project := var "/project" | flag "project" "string" "Project name" | prompt "Project?" "string" "myproject" }}
5+
6+
{{ $linuxkitPath := flag "bundle-path" "string" "Bundle directory" | prompt "Bundle directory?" "string" (env "PWD") }}
7+
{{ $bundle := flag "bundle" "string" "Bundle prefix" | prompt "Bundle prefix?" "string" "sshd"}}
8+
{{ $baseURL := flag "base-url" "string" "Base URL" | prompt "Base URL (from upload or ngrok)?" "string" "https://76c08e79.ngrok.io" }}
9+
{{ $facility := flag "facility" "string" "Facility" | prompt "Facility?" "string" "sjc1" }}
10+
{{ $checksum := fetch (cat "file://" $linuxkitPath "/" $bundle "-initrd.img" | nospace) | sha256sum }}
11+
12+
Tags:
13+
infrakit.created: {{ now | htmlDate }}
14+
infrakit.project: {{ $project }}
15+
infrakit.checksum: {{ $checksum }}
16+
17+
Properties:
18+
plan: baremetal_0
19+
facility: {{ $facility }}
20+
operating_system: custom_ipxe
21+
billing_cycle: hourly
22+
userdata: |
23+
#!ipxe
24+
25+
dhcp
26+
set base-url {{ $baseURL }}
27+
set kernel-params ip=dhcp nomodeset ro serial console=ttyS1,115200
28+
kernel ${base-url}/{{$bundle}}-kernel ${kernel-params}
29+
initrd ${base-url}/{{$bundle}}-initrd.img
30+
boot
31+
hostname_prefix: infrakit-test
32+
checksum: {{ $checksum }}

cmd/instance/packet/start.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
{{/* =% sh %= */}}
4+
5+
# This assumes there's a file that has the auth token and project id like
6+
# {
7+
# "token": "Z37q1Ua8zLvquNUyq2a6QiWSJf5jDR9C",
8+
# "projectID": "6f3ec5a6-1a90-4675-b27a-3727448730f7"
9+
# }
10+
11+
12+
{{ $project := var "/project" | flag `project` `string` `` | prompt `Project?` `string` `myproject`}}
13+
{{ $authFile := list `file://` (env `HOME`) `/.config/packet/auth.json` | join `` }}
14+
{{ $auth := include $authFile | jsonDecode }}
15+
16+
{{ $t := flag `token` `string` `Token` | prompt `Token?` `string` $auth.token }}
17+
{{ $p := flag `project-id` `string` `Project ID` | prompt `Project ID?` `string` $auth.projectID }}
18+
19+
infrakit-instance-packet \
20+
--project-id {{ $p }} --access-token {{ $t }} \
21+
--namespace-tags {{cat "infrakit.scope=" $project | nospace}} \
22+
--log 5 > {{env `INFRAKIT_HOME`}}/logs/instance-packet.log 2>&1 &
23+
24+
{{ var `started-packet` true }}
25+
26+
tail -f {{env `INFRAKIT_HOME`}}/logs/instance-packet.log

0 commit comments

Comments
 (0)