/
run.go
63 lines (58 loc) · 1.51 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cmd
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/axsh/openvdc/api"
"github.com/axsh/openvdc/cmd/openvdc/internal/util"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
func init() {
runCmd.Flags().Bool("auto-recovery", true, "Set auto recovery flag")
}
var runCmd = &cobra.Command{
Use: "run [ResourceTemplate ID/URI]",
Short: "Run an instance",
Long: "Run an instance",
Example: `
% openvdc run centos/7/lxc
% openvdc run https://raw.githubusercontent.com/axsh/openvdc/master/templates/centos/7/lxc.json
` + util.ExampleMergeTemplateOptions("openvdc run"),
DisableFlagParsing: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := util.PreRunHelpFlagCheckAndQuit(cmd, args)
if err != nil {
return err
}
err = cmd.ParseFlags(args)
if err != nil {
fmt.Println(err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
left := cmd.Flags().Args()
if len(left) < 1 {
return pflag.ErrHelp
}
templateSlug := left[0]
for i, a := range args {
if a == templateSlug {
left = args[i:]
break
}
}
req := prepareCreateAPICall(templateSlug, left, cmd.Flags())
return util.RemoteCall(func(conn *grpc.ClientConn) error {
c := api.NewInstanceClient(conn)
res, err := c.Run(context.Background(), req)
if err != nil {
log.WithError(err).Fatal("Disconnected abnormaly")
return err
}
fmt.Println(res.GetInstanceId())
return err
})
}}