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

Commit a8d3837

Browse files
author
David Chung
authored
HyperKit instance plugin (#489)
Signed-off-by: David Chung <[email protected]>
1 parent 26dbe34 commit a8d3837

37 files changed

+2766
-82
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ endif
132132
$(call build_binary,infrakit-instance-vagrant,github.com/docker/infrakit/examples/instance/vagrant)
133133
$(call build_binary,infrakit-instance-maas,github.com/docker/infrakit/examples/instance/maas)
134134
$(call build_binary,infrakit-instance-docker,github.com/docker/infrakit/examples/instance/docker)
135+
$(call build_binary,infrakit-instance-hyperkit,github.com/docker/infrakit/cmd/instance/hyperkit)
135136
$(call build_binary,infrakit-event-time,github.com/docker/infrakit/examples/event/time)
136137

137138
cli: build-cli

cmd/cli/instance/instance.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,26 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
126126
///////////////////////////////////////////////////////////////////////////////////
127127
// destroy
128128
destroy := &cobra.Command{
129-
Use: "destroy <instance ID>",
129+
Use: "destroy <instance ID>...",
130130
Short: "Destroy the instance",
131131
RunE: func(cmd *cobra.Command, args []string) error {
132132

133-
if len(args) != 1 {
133+
if len(args) < 1 {
134134
cmd.Usage()
135135
os.Exit(1)
136136
}
137137

138-
instanceID := instance.ID(args[0])
139-
err := instancePlugin.Destroy(instanceID)
138+
for _, a := range args {
140139

141-
if err == nil {
140+
instanceID := instance.ID(a)
141+
err := instancePlugin.Destroy(instanceID)
142+
143+
if err != nil {
144+
return err
145+
}
142146
fmt.Println("destroyed", instanceID)
143147
}
144-
return err
148+
return nil
145149
},
146150
}
147151

cmd/instance/hyperkit/main.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"os/user"
6+
"path/filepath"
7+
8+
"github.com/docker/infrakit/pkg/cli"
9+
logutil "github.com/docker/infrakit/pkg/log"
10+
"github.com/docker/infrakit/pkg/plugin/instance/hyperkit"
11+
"github.com/docker/infrakit/pkg/plugin/metadata"
12+
instance_plugin "github.com/docker/infrakit/pkg/rpc/instance"
13+
metadata_plugin "github.com/docker/infrakit/pkg/rpc/metadata"
14+
instance_spi "github.com/docker/infrakit/pkg/spi/instance"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
var log = logutil.New("module", "instance/hyperkit")
19+
20+
func init() {
21+
logutil.Configure(&logutil.ProdDefaults)
22+
}
23+
24+
func main() {
25+
26+
cmd := &cobra.Command{
27+
Use: os.Args[0],
28+
Short: "HyperKit instance plugin",
29+
}
30+
31+
defaultVMDir := filepath.Join(getHome(), ".infrakit/hyperkit-vms")
32+
33+
name := cmd.Flags().String("name", "instance-hyperkit", "Plugin name to advertise for discovery")
34+
logLevel := cmd.Flags().Int("log", cli.DefaultLogLevel, "Logging level. 0 is least verbose. Max is 5")
35+
36+
vmDir := cmd.Flags().String("vm-dir", defaultVMDir, "Directory where to store VM state")
37+
hyperkitCmd := cmd.Flags().String("hyperkit-cmd", "hyperkit", "Path to HyperKit executable")
38+
vpnkitSock := cmd.Flags().String("vpnkit-sock", "auto", "Path to VPNKit UNIX domain socket")
39+
listen := cmd.Flags().String("listen", "localhost:24865", "Listens on port")
40+
41+
cmd.RunE = func(c *cobra.Command, args []string) error {
42+
43+
os.MkdirAll(*vmDir, os.ModePerm)
44+
45+
cli.SetLogLevel(*logLevel)
46+
cli.RunListener(*listen, *name,
47+
instance_plugin.PluginServer(hyperkit.NewPlugin(*vmDir, *hyperkitCmd, *vpnkitSock)),
48+
metadata_plugin.PluginServer(metadata.NewPluginFromData(
49+
map[string]interface{}{
50+
"version": cli.Version,
51+
"revision": cli.Revision,
52+
"implements": instance_spi.InterfaceSpec,
53+
},
54+
)),
55+
)
56+
return nil
57+
}
58+
59+
cmd.AddCommand(cli.VersionCommand())
60+
61+
if err := cmd.Execute(); err != nil {
62+
log.Crit("Error", "err", err)
63+
os.Exit(1)
64+
}
65+
}
66+
67+
func getHome() string {
68+
if usr, err := user.Current(); err == nil {
69+
return usr.HomeDir
70+
}
71+
return os.Getenv("HOME")
72+
}

docs/playbooks/intro/darwin/index.ikb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# Start the plugin as local daemon
3+
start-daemon : start-plugin.ikt
4+
5+
# Create a single instance
6+
provision-instance : provision-instance.ikt
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{/* Input to create instance using the HyperKit instance plugin */}}
2+
{{/* =% instanceProvision "instance-hyperkit" true %= */}}
3+
4+
{{ $user := flag "user" "string" "username" | prompt "Please enter your user name:" "string" (env "USER")}}
5+
{{ $linuxkitPath := flag "linuxkit-dir" "string" "Linuxkit directory" | prompt "Linuxkit directory?" "string" (cat (env "HOME") "/projects/src/github.com/linuxkit/linuxkit" | nospace) }}
6+
{{ $bundle := flag "bundle" "string" "Bundle prefix" | prompt "Bundle prefix?" "string"}}
7+
{{ $privateIP := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "192.168.65.101" nil }}
8+
LogicalID: {{ $privateIP }}
9+
Tags:
10+
infrakit.created: {{ now | htmlDate }}
11+
infrakit.user: {{ $user }}
12+
13+
Init: |
14+
#!/bin/bash
15+
echo Hello world!
16+
17+
Properties:
18+
kernel: {{ cat $linuxkitPath "/" $bundle "-bzImage" | nospace }}
19+
initrd: {{ cat $linuxkitPath "/" $bundle "-initrd.img" | nospace }}
20+
cpus: 1
21+
memory: 1024
22+
disk_size: 100
23+
cmdline: {{ include (cat "file://" $linuxkitPath "/" $bundle "-cmdline" | nospace) }}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
{{/* =% sh %= */}}
4+
5+
{{ $hyperkit := flag "start-hyperkit" "bool" "Start HYPERKIT plugin" | prompt "Start HYPERKIT plugin?" "bool" "no" }}
6+
7+
{{ if $hyperkit }}
8+
9+
echo "Starting HYPERKIT plugin. This must be running on the Mac as a daemon and not as a container"
10+
11+
infrakit-instance-hyperkit --log 5
12+
13+
{{ end }}

docs/playbooks/intro/index.ikb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ events : events/index.ikb
1212

1313
aws : aws/index.ikb
1414
gcp : gcp/index.ikb
15+
darwin : darwin/index.ikb

docs/playbooks/intro/start-infrakit.ikt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ docker run -d --volumes-from infrakit --name manager \
4343
{{ source "aws/start-plugin.ikt" }}
4444

4545
{{ source "gcp/start-plugin.ikt" }}
46+
47+
{{ source "darwin/start-plugin.ikt" }}

pkg/cli/context.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ const (
2828

2929
// Context is the context for the running module
3030
type Context struct {
31-
cmd *cobra.Command
32-
src string
33-
input io.Reader
34-
exec bool
35-
run func(string) error
36-
script string
37-
plugins func() discovery.Plugins
31+
cmd *cobra.Command
32+
src string
33+
input io.Reader
34+
exec bool
35+
template *template.Template
36+
run func(string) error
37+
script string
38+
plugins func() discovery.Plugins
3839
}
3940

4041
// NewContext creates a context
@@ -250,6 +251,24 @@ func (c *Context) Funcs() []template.Function {
250251
return c.defineFlag(n, ftype, desc, defaultValue)
251252
},
252253
},
254+
{
255+
Name: "include",
256+
Func: func(p string, opt ...interface{}) (string, error) {
257+
if c.exec {
258+
return c.template.Include(p, opt...)
259+
}
260+
return "", nil
261+
},
262+
},
263+
{
264+
Name: "source",
265+
Func: func(p string, opt ...interface{}) (string, error) {
266+
if c.exec {
267+
return c.template.Source(p, opt...)
268+
}
269+
return "", nil
270+
},
271+
},
253272
{
254273
Name: "cond",
255274
Func: func(b interface{}, optional ...interface{}) func() (bool, interface{}) {
@@ -378,7 +397,7 @@ func (c *Context) loadBackends() error {
378397
if err != nil {
379398
return err
380399
}
381-
fmt.Println(id)
400+
fmt.Println(*id)
382401
return nil
383402
}
384403
return ""
@@ -497,6 +516,7 @@ func (c *Context) Execute() error {
497516
}
498517

499518
c.exec = true
519+
c.template = t
500520
script, err := ConfigureTemplate(t, c.plugins).Render(c)
501521
if err != nil {
502522
return err

0 commit comments

Comments
 (0)