-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_build.go
68 lines (52 loc) · 1.4 KB
/
cluster_build.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
package main
import (
"fmt"
"github.com/cipherboy/devbao/pkg/bao"
"github.com/urfave/cli/v2"
)
func ClusterFlags() []cli.Flag {
return []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Value: false,
Usage: "overwrite an existing node, if present",
},
}
}
func BuildClusterBuildCommand() *cli.Command {
c := &cli.Command{
Name: "build",
Aliases: []string{"b"},
ArgsUsage: "<cluster-name> <node-name>",
Usage: "build a new cluster with the given base node",
Action: RunClusterBuildCommand,
}
c.Flags = append(c.Flags, ClusterFlags()...)
return c
}
func RunClusterBuildCommand(cCtx *cli.Context) error {
if cCtx.Args().Len() != 2 {
return fmt.Errorf("missing required positional argument:\n\t<cluster-name>, the name of the cluster to create\n\t<node-name> the name of the node to add")
}
clusterName := cCtx.Args().First()
nodeName := cCtx.Args().Get(1)
force := cCtx.Bool("force")
if !force {
present, err := bao.ClusterExists(clusterName)
if err != nil {
return fmt.Errorf("error checking if cluster exists: %w", err)
}
if present {
return fmt.Errorf("refusing to override cluster %v", clusterName)
}
}
cluster, err := bao.BuildHACluster(clusterName, nodeName)
if err != nil {
return fmt.Errorf("failed to build cluster: %w", err)
}
if cluster == nil {
return fmt.Errorf("nil cluster: %w", err)
}
return nil
}