Skip to content

Commit

Permalink
Embed softwares.db and add custom_configs option
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Nov 18, 2024
1 parent e569b88 commit 825b9a9
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 15 deletions.
Binary file renamed softwares.db → db/softwares.db
Binary file not shown.
23 changes: 23 additions & 0 deletions db/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
package db

import (
"embed"
"github.com/cloud-barista/cm-grasshopper/common"
"github.com/cloud-barista/cm-grasshopper/pkg/api/rest/model"
"github.com/glebarez/sqlite"
"github.com/jollaman999/utils/fileutil"
"github.com/jollaman999/utils/logger"
"gorm.io/gorm"
"os"
)

//go:embed softwares.db
var embeddedDB embed.FS

var SoftwaresDB *gorm.DB
var DB *gorm.DB

func copyEmbeddedDB(dst string) error {
dbBytes, err := embeddedDB.ReadFile("softwares.db")
if err != nil {
return err
}

return os.WriteFile(dst, dbBytes, 0644)
}

func Open() error {
var err error

targetPath := common.RootPath + "/softwares.db"
if !fileutil.IsExist(targetPath) {
err := copyEmbeddedDB(targetPath)
if err != nil {
return err
}
}

SoftwaresDB, err = gorm.Open(sqlite.Open(common.RootPath+"/softwares.db"), &gorm.Config{})
if err != nil {
logger.Panicln(logger.ERROR, true, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"needed_packages": [
"nfs-kernel-server"
],
"custom_configs": [
"/etc/exports"
],
"os": "Ubuntu",
"os_version": "22.04",
"version": "latest"
Expand Down
31 changes: 16 additions & 15 deletions lib/software/configCopier.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ EOL
return nil
}

func parseConfigLine(line string) ConfigFile {
migrationLogger.Printf(DEBUG, "Parsing config line: %s\n", line)

parts := strings.SplitN(line, " [", 2)
conf := ConfigFile{
Path: strings.TrimSpace(parts[0]),
}

if len(parts) > 1 {
conf.Status = strings.TrimSuffix(parts[1], "]")
}

migrationLogger.Printf(DEBUG, "Parsed config - Path: %s, Status: %s\n", conf.Path, conf.Status)
return conf
}

func findConfigs(client *ssh.Client, packageName string) ([]ConfigFile, error) {
migrationLogger.Printf(INFO, "Starting config search for package: %s\n", packageName)

Expand Down Expand Up @@ -438,21 +454,6 @@ rm -f "$tmp_result"`
return configs, nil
}

func parseConfigLine(line string) ConfigFile {
migrationLogger.Printf(DEBUG, "Parsing config line: %s\n", line)

parts := strings.SplitN(line, " [", 2)
conf := ConfigFile{
Path: strings.TrimSpace(parts[0]),
}

if len(parts) > 1 {
conf.Status = strings.TrimSuffix(parts[1], "]")
}

migrationLogger.Printf(DEBUG, "Parsed config - Path: %s, Status: %s\n", conf.Path, conf.Status)
return conf
}
func copyConfigFiles(sourceClient *ssh.Client, targetClient *ssh.Client, configs []ConfigFile) error {
migrationLogger.Printf(INFO, "Starting config files copy process for %d files\n", len(configs))

Expand Down
27 changes: 27 additions & 0 deletions lib/software/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cloud-barista/cm-grasshopper/lib/ssh"
"github.com/cloud-barista/cm-grasshopper/pkg/api/rest/model"
"github.com/jollaman999/utils/logger"
"strings"
"time"
)

Expand Down Expand Up @@ -98,6 +99,32 @@ func MigrateSoftware(executionID string, executionList *[]model.MigrationSoftwar
return
}

sw, err := dao.SoftwareGet(execution.SoftwareID)
if err != nil {
logger.Println(logger.ERROR, true, "migrateSoftware: ExecutionID="+executionID+
", SoftwareID="+execution.SoftwareID+", Error="+err.Error())
updateStatus(i, "failed", err.Error(), false)

return
}

migrationLogger.Printf(INFO, "Starting to copy custom configs")
var customConfigs []ConfigFile
for _, customConfig := range strings.Split(sw.CustomConfigs, ",") {
customConfigs = append(customConfigs, ConfigFile{
Path: customConfig,
Status: "Custom",
})
}
err = copyConfigFiles(s, t, customConfigs)
if err != nil {
logger.Println(logger.ERROR, true, "migrateSoftware: ExecutionID="+executionID+
", SoftwareID="+execution.SoftwareID+", Error="+err.Error())
updateStatus(i, "failed", err.Error(), false)

return
}

err = serviceMigrator(s, t, execution.SoftwareName, executionID)
if err != nil {
logger.Println(logger.ERROR, true, "migrateSoftware: ExecutionID="+executionID+
Expand Down
12 changes: 12 additions & 0 deletions pkg/api/rest/controller/software.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ func RegisterSoftware(c echo.Context) error {
needToDeletePackages = needToDeletePackages[:len(needToDeletePackages)-1]
}

var customConfigs string
if len(softwareRegisterReq.CustomConfigs) > 0 {
for _, customConfig := range softwareRegisterReq.CustomConfigs {
if strings.Contains(customConfig, ",") {
return common.ReturnErrorMsg(c, "Each name of custom_configs should not contain ','")
}
customConfigs += customConfig + ","
}
customConfigs = customConfigs[:len(customConfigs)-1]
}

var id = uuid.New().String()

sw := model.Software{
Expand All @@ -213,6 +224,7 @@ func RegisterSoftware(c echo.Context) error {
MatchNames: matchNames,
NeededPackages: neededPackages,
NeedToDeletePackages: needToDeletePackages,
CustomConfigs: customConfigs,
RepoURL: softwareRegisterReq.RepoURL,
GPGKeyURL: softwareRegisterReq.GPGKeyURL,
RepoUseOSVersionCode: softwareRegisterReq.RepoUseOSVersionCode,
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ const docTemplate = `{
"created_at": {
"type": "string"
},
"custom_configs": {
"type": "string"
},
"gpg_key_url": {
"type": "string"
},
Expand Down Expand Up @@ -590,6 +593,12 @@ const docTemplate = `{
"architecture": {
"type": "string"
},
"custom_configs": {
"type": "array",
"items": {
"type": "string"
}
},
"gpg_key_url": {
"type": "string"
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@
"created_at": {
"type": "string"
},
"custom_configs": {
"type": "string"
},
"gpg_key_url": {
"type": "string"
},
Expand Down Expand Up @@ -583,6 +586,12 @@
"architecture": {
"type": "string"
},
"custom_configs": {
"type": "array",
"items": {
"type": "string"
}
},
"gpg_key_url": {
"type": "string"
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ definitions:
type: string
created_at:
type: string
custom_configs:
type: string
gpg_key_url:
type: string
install_type:
Expand Down Expand Up @@ -126,6 +128,10 @@ definitions:
properties:
architecture:
type: string
custom_configs:
items:
type: string
type: array
gpg_key_url:
type: string
install_type:
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/rest/model/software.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Software struct {
MatchNames string `gorm:"match_names" json:"match_names" validate:"required"`
NeededPackages string `json:"needed_packages" validate:"required"`
NeedToDeletePackages string `json:"need_to_delete_packages"`
CustomConfigs string `json:"custom_configs"`
RepoURL string `json:"repo_url"`
GPGKeyURL string `json:"gpg_key_url"`
RepoUseOSVersionCode bool `json:"repo_use_os_version_code" default:"false"`
Expand Down Expand Up @@ -73,6 +74,7 @@ type SoftwareRegisterReq struct {
MatchNames []string `json:"match_names" validate:"required"`
NeededPackages []string `json:"needed_packages" validate:"required"`
NeedToDeletePackages []string `json:"need_to_delete_packages"`
CustomConfigs []string `json:"custom_configs"`
RepoURL string `json:"repo_url"`
GPGKeyURL string `json:"gpg_key_url"`
RepoUseOSVersionCode bool `json:"repo_use_os_version_code" default:"false"`
Expand Down

0 comments on commit 825b9a9

Please sign in to comment.