build: allow mage to deploy all services
- Mage can now render templates for all services using: mage render all - Mage can now deploy all services using: mage deploy all - Deploy now depends on Render - If rendering Forgejo templates mage ensures that the Forgejo binary is downloaded first.
This commit is contained in:
parent
4e5ed6e9e5
commit
d23e6e531f
3 changed files with 92 additions and 50 deletions
41
magefiles/deploy.go
Normal file
41
magefiles/deploy.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
//go:build mage
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/magefile/mage/mg"
|
||||
"github.com/magefile/mage/sh"
|
||||
)
|
||||
|
||||
// Deploy deploys the services to the Flow Platform.
|
||||
func Deploy(name string) error {
|
||||
mg.Deps(
|
||||
mg.F(Render, name),
|
||||
)
|
||||
|
||||
cfg, err := newConfig(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load the configuration; %w", err)
|
||||
}
|
||||
|
||||
os.Setenv("DOCKER_HOST", cfg.DockerHost)
|
||||
|
||||
command := []string{
|
||||
"docker",
|
||||
"compose",
|
||||
"--project-directory",
|
||||
rootBuildDir+"/compose",
|
||||
"up",
|
||||
"-d",
|
||||
"--build",
|
||||
}
|
||||
|
||||
if name != "all" {
|
||||
command = append(command, name)
|
||||
}
|
||||
|
||||
return sh.Run(command[0], command[1:]...)
|
||||
}
|
|
@ -7,14 +7,13 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/magefile/mage/sh"
|
||||
"github.com/magefile/mage/mg"
|
||||
)
|
||||
|
||||
const (
|
||||
configFile string = "./config/services.json"
|
||||
rootBuildDir string = "./build"
|
||||
templateExtension string = ".gotmpl"
|
||||
rootTemplatesDir string = "./templates"
|
||||
configFile string = "./config/services.json"
|
||||
rootBuildDir string = "./build"
|
||||
templateExtension string = ".gotmpl"
|
||||
rootTemplatesDir string = "./templates"
|
||||
)
|
||||
|
||||
// Clean cleans the workspace.
|
||||
|
@ -39,51 +38,6 @@ func Clean() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Render renders the template files.
|
||||
func Render(name string) error {
|
||||
if name == "forgejo" {
|
||||
mg.Deps(DownloadForgejo)
|
||||
}
|
||||
|
||||
cfg, err := newConfig(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load the configuration; %v", err)
|
||||
}
|
||||
|
||||
if name != "compose" {
|
||||
if err := render(cfg, "compose"); err != nil {
|
||||
return fmt.Errorf("an error occurred whilst rendering the compose file; %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := render(cfg, name); err != nil {
|
||||
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deploy deploys the services to the Flow Platform.
|
||||
func Deploy(name string) error {
|
||||
cfg, err := newConfig(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load the configuration; %w", err)
|
||||
}
|
||||
|
||||
os.Setenv("DOCKER_HOST", cfg.DockerHost)
|
||||
|
||||
return sh.Run(
|
||||
"docker",
|
||||
"compose",
|
||||
"--project-directory",
|
||||
rootBuildDir+"/compose",
|
||||
"up",
|
||||
"-d",
|
||||
"--build",
|
||||
name,
|
||||
)
|
||||
}
|
||||
|
||||
// DownloadForgejo downloads the Forgejo binary from Codeberg.
|
||||
func DownloadForgejo() error {
|
||||
cfg, err := newConfig(configFile)
|
||||
|
|
|
@ -4,12 +4,59 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/magefile/mage/mg"
|
||||
)
|
||||
|
||||
// Render renders the template files.
|
||||
func Render(name string) error {
|
||||
cfg, err := newConfig(configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load the configuration; %v", err)
|
||||
}
|
||||
|
||||
if name == "forgejo" || name == "all" {
|
||||
mg.Deps(DownloadForgejo)
|
||||
}
|
||||
|
||||
if name == "all" {
|
||||
objects, err := os.ReadDir(rootTemplatesDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to read the templates directory; %w", err)
|
||||
}
|
||||
|
||||
for _, o := range objects {
|
||||
if !o.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
dirName := o.Name()
|
||||
|
||||
log.Printf("Rendering templates for %s.\n", dirName)
|
||||
if err := render(cfg, o.Name()); err != nil {
|
||||
return fmt.Errorf("unable to render templates for %s; %w", dirName, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if name != "compose" {
|
||||
mg.Deps(
|
||||
mg.F(Render, "compose"),
|
||||
)
|
||||
}
|
||||
|
||||
if err := render(cfg, name); err != nil {
|
||||
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func render(cfg config, component string) error {
|
||||
buildDirName := filepath.Join(rootBuildDir, component)
|
||||
if err := os.MkdirAll(buildDirName, 0o750); err != nil {
|
||||
|
|
Loading…
Reference in a new issue