Skip to content

Commit a58f65a

Browse files
committed
add export cmd for export the yaml that tools-v2 used
1 parent e362387 commit a58f65a

File tree

10 files changed

+202
-11
lines changed

10 files changed

+202
-11
lines changed

cli/command/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func addSubCommands(cmd *cobra.Command, curveadm *cli.CurveAdm) {
8484
NewStopCommand(curveadm), // curveadm stop
8585
NewSupportCommand(curveadm), // curveadm support
8686
NewUpgradeCommand(curveadm), // curveadm upgrade
87+
NewExportCommand(curveadm), // curveadm export
8788
// commonly used shorthands
8889
hosts.NewSSHCommand(curveadm), // curveadm ssh
8990
hosts.NewPlaybookCommand(curveadm), // curveadm playbook

cli/command/export.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2023-11-9
20+
* Author: Jiang Jun (youarefree123)
21+
*/
22+
23+
// __SIGN_BY_YOUAREFREE123__
24+
25+
package command
26+
27+
import (
28+
"path"
29+
"strings"
30+
31+
"github.com/fatih/color"
32+
"github.com/opencurve/curveadm/cli/cli"
33+
comm "github.com/opencurve/curveadm/internal/common"
34+
"github.com/opencurve/curveadm/internal/configure/topology"
35+
"github.com/opencurve/curveadm/internal/errno"
36+
"github.com/opencurve/curveadm/internal/playbook"
37+
cliutil "github.com/opencurve/curveadm/internal/utils"
38+
"github.com/spf13/cobra"
39+
)
40+
41+
var (
42+
GET_EXPORT_PLAYBOOK_STEPS = []int{
43+
playbook.EXPORT_TOOLSV2_CONF,
44+
}
45+
)
46+
47+
type exportOptions struct {
48+
output string
49+
}
50+
51+
func checkExportOptions(curveadm *cli.CurveAdm, options exportOptions) error {
52+
if !strings.HasPrefix(options.output, "/") {
53+
return errno.ERR_EXPORT_TOOLSV2_CONF_REQUIRE_ABSOLUTE_PATH.
54+
F("/path/to/curve.yaml: %s", options.output)
55+
}
56+
return nil
57+
}
58+
59+
func NewExportCommand(curveadm *cli.CurveAdm) *cobra.Command {
60+
var options exportOptions
61+
62+
cmd := &cobra.Command{
63+
Use: "export [OPTIONS]",
64+
Short: "Export curve.yaml",
65+
Args: cliutil.NoArgs,
66+
PreRunE: func(cmd *cobra.Command, args []string) error {
67+
return checkExportOptions(curveadm, options)
68+
},
69+
RunE: func(cmd *cobra.Command, args []string) error {
70+
return runExport(curveadm, options)
71+
},
72+
DisableFlagsInUseLine: true,
73+
}
74+
75+
flags := cmd.Flags()
76+
flags.StringVarP(&options.output, "path", "p", path.Join(curveadm.PluginDir(), "curve.yaml"), "Path where the exported YAML is stored")
77+
return cmd
78+
}
79+
80+
func genExportPlaybook(curveadm *cli.CurveAdm,
81+
dcs []*topology.DeployConfig,
82+
options exportOptions) (*playbook.Playbook, error) {
83+
84+
steps := GET_EXPORT_PLAYBOOK_STEPS
85+
pb := playbook.NewPlaybook(curveadm)
86+
for _, step := range steps {
87+
pb.AddStep(&playbook.PlaybookStep{
88+
Type: step,
89+
Configs: dcs[:1],
90+
Options: map[string]interface{}{
91+
comm.KEY_TOOLSV2_CONF_PATH: options.output,
92+
},
93+
})
94+
}
95+
96+
return pb, nil
97+
}
98+
99+
func runExport(curveadm *cli.CurveAdm, options exportOptions) error {
100+
// 1) parse cluster topology
101+
dcs, err := curveadm.ParseTopology()
102+
if err != nil {
103+
return err
104+
}
105+
106+
// 2) generate get export playbook
107+
pb, err := genExportPlaybook(curveadm, dcs, options)
108+
if err != nil {
109+
return err
110+
}
111+
112+
// 3) run playground
113+
err = pb.Run()
114+
if err != nil {
115+
return err
116+
}
117+
118+
// 4) print success prompt
119+
curveadm.WriteOutln("")
120+
curveadm.WriteOutln(color.GreenString("Export curve.yaml to %s success ^_^"), options.output)
121+
return err
122+
}

internal/common/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
POOLSET = "poolset"
5050
POOLSET_DISK_TYPE = "poolset-disktype"
5151
KEY_NUMBER_OF_CHUNKSERVER = "NUMBER_OF_CHUNKSERVER"
52+
KEY_TOOLSV2_CONF_PATH = "TOOLSV2_CONF_PATH"
5253

5354
// format
5455
KEY_ALL_FORMAT_STATUS = "ALL_FORMAT_STATUS"

internal/errno/errno.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ var (
253253
ERR_UNSUPPORT_CLEAN_ITEM = EC(210005, "unsupport clean item")
254254
ERR_NO_SERVICES_MATCHED = EC(210006, "no services matched")
255255
// TODO: please check pool set disk type
256-
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
257-
ERR_UNSUPPORT_DEPLOY_TYPE = EC(210008, "unknown deploy type")
256+
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
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)
@@ -272,6 +272,8 @@ var (
272272
ERR_VOLUME_BLOCKSIZE_BE_MULTIPLE_OF_512 = EC(221011, "volume block size be a multiple of 512B, like 1KiB, 2KiB, 3KiB...")
273273
// 222: command options (client/fs)
274274
ERR_FS_MOUNTPOINT_REQUIRE_ABSOLUTE_PATH = EC(222000, "mount point must be an absolute path")
275+
// 223: command options (export)
276+
ERR_EXPORT_TOOLSV2_CONF_REQUIRE_ABSOLUTE_PATH = EC(223000, "/path/to/curve.yaml must be an absolute path")
275277

276278
// 230: command options (playground)
277279
ERR_UNSUPPORT_PLAYGROUND_KIND = EC(230000, "unsupport playground kind")

internal/playbook/factory.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const (
8383
GET_CLIENT_STATUS
8484
INSTALL_CLIENT
8585
UNINSTALL_CLIENT
86+
EXPORT_TOOLSV2_CONF
8687

8788
// bs
8889
FORMAT_CHUNKFILE_POOL
@@ -247,6 +248,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
247248
t, err = comm.NewInstallClientTask(curveadm, config.GetCC(i))
248249
case UNINSTALL_CLIENT:
249250
t, err = comm.NewUninstallClientTask(curveadm, nil)
251+
case EXPORT_TOOLSV2_CONF:
252+
t, err = comm.NewExportToolsV2ConfTask(curveadm, config.GetDC(i))
250253
// bs
251254
case FORMAT_CHUNKFILE_POOL:
252255
t, err = bs.NewFormatChunkfilePoolTask(curveadm, config.GetFC(i))

internal/task/task/bs/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
)
3838

3939
const (
40-
TOOLS_V2_CONFIG_DELIMITER = ": "
40+
TOOLS_V2_CONFIG_DELIMITER = ":"
4141
TOOLS_V2_CONFIG_SRC_PATH = "/curvebs/conf/curve.yaml"
4242
TOOLS_V2_CONFIG_DEST_PATH = "/etc/curve/curve.yaml"
4343
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2023-11-12
20+
* Author: Jiang Jun (youarefree123)
21+
*/
22+
23+
package common
24+
25+
import (
26+
"fmt"
27+
28+
"github.com/opencurve/curveadm/cli/cli"
29+
comm "github.com/opencurve/curveadm/internal/common"
30+
"github.com/opencurve/curveadm/internal/configure/topology"
31+
"github.com/opencurve/curveadm/internal/task/step"
32+
"github.com/opencurve/curveadm/internal/task/task"
33+
)
34+
35+
func NewExportToolsV2ConfTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
36+
serviceId := curveadm.GetServiceId(dc.GetId())
37+
containerId, err := curveadm.Storage().GetContainerId(serviceId)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
hc, err := curveadm.GetHost(dc.GetHost())
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
var ToolsV2Conf string
48+
localPath := curveadm.MemStorage().Get(comm.KEY_TOOLSV2_CONF_PATH).(string)
49+
subname := fmt.Sprintf("output=%s", localPath)
50+
t := task.NewTask("Export curve.yaml", subname, hc.GetSSHConfig())
51+
52+
t.AddStep(&step.ReadFile{
53+
ContainerId: containerId,
54+
ContainerSrcPath: dc.GetProjectLayout().ToolsV2ConfSystemPath,
55+
Content: &ToolsV2Conf,
56+
ExecOptions: curveadm.ExecOptions(),
57+
})
58+
59+
t.AddStep(&step.InstallFile{
60+
Content: &ToolsV2Conf,
61+
HostDestPath: localPath,
62+
ExecOptions: curveadm.ExecOptions(),
63+
})
64+
65+
return t, nil
66+
}

internal/task/task/common/sync_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
const (
4040
DEFAULT_CONFIG_DELIMITER = "="
4141
ETCD_CONFIG_DELIMITER = ": "
42-
TOOLS_V2_CONFIG_DELIMITER = ": "
42+
TOOLS_V2_CONFIG_DELIMITER = ":"
4343

4444
CURVE_CRONTAB_FILE = "/tmp/curve_crontab"
4545
)

internal/task/task/fs/mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const (
4545
FORMAT_MOUNT_OPTION = "type=bind,source=%s,target=%s,bind-propagation=rshared"
4646

4747
CLIENT_CONFIG_DELIMITER = "="
48-
TOOLS_V2_CONFIG_DELIMITER = ": "
48+
TOOLS_V2_CONFIG_DELIMITER = ":"
4949

5050
KEY_CURVEBS_CLUSTER = "curvebs.cluster"
5151

pkg/variable/variables.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,11 @@ func (vars *Variables) Rendering(s string) (string, error) {
139139
return s, nil
140140
}
141141

142-
var err error
143142
value := vars.r.ReplaceAllStringFunc(s, func(name string) string {
144-
val, e := vars.Get(name[2 : len(name)-1])
145-
if e != nil && err == nil {
146-
err = e
147-
}
143+
val, _ := vars.Get(name[2 : len(name)-1])
148144
return val
149145
})
150-
return value, err
146+
return value, nil
151147
}
152148

153149
func (vars *Variables) Debug() {

0 commit comments

Comments
 (0)