forked from linuxkit/linuxkit
/
run_openstack.go
88 lines (73 loc) · 2.47 KB
/
run_openstack.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"strings"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
const (
defaultOSFlavor = "m1.tiny"
)
func runOpenStackCmd() *cobra.Command {
var (
flavorName string
instanceName string
networkID string
secGroups string
keyName string
)
cmd := &cobra.Command{
Use: "openstack",
Short: "launch an openstack instance using an existing image",
Long: `Launch an openstack instance using an existing image.
'name' is the name of an OpenStack image that has already been uploaded using 'linuxkit push'.
`,
Args: cobra.ExactArgs(1),
Example: "linuxkit run openstack [options] [name]",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
if instanceName == "" {
instanceName = name
}
client, err := clientconfig.NewServiceClient("compute", nil)
if err != nil {
return fmt.Errorf("Unable to create Compute client, %s", err)
}
network := servers.Network{
UUID: networkID,
}
var serverOpts servers.CreateOptsBuilder
serverOpts = &servers.CreateOpts{
FlavorName: flavorName,
ImageName: name,
Name: instanceName,
Networks: []servers.Network{network},
ServiceClient: client,
SecurityGroups: strings.Split(secGroups, ","),
}
if keyName != "" {
serverOpts = &keypairs.CreateOptsExt{
CreateOptsBuilder: serverOpts,
KeyName: keyName,
}
}
server, err := servers.Create(client, serverOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to create server: %w", err)
}
_ = servers.WaitForStatus(client, server.ID, "ACTIVE", 600)
log.Infof("Server created, UUID is %s", server.ID)
fmt.Println(server.ID)
return nil
},
}
cmd.Flags().StringVar(&flavorName, "flavor", defaultOSFlavor, "Instance size (flavor)")
cmd.Flags().StringVar(&instanceName, "instancename", "", "Name of instance. Defaults to the name of the image if not specified")
cmd.Flags().StringVar(&networkID, "network", "", "The ID of the network to attach the instance to")
cmd.Flags().StringVar(&secGroups, "sec-groups", "default", "Security Group names separated by comma")
cmd.Flags().StringVar(&keyName, "keyname", "", "The name of the SSH keypair to associate with the instance")
return cmd
}