From 43a78573f594893bdf867abedd01a4df3d49c5c4 Mon Sep 17 00:00:00 2001 From: rmanach Date: Fri, 4 Apr 2025 15:50:02 +0200 Subject: [PATCH] fix hmdeploy.json with relative path --- README.md | 4 +++- deployers/commons.go | 3 +-- deployers/nginx.go | 7 ++----- deployers/swarm.go | 16 ++++++++++------ main.go | 5 ++--- models/project.go | 35 ++++++++++++++++++++++------------- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 704b9fa..c1edf0a 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,9 @@ hmdeploy --path /path/my-project ## Next steps * Improve the CLI arguments -* We'll see... +* Destroy +* post-install script +* Deals with bugs ## Contact For issues, questions or anything contact me at: admin@thegux.fr diff --git a/deployers/commons.go b/deployers/commons.go index 96f09fc..58e4a6c 100644 --- a/deployers/commons.go +++ b/deployers/commons.go @@ -6,9 +6,8 @@ import ( "sync/atomic" "time" - "github.com/rs/zerolog/log" - "gitea.thegux.fr/hmdeploy/models" + "github.com/rs/zerolog/log" ) var ErrContextDone = errors.New("unable to execute, context done") diff --git a/deployers/nginx.go b/deployers/nginx.go index 24d6f6e..c04a53c 100644 --- a/deployers/nginx.go +++ b/deployers/nginx.go @@ -3,12 +3,10 @@ package deployers import ( "context" "fmt" - "path/filepath" - - "github.com/rs/zerolog/log" "gitea.thegux.fr/hmdeploy/connection" "gitea.thegux.fr/hmdeploy/models" + "github.com/rs/zerolog/log" ) // NginxDeployer handles the deployment of an Nginx configuration. @@ -77,12 +75,11 @@ func (nd *NginxDeployer) Build() error { default: } - nginxPath := filepath.Join(nd.project.Dir, filepath.Base(nd.project.Deps.NginxFile)) nginxConf := nd.project.Name + ".conf" log.Info().Str("nginx", nginxConf).Msg("transferring nginx conf...") - if err := nd.conn.CopyFile(nginxPath, nginxConf); err != nil { + if err := nd.conn.CopyFile(nd.project.Deps.NginxFile, nginxConf); err != nil { nd.setDone(err) return err } diff --git a/deployers/swarm.go b/deployers/swarm.go index 3c8af9d..02e9a2a 100644 --- a/deployers/swarm.go +++ b/deployers/swarm.go @@ -7,12 +7,11 @@ import ( "os" "path/filepath" - "github.com/rs/zerolog/log" - "gitea.thegux.fr/hmdeploy/connection" "gitea.thegux.fr/hmdeploy/docker" "gitea.thegux.fr/hmdeploy/models" "gitea.thegux.fr/hmdeploy/utils" + "github.com/rs/zerolog/log" ) var ErrSwarmDeployerNoArchive = errors.New("no archive found to be deployed") @@ -107,7 +106,10 @@ func (sd *SwarmDeployer) Build() error { defer os.Remove(tarFile) //nolint: errcheck // defered - filesToArchive = append(filesToArchive, tarFile) + // copy the file directly instead of adding it in the tar archive + if err := sd.conn.CopyFile(tarFile, filepath.Base(tarFile)); err != nil { + return err + } log.Info().Str("image", imageName).Msg("image added to archive") } @@ -115,13 +117,15 @@ func (sd *SwarmDeployer) Build() error { if envFilePath := sd.project.Deps.EnvFile; envFilePath != "" { filesToArchive = append( filesToArchive, - filepath.Join(sd.project.Dir, filepath.Base(envFilePath)), + envFilePath, ) log.Info().Msg(".env file added to the archive for deployment") } - composeFileBase := filepath.Base(sd.project.Deps.ComposeFile) - filesToArchive = append(filesToArchive, filepath.Join(sd.project.Dir, composeFileBase)) + filesToArchive = append( + filesToArchive, + sd.project.Deps.ComposeFile, + ) archivePath, err := utils.CreateArchive( sd.project.Dir, diff --git a/main.go b/main.go index 8825a61..256c1cf 100644 --- a/main.go +++ b/main.go @@ -11,13 +11,12 @@ import ( "path" "sync" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "gitea.thegux.fr/hmdeploy/deployers" "gitea.thegux.fr/hmdeploy/docker" "gitea.thegux.fr/hmdeploy/models" "gitea.thegux.fr/hmdeploy/scheduler" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) const ( diff --git a/models/project.go b/models/project.go index 2582ff7..949f19a 100644 --- a/models/project.go +++ b/models/project.go @@ -23,28 +23,34 @@ const ( var ErrProjectConfFile = errors.New("project error") -func getFileInfo(baseDir, filePath string) (fs.FileInfo, error) { +func getFileInfo(baseDir, filePath string) (string, fs.FileInfo, error) { var fInf fs.FileInfo - filePath = filepath.Clean(filePath) - filePath = filepath.Join(baseDir, filePath) + if !filepath.IsAbs(filePath) { + filePath = filepath.Join(baseDir, filePath) - fileAbsPath, err := filepath.Abs(filePath) - if err != nil { - return fInf, fmt.Errorf("%w, file=%s, err=%v", ErrProjectConfFile, fileAbsPath, err) + filePath, err := filepath.Abs(filePath) + if err != nil { + return filePath, fInf, fmt.Errorf( + "%w, file=%s, err=%v", + ErrProjectConfFile, + filePath, + err, + ) + } } - fInf, err = os.Stat(fileAbsPath) + fInf, err := os.Stat(filePath) if err != nil { - return fInf, fmt.Errorf( + return filePath, fInf, fmt.Errorf( "%w, unable to stat file=%s, err=%v", ErrProjectConfFile, - fileAbsPath, + filePath, err, ) } - return fInf, nil + return filePath, fInf, nil } // Project handles the details and file informations of your project. @@ -65,27 +71,30 @@ type Project struct { } func (p *Project) validate() error { - cfs, err := getFileInfo(p.Dir, p.Deps.ComposeFile) + cpath, cfs, err := getFileInfo(p.Dir, p.Deps.ComposeFile) if err != nil { return err } p.Deps.ComposeFileInfo = cfs + p.Deps.ComposeFile = cpath if p.Deps.EnvFile != "" { - efs, err := getFileInfo(p.Dir, p.Deps.EnvFile) + epath, efs, err := getFileInfo(p.Dir, p.Deps.EnvFile) if err != nil { return err } p.Deps.EnvFileInfo = efs + p.Deps.EnvFile = epath } else { log.Warn().Msg("no .env file provided, hoping one it's set elsewhere...") } - nfs, err := getFileInfo(p.Dir, p.Deps.NginxFile) + npath, nfs, err := getFileInfo(p.Dir, p.Deps.NginxFile) if err != nil { return err } p.Deps.NginxFileInfo = nfs + p.Deps.NginxFile = npath return nil }