provider-kairos/internal/cli/start.go
Mauro Morales d88983a906
Extract some commands into kairosctl (#310)
This PR extracts the registration command into a `kairos-register`
binary of its own. The old sub command is kept so users can see a
deprecation notice and adapt for a future release when it's removed. The
version number is shared between binaries.

⚠️ I'm not entirely sure about the gorelease, and would benefit
from a couple of extra 👀 on this, thanks!

relates to kairos-io/kairos#1211

---------

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
Signed-off-by: Mauro Morales <contact@mauromorales.com>
Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
Signed-off-by: GitHub <noreply@github.com>
Co-authored-by: Dimitris Karakasilis <dimitris@karakasilis.me>
Co-authored-by: Itxaka <itxaka.garcia@spectrocloud.com>
Co-authored-by: ci-robbot [bot] <105103991+ci-robbot@users.noreply.github.com>
Co-authored-by: mudler <mudler@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2023-04-21 11:04:15 +02:00

147 lines
3.7 KiB
Go

package cli
import (
"fmt"
"os"
"strconv"
providerConfig "github.com/kairos-io/provider-kairos/internal/provider/config"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v1"
"github.com/mudler/edgevpn/pkg/node"
)
// do not edit version here, it is set by LDFLAGS
// -X 'github.com/kairos-io/provider-kairos/internal/cli.VERSION=$VERSION'
// see Earthlfile.
var VERSION = "0.0.0"
var Author = "Ettore Di Giacinto"
var networkAPI = []cli.Flag{
&cli.StringFlag{
Name: "api",
Usage: "API Address",
Value: "http://localhost:8080",
},
&cli.StringFlag{
Name: "network-id",
Value: "kairos",
Usage: "Kubernetes Network Deployment ID",
},
}
const recoveryAddr = "127.0.0.1:2222"
func Start() error {
toolName := "kairos"
app := &cli.App{
Name: toolName,
Version: VERSION,
Authors: []*cli.Author{
{
Name: Author,
},
},
Usage: "kairos CLI to bootstrap, upgrade, connect and manage a kairos network",
Description: `
The kairos CLI can be used to manage a kairos box and perform all day-two tasks, like:
- register a node (WARNING: this command will be deprecated in the next release, use the kairosctl binary instead)
- connect to a node in recovery mode
- to establish a VPN connection
- set, list roles
- interact with the network API
and much more.
For all the example cases, see: https://kairos.io/docs/
`,
UsageText: ``,
Copyright: Author,
Commands: []*cli.Command{
{
Name: "recovery-ssh-server",
UsageText: "recovery-ssh-server",
Usage: "Starts SSH recovery service",
Description: `
Spawn up a simple standalone ssh server over p2p
`,
ArgsUsage: "Spawn up a simple standalone ssh server over p2p",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "token",
EnvVars: []string{"TOKEN"},
},
&cli.StringFlag{
Name: "service",
EnvVars: []string{"SERVICE"},
},
&cli.StringFlag{
Name: "password",
EnvVars: []string{"PASSWORD"},
},
&cli.StringFlag{
Name: "listen",
EnvVars: []string{"LISTEN"},
Value: recoveryAddr,
},
},
Action: func(c *cli.Context) error {
return StartRecoveryService(c.String("token"), c.String("service"), c.String("password"), c.String("listen"))
},
},
RegisterCMD(toolName),
BridgeCMD(toolName),
&GetKubeConfigCMD,
&RoleCMD,
{
Name: "create-config",
Aliases: []string{"c"},
UsageText: "Create a config with a generated network token",
Usage: "Creates a pristine config file",
Description: `
Prints a vanilla YAML configuration on screen which can be used to bootstrap a kairos network.
`,
ArgsUsage: "Optionally takes a token rotation interval (seconds)",
Action: func(c *cli.Context) error {
l := int(^uint(0) >> 1)
if c.Args().Present() {
if i, err := strconv.Atoi(c.Args().Get(0)); err == nil {
l = i
}
}
cc := &providerConfig.Config{P2P: &providerConfig.P2P{NetworkToken: node.GenerateNewConnectionData(l).Base64()}}
y, _ := yaml.Marshal(cc)
fmt.Println(string(y))
return nil
},
},
{
Name: "generate-token",
Aliases: []string{"g"},
UsageText: "Generate a network token",
Usage: "Creates a new token",
Description: `
Generates a new token which can be used to bootstrap a kairos network.
`,
ArgsUsage: "Optionally takes a token rotation interval (seconds)",
Action: func(c *cli.Context) error {
l := int(^uint(0) >> 1)
if c.Args().Present() {
if i, err := strconv.Atoi(c.Args().Get(0)); err == nil {
l = i
}
}
fmt.Println(node.GenerateNewConnectionData(l).Base64())
return nil
},
},
},
}
return app.Run(os.Args)
}