2023-11-26 07:46:10 +00:00
|
|
|
package actions
|
2023-11-24 09:56:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2023-12-17 08:51:11 +00:00
|
|
|
"time"
|
2023-11-24 09:56:35 +00:00
|
|
|
|
|
|
|
"flow/services/internal"
|
2023-11-26 07:46:10 +00:00
|
|
|
"flow/services/internal/bundle"
|
2023-11-24 09:56:35 +00:00
|
|
|
"flow/services/internal/config"
|
|
|
|
"flow/services/internal/services"
|
|
|
|
"flow/services/internal/services/forgejo"
|
|
|
|
"flow/services/internal/services/gotosocial"
|
|
|
|
"flow/services/internal/services/woodpecker"
|
|
|
|
)
|
|
|
|
|
|
|
|
const templateExtension string = ".gotmpl"
|
|
|
|
|
|
|
|
func Prepare(environment, service string) error {
|
|
|
|
cfg, err := config.NewConfig(environment)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load the configuration; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := prepareCompose(cfg, environment); err != nil {
|
|
|
|
return fmt.Errorf("unable to prepare the compose file; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if service == "all" {
|
|
|
|
if err := prepareAllServices(cfg, environment); err != nil {
|
|
|
|
return fmt.Errorf("error preparing the services; %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := prepareService(cfg, environment, service); err != nil {
|
|
|
|
return fmt.Errorf("error preparing %q; %w", service, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareCompose(cfg config.Config, environment string) error {
|
|
|
|
compose := "compose"
|
|
|
|
|
|
|
|
buildDir := filepath.Join(internal.RootBuildDir, environment, compose)
|
|
|
|
|
|
|
|
if _, err := os.Stat(buildDir); err != nil {
|
|
|
|
if err := os.MkdirAll(buildDir, 0o700); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", buildDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := render(cfg, environment, compose); err != nil {
|
|
|
|
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareAllServices(cfg config.Config, environment string) error {
|
|
|
|
allServices := services.All()
|
|
|
|
|
|
|
|
for i := range allServices {
|
|
|
|
if err := prepareService(cfg, environment, allServices[i]); err != nil {
|
|
|
|
return fmt.Errorf("error preparing %q; %w", allServices[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareService(cfg config.Config, environment, service string) error {
|
|
|
|
buildDir := filepath.Join(internal.RootBuildDir, environment, service)
|
|
|
|
|
|
|
|
if _, err := os.Stat(buildDir); err != nil {
|
|
|
|
if err := os.MkdirAll(buildDir, 0o700); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", buildDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 07:46:10 +00:00
|
|
|
if err := downloadBundle(cfg, environment, service); err != nil {
|
2023-11-24 09:56:35 +00:00
|
|
|
return fmt.Errorf("error downloading service files for %q; %w", service, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyAssets(environment, service); err != nil {
|
|
|
|
return fmt.Errorf("unable to copy the assets for %s; %w", service, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := render(cfg, environment, service); err != nil {
|
|
|
|
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-26 07:46:10 +00:00
|
|
|
func downloadBundle(cfg config.Config, environment, service string) error {
|
|
|
|
var b bundle.Bundle
|
|
|
|
|
2023-11-24 09:56:35 +00:00
|
|
|
switch service {
|
|
|
|
case services.Forgejo:
|
2023-11-26 07:46:10 +00:00
|
|
|
b = forgejo.Bundle(environment, cfg.Forgejo.Version)
|
2023-11-24 09:56:35 +00:00
|
|
|
case services.Gotosocial:
|
2023-11-26 07:46:10 +00:00
|
|
|
b = gotosocial.Bundle(environment, cfg.GoToSocial.Version)
|
2023-11-24 09:56:35 +00:00
|
|
|
case services.Woodpecker:
|
2023-11-26 07:46:10 +00:00
|
|
|
b = woodpecker.Bundle(environment, cfg.Woodpecker.Version)
|
2023-11-24 09:56:35 +00:00
|
|
|
default:
|
|
|
|
fmt.Printf("There's no files to download for %q.\n", service)
|
2023-11-26 07:46:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Download(b); err != nil {
|
|
|
|
return fmt.Errorf("error downloading the files for %q; %w", service, err)
|
2023-11-24 09:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func render(cfg config.Config, environment, component string) error {
|
|
|
|
buildDirName := filepath.Join(internal.RootBuildDir, environment, component)
|
|
|
|
|
|
|
|
templateDirName := filepath.Join(internal.RootTemplatesDir, component)
|
|
|
|
|
|
|
|
_, err := os.Stat(templateDirName)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Printf("There's no template directory for %q.\n", component)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := os.ReadDir(templateDirName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read files from %s; %w ", templateDirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"default": defaultValue,
|
2023-12-17 08:51:11 +00:00
|
|
|
"timestamp": timestamp(),
|
2023-11-24 09:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
err := func() error {
|
|
|
|
templateFilename := f.Name()
|
|
|
|
|
|
|
|
if f.IsDir() || !strings.HasSuffix(templateFilename, templateExtension) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
outputFilename := strings.TrimSuffix(templateFilename, templateExtension)
|
|
|
|
outputPath := filepath.Join(buildDirName, outputFilename)
|
|
|
|
|
|
|
|
file, err := os.Create(outputPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the file '%s'; %w", outputPath, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
templatePath := filepath.Join(templateDirName, templateFilename)
|
|
|
|
tmpl, err := template.New(templateFilename).Funcs(funcMap).ParseFiles(templatePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create a new template value from '%s'; %w", templateFilename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tmpl.Execute(file, cfg); err != nil {
|
|
|
|
return fmt.Errorf("unable to render the template to '%s'; %w", outputPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("an error occurred whilst rendering the templates for '%s'; %w", component, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyAssets(environment, service string) error {
|
|
|
|
assetsDirName := filepath.Join(internal.RootAssetsDir, service)
|
|
|
|
if _, err := os.Stat(assetsDirName); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Printf("There's no assets directory for %q.\n", service)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buildDirName := filepath.Join(internal.RootBuildDir, environment, service, "assets")
|
|
|
|
|
|
|
|
walkDirFunc := func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == assetsDirName {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buildAssetPath := filepath.Join(buildDirName, strings.TrimPrefix(path, assetsDirName))
|
|
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
if err := os.MkdirAll(buildAssetPath, 0o750); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
dest, err := os.Create(buildAssetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dest.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(dest, source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := filepath.WalkDir(assetsDirName, walkDirFunc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error walking the path '%s'; %w", assetsDirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultValue(value, defaultValue string) string {
|
|
|
|
if value == "" {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
2023-12-17 08:51:11 +00:00
|
|
|
|
|
|
|
func timestamp() func() string {
|
|
|
|
now := time.Now().Format("20060102-1504")
|
|
|
|
|
|
|
|
return func() string {
|
|
|
|
return now
|
|
|
|
}
|
|
|
|
}
|