Skip to content
This repository was archived by the owner on Oct 17, 2022. It is now read-only.

Commit ab35868

Browse files
committed
Port ccloudvm to cobra
Introduce cobra command parsing and call into existing ccvm functions to do the underlying work. Signed-off-by: Rob Bradford <[email protected]>
1 parent 3944214 commit ab35868

File tree

10 files changed

+345
-138
lines changed

10 files changed

+345
-138
lines changed

ccvm/ccvm.go

+2-44
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package ccvm
1818

1919
import (
2020
"context"
21-
"errors"
2221
"flag"
2322
"fmt"
2423
"os"
@@ -98,7 +97,8 @@ func (d *drives) Set(value string) error {
9897
return nil
9998
}
10099

101-
func vmFlags(fs *flag.FlagSet, customSpec *VMSpec) {
100+
// VMFlags provides common flags for customising a workload
101+
func VMFlags(fs *flag.FlagSet, customSpec *VMSpec) {
102102
fs.IntVar(&customSpec.MemGiB, "mem", customSpec.MemGiB, "Gigabytes of RAM allocated to VM")
103103
fs.IntVar(&customSpec.CPUs, "cpus", customSpec.CPUs, "VCPUs assigned to VM")
104104
fs.Var(&customSpec.Mounts, "mount", "directory to mount in guest VM via 9p. Format is tag,security_model,path")
@@ -128,48 +128,6 @@ func checkDirectory(dir string) error {
128128
return nil
129129
}
130130

131-
// CreateFlags parses the flags to the create command
132-
func CreateFlags() (string, bool, bool, VMSpec, error) {
133-
var debug bool
134-
var update bool
135-
var customSpec VMSpec
136-
fs := flag.NewFlagSet("create", flag.ExitOnError)
137-
fs.Usage = func() {
138-
fmt.Fprintf(os.Stderr, "Usage: %s create <workload> \n\n", os.Args[0])
139-
fmt.Fprintf(os.Stderr, " <workload>\tName of the workload to create\n\n")
140-
fs.PrintDefaults()
141-
}
142-
vmFlags(fs, &customSpec)
143-
fs.BoolVar(&debug, "debug", false, "Enables debug mode")
144-
fs.BoolVar(&update, "package-upgrade", false,
145-
"Hint to enable or disable update of VM packages. Should be true or false")
146-
147-
if err := fs.Parse(flag.Args()[1:]); err != nil {
148-
return "", debug, update, customSpec, err
149-
}
150-
151-
if fs.NArg() != 1 {
152-
fs.Usage()
153-
return "", debug, update, customSpec, errors.New("no workload specified")
154-
}
155-
workloadName := fs.Arg(0)
156-
157-
return workloadName, debug, update, customSpec, nil
158-
}
159-
160-
// StartFlags parsed the flags for the start command
161-
func StartFlags() (VMSpec, error) {
162-
var customSpec VMSpec
163-
164-
fs := flag.NewFlagSet("start", flag.ExitOnError)
165-
vmFlags(fs, &customSpec)
166-
if err := fs.Parse(flag.Args()[1:]); err != nil {
167-
return customSpec, err
168-
}
169-
170-
return customSpec, nil
171-
}
172-
173131
func prepareCreate(ctx context.Context, workloadName string, debug bool, update bool, customSpec *VMSpec) (*workload, *workspace, error) {
174132
ws, err := prepareEnv(ctx)
175133
if err != nil {

cmd/connect.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/intel/ccloudvm/ccvm"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var connectCmd = &cobra.Command{
25+
Use: "connect",
26+
Short: "Connects to a VM via SSH",
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
ctx, cancelFunc := getSignalContext()
29+
defer cancelFunc()
30+
31+
return ccvm.Connect(ctx)
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(connectCmd)
37+
}

cmd/create.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"flag"
21+
22+
"github.com/intel/ccloudvm/ccvm"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var createSpec ccvm.VMSpec
27+
var createDebug bool
28+
var createPackageUpgrade bool
29+
30+
var createCmd = &cobra.Command{
31+
Use: "create",
32+
Short: "Creates a new VM",
33+
Args: cobra.ExactArgs(1),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
ctx, cancelFunc := getSignalContext()
36+
defer cancelFunc()
37+
38+
return ccvm.Create(ctx, args[0], createDebug, createPackageUpgrade, &createSpec)
39+
},
40+
}
41+
42+
func init() {
43+
rootCmd.AddCommand(createCmd)
44+
45+
var flags flag.FlagSet
46+
ccvm.VMFlags(&flags, &createSpec)
47+
48+
createCmd.Flags().AddGoFlagSet(&flags)
49+
createCmd.Flags().Bool("debug", false, "Enable debugging mode")
50+
createCmd.Flags().Bool("package-upgrade", true, "Hint as to whether to upgrade packages on creation")
51+
}

cmd/delete.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/intel/ccloudvm/ccvm"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var deleteCmd = &cobra.Command{
25+
Use: "delete",
26+
Short: "Stops and deletes a VM",
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
ctx, cancelFunc := getSignalContext()
29+
defer cancelFunc()
30+
31+
return ccvm.Delete(ctx)
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(deleteCmd)
37+
}

cmd/quit.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/intel/ccloudvm/ccvm"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var quitCmd = &cobra.Command{
25+
Use: "quit",
26+
Short: "Forceably quits a running VM",
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
ctx, cancelFunc := getSignalContext()
29+
defer cancelFunc()
30+
31+
return ccvm.Quit(ctx)
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(quitCmd)
37+
}

cmd/root.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
"os/signal"
24+
"syscall"
25+
26+
"github.com/spf13/cobra"
27+
)
28+
29+
var rootCmd = &cobra.Command{
30+
Use: "ccloudvm",
31+
Short: "Configurable Cloud VM (ccloudvm) allows the creation and management of VMs from cloud-init files",
32+
}
33+
34+
// Execute is the entry into the cmd package from the main package.
35+
func Execute() {
36+
if err := rootCmd.Execute(); err != nil {
37+
fmt.Println(err)
38+
os.Exit(1)
39+
}
40+
}
41+
42+
func getSignalContext() (context.Context, context.CancelFunc) {
43+
ctx, cancelFunc := context.WithCancel(context.Background())
44+
45+
sigCh := make(chan os.Signal, 1)
46+
go func() {
47+
for {
48+
select {
49+
case <-sigCh:
50+
cancelFunc()
51+
case <-ctx.Done():
52+
break
53+
}
54+
}
55+
}()
56+
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
57+
return ctx, cancelFunc
58+
}

cmd/start.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"flag"
21+
22+
"github.com/intel/ccloudvm/ccvm"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var startSpec ccvm.VMSpec
27+
28+
var startCmd = &cobra.Command{
29+
Use: "start",
30+
Short: "Boots a stopped VM",
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
ctx, cancelFunc := getSignalContext()
33+
defer cancelFunc()
34+
35+
return ccvm.Start(ctx, &startSpec)
36+
},
37+
}
38+
39+
func init() {
40+
rootCmd.AddCommand(startCmd)
41+
42+
var flags flag.FlagSet
43+
ccvm.VMFlags(&flags, &createSpec)
44+
45+
startCmd.Flags().AddGoFlagSet(&flags)
46+
}

cmd/status.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
// Copyright (c) 2018 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/intel/ccloudvm/ccvm"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var statusCmd = &cobra.Command{
25+
Use: "status",
26+
Short: "Prints status information about a VM",
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
ctx, cancelFunc := getSignalContext()
29+
defer cancelFunc()
30+
31+
return ccvm.Status(ctx)
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(statusCmd)
37+
}

0 commit comments

Comments
 (0)