From 55e0ba3126eab43e3b5d24d2cb443736641b7207 Mon Sep 17 00:00:00 2001 From: bupd Date: Thu, 20 Jun 2024 03:26:13 +0530 Subject: [PATCH] Add: Dagger integration for building and running Harbor Satellite Signed-off-by: bupd --- ci/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ main.go | 4 +++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/ci/main.go b/ci/main.go index 2b71c0c..c95df61 100644 --- a/ci/main.go +++ b/ci/main.go @@ -2,8 +2,10 @@ package main import ( "context" + "log" "log/slog" "os" + "path/filepath" "dagger.io/dagger" ) @@ -38,4 +40,44 @@ func main() { } // print output slog.Info("Hello from Dagger!", "version", version) + + buildSatellite(client, ctx) +} + +// buildSatellite builds and runs the Satellite service +func buildSatellite(client *dagger.Client, ctx context.Context) { + slog.Info("Starting the Satellite build process...") + + // Get the directory of project located one level up from the current working directory + parentDir, err := getProjectDir() + if err != nil { + log.Fatalf("Error getting parent directory: %v", err) + } + + // Use the parent directory path in Dagger + dir := client.Host().Directory(parentDir) + + // Configure and build the container + container := client.Container(). + From(imageVersion). + WithDirectory(appDir, dir). + WithWorkdir(appDir). + WithExec([]string{"go", "build", "-o", appBinary, sourceFile}). + WithExposedPort(containerPort). + WithExec([]string{"./" + appBinary}) + + // Start the service + _, err = container.AsService().Start(ctx) + if err != nil { + log.Fatalf("Error starting the Satellite service: %v", err) + } +} + +// getProjectDir gets the directory of the project +func getProjectDir() (string, error) { + currentDir, err := os.Getwd() + if err != nil { + return "", err + } + return filepath.Dir(currentDir), nil } diff --git a/main.go b/main.go index df3c04b..d2d2cd1 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,9 @@ func run() error { Addr: ":9090", Handler: mux, } + + fmt.Println("program running on port 9090") + g.Go(func() error { if err := metricsSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { return err @@ -105,7 +108,6 @@ func run() error { cancel() return err } - }) }