Skip to content

Commit

Permalink
feat(autok3s): support command start and stop
Browse files Browse the repository at this point in the history
Signed-off-by: NewGr8Player <273221594@qq.com>
  • Loading branch information
NewGr8Player authored and rancher-sy-bot committed Sep 25, 2020
1 parent 1b47c1a commit f3435e8
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 10 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ sudo autok3s ... \
--k3s-version="v1.18.9-k3s1"
```

### Start K3s Cluster
```bash
sudo autok3s start \
--provider alibaba \
--region <region> \
--name <cluster name>
```

### Stop K3s Cluster
```bash
sudo autok3s stop \
--provider alibaba \
--region <region> \
--name <cluster name>
```

### Delete K3s Cluster
```bash
sudo autok3s delete \
Expand Down
62 changes: 62 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"github.com/cnrancher/autok3s/cmd/common"
"github.com/cnrancher/autok3s/pkg/providers"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
startCmd = &cobra.Command{
Use: "start",
Short: "Start k3s cluster",
Example: ` autok3s start --name cluster`,
}
stProvider = ""
stP providers.Provider
)

func init() {
startCmd.Flags().StringVarP(&stProvider, "provider", "p", stProvider, "Provider is a module which provides an interface for managing cloud resources")
}

func StartCommand() *cobra.Command {

pStr := common.FlagHackLookup("--provider")

if pStr != "" {
if reg, err := providers.Register(pStr); err != nil {
logrus.Fatalln(err)
} else {
stP = reg
}

startCmd.Flags().AddFlagSet(stP.GetCredentialFlags(startCmd))
startCmd.Flags().AddFlagSet(stP.GetStartFlags(startCmd))
}

startCmd.Run = func(cmd *cobra.Command, args []string) {
common.BindPFlags(cmd, stP)

// read options from config.
if err := viper.ReadInConfig(); err != nil {
logrus.Fatalln(err)
}

// sync config data to local cfg path.
if err := viper.WriteConfig(); err != nil {
logrus.Fatalln(err)
}

stP.GenerateClusterName()

if err := stP.StartK3sCluster(); err != nil {
logrus.Fatalln(err)
}
}

return startCmd
}
64 changes: 64 additions & 0 deletions cmd/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"github.com/cnrancher/autok3s/cmd/common"
"github.com/cnrancher/autok3s/pkg/providers"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop k3s cluster",
Example: ` autok3s stop --name cluster`,
}
spProvider = ""
spForce = false
spP providers.Provider
)

func init() {
stopCmd.Flags().StringVarP(&spProvider, "provider", "p", spProvider, "Provider is a module which provides an interface for managing cloud resources")
stopCmd.Flags().BoolVarP(&spForce, "force", "f", spForce, "Force stop cluster")
}

func StopCommand() *cobra.Command {

pStr := common.FlagHackLookup("--provider")

if pStr != "" {
if reg, err := providers.Register(pStr); err != nil {
logrus.Fatalln(err)
} else {
spP = reg
}

stopCmd.Flags().AddFlagSet(spP.GetCredentialFlags(stopCmd))
stopCmd.Flags().AddFlagSet(spP.GetStopFlags(stopCmd))
}

stopCmd.Run = func(cmd *cobra.Command, args []string) {
common.BindPFlags(cmd, spP)

// read options from config.
if err := viper.ReadInConfig(); err != nil {
logrus.Fatalln(err)
}

// sync config data to local cfg path.
if err := viper.WriteConfig(); err != nil {
logrus.Fatalln(err)
}

spP.GenerateClusterName()

if err := spP.StopK3sCluster(spForce); err != nil {
logrus.Fatalln(err)
}
}

return stopCmd
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func main() {

rootCmd := cmd.Command()
rootCmd.AddCommand(cmd.CompletionCommand(), cmd.VersionCommand(gitVersion, gitCommit, gitTreeState, buildDate),
cmd.ListCommand(), cmd.CreateCommand(), cmd.JoinCommand(), cmd.KubectlCommand(), cmd.DeleteCommand())
cmd.ListCommand(), cmd.CreateCommand(), cmd.JoinCommand(), cmd.KubectlCommand(), cmd.DeleteCommand(),
cmd.StartCommand(), cmd.StopCommand())

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
Loading

0 comments on commit f3435e8

Please # to comment.