Skip to content

Commit

Permalink
refactor config and move Host to config
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang0 authored and anrs committed May 29, 2023
1 parent 61c2886 commit 589f965
Show file tree
Hide file tree
Showing 24 changed files with 361 additions and 384 deletions.
40 changes: 34 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/urfave/cli/v2"

"github.com/projecteru2/yavirt/cmd/guest"
"github.com/projecteru2/yavirt/cmd/host"
"github.com/projecteru2/yavirt/cmd/image"
"github.com/projecteru2/yavirt/cmd/maint"
"github.com/projecteru2/yavirt/cmd/network"
Expand All @@ -22,21 +21,50 @@ func main() {

app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: "/etc/eru/yavirtd.toml",
Usage: "config file path for yavirt, in yaml",
EnvVars: []string{"ERU_YAVIRT_CONFIG_PATH"},
},
&cli.StringFlag{
Name: "log-level",
Value: "INFO",
Usage: "set log level",
EnvVars: []string{"ERU_YAVIRT_LOG_LEVEL"},
},
&cli.StringSliceFlag{
Name: "config",
Usage: "config files",
Required: true,
Name: "core-addrs",
Value: cli.NewStringSlice(),
Usage: "core addresses",
EnvVars: []string{"ERU_YAVIRT_CORE_ADDRS"},
},
&cli.StringFlag{
Name: "core-username",
Value: "",
Usage: "core username",
EnvVars: []string{"ERU_YAVIRT_CORE_USERNAME"},
},
&cli.StringFlag{
Name: "core-password",
Value: "",
Usage: "core password",
EnvVars: []string{"ERU_YAVIRT_CORE_PASSWORD"},
},
&cli.StringFlag{
Name: "hostname",
Value: "",
Usage: "change hostname",
EnvVars: []string{"ERU_HOSTNAME", "HOSTNAME"},
},
&cli.BoolFlag{
Name: "skip-setup-host",
Value: false,
},
},

Commands: []*cli.Command{
guest.Command(),
image.Command(),
host.Command(),
network.Command(),
maint.Command(),
},
Expand Down
7 changes: 2 additions & 5 deletions cmd/guest/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/urfave/cli/v2"

"github.com/projecteru2/yavirt/cmd/run"
"github.com/projecteru2/yavirt/configs"
"github.com/projecteru2/yavirt/internal/models"
"github.com/projecteru2/yavirt/pkg/utils"
)

func listFlags() []cli.Flag {
Expand All @@ -31,10 +31,7 @@ func list(c *cli.Context, _ run.Runtime) error {
} else {
nodename := c.String("node")
if len(nodename) < 1 {
nodename, err = utils.Hostname()
if err != nil {
return err
}
nodename = configs.Hostname()
}
guests, err = models.GetNodeGuests(nodename)
}
Expand Down
73 changes: 0 additions & 73 deletions cmd/host/add.go

This file was deleted.

28 changes: 0 additions & 28 deletions cmd/host/get.go

This file was deleted.

25 changes: 0 additions & 25 deletions cmd/host/host.go

This file was deleted.

5 changes: 1 addition & 4 deletions cmd/maint/fasten.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/projecteru2/yavirt/pkg/libvirt"
"github.com/projecteru2/yavirt/pkg/netx"
"github.com/projecteru2/yavirt/pkg/store"
"github.com/projecteru2/yavirt/pkg/utils"
)

var intIPSubnets = map[int64]int64{}
Expand Down Expand Up @@ -154,9 +153,7 @@ func fastenDangling(id string, virt *libvirt.Libvirtee) error {
if err != nil {
return errors.Trace(err)
}
if guest.HostName, err = utils.Hostname(); err != nil {
return errors.Trace(err)
}
guest.HostName = configs.Hostname()
guest.ID = id
guest.ImageName = "ubuntu1604-sto"

Expand Down
28 changes: 11 additions & 17 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/projecteru2/yavirt/pkg/idgen"
"github.com/projecteru2/yavirt/pkg/netx"
"github.com/projecteru2/yavirt/pkg/store"
"github.com/projecteru2/yavirt/pkg/utils"
)

var runtime Runtime
Expand All @@ -31,7 +30,6 @@ type Runner func(*cli.Context, Runtime) error

// Runtime .
type Runtime struct {
ConfigFiles []string
SkipSetupHost bool
Host *models.Host
Device *device.Driver
Expand Down Expand Up @@ -62,7 +60,14 @@ func (r Runtime) ConvDecimal(ipv4 string) int64 {
// Run .
func Run(fn Runner) cli.ActionFunc {
return func(c *cli.Context) error {
runtime.ConfigFiles = c.StringSlice("config")
cfg := &configs.Conf

if err := cfg.Load([]string{c.String("config")}); err != nil {
return errors.Trace(err)
}
if err := cfg.Prepare(c); err != nil {
return err
}
runtime.SkipSetupHost = c.Bool("skip-setup-host")
runtime.Guest = manager.New()
// when add host, we need skip host setup
Expand All @@ -78,12 +83,6 @@ func Run(fn Runner) cli.ActionFunc {
}

func setup() error {
if len(runtime.ConfigFiles) > 0 {
if err := configs.Conf.Load(runtime.ConfigFiles); err != nil {
return errors.Trace(err)
}
}

if err := store.Setup("etcd"); err != nil {
return errors.Trace(err)
}
Expand All @@ -107,16 +106,11 @@ func setup() error {
return nil
}

func setupHost() error {
hn, err := utils.Hostname()
if err != nil {
func setupHost() (err error) {
if runtime.Host, err = models.LoadHost(); err != nil {
return errors.Trace(err)
}

if runtime.Host, err = models.LoadHost(hn); err != nil {
return errors.Annotatef(err, "invalid hostname %s", hn)
}

return nil
}

Expand All @@ -136,7 +130,7 @@ func setupCalico() (err error) {
}

var outboundIP string
if outboundIP, err = netx.GetOutboundIP(configs.Conf.CoreAddr); err != nil {
if outboundIP, err = netx.GetOutboundIP(configs.Conf.Core.Addrs[0]); err != nil {
return
}

Expand Down
Loading

0 comments on commit 589f965

Please sign in to comment.