services/magefiles/magefile.go
Dan Anglin 9d2d5f36cc
refactor: move deploy code to internal package
Move the deploy code to an internal package in preparation for the
support for multiple mage targets deploying services to the Flow
Platform.

Add an option to deploy a service in the foreground.
2023-11-25 15:52:25 +00:00

106 lines
2.3 KiB
Go

//go:build mage
package main
import (
"flow/services/internal"
"flow/services/internal/config"
"flow/services/internal/deploy"
"flow/services/internal/prepare"
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Clean cleans the workspace.
func Clean(environment string) error {
buildDir := filepath.Join(internal.RootBuildDir, environment)
objects, err := os.ReadDir(buildDir)
if err != nil {
return err
}
for i := range objects {
name := objects[i].Name()
if name != ".gitkeep" {
if err := sh.Rm(buildDir + "/" + name); err != nil {
return err
}
}
}
return nil
}
// Deploy deploys the service(s) to the Flow Platform.
func Deploy(environment, service string) error {
mg.Deps(
mg.F(Prepare, environment, service),
)
cfg, err := config.NewConfig(environment)
if err != nil {
return fmt.Errorf("unable to load the configuration; %w", err)
}
if err := deploy.Deploy(cfg.Docker.Host, environment, service, true); err != nil {
return fmt.Errorf("error deploying %q; %w", service, err)
}
return nil
}
// Prepare prepares the service's build directory.
func Prepare(environment, service string) error {
if err := prepare.Prepare(environment, service); err != nil {
return fmt.Errorf("error running preparations; %w", err)
}
return nil
}
// Shutdown shuts down the Flow Platform in a given environment.
func Shutdown(environment string) error {
cfg, err := config.NewConfig(environment)
if err != nil {
return fmt.Errorf("unable to load the configuration; %w", err)
}
os.Setenv("DOCKER_HOST", cfg.Docker.Host)
command := []string{
"docker",
"compose",
"--project-directory",
fmt.Sprintf("%s/%s/compose", internal.RootBuildDir, environment),
"down",
}
return sh.RunV(command[0], command[1:]...)
}
// Stop stops a running service within the Flow Platform.
func Stop(environment, service string) error {
cfg, err := config.NewConfig(environment)
if err != nil {
return fmt.Errorf("unable to load the configuration; %w", err)
}
os.Setenv("DOCKER_HOST", cfg.Docker.Host)
command := []string{
"docker",
"compose",
"--project-directory",
fmt.Sprintf("%s/%s/compose", internal.RootBuildDir, environment),
"stop",
service,
}
return sh.RunV(command[0], command[1:]...)
}