Skip to content

Commit

Permalink
feat(autok3s): support airgap API function
Browse files Browse the repository at this point in the history
The airgap API function is under /v1/pacakges and supports:
- create
- update
- remove
- list/get
- import
- export
- update install script
  • Loading branch information
orangedeng authored and Jason-ZW committed Aug 19, 2022
1 parent c4685d2 commit e0e30a3
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 233 deletions.
3 changes: 1 addition & 2 deletions cmd/airgap/args.go
Expand Up @@ -33,8 +33,7 @@ func validateName(name string) error {
if name == "" {
return errNameRequire
}
pkgs, _ := common.DefaultDB.ListPackages(&name)
if len(pkgs) > 0 {
if err := common.DefaultDB.PackageExists(name); err == nil {
return fmt.Errorf("package name %s already exists", name)
}
if errs := validation.IsDNS1123Subdomain(name); len(errs) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -105,6 +105,7 @@ require (
github.com/Masterminds/semver v1.5.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.5
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.32.0
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
Expand Down Expand Up @@ -183,7 +184,7 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/mount v0.3.0 // indirect
Expand All @@ -198,7 +199,6 @@ require (
github.com/openshift/custom-resource-status v0.0.0-20200602122900-c002fd1547ca // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/sftp v1.13.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1398,8 +1398,9 @@ github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
Expand Down Expand Up @@ -2229,7 +2230,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
2 changes: 1 addition & 1 deletion pkg/airgap/download.go
Expand Up @@ -117,7 +117,7 @@ func (d *Downloader) DownloadPackage() (string, error) {
if len(toAddArchs) == 0 &&
len(toDelArchs) == 0 &&
isDone(d.basePath) {
d.logger.Info("the package %s is ready, skip downloading resources.", d.pkg.Name)
d.logger.Infof("the package %s is ready, skip downloading resources.", d.pkg.Name)
return d.basePath, nil
}
if err := d.writeVersion(); err != nil {
Expand Down
42 changes: 25 additions & 17 deletions pkg/airgap/tarfiles.go
Expand Up @@ -10,20 +10,11 @@ import (
"path/filepath"
)

func SaveToTmp(path, name string) (string, error) {
func ReadToTmp(fp io.Reader, name string) (string, error) {
rtn := TempDir(name)
if err := os.MkdirAll(rtn, 0755); err != nil {
return rtn, err
}
if _, err := os.Lstat(path); err != nil {
return "", err
}

fp, err := os.Open(path)
if err != nil {
return "", err
}
defer fp.Close()
gzReader, err := gzip.NewReader(fp)
if err != nil {
return "", err
Expand Down Expand Up @@ -71,15 +62,20 @@ func SaveToTmp(path, name string) (string, error) {
return rtn, nil
}

func TarAndGzip(from, to string) error {
_, err := os.Lstat(to)
if err != nil && !os.IsNotExist(err) {
return err
func SaveToTmp(path, name string) (string, error) {
if _, err := os.Lstat(path); err != nil {
return "", err
}
if err == nil {
return fmt.Errorf("file %s exists, stop exporting", to)

fp, err := os.Open(path)
if err != nil {
return "", err
}
defer fp.Close()
return ReadToTmp(fp, name)
}

func TarAndGzipToWriter(from string, to io.Writer) error {
// related path to real path
files := map[string]string{}
if err := filepath.Walk(from, func(path string, info fs.FileInfo, err error) error {
Expand All @@ -93,12 +89,24 @@ func TarAndGzip(from, to string) error {
}); err != nil {
return err
}
return createArchive(files, to)
}

func TarAndGzip(from, to string) error {
_, err := os.Lstat(to)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
return fmt.Errorf("file %s exists, stop exporting", to)
}

toFile, err := os.Create(to)
if err != nil {
return err
}
defer toFile.Close()
return createArchive(files, toFile)
return TarAndGzipToWriter(from, toFile)
}

func createArchive(files map[string]string, buf io.Writer) error {
Expand Down
26 changes: 8 additions & 18 deletions pkg/common/db.go
Expand Up @@ -94,23 +94,6 @@ var (
is_default bool,
unique (name, provider)
);`,
`CREATE TABLE IF NOT EXISTS credentials
(
id integer not null primary key autoincrement,
provider TEXT not null,
secrets BLOB
);`,
`CREATE TABLE IF NOT EXISTS explorers
(
context_name TEXT not null primary key,
enabled bool,
port int
);`,
`CREATE TABLE IF NOT EXISTS settings
(
name TEXT not null primary key,
value BLOB
);`,
}
)

Expand All @@ -127,7 +110,14 @@ func InitStorage(ctx context.Context) error {
}

setup(store.DB)
if err := store.DB.AutoMigrate(&ClusterState{}, &Template{}, &Package{}); err != nil {
if err := store.DB.AutoMigrate(
&ClusterState{},
&Template{},
&Package{},
&Credential{},
&Explorer{},
&Setting{},
); err != nil {
return err
}

Expand Down

0 comments on commit e0e30a3

Please sign in to comment.