Skip to content

Commit

Permalink
Do not embed softwares.db
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Nov 18, 2024
1 parent 6023599 commit e17ab86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ COPY --from=builder /go/src/github.com/cloud-barista/cm-grasshopper/conf /conf
COPY --from=builder /go/src/github.com/cloud-barista/cm-grasshopper/cmd/cm-grasshopper/cm-grasshopper /cm-grasshopper

ADD playbook/ /playbook
ADD softwares.db /softwares.db

RUN mkdir -p /root/.cm-grasshopper/
RUN curl --ipv4 https://raw.githubusercontent.com/cloud-barista/cm-honeybee/main/server/_default_key/honeybee.key -o /root/.cm-grasshopper/honeybee.key
Expand Down
33 changes: 23 additions & 10 deletions db/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
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"
"io"
"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")
func copyFile(src string, dst string) (err error) {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer func() {
_ = sourceFile.Close()
}()

return os.WriteFile(dst, dbBytes, 0644)
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
_ = destFile.Close()
}()

_, err = io.Copy(destFile, sourceFile)

return err
}

func Open() error {
var err error

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

Expand Down
File renamed without changes.

0 comments on commit e17ab86

Please sign in to comment.