Skip to content

Commit e2f7782

Browse files
committed
Feature: now we support the rqlite which is distributed database as curveadm's database (opencurve#206).
Signed-off-by: Wine93 <[email protected]>
1 parent 5756146 commit e2f7782

File tree

14 files changed

+693
-388
lines changed

14 files changed

+693
-388
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: build debug test tar
1+
.PHONY: build debug install test upload lint
22

33
# go env
44
GOPROXY := "https://goproxy.cn,direct"
@@ -60,6 +60,9 @@ build:
6060
debug:
6161
$(GOENV) $(GO) build -o $(OUTPUT) $(DEBUG_FLAGS) $(PACKAGES)
6262

63+
install:
64+
cp bin/curveadm ~/.curveadm/bin
65+
6366
test:
6467
$(GO_TEST) $(TEST_FLAGS) ./...
6568

cli/cli/cli.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ type CurveAdm struct {
5252
pluginDir string
5353
logDir string
5454
tempDir string
55-
dbpath string
5655
logpath string
5756
config *configure.CurveAdmConfig
5857

@@ -142,9 +141,9 @@ func (curveadm *CurveAdm) init() error {
142141
// (4) Init error code
143142
errno.Init(logpath)
144143

145-
// (5) New storage: create table in sqlite
146-
dbpath := fmt.Sprintf("%s/curveadm.db", curveadm.dataDir)
147-
s, err := storage.NewStorage(dbpath)
144+
// (5) New storage: create table in sqlite/rqlite
145+
dbUrl := config.GetDBUrl()
146+
s, err := storage.NewStorage(dbUrl)
148147
if err != nil {
149148
log.Error("Init SQLite database failed",
150149
log.Field("Error", err))
@@ -174,7 +173,6 @@ func (curveadm *CurveAdm) init() error {
174173
log.Field("ClusterName", cluster.Name))
175174
}
176175

177-
curveadm.dbpath = dbpath
178176
curveadm.logpath = logpath
179177
curveadm.config = config
180178
curveadm.in = os.Stdin
@@ -253,7 +251,6 @@ func (curveadm *CurveAdm) DataDir() string { return curveadm.d
253251
func (curveadm *CurveAdm) PluginDir() string { return curveadm.pluginDir }
254252
func (curveadm *CurveAdm) LogDir() string { return curveadm.logDir }
255253
func (curveadm *CurveAdm) TempDir() string { return curveadm.tempDir }
256-
func (curveadm *CurveAdm) DBPath() string { return curveadm.dbpath }
257254
func (curveadm *CurveAdm) LogPath() string { return curveadm.logpath }
258255
func (curveadm *CurveAdm) Config() *configure.CurveAdmConfig { return curveadm.config }
259256
func (curveadm *CurveAdm) SudoAlias() string { return curveadm.config.GetSudoAlias() }

cli/command/exec.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
* Author: Jingli Chen (Wine93)
2121
*/
2222

23-
// __SIGN_BY_WINE93__
24-
2523
package command
2624

2725
import (
@@ -60,6 +58,11 @@ func NewExecCommand(curveadm *cli.CurveAdm) *cobra.Command {
6058
return cmd
6159
}
6260

61+
// exec:
62+
// 1. parse cluster topology
63+
// 2. filter service
64+
// 3. get container id
65+
// 4. exec cmd in remote container
6366
func runExec(curveadm *cli.CurveAdm, options execOptions) error {
6467
// 1) parse cluster topology
6568
dcs, err := curveadm.ParseTopology()

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ require (
7575
github.com/prometheus/common v0.37.0 // indirect
7676
github.com/prometheus/procfs v0.8.0 // indirect
7777
github.com/rivo/uniseg v0.4.3 // indirect
78+
github.com/rqlite/gorqlite v0.0.0-20230310040812-ec5e524a562e
7879
github.com/sirupsen/logrus v1.9.0 // indirect
7980
github.com/spf13/afero v1.9.3 // indirect
8081
github.com/spf13/cast v1.5.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
741741
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
742742
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
743743
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
744+
github.com/rqlite/gorqlite v0.0.0-20230310040812-ec5e524a562e h1:updBXFrJFAJO/3b/mctukZQEIVUq09iwV/wireIlZFA=
745+
github.com/rqlite/gorqlite v0.0.0-20230310040812-ec5e524a562e/go.mod h1:xF/KoXmrRyahPfo5L7Szb5cAAUl53dMWBh9cMruGEZg=
744746
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
745747
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
746748
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=

internal/configure/curveadm/curveadm.go

+85-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
package curveadm
2626

2727
import (
28+
"fmt"
29+
"os"
30+
"regexp"
31+
2832
"github.com/opencurve/curveadm/internal/build"
2933
"github.com/opencurve/curveadm/internal/errno"
3034
"github.com/opencurve/curveadm/internal/utils"
@@ -40,6 +44,9 @@ import (
4044
* [ssh_connections]
4145
* retries = 3
4246
* timeout = 10
47+
*
48+
* [database]
49+
* url = "sqlite:///home/curve/.curveadm/data/curveadm.db"
4350
*/
4451
const (
4552
KEY_LOG_LEVEL = "log_level"
@@ -48,6 +55,13 @@ const (
4855
KEY_AUTO_UPGRADE = "auto_upgrade"
4956
KEY_SSH_RETRIES = "retries"
5057
KEY_SSH_TIMEOUT = "timeout"
58+
KEY_DB_URL = "url"
59+
60+
// rqlite://127.0.0.1:4000
61+
// sqlite:///home/curve/.curveadm/data/curveadm.db
62+
REGEX_DB_URL = "^(sqlite|rqlite)://(.+)$"
63+
DB_SQLITE = "sqlite"
64+
DB_RQLITE = "rqlite"
5165

5266
WITHOUT_SUDO = " "
5367
)
@@ -60,26 +74,19 @@ type (
6074
AutoUpgrade bool
6175
SSHRetries int
6276
SSHTimeout int
77+
DBUrl string
6378
}
6479

6580
CurveAdm struct {
6681
Defaults map[string]interface{} `mapstructure:"defaults"`
6782
SSHConnections map[string]interface{} `mapstructure:"ssh_connections"`
83+
DataBase map[string]interface{} `mapstructure:"database"`
6884
}
6985
)
7086

7187
var (
7288
GlobalCurveAdmConfig *CurveAdmConfig
7389

74-
defaultCurveAdmConfig = &CurveAdmConfig{
75-
LogLevel: "error",
76-
SudoAlias: "sudo",
77-
Timeout: 180,
78-
AutoUpgrade: true,
79-
SSHRetries: 3,
80-
SSHTimeout: 10,
81-
}
82-
8390
SUPPORT_LOG_LEVEL = map[string]bool{
8491
"debug": true,
8592
"info": true,
@@ -92,6 +99,20 @@ func ReplaceGlobals(cfg *CurveAdmConfig) {
9299
GlobalCurveAdmConfig = cfg
93100
}
94101

102+
func newDefault() *CurveAdmConfig {
103+
home, _ := os.UserHomeDir()
104+
cfg := &CurveAdmConfig{
105+
LogLevel: "error",
106+
SudoAlias: "sudo",
107+
Timeout: 180,
108+
AutoUpgrade: true,
109+
SSHRetries: 3,
110+
SSHTimeout: 10,
111+
DBUrl: fmt.Sprintf("sqlite://%s/.curveadm/data/curveadm.db", home),
112+
}
113+
return cfg
114+
}
115+
95116
// TODO(P2): using ItemSet to check value type
96117
func requirePositiveInt(k string, v interface{}) (int, error) {
97118
num, ok := utils.Str2Int(v.(string))
@@ -141,6 +162,7 @@ func parseDefaultsSection(cfg *CurveAdmConfig, defaults map[string]interface{})
141162
}
142163
cfg.Timeout = num
143164

165+
// auto upgrade
144166
case KEY_AUTO_UPGRADE:
145167
yes, err := requirePositiveBool(KEY_AUTO_UPGRADE, v)
146168
if err != nil {
@@ -189,8 +211,39 @@ func parseConnectionSection(cfg *CurveAdmConfig, connection map[string]interface
189211
return nil
190212
}
191213

214+
func parseDatabaseSection(cfg *CurveAdmConfig, database map[string]interface{}) error {
215+
if database == nil {
216+
return nil
217+
}
218+
219+
for k, v := range database {
220+
switch k {
221+
// database url
222+
case KEY_DB_URL:
223+
dbUrl := v.(string)
224+
pattern := regexp.MustCompile(REGEX_DB_URL)
225+
mu := pattern.FindStringSubmatch(dbUrl)
226+
if len(mu) == 0 {
227+
return errno.ERR_UNSUPPORT_CURVEADM_DATABASE_URL.F("url: %s", dbUrl)
228+
}
229+
cfg.DBUrl = dbUrl
230+
231+
default:
232+
return errno.ERR_UNSUPPORT_CURVEADM_CONFIGURE_ITEM.
233+
F("%s: %s", k, v)
234+
}
235+
}
236+
237+
return nil
238+
}
239+
240+
type sectionParser struct {
241+
parser func(*CurveAdmConfig, map[string]interface{}) error
242+
section map[string]interface{}
243+
}
244+
192245
func ParseCurveAdmConfig(filename string) (*CurveAdmConfig, error) {
193-
cfg := defaultCurveAdmConfig
246+
cfg := newDefault()
194247
if !utils.PathExist(filename) {
195248
build.DEBUG(build.DEBUG_CURVEADM_CONFIGURE, cfg)
196249
return cfg, nil
@@ -211,13 +264,16 @@ func ParseCurveAdmConfig(filename string) (*CurveAdmConfig, error) {
211264
return nil, errno.ERR_PARSE_CURVRADM_CONFIGURE_FAILED.E(err)
212265
}
213266

214-
err = parseDefaultsSection(cfg, global.Defaults)
215-
if err != nil {
216-
return nil, err
267+
items := []sectionParser{
268+
{parseDefaultsSection, global.Defaults},
269+
{parseConnectionSection, global.SSHConnections},
270+
{parseDatabaseSection, global.DataBase},
217271
}
218-
err = parseConnectionSection(cfg, global.SSHConnections)
219-
if err != nil {
220-
return nil, err
272+
for _, item := range items {
273+
err := item.parser(cfg, item.section)
274+
if err != nil {
275+
return nil, err
276+
}
221277
}
222278

223279
build.DEBUG(build.DEBUG_CURVEADM_CONFIGURE, cfg)
@@ -235,3 +291,16 @@ func (cfg *CurveAdmConfig) GetSudoAlias() string {
235291
}
236292
return cfg.SudoAlias
237293
}
294+
295+
func (cfg *CurveAdmConfig) GetDBUrl() string {
296+
return cfg.DBUrl
297+
}
298+
299+
func (cfg *CurveAdmConfig) GetDBPath() string {
300+
pattern := regexp.MustCompile(REGEX_DB_URL)
301+
mu := pattern.FindStringSubmatch(cfg.DBUrl)
302+
if len(mu) == 0 || mu[1] != DB_SQLITE {
303+
return ""
304+
}
305+
return mu[2]
306+
}

internal/errno/errno.go

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ var (
276276
// 311: configure (curveadm.cfg: invalid configure value)
277277
ERR_UNSUPPORT_CURVEADM_LOG_LEVEL = EC(311000, "unsupport curveadm log level")
278278
ERR_UNSUPPORT_CURVEADM_CONFIGURE_ITEM = EC(311001, "unsupport curveadm configure item")
279+
ERR_UNSUPPORT_CURVEADM_DATABASE_URL = EC(311002, "unsupport curveadm database url")
279280

280281
// 320: configure (hosts.yaml: parse failed)
281282
ERR_HOSTS_FILE_NOT_FOUND = EC(320000, "hosts file not found")

internal/storage/driver/driver.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2023 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the Licensele().
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2023-05-24
20+
* Author: Jingli Chen (Wine93)
21+
*/
22+
23+
package driver
24+
25+
type IQueryResult interface {
26+
Next() bool
27+
Scan(dest ...any) error
28+
Close() error
29+
}
30+
31+
type IWriteResult interface {
32+
LastInsertId() (int64, error)
33+
}
34+
35+
type IDataBaseDriver interface {
36+
Open(dbUrl string) error
37+
Close() error
38+
Query(query string, args ...any) (IQueryResult, error)
39+
Write(query string, args ...any) (IWriteResult, error)
40+
}

0 commit comments

Comments
 (0)