Dan Anglin
25325e2856
Changes: - Most of the go code is now located in internal packages. - Code refactored and simplified in some cases. - Removed the 'download' mage target and integrated the download code into the internal 'prepare' package. - Moved all mage target code to magefile.go. - Added missing descriptions to the mage targets. - Updated go.mod. Fixed: - Created a custom function to validate the checksum of the downloaded Woodpecker tar file. - Specified the environment when running 'clean'.
117 lines
2.4 KiB
Go
117 lines
2.4 KiB
Go
//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:]...)
|
|
}
|