fix hmdeploy.json with relative path

This commit is contained in:
rmanach 2025-04-04 15:50:02 +02:00
parent a70030c747
commit 43a78573f5
6 changed files with 40 additions and 30 deletions

View File

@ -127,7 +127,9 @@ hmdeploy --path /path/my-project
## Next steps ## Next steps
* Improve the CLI arguments * Improve the CLI arguments
* We'll see... * Destroy
* post-install script
* Deals with bugs
## Contact ## Contact
For issues, questions or anything contact me at: admin@thegux.fr For issues, questions or anything contact me at: admin@thegux.fr

View File

@ -6,9 +6,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/rs/zerolog/log"
"gitea.thegux.fr/hmdeploy/models" "gitea.thegux.fr/hmdeploy/models"
"github.com/rs/zerolog/log"
) )
var ErrContextDone = errors.New("unable to execute, context done") var ErrContextDone = errors.New("unable to execute, context done")

View File

@ -3,12 +3,10 @@ package deployers
import ( import (
"context" "context"
"fmt" "fmt"
"path/filepath"
"github.com/rs/zerolog/log"
"gitea.thegux.fr/hmdeploy/connection" "gitea.thegux.fr/hmdeploy/connection"
"gitea.thegux.fr/hmdeploy/models" "gitea.thegux.fr/hmdeploy/models"
"github.com/rs/zerolog/log"
) )
// NginxDeployer handles the deployment of an Nginx configuration. // NginxDeployer handles the deployment of an Nginx configuration.
@ -77,12 +75,11 @@ func (nd *NginxDeployer) Build() error {
default: default:
} }
nginxPath := filepath.Join(nd.project.Dir, filepath.Base(nd.project.Deps.NginxFile))
nginxConf := nd.project.Name + ".conf" nginxConf := nd.project.Name + ".conf"
log.Info().Str("nginx", nginxConf).Msg("transferring nginx 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) nd.setDone(err)
return err return err
} }

View File

@ -7,12 +7,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/rs/zerolog/log"
"gitea.thegux.fr/hmdeploy/connection" "gitea.thegux.fr/hmdeploy/connection"
"gitea.thegux.fr/hmdeploy/docker" "gitea.thegux.fr/hmdeploy/docker"
"gitea.thegux.fr/hmdeploy/models" "gitea.thegux.fr/hmdeploy/models"
"gitea.thegux.fr/hmdeploy/utils" "gitea.thegux.fr/hmdeploy/utils"
"github.com/rs/zerolog/log"
) )
var ErrSwarmDeployerNoArchive = errors.New("no archive found to be deployed") 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 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") 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 != "" { if envFilePath := sd.project.Deps.EnvFile; envFilePath != "" {
filesToArchive = append( filesToArchive = append(
filesToArchive, filesToArchive,
filepath.Join(sd.project.Dir, filepath.Base(envFilePath)), envFilePath,
) )
log.Info().Msg(".env file added to the archive for deployment") log.Info().Msg(".env file added to the archive for deployment")
} }
composeFileBase := filepath.Base(sd.project.Deps.ComposeFile) filesToArchive = append(
filesToArchive = append(filesToArchive, filepath.Join(sd.project.Dir, composeFileBase)) filesToArchive,
sd.project.Deps.ComposeFile,
)
archivePath, err := utils.CreateArchive( archivePath, err := utils.CreateArchive(
sd.project.Dir, sd.project.Dir,

View File

@ -11,13 +11,12 @@ import (
"path" "path"
"sync" "sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gitea.thegux.fr/hmdeploy/deployers" "gitea.thegux.fr/hmdeploy/deployers"
"gitea.thegux.fr/hmdeploy/docker" "gitea.thegux.fr/hmdeploy/docker"
"gitea.thegux.fr/hmdeploy/models" "gitea.thegux.fr/hmdeploy/models"
"gitea.thegux.fr/hmdeploy/scheduler" "gitea.thegux.fr/hmdeploy/scheduler"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
) )
const ( const (

View File

@ -23,28 +23,34 @@ const (
var ErrProjectConfFile = errors.New("project error") 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 var fInf fs.FileInfo
filePath = filepath.Clean(filePath) if !filepath.IsAbs(filePath) {
filePath = filepath.Join(baseDir, filePath) filePath = filepath.Join(baseDir, filePath)
fileAbsPath, err := filepath.Abs(filePath) filePath, err := filepath.Abs(filePath)
if err != nil { if err != nil {
return fInf, fmt.Errorf("%w, file=%s, err=%v", ErrProjectConfFile, fileAbsPath, err) 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 { if err != nil {
return fInf, fmt.Errorf( return filePath, fInf, fmt.Errorf(
"%w, unable to stat file=%s, err=%v", "%w, unable to stat file=%s, err=%v",
ErrProjectConfFile, ErrProjectConfFile,
fileAbsPath, filePath,
err, err,
) )
} }
return fInf, nil return filePath, fInf, nil
} }
// Project handles the details and file informations of your project. // Project handles the details and file informations of your project.
@ -65,27 +71,30 @@ type Project struct {
} }
func (p *Project) validate() error { 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 { if err != nil {
return err return err
} }
p.Deps.ComposeFileInfo = cfs p.Deps.ComposeFileInfo = cfs
p.Deps.ComposeFile = cpath
if p.Deps.EnvFile != "" { if p.Deps.EnvFile != "" {
efs, err := getFileInfo(p.Dir, p.Deps.EnvFile) epath, efs, err := getFileInfo(p.Dir, p.Deps.EnvFile)
if err != nil { if err != nil {
return err return err
} }
p.Deps.EnvFileInfo = efs p.Deps.EnvFileInfo = efs
p.Deps.EnvFile = epath
} else { } else {
log.Warn().Msg("no .env file provided, hoping one it's set elsewhere...") 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 { if err != nil {
return err return err
} }
p.Deps.NginxFileInfo = nfs p.Deps.NginxFileInfo = nfs
p.Deps.NginxFile = npath
return nil return nil
} }