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.
This commit is contained in:
Dan Anglin 2023-11-25 15:52:25 +00:00
parent ba68c51594
commit 9d2d5f36cc
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 39 additions and 18 deletions

32
internal/deploy/deploy.go Normal file
View file

@ -0,0 +1,32 @@
package deploy
import (
"flow/services/internal"
"fmt"
"os"
"github.com/magefile/mage/sh"
)
func Deploy(dockerHost, environment, name string, daemon bool) error {
os.Setenv("DOCKER_HOST", dockerHost)
command := []string{
"docker",
"compose",
"--project-directory",
fmt.Sprintf("%s/%s/compose", internal.RootBuildDir, environment),
"up",
"--build",
}
if daemon {
command = append(command, "-d")
}
if name != "all" {
command = append(command, name)
}
return sh.RunV(command[0], command[1:]...)
}

View file

@ -5,6 +5,7 @@ package main
import (
"flow/services/internal"
"flow/services/internal/config"
"flow/services/internal/deploy"
"flow/services/internal/prepare"
"fmt"
"os"
@ -36,10 +37,10 @@ func Clean(environment string) error {
return nil
}
// Deploy deploys the services to the Flow Platform.
func Deploy(environment, name string) error {
// Deploy deploys the service(s) to the Flow Platform.
func Deploy(environment, service string) error {
mg.Deps(
mg.F(Prepare, environment, name),
mg.F(Prepare, environment, service),
)
cfg, err := config.NewConfig(environment)
@ -47,23 +48,11 @@ func Deploy(environment, name string) error {
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 err := deploy.Deploy(cfg.Docker.Host, environment, service, true); err != nil {
return fmt.Errorf("error deploying %q; %w", service, err)
}
if name != "all" {
command = append(command, name)
}
return sh.RunV(command[0], command[1:]...)
return nil
}
// Prepare prepares the service's build directory.