-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]curveadm: add export cmd for export the yaml that tools-v2 used
Signed-off-by: youarefree123 <[email protected]>
- Loading branch information
1 parent
48abdf1
commit 2f63acf
Showing
7 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2023-11-9 | ||
* Author: Jiang Jun (youarefree123) | ||
*/ | ||
|
||
// __SIGN_BY_YOUAREFREE123__ | ||
|
||
package command | ||
|
||
import ( | ||
"path" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/opencurve/curveadm/cli/cli" | ||
comm "github.com/opencurve/curveadm/internal/common" | ||
"github.com/opencurve/curveadm/internal/configure/topology" | ||
"github.com/opencurve/curveadm/internal/errno" | ||
"github.com/opencurve/curveadm/internal/playbook" | ||
cliutil "github.com/opencurve/curveadm/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
GET_EXPORT_PLAYBOOK_STEPS = []int{ | ||
playbook.SYNC_CONFIG, | ||
playbook.EXPORT_TOOLSV2_CONF, | ||
} | ||
) | ||
|
||
type exportOptions struct { | ||
output string | ||
} | ||
|
||
func checkExportOptions(curveadm *cli.CurveAdm, options exportOptions) error { | ||
if !strings.HasPrefix(options.output, "/") { | ||
return errno.ERR_EXPORT_TOOLSV2_CONF_REQUIRE_ABSOLUTE_PATH. | ||
F("/path/to/curve.yaml: %s", options.output) | ||
} | ||
return nil | ||
} | ||
|
||
func NewExportCommand(curveadm *cli.CurveAdm) *cobra.Command { | ||
var options exportOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "export [OPTIONS]", | ||
Short: "Export curve.yaml", | ||
Args: cliutil.NoArgs, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return checkExportOptions(curveadm, options) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runExport(curveadm, options) | ||
}, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVarP(&options.output, "output", "o", path.Join(curveadm.PluginDir(), "curve.yaml"), "Path where the exported YAML is stored") | ||
|
||
return cmd | ||
} | ||
|
||
func genExportPlaybook(curveadm *cli.CurveAdm, | ||
dcs []*topology.DeployConfig, | ||
options exportOptions) (*playbook.Playbook, error) { | ||
|
||
steps := GET_EXPORT_PLAYBOOK_STEPS | ||
|
||
pb := playbook.NewPlaybook(curveadm) | ||
for _, step := range steps { | ||
if step == playbook.EXPORT_TOOLSV2_CONF { | ||
pb.AddStep(&playbook.PlaybookStep{ | ||
Type: step, | ||
Configs: dcs[:1], | ||
Options: map[string]interface{}{ | ||
comm.KEY_TOOLSV2_CONF_PATH: options.output, | ||
}, | ||
}) | ||
} else { | ||
pb.AddStep(&playbook.PlaybookStep{ | ||
Type: step, | ||
Configs: dcs, | ||
Options: map[string]interface{}{ | ||
comm.KEY_TOOLSV2_CONF_PATH: options.output, | ||
}, | ||
}) | ||
} | ||
|
||
} | ||
|
||
return pb, nil | ||
} | ||
|
||
func runExport(curveadm *cli.CurveAdm, options exportOptions) error { | ||
// 1) parse cluster topology | ||
dcs, err := curveadm.ParseTopology() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2) generate get export playbook | ||
pb, err := genExportPlaybook(curveadm, dcs, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 3) run playground | ||
err = pb.Run() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 4) print success prompt | ||
curveadm.WriteOutln("") | ||
curveadm.WriteOutln(color.GreenString("Export curve.yaml to %s success ^_^"), options.output) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2023-11-12 | ||
* Author: Jiang Jun (youarefree123) | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/opencurve/curveadm/cli/cli" | ||
comm "github.com/opencurve/curveadm/internal/common" | ||
"github.com/opencurve/curveadm/internal/configure/topology" | ||
"github.com/opencurve/curveadm/internal/task/step" | ||
"github.com/opencurve/curveadm/internal/task/task" | ||
) | ||
|
||
func NewExportToolsV2ConfTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) { | ||
serviceId := curveadm.GetServiceId(dc.GetId()) | ||
containerId, err := curveadm.Storage().GetContainerId(serviceId) | ||
if curveadm.IsSkip(dc) { | ||
return nil, nil | ||
} else if err != nil { | ||
return nil, err | ||
} else if len(containerId) == 0 { | ||
return nil, nil | ||
} else if containerId == comm.CLEANED_CONTAINER_ID { | ||
return nil, nil | ||
} | ||
hc, err := curveadm.GetHost(dc.GetHost()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ToolsV2Conf string | ||
localPath := curveadm.MemStorage().Get(comm.KEY_TOOLSV2_CONF_PATH).(string) | ||
subname := fmt.Sprintf("output=%s", localPath) | ||
t := task.NewTask("Export curve.yaml", subname, hc.GetSSHConfig()) | ||
|
||
t.AddStep(&step.ReadFile{ | ||
ContainerId: containerId, | ||
ContainerSrcPath: dc.GetProjectLayout().ToolsV2ConfSystemPath, | ||
Content: &ToolsV2Conf, | ||
ExecOptions: curveadm.ExecOptions(), | ||
}) | ||
|
||
t.AddStep(&step.InstallFile{ | ||
Content: &ToolsV2Conf, | ||
HostDestPath: localPath, | ||
ExecOptions: curveadm.ExecOptions(), | ||
}) | ||
|
||
return t, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters