Skip to content

Commit 4d99c3d

Browse files
committed
Feature(cluster add): add deploy type parameter
Signed-off-by: WhereAreBugs <[email protected]>
1 parent 9d83a9c commit 4d99c3d

File tree

7 files changed

+69
-11
lines changed

7 files changed

+69
-11
lines changed

cli/cli/cli.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type CurveAdm struct {
6969
clusterName string // current cluster name
7070
clusterTopologyData string // cluster topology
7171
clusterPoolData string // cluster pool
72+
clusterType string // cluster type like develop, production, etc.
7273
monitor storage.Monitor
7374
}
7475

@@ -195,7 +196,7 @@ func (curveadm *CurveAdm) init() error {
195196
curveadm.clusterTopologyData = cluster.Topology
196197
curveadm.clusterPoolData = cluster.Pool
197198
curveadm.monitor = monitor
198-
199+
curveadm.clusterType = cluster.Type
199200
return nil
200201
}
201202

@@ -276,6 +277,7 @@ func (curveadm *CurveAdm) ClusterUUId() string { return curveadm.c
276277
func (curveadm *CurveAdm) ClusterName() string { return curveadm.clusterName }
277278
func (curveadm *CurveAdm) ClusterTopologyData() string { return curveadm.clusterTopologyData }
278279
func (curveadm *CurveAdm) ClusterPoolData() string { return curveadm.clusterPoolData }
280+
func (curveadm *CurveAdm) ClusterType() string { return curveadm.clusterType }
279281
func (curveadm *CurveAdm) Monitor() storage.Monitor { return curveadm.monitor }
280282

281283
func (curveadm *CurveAdm) GetHost(host string) (*hosts.HostConfig, error) {

cli/command/cluster/add.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ var (
4747
CHECK_TOPOLOGY_PLAYBOOK_STEPS = []int{
4848
playbook.CHECK_TOPOLOGY,
4949
}
50+
SUPPORTED_DEPLOY_TYPES = []string{
51+
"production",
52+
"test",
53+
"develop",
54+
}
5055
)
5156

5257
type addOptions struct {
5358
name string
5459
descriotion string
5560
filename string
61+
deployType string
5662
}
5763

5864
func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
@@ -63,6 +69,9 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
6369
Short: "Add cluster",
6470
Args: utils.ExactArgs(1),
6571
Example: ADD_EXAMPLE,
72+
PreRunE: func(cmd *cobra.Command, args []string) error {
73+
return checkAddOptions(cmd)
74+
},
6675
RunE: func(cmd *cobra.Command, args []string) error {
6776
options.name = args[0]
6877
return runAdd(curveadm, options)
@@ -73,7 +82,7 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
7382
flags := cmd.Flags()
7483
flags.StringVarP(&options.descriotion, "description", "m", "", "Description for cluster")
7584
flags.StringVarP(&options.filename, "topology", "f", "", "Specify the path of topology file")
76-
85+
flags.StringVar(&options.deployType, "type", "develop", "Specify the type of cluster")
7786
return cmd
7887
}
7988

@@ -134,6 +143,19 @@ func checkTopology(curveadm *cli.CurveAdm, data string, options addOptions) erro
134143
return pb.Run()
135144
}
136145

146+
func checkAddOptions(cmd *cobra.Command) error {
147+
deployType, err := cmd.Flags().GetString("deploy-type")
148+
if err != nil {
149+
return err
150+
}
151+
for _, t := range SUPPORTED_DEPLOY_TYPES {
152+
if deployType == t {
153+
return nil
154+
}
155+
}
156+
return errno.ERR_UNSUPPORT_DEPLOY_TYPE.F("deploy type: %s", deployType)
157+
}
158+
137159
func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
138160
// 1) check wether cluster already exist
139161
name := options.name
@@ -163,7 +185,7 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
163185

164186
// 4) insert cluster (with topology) into database
165187
uuid := uuid.NewString()
166-
err = storage.InsertCluster(name, uuid, options.descriotion, data)
188+
err = storage.InsertCluster(name, uuid, options.descriotion, data, options.deployType)
167189
if err != nil {
168190
return errno.ERR_INSERT_CLUSTER_FAILED.E(err)
169191
}

cli/command/cluster/import.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func importCluster(storage *storage.Storage, dbfile, name string) error {
100100
}
101101

102102
// insert cluster
103-
err = storage.InsertCluster(name, cluster.UUId, cluster.Description, cluster.Topology)
103+
err = storage.InsertCluster(name, cluster.UUId, cluster.Description, cluster.Topology, cluster.Type)
104104
if err != nil {
105105
return err
106106
}

cli/command/deploy.go

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ func displayDeployTitle(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig) {
302302
curveadm.WriteOutln("Cluster Name : %s", curveadm.ClusterName())
303303
curveadm.WriteOutln("Cluster Kind : %s", dcs[0].GetKind())
304304
curveadm.WriteOutln("Cluster Services: %s", serviceStats(dcs))
305+
curveadm.WriteOutln("Cluster Type : %s", curveadm.ClusterType())
305306
curveadm.WriteOutln("")
306307
}
307308

internal/errno/errno.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ var (
254254
ERR_NO_SERVICES_MATCHED = EC(210006, "no services matched")
255255
// TODO: please check pool set disk type
256256
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
257-
257+
ERR_UNSUPPORT_DEPLOY_TYPE = EC(210008, "unknown deploy type")
258258
// 220: commad options (client common)
259259
ERR_UNSUPPORT_CLIENT_KIND = EC(220000, "unsupport client kind")
260260
// 221: command options (client/bs)

internal/storage/sql.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type Cluster struct {
103103
Topology string
104104
Pool string
105105
Current bool
106+
Type string
106107
}
107108

108109
var (
@@ -116,15 +117,21 @@ var (
116117
topology TEXT NULL,
117118
pool TEXT NULL,
118119
create_time DATE NOT NULL,
119-
current INTEGER DEFAULT 0
120+
current INTEGER DEFAULT 0,
121+
type TEXT NOT NULL
120122
)
121123
`
122124

123125
// insert cluster
124126
InsertCluster = `
125-
INSERT INTO clusters(uuid, name, description, topology, pool, create_time)
126-
VALUES(?, ?, ?, ?, "", datetime('now','localtime'))
127+
INSERT INTO clusters(uuid, name, description, topology, type, pool, create_time)
128+
VALUES(?, ?, ?, ?, ?, "", datetime('now','localtime'))
127129
`
130+
// check new cluster column
131+
GetTypeFiled = `SELECT COUNT(*) FROM pragma_table_info('clusters') WHERE name = 'type'`
132+
133+
// update new cluster column
134+
UpdateCluster = `ALTER TABLE clusters ADD COLUMN type TEXT NOT NULL DEFAULT 'develop'`
128135

129136
// delete cluster
130137
DeleteCluster = `DELETE from clusters WHERE name = ?`

internal/storage/storage.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ func (s *Storage) init() error {
9191
}
9292
}
9393

94-
return nil
94+
flag, err := s.CheckTypeFiledExist()
95+
if err != nil {
96+
return err
97+
}
98+
if !flag {
99+
_, err = s.db.Write(UpdateCluster)
100+
}
101+
return err
95102
}
96103

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

162169
// cluster
163-
func (s *Storage) InsertCluster(name, uuid, description, topology string) error {
164-
return s.write(InsertCluster, uuid, name, description, topology)
170+
func (s *Storage) InsertCluster(name, uuid, description, topology, deployType string) error {
171+
return s.write(InsertCluster, uuid, name, description, topology, deployType)
172+
}
173+
174+
func (s *Storage) CheckTypeFiledExist() (bool, error) {
175+
result, err := s.db.Query(GetTypeFiled)
176+
if err != nil {
177+
return false, err
178+
}
179+
defer result.Close()
180+
181+
var isFiledExist bool
182+
for result.Next() {
183+
err = result.Scan(&isFiledExist)
184+
if err != nil {
185+
return false, err
186+
}
187+
break
188+
}
189+
return isFiledExist, nil
165190
}
166191

167192
func (s *Storage) DeleteCluster(name string) error {
@@ -187,6 +212,7 @@ func (s *Storage) getClusters(query string, args ...interface{}) ([]Cluster, err
187212
&cluster.Pool,
188213
&cluster.CreateTime,
189214
&cluster.Current,
215+
&cluster.Type,
190216
)
191217
if err != nil {
192218
return nil, err

0 commit comments

Comments
 (0)