2023-11-24 09:56:35 +00:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flow/services/internal"
|
2023-11-26 07:46:10 +00:00
|
|
|
"flow/services/internal/actions"
|
2023-11-24 09:56:35 +00:00
|
|
|
"flow/services/internal/config"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2023-11-25 15:52:25 +00:00
|
|
|
// Deploy deploys the service(s) to the Flow Platform.
|
|
|
|
func Deploy(environment, service string) error {
|
2023-11-24 09:56:35 +00:00
|
|
|
mg.Deps(
|
2023-11-25 15:52:25 +00:00
|
|
|
mg.F(Prepare, environment, service),
|
2023-11-24 09:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
cfg, err := config.NewConfig(environment)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load the configuration; %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-26 07:46:10 +00:00
|
|
|
if err := actions.Deploy(cfg.Docker.Host, environment, service, true); err != nil {
|
2023-11-25 15:52:25 +00:00
|
|
|
return fmt.Errorf("error deploying %q; %w", service, err)
|
2023-11-24 09:56:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 15:52:25 +00:00
|
|
|
return nil
|
2023-11-24 09:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare prepares the service's build directory.
|
|
|
|
func Prepare(environment, service string) error {
|
2023-11-26 07:46:10 +00:00
|
|
|
if err := actions.Prepare(environment, service); err != nil {
|
2023-11-24 09:56:35 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-26 07:46:10 +00:00
|
|
|
if err := actions.Stop(cfg.Docker.Host, environment, service); err != nil {
|
|
|
|
return fmt.Errorf("error stopping %q; %w", service, err)
|
2023-11-24 09:56:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 07:46:10 +00:00
|
|
|
return nil
|
2023-11-24 09:56:35 +00:00
|
|
|
}
|