Skip to content

Commit 8c2ad94

Browse files
wfarnerDavid Chung
authored and
David Chung
committed
CLI: only accept configs by path name rather than stdin (docker-archive#229)
1 parent fc28fc5 commit 8c2ad94

File tree

11 files changed

+193
-248
lines changed

11 files changed

+193
-248
lines changed

cmd/cli/README.md

+24-80
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
InfraKit CLI
22
============
33

4-
This is a CLI for working with various infrakit plugins. In the simplest form, an InfraKit plugin
5-
is simply a daemon that communicates over unix domain sockets. InfraKit plugins can find each
6-
other by the socket files in a common directory. The CLI uses the common directory as a discovery
7-
mechanism and offers various subcommands for working with plugins. In general, plugin methods are
8-
exposed as verbs and configuration JSON can be read from local file or standard input.
4+
This is a developer CLI for working with various _InfraKit_ plugins. The CLI offers several subcommands for working
5+
with plugins. In general, plugin methods are exposed as verbs and configuration JSON can be read from local file.
96

107
## Building
118

@@ -24,7 +21,7 @@ instance-file ~/.infrakit/plugins/instance-file
2421
```
2522

2623
Once you know the plugins by name, you can make calls to them. For example, the instance plugin
27-
`instance-file` is a simple plugin that "provisions" an instance by writing the instructions to
24+
`instance-file` is a Plugin that "provisions" instances by writing the instructions to
2825
a file in a local directory.
2926

3027
You can access the following plugins and their methods via command line:
@@ -37,89 +34,35 @@ You can access the following plugins and their methods via command line:
3734

3835
Using the plugin `instance-file` as an example:
3936

40-
`describe` calls the `DescribeInstances` endpoint of the plugin:
37+
### Validate
4138

4239
```
43-
$ build/infrakit instance --name instance-file describe
44-
ID LOGICAL TAGS
45-
instance-1474850397 - group=test,instanceType=small
46-
instance-1474850412 - group=test2,instanceType=small
47-
instance-1474851747 logic2 instanceType=small,group=test2
48-
```
49-
50-
Validate - send the config JSON via stdin:
51-
52-
```
53-
$ build/infrakit instance --name instance-file validate << EOF
54-
> {
55-
> "Properties" : {
56-
> "version" : "v0.0.1",
57-
> "groups" : {
58-
> "managers" : {
59-
> "driver" : "infrakit/quorum"
60-
> },
61-
> "small" : {
62-
> "driver" : "infrakit/scaler",
63-
> "properties" : {
64-
> "size" : 3
65-
> }
66-
> },
67-
> "large" : {
68-
> "driver" : "infrakit/scaler",
69-
> "properties" : {
70-
> "size" : 3
71-
> }
72-
> }
73-
> }
74-
> },
75-
> "Tags" : {
76-
> "instanceType" : "small",
77-
> "group" : "test2"
78-
> },
79-
> "Init" : "#!/bin/sh\napt-get install -y wget",
80-
> "LogicalID" : "logic2"
81-
> }
82-
> EOF
40+
$ cat << EOF > instance.json
41+
{
42+
"Properties": {
43+
"version": "v0.0.1"
44+
},
45+
"Tags": {
46+
"instanceType": "small",
47+
"group": "test2"
48+
},
49+
"Init": "#!/bin/sh\napt-get install -y wget",
50+
"LogicalID": "logic2"
51+
}
52+
EOF
53+
54+
$ build/infrakit instance --name instance-file instance.json
8355
validate:ok
8456
```
8557

86-
Provision - send via stdin:
58+
### Provision
8759

8860
```
89-
$ build/infrakit instance --name instance-file provision << EOF
90-
> {
91-
> "Properties" : {
92-
> "version" : "v0.0.1",
93-
> "groups" : {
94-
> "managers" : {
95-
> "driver" : "infrakit/quorum"
96-
> },
97-
> "small" : {
98-
> "driver" : "infrakit/scaler",
99-
> "properties" : {
100-
> "size" : 3
101-
> }
102-
> },
103-
> "large" : {
104-
> "driver" : "infrakit/scaler",
105-
> "properties" : {
106-
> "size" : 3
107-
> }
108-
> }
109-
> }
110-
> },
111-
> "Tags" : {
112-
> "instanceType" : "small",
113-
> "group" : "test2"
114-
> },
115-
> "Init" : "#!/bin/sh\napt-get install -y wget",
116-
> "LogicalID" : "logic2"
117-
> }
118-
> EOF
61+
$ build/infrakit instance --name instance-file provision instance.json
11962
instance-1474873473
12063
```
12164

122-
List instances
65+
### List instances
12366

12467
```
12568
$ build/infrakit instance --name instance-file describe
@@ -129,7 +72,8 @@ instance-1474850412 - group=test2,instan
12972
instance-1474851747 logic2 group=test2,instanceType=small
13073
instance-1474873473 logic2 group=test2,instanceType=small
13174
```
132-
Destroy
75+
76+
### Destroy
13377

13478
```
13579
$ build/infrakit instance --name instance-file destroy instance-1474873473

cmd/cli/flavor.go

+44-15
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"strings"
7-
6+
log "github.com/Sirupsen/logrus"
87
"github.com/docker/infrakit/discovery"
98
"github.com/docker/infrakit/spi/flavor"
109
flavor_plugin "github.com/docker/infrakit/spi/http/flavor"
1110
"github.com/docker/infrakit/spi/instance"
1211
"github.com/spf13/cobra"
12+
"io/ioutil"
13+
"os"
14+
"strings"
1315
)
1416

1517
func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
@@ -34,12 +36,23 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
3436
cmd.PersistentFlags().StringVar(&name, "name", name, "Name of plugin")
3537

3638
validate := &cobra.Command{
37-
Use: "validate",
38-
Short: "validate input",
39+
Use: "validate <flavor configuration file>",
40+
Short: "validate a flavor configuration",
3941
RunE: func(cmd *cobra.Command, args []string) error {
4042
assertNotNil("no plugin", flavorPlugin)
4143

42-
alloc, err := flavorPlugin.Validate(json.RawMessage(getInput(args)))
44+
if len(args) != 1 {
45+
cmd.Usage()
46+
os.Exit(1)
47+
}
48+
49+
buff, err := ioutil.ReadFile(args[0])
50+
if err != nil {
51+
log.Error(err)
52+
os.Exit(1)
53+
}
54+
55+
alloc, err := flavorPlugin.Validate(json.RawMessage(buff))
4356
if err == nil {
4457
if buff, err2 := json.MarshalIndent(alloc, " ", " "); err2 == nil {
4558
fmt.Println("validate", string(buff))
@@ -53,22 +66,34 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
5366

5467
flavorPropertiesFile := ""
5568
prepare := &cobra.Command{
56-
Use: "prepare",
57-
Short: "prepare the provision data",
69+
Use: "prepare <instance Spec JSON file>",
70+
Short: "prepare provisioning inputs for an instance",
5871
RunE: func(cmd *cobra.Command, args []string) error {
5972
assertNotNil("no plugin", flavorPlugin)
6073

61-
buff := getInput(args)
62-
spec := instance.Spec{}
63-
err := json.Unmarshal(buff, &spec)
74+
if len(args) != 1 {
75+
cmd.Usage()
76+
os.Exit(1)
77+
}
78+
79+
buff, err := ioutil.ReadFile(args[0])
6480
if err != nil {
81+
log.Error(err)
82+
os.Exit(1)
83+
}
84+
85+
spec := instance.Spec{}
86+
if err := json.Unmarshal(buff, &spec); err != nil {
6587
return err
6688
}
6789

68-
// the flavor's config is in a flag.
69-
buffFlavor := getInput([]string{flavorPropertiesFile})
90+
buff, err = ioutil.ReadFile(args[0])
91+
if err != nil {
92+
log.Error(err)
93+
os.Exit(1)
94+
}
7095

71-
spec, err = flavorPlugin.Prepare(json.RawMessage(buffFlavor), spec)
96+
spec, err = flavorPlugin.Prepare(json.RawMessage(buff), spec)
7297
if err == nil {
7398
buff, err = json.MarshalIndent(spec, " ", " ")
7499
if err == nil {
@@ -78,14 +103,18 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
78103
return err
79104
},
80105
}
81-
prepare.Flags().StringVar(&flavorPropertiesFile, "properties", flavorPropertiesFile, "Path to flavor properties")
106+
prepare.Flags().StringVar(
107+
&flavorPropertiesFile,
108+
"properties",
109+
flavorPropertiesFile,
110+
"Path to flavor properties")
82111

83112
tags := []string{}
84113
id := ""
85114
logicalID := ""
86115
healthy := &cobra.Command{
87116
Use: "healthy",
88-
Short: "checks for health",
117+
Short: "checks if an instance is considered healthy",
89118
RunE: func(cmd *cobra.Command, args []string) error {
90119
assertNotNil("no plugin", flavorPlugin)
91120

0 commit comments

Comments
 (0)