services/magefiles/magefile.go

118 lines
2.4 KiB
Go
Raw Normal View History

//go:build mage
package main
import (
"flow/services/internal"
"flow/services/internal/config"
"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 services to the Flow Platform.
func Deploy(environment, name string) error {
mg.Deps(
mg.F(Prepare, environment, name),
)
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),
"up",
"-d",
"--build",
}
if name != "all" {
command = append(command, name)
}
return sh.RunV(command[0], command[1:]...)
}
// 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:]...)
}