Skip to content

Commit fbdb901

Browse files
committed
Feature(cluster add): add deploy type parameter
Signed-off-by: WhereAreBugs <[email protected]>
1 parent 580b26d commit fbdb901

File tree

7 files changed

+89
-17
lines changed

7 files changed

+89
-17
lines changed

cli/cli/cli.go

Lines changed: 3 additions & 1 deletion
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

Lines changed: 24 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,52 +103,69 @@ type Cluster struct {
103103
Topology string
104104
Pool string
105105
Current bool
106+
Type string
106107
}
107108

108109
var (
109110
// table: clusters
110111
CreateClustersTable = `
111-
CREATE TABLE IF NOT EXISTS clusters (
112+
CREATE TABLE IF NOT EXISTS clusters_new (
112113
id INTEGER PRIMARY KEY AUTOINCREMENT,
113114
uuid TEXT NOT NULL,
114115
name TEXT NOT NULL UNIQUE,
115116
description TEXT,
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_new(uuid, name, description, topology, type, pool, create_time)
128+
VALUES(?, ?, ?, ?, ?, "", datetime('now','localtime'))
129+
`
130+
// check if view exists
131+
CheckMigrationNeeded = `SELECT COUNT(*) FROM sqlite_master WHERE type='view' AND name = 'clusters'`
132+
133+
// update new cluster column
134+
AddTypeField = `ALTER TABLE clusters_new ADD COLUMN type TEXT NOT NULL DEFAULT 'develop'`
135+
136+
// rename clusters table
137+
RenameClusters = `ALTER TABLE clusters RENAME TO clusters_new`
138+
139+
// create view for old table
140+
CreateClustersView = `
141+
CREATE VIEW IF NOT EXISTS clusters AS
142+
SELECT id, uuid, name, description, topology, pool, create_time, current
143+
FROM clusters_new
127144
`
128145

129146
// delete cluster
130-
DeleteCluster = `DELETE from clusters WHERE name = ?`
147+
DeleteCluster = `DELETE from clusters_new WHERE name = ?`
131148

132149
// select cluster
133-
SelectCluster = `SELECT * FROM clusters WHERE name LIKE ?`
150+
SelectCluster = `SELECT * FROM clusters_new WHERE name LIKE ?`
134151

135152
// get current cluster
136-
GetCurrentCluster = `SELECT * FROM clusters WHERE current = 1`
153+
GetCurrentCluster = `SELECT * FROM clusters_new WHERE current = 1`
137154

138155
// checkout cluster
139156
CheckoutCluster = `
140-
UPDATE clusters
157+
UPDATE clusters_new
141158
SET current = CASE name
142159
WHEN ? THEN 1
143160
ELSE 0
144161
END
145162
`
146163

147164
// set cluster topology
148-
SetClusterTopology = `UPDATE clusters SET topology = ? WHERE id = ?`
165+
SetClusterTopology = `UPDATE clusters_new SET topology = ? WHERE id = ?`
149166

150167
// set cluster pool
151-
SetClusterPool = `UPDATE clusters SET topology = ?, pool = ? WHERE id = ?`
168+
SetClusterPool = `UPDATE clusters_new SET topology = ?, pool = ? WHERE id = ?`
152169
)
153170

154171
// service

internal/storage/storage.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ func (s *Storage) init() error {
8282
CreateAuditTable,
8383
CreateMonitorTable,
8484
CreateAnyTable,
85+
CreateClustersView,
86+
}
87+
88+
tablesOK, err := s.CheckViewExists()
89+
if err != nil {
90+
return err
91+
}
92+
if !tablesOK {
93+
// allow failed to execute
94+
_, err = s.db.Write(RenameClusters)
95+
_, err = s.db.Write(AddTypeField)
8596
}
8697

8798
for _, sql := range sqls {
@@ -160,8 +171,26 @@ func (s *Storage) GetHostses() ([]Hosts, error) {
160171
}
161172

162173
// cluster
163-
func (s *Storage) InsertCluster(name, uuid, description, topology string) error {
164-
return s.write(InsertCluster, uuid, name, description, topology)
174+
func (s *Storage) InsertCluster(name, uuid, description, topology, deployType string) error {
175+
return s.write(InsertCluster, uuid, name, description, topology, deployType)
176+
}
177+
178+
func (s *Storage) CheckViewExists() (bool, error) {
179+
result, err := s.db.Query(CheckMigrationNeeded)
180+
if err != nil {
181+
return false, err
182+
}
183+
defer result.Close()
184+
185+
var needed bool
186+
for result.Next() {
187+
err = result.Scan(&needed)
188+
if err != nil {
189+
return false, err
190+
}
191+
break
192+
}
193+
return needed, nil
165194
}
166195

167196
func (s *Storage) DeleteCluster(name string) error {
@@ -187,6 +216,7 @@ func (s *Storage) getClusters(query string, args ...interface{}) ([]Cluster, err
187216
&cluster.Pool,
188217
&cluster.CreateTime,
189218
&cluster.Current,
219+
&cluster.Type,
190220
)
191221
if err != nil {
192222
return nil, err

0 commit comments

Comments
 (0)