diff --git a/app/cmd/init.go b/app/cmd/init.go index 929717c3f89f6d896bc2bd15e65669db2522a0a1..9cf292ed5cd9c8ed12e83820d3155ceb34080006 100755 --- a/app/cmd/init.go +++ b/app/cmd/init.go @@ -13,20 +13,35 @@ 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. */ + package cmd import ( + "fmt" + + "gitee.com/openeuler/nestos-kubernetes-deployer/app/apis/nkd" phases "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/init" "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" + "gitee.com/openeuler/nestos-kubernetes-deployer/app/util/config" "github.com/spf13/cobra" ) +type initData struct { + cfg *nkd.Nkd +} + func NewInitCommand() *cobra.Command { initRunner := workflow.NewRunner() cmd := &cobra.Command{ Use: "init", Short: "Use this command to init insert or config", RunE: func(cmd *cobra.Command, args []string) error { + c, err := initRunner.InitData(args) + if err != nil { + return err + } + data := c.(*initData) + fmt.Println(data.cfg) return initRunner.Run() }, } @@ -34,5 +49,27 @@ func NewInitCommand() *cobra.Command { phases.NewGenerateCertsCmd() initRunner.AppendPhase(phases.NewGenerateCertsCmd()) initRunner.AppendPhase(phases.NewGenerateIgnCmd()) + initRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) { + data, err := newInitData(cmd, args) + if err != nil { + return nil, err + } + return data, nil + }) return cmd } + +func (i *initData) Cfg() *nkd.Nkd { + return i.cfg +} +func newInitData(cmd *cobra.Command, args []string) (*initData, error) { + + var newNkd *nkd.Nkd + cfg, err := config.LoadOrDefaultInitConfiguration("path", newNkd) + if err != nil { + return nil, err + } + return &initData{ + cfg: cfg, + }, nil +} diff --git a/app/cmd/phases/init/cert.go b/app/cmd/phases/init/cert.go index 542a86fd200064b3d731ddfe8141b59a10a49c69..41fa4d7ce77014d2aebe73a1fa72d87b7e295dad 100644 --- a/app/cmd/phases/init/cert.go +++ b/app/cmd/phases/init/cert.go @@ -15,7 +15,11 @@ limitations under the License. */ package phases -import "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" +import ( + "fmt" + + "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" +) func NewGenerateCertsCmd() workflow.Phase { return workflow.Phase{ @@ -25,6 +29,8 @@ func NewGenerateCertsCmd() workflow.Phase { } } -func runGenerateCertsConfig() error { +func runGenerateCertsConfig(r workflow.RunData) error { + data := r.(InitData) + fmt.Println(data.Cfg()) return nil } diff --git a/app/cmd/phases/init/data.go b/app/cmd/phases/init/data.go new file mode 100644 index 0000000000000000000000000000000000000000..a0411cc4f795b4c4848cb84e66447325269336da --- /dev/null +++ b/app/cmd/phases/init/data.go @@ -0,0 +1,23 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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. +*/ + +package phases + +import "gitee.com/openeuler/nestos-kubernetes-deployer/app/apis/nkd" + +type InitData interface { + Cfg() *nkd.Nkd +} diff --git a/app/cmd/phases/init/ign.go b/app/cmd/phases/init/ign.go index 74cf5a85087fd6adebebe12eca4ddb630e4086d2..6d5b82f6202bb3efa9d9ab08cf6476a01a817eb4 100644 --- a/app/cmd/phases/init/ign.go +++ b/app/cmd/phases/init/ign.go @@ -25,6 +25,7 @@ func NewGenerateIgnCmd() workflow.Phase { } } -func runGenerateIgnConfig() error { +func runGenerateIgnConfig(r workflow.RunData) error { + return nil } diff --git a/app/cmd/phases/workflow/phase.go b/app/cmd/phases/workflow/phase.go index be993ea483baf9a75a4496a088fc7222c6d95096..63854613bb114e77bc4616366a3c6d9d43de87be 100644 --- a/app/cmd/phases/workflow/phase.go +++ b/app/cmd/phases/workflow/phase.go @@ -19,5 +19,5 @@ type Phase struct { Name string Short string Phases []Phase - Run func() error + Run func(data RunData) error } diff --git a/app/cmd/phases/workflow/runner.go b/app/cmd/phases/workflow/runner.go index 99e040e22db3318c941df6cb9f2c432223dbab10..92bb91485187e08abd29ef6f64bc336a734911ef 100644 --- a/app/cmd/phases/workflow/runner.go +++ b/app/cmd/phases/workflow/runner.go @@ -17,13 +17,20 @@ package workflow import ( "strings" + + "github.com/spf13/cobra" ) type Runner struct { - Phases []Phase - phaseRunners []*phaseRunner + Phases []Phase + phaseRunners []*phaseRunner + runData RunData + runDataInitializer func(*cobra.Command, []string) (RunData, error) + runCmd *cobra.Command } +type RunData interface{} + type phaseRunner struct { Phase parent *phaseRunner @@ -39,12 +46,28 @@ func NewRunner() *Runner { } } +func (r *Runner) InitData(args []string) (RunData, error) { + if r.runData == nil && r.runDataInitializer != nil { + var err error + if r.runData, err = r.runDataInitializer(r.runCmd, args); err != nil { + return nil, err + } + return r.runData, nil + } + return nil, nil +} + +func (r *Runner) SetDataInitializer(builder func(*cobra.Command, []string) (RunData, error)) { + r.runDataInitializer = builder +} + func (r *Runner) Run() error { r.prepareForExcution() + data := r.runData err := r.VisitAll(func(p *phaseRunner) error { if p.Run != nil { - if err := p.Run(); err != nil { + if err := p.Run(data); err != nil { return err } } diff --git a/app/util/config/initconfiguration.go b/app/util/config/initconfiguration.go new file mode 100644 index 0000000000000000000000000000000000000000..d8449bfd351db48d1ab06bcfd364f20275f36bff --- /dev/null +++ b/app/util/config/initconfiguration.go @@ -0,0 +1,57 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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. +*/ + +package config + +import ( + "fmt" + "io/ioutil" + + "gitee.com/openeuler/nestos-kubernetes-deployer/app/apis/nkd" + "gopkg.in/yaml.v2" +) + +func LoadOrDefaultInitConfiguration(cfgPath string, cfg *nkd.Nkd) (*nkd.Nkd, error) { + if cfgPath != "" { + cfg, err := LoadInitConfigurationFromFile() + if err != nil { + return nil, err + } + return cfg, nil + } + cfg, err := DefaultinitConfiguration() + if err != nil { + return nil, err + } + return cfg, nil + +} + +func LoadInitConfigurationFromFile() (*nkd.Nkd, error) { + conf := new(nkd.Nkd) + yamlFile, err := ioutil.ReadFile("test.yaml") + fmt.Println(err) + if err != nil { + return nil, err + } + err = yaml.Unmarshal(yamlFile, conf) + fmt.Println(conf.Infra.Platform) + return conf, nil +} + +func DefaultinitConfiguration() (*nkd.Nkd, error) { + return nil, nil +}