gear: Add c3os bundles

This commit is contained in:
Ettore Di Giacinto
2022-07-13 22:23:47 +00:00
committed by Itxaka
parent 2e86e483a7
commit 5f1ec1c03e

View File

@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
retry "github.com/avast/retry-go" retry "github.com/avast/retry-go"
"github.com/c3os-io/c3os/internal/machine" "github.com/c3os-io/c3os/internal/machine"
@@ -41,10 +42,38 @@ type Config struct {
K3s K3s `yaml:"k3s,omitempty"` K3s K3s `yaml:"k3s,omitempty"`
VPN map[string]string `yaml:"vpn,omitempty"` VPN map[string]string `yaml:"vpn,omitempty"`
//cloudFileContent string //cloudFileContent string
originalData map[string]interface{} originalData map[string]interface{}
location string location string
ConfigURL string `yaml:"config_url,omitempty"` ConfigURL string `yaml:"config_url,omitempty"`
Options map[string]string `yaml:"options,omitempty"` Options map[string]string `yaml:"options,omitempty"`
IgnoreBundleErrors bool `yaml:"ignore_bundles_errors,omitempty"`
Bundles Bundles `yaml:"bundles,omitempty"`
}
type Bundles []Bundle
type Bundle struct {
Repository string `yaml:"repository,omitempty"`
Rootfs string `yaml:"rootfs_path,omitempty"`
DB string `yaml:"db_path,omitempty"`
Targets []string `yaml:"targets,omitempty"`
}
func (b Bundles) Options() (res [][]machine.BundleOption) {
for _, bundle := range b {
for _, t := range bundle.Targets {
opts := []machine.BundleOption{machine.WithRepository(bundle.Repository), machine.WithTarget(t)}
if bundle.Rootfs != "" {
opts = append(opts, machine.WithRootFS(bundle.Rootfs))
}
if bundle.DB != "" {
opts = append(opts, machine.WithDBHPath(bundle.DB))
}
res = append(res, opts)
}
}
return
} }
func (c Config) Data() map[string]interface{} { func (c Config) Data() map[string]interface{} {
@@ -63,6 +92,15 @@ func (c Config) String() string {
return string(dat) return string(dat)
} }
func (c Config) IsValid() bool {
return c.C3OS != nil ||
c.K3s.Enabled ||
c.K3sAgent.Enabled ||
c.ConfigURL != "" ||
len(c.Bundles) != 0 ||
len(c.VPN) != 0
}
func Scan(opts ...Option) (c *Config, err error) { func Scan(opts ...Option) (c *Config, err error) {
o := &Options{} o := &Options{}
@@ -81,6 +119,8 @@ func Scan(opts ...Option) (c *Config, err error) {
} }
} }
configFound := false
lastYamlFileFound := ""
for _, f := range files { for _, f := range files {
if fileSize(f) > 1.0 { if fileSize(f) > 1.0 {
//fmt.Println("warning: Skipping file ", f, "as exceeds 1 MB in size") //fmt.Println("warning: Skipping file ", f, "as exceeds 1 MB in size")
@@ -89,12 +129,28 @@ func Scan(opts ...Option) (c *Config, err error) {
b, err := ioutil.ReadFile(f) b, err := ioutil.ReadFile(f)
if err == nil { if err == nil {
yaml.Unmarshal(b, c) yaml.Unmarshal(b, c)
if c.C3OS != nil || c.K3s.Enabled || c.K3sAgent.Enabled { if c.IsValid() {
// c.cloudFileContent = string(b) // c.cloudFileContent = string(b)
c.location = f c.location = f
yaml.Unmarshal(b, &c.originalData) yaml.Unmarshal(b, &c.originalData)
configFound = true
break break
} }
// record back the only yaml file found (if any)
if strings.HasSuffix(strings.ToLower(f), "yaml") || strings.HasSuffix(strings.ToLower(f), "yml") {
lastYamlFileFound = f
}
}
}
// use last recorded if no config is found valid
if !configFound && lastYamlFileFound != "" {
b, err := ioutil.ReadFile(lastYamlFileFound)
if err == nil {
yaml.Unmarshal(b, c)
c.location = lastYamlFileFound
yaml.Unmarshal(b, &c.originalData)
} }
} }