168 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			168 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package deployers
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"fmt"
 | |
| 	"path/filepath"
 | |
| 
 | |
| 	"gitea.thegux.fr/hmdeploy/models"
 | |
| 	"gitea.thegux.fr/hmdeploy/utils"
 | |
| 	"github.com/rs/zerolog/log"
 | |
| )
 | |
| 
 | |
| // NginxDeployer handles the deployment of an Nginx configuration.
 | |
| type NginxDeployer struct {
 | |
| 	*deployer
 | |
| }
 | |
| 
 | |
| var _ IDeployer = (*NginxDeployer)(nil)
 | |
| 
 | |
| func NewNginxDeployer(
 | |
| 	ctx context.Context,
 | |
| 	project *models.Project,
 | |
| 	netInfo *models.HMNetInfo,
 | |
| ) (NginxDeployer, error) {
 | |
| 	var nd NginxDeployer
 | |
| 
 | |
| 	deployer, err := newDeployer(ctx, Nginx, project, netInfo)
 | |
| 	if err != nil {
 | |
| 		return nd, err
 | |
| 	}
 | |
| 	nd.deployer = deployer
 | |
| 
 | |
| 	return nd, nil
 | |
| }
 | |
| 
 | |
| func (nd NginxDeployer) getConfPath() string {
 | |
| 	return nd.project.GetNginxConfPath()
 | |
| }
 | |
| 
 | |
| func (nd NginxDeployer) getAssetsPath() string {
 | |
| 	return nd.project.GetNginxAssetsPath()
 | |
| }
 | |
| 
 | |
| func (nd *NginxDeployer) Build() error {
 | |
| 	nd.processing.Store(true)
 | |
| 	defer nd.processing.Store(false)
 | |
| 
 | |
| 	select {
 | |
| 	case <-nd.ctx.Done():
 | |
| 		nd.setDone(nil)
 | |
| 		return fmt.Errorf("%w, build nginx archive skipped", ErrContextDone)
 | |
| 	default:
 | |
| 	}
 | |
| 
 | |
| 	log.Info().Msg("building nginx archive for deployment...")
 | |
| 
 | |
| 	filesToArchive := []string{}
 | |
| 	if ap := nd.getAssetsPath(); ap != "" {
 | |
| 		filesToArchive = append(filesToArchive, ap)
 | |
| 	}
 | |
| 	if cp := nd.getConfPath(); cp != "" {
 | |
| 		filesToArchive = append(filesToArchive, cp)
 | |
| 	}
 | |
| 
 | |
| 	if len(filesToArchive) == 0 {
 | |
| 		return ErrEmptyArchive
 | |
| 	}
 | |
| 
 | |
| 	archivePath, err := utils.CreateArchive(
 | |
| 		nd.project.Dir,
 | |
| 		fmt.Sprintf("%s-%s", nd.project.Name, "nginx"),
 | |
| 		filesToArchive...,
 | |
| 	)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	nd.archivePath = archivePath
 | |
| 
 | |
| 	log.Info().Str("archive", archivePath).Msg("nginx archive built")
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (nd *NginxDeployer) Deploy() error {
 | |
| 	nd.processing.Store(true)
 | |
| 	defer nd.processing.Store(false)
 | |
| 
 | |
| 	select {
 | |
| 	case <-nd.ctx.Done():
 | |
| 		nd.setDone(nil)
 | |
| 		return fmt.Errorf("%w, nginx deployment skipped", ErrContextDone)
 | |
| 	default:
 | |
| 	}
 | |
| 
 | |
| 	if err := nd.copyUntarArchive(); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	if ap := nd.getAssetsPath(); ap != "" {
 | |
| 		log.Info().Msg("deploying nginx assets...")
 | |
| 
 | |
| 		if _, err := nd.conn.Execute(
 | |
| 			fmt.Sprintf(
 | |
| 				"rm -rf /var/www/static/%s/* && mkdir -p /var/www/static/%s && mv %s/* /var/www/static/%s",
 | |
| 				nd.project.Name,
 | |
| 				nd.project.Name,
 | |
| 				filepath.Join(nd.deploymentDir, filepath.Base(nd.getAssetsPath())),
 | |
| 				nd.project.Name,
 | |
| 			),
 | |
| 		); err != nil {
 | |
| 			nd.setDone(err)
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	if cp := nd.getConfPath(); cp != "" {
 | |
| 		nginxConf := nd.project.Name + ".conf"
 | |
| 		log.Info().Str("nginx", nginxConf).Msg("deploying nginx conf...")
 | |
| 
 | |
| 		if _, err := nd.conn.Execute(
 | |
| 			fmt.Sprintf(
 | |
| 				"mv %s /etc/nginx/sites-available/%s && ln -sf /etc/nginx/sites-available/%s /etc/nginx/sites-enabled/%s",
 | |
| 				filepath.Join(nd.deploymentDir, filepath.Base(cp)),
 | |
| 				nginxConf,
 | |
| 				nginxConf,
 | |
| 				nginxConf,
 | |
| 			),
 | |
| 		); err != nil {
 | |
| 			nd.setDone(err)
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	log.Info().Msg("nginx project successfully deployed")
 | |
| 	nd.setDone(nil)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (nd *NginxDeployer) Destroy() (err error) {
 | |
| 	nd.processing.Store(true)
 | |
| 	defer nd.processing.Store(false)
 | |
| 
 | |
| 	select {
 | |
| 	case <-nd.ctx.Done():
 | |
| 		nd.setDone(nil)
 | |
| 		return fmt.Errorf("%w, nginx destroy skipped", ErrContextDone)
 | |
| 	default:
 | |
| 	}
 | |
| 
 | |
| 	nginxConf := nd.project.Name + ".conf"
 | |
| 
 | |
| 	log.Info().Str("nginx", nginxConf).Msg("destroying nginx conf...")
 | |
| 
 | |
| 	_, err = nd.conn.Execute(
 | |
| 		fmt.Sprintf(
 | |
| 			"rm -f /etc/nginx/sites-enabled/%s",
 | |
| 			nginxConf,
 | |
| 		),
 | |
| 	)
 | |
| 	nd.setDone(err)
 | |
| 
 | |
| 	if err == nil {
 | |
| 		log.Info().Str("nginx", nginxConf).Msg("nginx conf successfully destroyed")
 | |
| 	}
 | |
| 
 | |
| 	return err
 | |
| }
 | 
