feature: integrate opentofu into cmd/api

This commit is contained in:
2026-03-31 23:03:15 +00:00
parent 82011a3d42
commit 584475b47d
7 changed files with 91 additions and 19 deletions
+14 -1
View File
@@ -1,6 +1,7 @@
package api
import (
"context"
"sync"
"time"
@@ -10,16 +11,26 @@ import (
"github.com/gofiber/fiber/v3/middleware/cors"
"github.com/gofiber/fiber/v3/middleware/recover"
"github.com/gofiber/fiber/v3/middleware/responsetime"
"github.com/kataras/golog"
"github.com/opentofu/tofu-exec/tfexec"
)
const idleTimeout time.Duration = 5 * time.Second
var apiAppPort string = config.Config("ORCHESTRA_API_PORT", ":9810")
var tofu, tofuDir, workPath = config.GetTofu(config.Config("ORCHESTRA_API_TOFU_VERSION", "1.11.5"))
var app *fiber.App
func StartAPIServer(wg *sync.WaitGroup) {
defer wg.Done()
err := tofu.Init(context.Background(), tfexec.Upgrade(true))
if err != nil {
golog.Infof("Tofudir: %s", tofuDir)
golog.Fatalf("Error with OpenTofu: %s", err)
}
app = fiber.New(fiber.Config{IdleTimeout: idleTimeout})
app.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, //
@@ -35,9 +46,11 @@ func StartAPIServer(wg *sync.WaitGroup) {
routes.GlobalRouter(prefix)
config.GetLogger().Fatal(app.Listen(apiAppPort))
golog.Fatal(app.Listen(apiAppPort))
}
func StopAPIService() {
app.Shutdown()
config.RemoveFolder(tofuDir)
config.RemoveFolder(workPath)
}
+2 -1
View File
@@ -13,6 +13,7 @@ import (
"github.com/gofiber/fiber/v3/middleware/responsetime"
"github.com/gofiber/fiber/v3/middleware/session"
"github.com/gofiber/fiber/v3/middleware/static"
"github.com/kataras/golog"
)
const idleTimeout time.Duration = 5 * time.Second
@@ -59,7 +60,7 @@ func StartWebServer(wg *sync.WaitGroup) {
})
})
config.GetLogger().Fatal(app.Listen(webAppPort))
golog.Fatal(app.Listen(webAppPort))
}
func StopWebService() {
-14
View File
@@ -1,14 +0,0 @@
package config
import (
"github.com/kataras/golog"
)
var logger *golog.Logger
func GetLogger() *golog.Logger {
if logger == nil {
logger = golog.New()
}
return logger
}
+60
View File
@@ -0,0 +1,60 @@
package config
import (
"context"
"log"
"os"
"path/filepath"
"runtime"
"github.com/kataras/golog"
"github.com/opentofu/tofu-exec/tfexec"
"github.com/opentofu/tofudl"
)
func PrepareTemporaryDictionary(dir string) string {
tempDir, err := os.MkdirTemp("", dir)
if err != nil {
golog.Fatalf("Can not create a temporary folder... %s", err)
}
return tempDir
}
func RemoveFolder(path string) {
err := os.RemoveAll(path)
if err == nil {
golog.Fatalf("Can not remove the temporary folder... %s", err)
}
}
func DownloadTofu(tofuVersion string, dir string) string {
opts := tofudl.DownloadOptVersion(tofudl.Version(tofuVersion))
dl, err := tofudl.New()
if err != nil {
log.Fatalf("Error when instantiating tofudl %s", err)
}
binary, _ := dl.Download(context.TODO(), opts)
execPath := filepath.Join(dir, "tofu")
// Windows executable case
if runtime.GOOS == "windows" {
execPath += ".exe"
}
if err := os.WriteFile(execPath, binary, 0755); err != nil {
golog.Fatalf("Error when writing the file %s: %s", execPath, err)
}
return execPath
}
func GetTofu(tofuVersion string) (*tfexec.Tofu, string, string) {
tofuDir := PrepareTemporaryDictionary("go-orchestra-opentofu-")
workPath := PrepareTemporaryDictionary("go-orchestra-opentofu-work-dir-")
execPath := DownloadTofu(tofuVersion, tofuDir)
tofu, err := tfexec.NewTofu(workPath, execPath)
if err != nil {
golog.Fatalf("Error running Tofu: %s", err)
}
return tofu, tofuDir, workPath
}
+4
View File
@@ -17,6 +17,7 @@ require (
github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f // indirect
github.com/ProtonMail/gopenpgp/v2 v2.7.5 // indirect
github.com/andybalholm/brotli v1.2.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/bmatcuk/doublestar/v4 v4.6.0 // indirect
github.com/bradleyfalzon/ghinstallation/v2 v2.5.0 // indirect
@@ -37,7 +38,9 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/copywrite v0.22.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible // indirect
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
@@ -66,6 +69,7 @@ require (
github.com/tinylib/msgp v1.6.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.69.0 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
golang.org/x/crypto v0.49.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
+8
View File
@@ -21,6 +21,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
@@ -183,6 +185,8 @@ github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdv
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
@@ -191,6 +195,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7orfb5Ltvec=
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
github.com/hashicorp/vault/api v1.0.4/go.mod h1:gDcqh3WGcR1cpF5AJz/B1UFheUEneMoIospckxBxk6Q=
github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvhkWnjtSYCaS2M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
@@ -390,6 +396,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8=
github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY=
+3 -3
View File
@@ -8,7 +8,7 @@ import (
"github.com/BlackChaosNL/Orchestra/cmd/api"
"github.com/BlackChaosNL/Orchestra/cmd/web"
"github.com/BlackChaosNL/Orchestra/config"
"github.com/kataras/golog"
)
/*
@@ -31,11 +31,11 @@ func main() {
wg.Wait()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGABRT)
_ = <-c // Wait for signal from OS.Signal
api.StopAPIService()
web.StopWebService()
config.GetLogger().Info("Gracefully shutting down...")
golog.Info("Gracefully shutting down...")
}