Skip to content

Commit

Permalink
Feature(cluster add): add deploy type parameter
Browse files Browse the repository at this point in the history
Signed-off-by: WhereAreBugs <[email protected]>
  • Loading branch information
WhereAreBugs committed Dec 4, 2023
1 parent 9d83a9c commit fafb9de
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cli/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type CurveAdm struct {
clusterName string // current cluster name
clusterTopologyData string // cluster topology
clusterPoolData string // cluster pool
clusterType string // cluster type like develop, production, etc.
monitor storage.Monitor
}

Expand Down Expand Up @@ -195,7 +196,7 @@ func (curveadm *CurveAdm) init() error {
curveadm.clusterTopologyData = cluster.Topology
curveadm.clusterPoolData = cluster.Pool
curveadm.monitor = monitor

curveadm.clusterType = cluster.Type
return nil
}

Expand Down Expand Up @@ -276,6 +277,7 @@ func (curveadm *CurveAdm) ClusterUUId() string { return curveadm.c
func (curveadm *CurveAdm) ClusterName() string { return curveadm.clusterName }
func (curveadm *CurveAdm) ClusterTopologyData() string { return curveadm.clusterTopologyData }
func (curveadm *CurveAdm) ClusterPoolData() string { return curveadm.clusterPoolData }
func (curveadm *CurveAdm) ClusterType() string { return curveadm.clusterType }
func (curveadm *CurveAdm) Monitor() storage.Monitor { return curveadm.monitor }

func (curveadm *CurveAdm) GetHost(host string) (*hosts.HostConfig, error) {
Expand Down
29 changes: 26 additions & 3 deletions cli/command/cluster/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ const (
ADD_EXAMPLE = `Examples:
$ curveadm add my-cluster # Add a cluster named 'my-cluster'
$ curveadm add my-cluster -m "deploy for test" # Add a cluster with description
$ curveadm add my-cluster -f /path/to/topology.yaml # Add a cluster with specified topology`
$ curveadm add my-cluster -f /path/to/topology.yaml # Add a cluster with specified topology
$ curveadm add my-cluster -t develop # Add a cluster with type 'develop'`
)

var (
CHECK_TOPOLOGY_PLAYBOOK_STEPS = []int{
playbook.CHECK_TOPOLOGY,
}
SUPPORTED_DEPLOY_TYPES = []string{
"production",
"test",
"develop",
}
)

type addOptions struct {
name string
descriotion string
filename string
deployType string
}

func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -63,6 +70,9 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
Short: "Add cluster",
Args: utils.ExactArgs(1),
Example: ADD_EXAMPLE,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkAddOptions(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
options.name = args[0]
return runAdd(curveadm, options)
Expand All @@ -73,7 +83,7 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags := cmd.Flags()
flags.StringVarP(&options.descriotion, "description", "m", "", "Description for cluster")
flags.StringVarP(&options.filename, "topology", "f", "", "Specify the path of topology file")

flags.StringVarP(&options.deployType, "deploy-type", "t", "develop", "Specify the deploy type of cluster")
return cmd
}

Expand Down Expand Up @@ -134,6 +144,19 @@ func checkTopology(curveadm *cli.CurveAdm, data string, options addOptions) erro
return pb.Run()
}

func checkAddOptions(cmd *cobra.Command) error {
deployType, err := cmd.Flags().GetString("deploy-type")
if err != nil {
return err
}
for _, t := range SUPPORTED_DEPLOY_TYPES {
if deployType == t {
return nil
}
}
return errno.ERR_UNSUPPORT_DEPLOY_TYPE.F("deploy type: %s", deployType)
}

func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
// 1) check wether cluster already exist
name := options.name
Expand Down Expand Up @@ -163,7 +186,7 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error {

// 4) insert cluster (with topology) into database
uuid := uuid.NewString()
err = storage.InsertCluster(name, uuid, options.descriotion, data)
err = storage.InsertCluster(name, uuid, options.descriotion, data, options.deployType)
if err != nil {
return errno.ERR_INSERT_CLUSTER_FAILED.E(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func importCluster(storage *storage.Storage, dbfile, name string) error {
}

// insert cluster
err = storage.InsertCluster(name, cluster.UUId, cluster.Description, cluster.Topology)
err = storage.InsertCluster(name, cluster.UUId, cluster.Description, cluster.Topology, cluster.Type)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cli/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func displayDeployTitle(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig) {
curveadm.WriteOutln("Cluster Name : %s", curveadm.ClusterName())
curveadm.WriteOutln("Cluster Kind : %s", dcs[0].GetKind())
curveadm.WriteOutln("Cluster Services: %s", serviceStats(dcs))
curveadm.WriteOutln("Cluster Type : %s", curveadm.ClusterType())
curveadm.WriteOutln("")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/errno/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ var (
ERR_NO_SERVICES_MATCHED = EC(210006, "no services matched")
// TODO: please check pool set disk type
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")

ERR_UNSUPPORT_DEPLOY_TYPE = EC(210008, "unknown deploy type")
// 220: commad options (client common)
ERR_UNSUPPORT_CLIENT_KIND = EC(220000, "unsupport client kind")
// 221: command options (client/bs)
Expand Down
13 changes: 10 additions & 3 deletions internal/storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Cluster struct {
Topology string
Pool string
Current bool
Type string
}

var (
Expand All @@ -116,15 +117,21 @@ var (
topology TEXT NULL,
pool TEXT NULL,
create_time DATE NOT NULL,
current INTEGER DEFAULT 0
current INTEGER DEFAULT 0,
type TEXT NOT NULL
)
`

// insert cluster
InsertCluster = `
INSERT INTO clusters(uuid, name, description, topology, pool, create_time)
VALUES(?, ?, ?, ?, "", datetime('now','localtime'))
INSERT INTO clusters(uuid, name, description, topology, type, pool, create_time)
VALUES(?, ?, ?, ?, ?, "", datetime('now','localtime'))
`
// check new cluster column
GetTypeFiled = `SELECT COUNT(*) FROM pragma_table_info('clusters') WHERE name = 'type'`

// update new cluster column
UpdateCluster = `ALTER TABLE clusters ADD COLUMN type TEXT NOT NULL DEFAULT 'develop'`

// delete cluster
DeleteCluster = `DELETE from clusters WHERE name = ?`
Expand Down
32 changes: 29 additions & 3 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ func (s *Storage) init() error {
}
}

return nil
flag, err := s.CheckTypeFiledExist()
if err != nil {
return err
}
if flag == 0 {
_, err = s.db.Write(UpdateCluster)
}
return err
}

func (s *Storage) write(query string, args ...any) error {
Expand Down Expand Up @@ -160,8 +167,26 @@ func (s *Storage) GetHostses() ([]Hosts, error) {
}

// cluster
func (s *Storage) InsertCluster(name, uuid, description, topology string) error {
return s.write(InsertCluster, uuid, name, description, topology)
func (s *Storage) InsertCluster(name, uuid, description, topology, deployType string) error {
return s.write(InsertCluster, uuid, name, description, topology, deployType)
}

func (s *Storage) CheckTypeFiledExist() (int, error) {
result, err := s.db.Query(GetTypeFiled)
if err != nil {
return -1, err
}
defer result.Close()

var isFiledExist int
for result.Next() {
err = result.Scan(&isFiledExist)
if err != nil {
return -1, err
}
break
}
return isFiledExist, nil
}

func (s *Storage) DeleteCluster(name string) error {
Expand All @@ -187,6 +212,7 @@ func (s *Storage) getClusters(query string, args ...interface{}) ([]Cluster, err
&cluster.Pool,
&cluster.CreateTime,
&cluster.Current,
&cluster.Type,
)
if err != nil {
return nil, err
Expand Down

0 comments on commit fafb9de

Please sign in to comment.