Skip to content

Commit

Permalink
cloudapi: git sha version api and journal for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Nov 20, 2024
1 parent ff2660e commit 3980d11
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 177 deletions.
2 changes: 1 addition & 1 deletion cmd/osbuild-composer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
logLevel, err := logrus.ParseLevel(config.LogLevel)

logrus.SetReportCaller(true)
// Add context hook to log operation_id and external_id
logrus.AddHook(&common.BuildHook{})
logrus.AddHook(&common.ContextHook{})

if err == nil {
Expand Down
11 changes: 11 additions & 0 deletions cmd/osbuild-worker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type composerConfig struct {
Proxy string `toml:"proxy"`
}

type loggingConfig struct {
Format string `toml:"format"` // journal, text or json (defaults to jorunal or text depending on the OS)
Level string `toml:"level"`
NoDiscard bool `toml:"no_discard"` // do not discard stdout/stderr logs (useful for debugging)
}

type kerberosConfig struct {
Principal string `toml:"principal"`
KeyTab string `toml:"keytab"`
Expand Down Expand Up @@ -88,6 +94,7 @@ type repositoryMTLSConfig struct {

type workerConfig struct {
Composer *composerConfig `toml:"composer"`
Logging *loggingConfig `toml:"logging"`
Koji map[string]kojiServerConfig `toml:"koji"`
GCP *gcpConfig `toml:"gcp"`
Azure *azureConfig `toml:"azure"`
Expand Down Expand Up @@ -115,6 +122,10 @@ func parseConfig(file string) (*workerConfig, error) {
Type: "host",
},
DeploymentChannel: "local",
Logging: &loggingConfig{
Format: "journal",
Level: "info",
},
}

_, err := toml.DecodeFile(file, &config)
Expand Down
42 changes: 42 additions & 0 deletions cmd/osbuild-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/url"
"os"
"path"
Expand All @@ -16,11 +18,13 @@ import (
slogger "github.com/osbuild/osbuild-composer/pkg/splunk_logger"

"github.com/BurntSushi/toml"
"github.com/coreos/go-systemd/journal"
"github.com/sirupsen/logrus"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/dnfjson"
"github.com/osbuild/osbuild-composer/internal/cloud/awscloud"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/upload/azure"
"github.com/osbuild/osbuild-composer/internal/upload/koji"
"github.com/osbuild/osbuild-composer/internal/upload/oci"
Expand Down Expand Up @@ -167,6 +171,11 @@ func RequestAndRunJob(client *worker.Client, acceptedJobTypes []string, jobImpls
}

func main() {
// Redirect Go standard logger into logrus before it's used by other packages
log.SetOutput(common.Logger())
// Ensure the Go standard logger does not have any prefix or timestamp
log.SetFlags(0)

var unix bool
flag.BoolVar(&unix, "unix", false, "Interpret 'address' as a path to a unix domain socket instead of a network address")

Expand All @@ -188,6 +197,39 @@ func main() {
logrus.Fatalf("Could not load config file '%s': %v", configFile, err)
}

logrus.SetReportCaller(true)
logrus.AddHook(&common.BuildHook{})
logrus.SetOutput(os.Stdout)
logLevel, err := logrus.ParseLevel(config.Logging.Level)
if err == nil {
logrus.SetLevel(logLevel)
} else {
logrus.Info("Failed to load loglevel from config:", err)
}

// logger configuration
switch config.Logging.Format {
case "journal", "":
// If we are running under systemd, use the journal. Otherwise,
// fallback to text formatter.
if journal.Enabled() {
logrus.Info("Switching to journal logging mode, use logging.no_discard to keep writing to standard output/error")
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.AddHook(&common.JournalHook{})
if !config.Logging.NoDiscard {
logrus.SetOutput(io.Discard)
}
} else {
logrus.SetFormatter(&logrus.TextFormatter{})
}
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
logrus.Infof("Failed to set logging format from config, '%s' is not a valid option", config.Logging.Format)
}

logrus.Info("Composer configuration:")
encoder := toml.NewEncoder(logrus.StandardLogger().WriterLevel(logrus.InfoLevel))
err = encoder.Encode(&config)
Expand Down
13 changes: 13 additions & 0 deletions internal/cloudapi/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ func (b binder) Bind(i interface{}, ctx echo.Context) error {
return nil
}

func (h *apiHandlers) GetVersion(ctx echo.Context) error {
spec, err := GetSwagger()
if err != nil {
return HTTPError(ErrorFailedToLoadOpenAPISpec)
}
version := Version{
Version: spec.Info.Version,
BuildCommit: common.ToPtr(common.BuildCommit),
BuildTime: common.ToPtr(common.BuildTime),
}
return ctx.JSON(http.StatusOK, version)
}

func (h *apiHandlers) GetOpenapi(ctx echo.Context) error {
spec, err := GetSwagger()
if err != nil {
Expand Down
374 changes: 198 additions & 176 deletions internal/cloudapi/v2/openapi.v2.gen.go

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions internal/cloudapi/v2/openapi.v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ servers:
description: current domain

paths:
/version:
get:
summary: get the service version
description: "get the service version"
operationId: getVersion
tags:
- meta
responses:
'200':
description: a service version
content:
application/json:
schema:
$ref: '#/components/schemas/Version'

/openapi:
get:
operationId: getOpenapi
Expand Down Expand Up @@ -487,6 +502,17 @@ paths:

components:
schemas:
Version:
required:
- version
properties:
version:
type: string
build_time:
type: string
build_commit:
type: string

ObjectReference:
type: object
required:
Expand Down
16 changes: 16 additions & 0 deletions internal/cloudapi/v2/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
v2 "github.com/osbuild/osbuild-composer/internal/cloudapi/v2"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/jobqueue/fsjobqueue"
"github.com/osbuild/osbuild-composer/internal/target"
"github.com/osbuild/osbuild-composer/internal/test"
Expand Down Expand Up @@ -195,6 +196,21 @@ func newV2Server(t *testing.T, dir string, depsolveChannels []string, enableJWT
return v2Server, workerServer, q, cancelWithWait
}

func TestVersion(t *testing.T) {
srv, _, _, cancel := newV2Server(t, t.TempDir(), []string{""}, false, false)
defer cancel()

common.BuildCommit = "abcdef"
common.BuildTime = "2013-05-13T00:00:00Z"

test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET", "/api/image-builder-composer/v2/version", ``, http.StatusOK, `
{
"version": "2",
"build_commit": "abcdef",
"build_time": "2013-05-13T00:00:00Z"
}`, "operation_id", "details")
}

func TestUnknownRoute(t *testing.T) {
srv, _, _, cancel := newV2Server(t, t.TempDir(), []string{""}, false, false)
defer cancel()
Expand Down
26 changes: 26 additions & 0 deletions internal/common/build_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package common

import (
"github.com/sirupsen/logrus"
)

type BuildHook struct {
}

func (h *BuildHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.DebugLevel,
logrus.InfoLevel,
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}

func (h *BuildHook) Fire(e *logrus.Entry) error {
e.Data["build_commit"] = BuildCommit
e.Data["build_time"] = BuildTime

return nil
}
36 changes: 36 additions & 0 deletions internal/common/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package common

import "runtime/debug"

var (
// Git SHA commit (first 4 characters)
BuildCommit string

// Build date and time
BuildTime string

// BuildGoVersion carries Go version the binary was built with
BuildGoVersion string
)

func init() {
bi, ok := debug.ReadBuildInfo()

if !ok {
BuildTime = "N/A"
BuildCommit = "HEAD"
}

BuildGoVersion = bi.GoVersion

for _, bs := range bi.Settings {
switch bs.Key {
case "vcs.revision":
if len(bs.Value) > 6 {
BuildCommit = bs.Value[0:6]
}
case "vcs.time":
BuildTime = bs.Value
}
}
}

0 comments on commit 3980d11

Please sign in to comment.