Skip to content

Commit c1e885b

Browse files
committed
cloudapi: git sha version api and journal for workers
1 parent ff2660e commit c1e885b

File tree

10 files changed

+385
-177
lines changed

10 files changed

+385
-177
lines changed

cmd/osbuild-composer/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050
logLevel, err := logrus.ParseLevel(config.LogLevel)
5151

5252
logrus.SetReportCaller(true)
53-
// Add context hook to log operation_id and external_id
53+
logrus.AddHook(&common.BuildHook{})
5454
logrus.AddHook(&common.ContextHook{})
5555

5656
if err == nil {

cmd/osbuild-worker/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ type composerConfig struct {
1313
Proxy string `toml:"proxy"`
1414
}
1515

16+
type loggingConfig struct {
17+
Format string `toml:"format"` // journal, text or json (defaults to jorunal or text depending on the OS)
18+
Level string `toml:"level"`
19+
NoDiscard bool `toml:"no_discard"` // do not discard stdout/stderr logs (useful for debugging)
20+
}
21+
1622
type kerberosConfig struct {
1723
Principal string `toml:"principal"`
1824
KeyTab string `toml:"keytab"`
@@ -88,6 +94,7 @@ type repositoryMTLSConfig struct {
8894

8995
type workerConfig struct {
9096
Composer *composerConfig `toml:"composer"`
97+
Logging *loggingConfig `toml:"logging"`
9198
Koji map[string]kojiServerConfig `toml:"koji"`
9299
GCP *gcpConfig `toml:"gcp"`
93100
Azure *azureConfig `toml:"azure"`
@@ -115,6 +122,10 @@ func parseConfig(file string) (*workerConfig, error) {
115122
Type: "host",
116123
},
117124
DeploymentChannel: "local",
125+
Logging: &loggingConfig{
126+
Format: "journal",
127+
Level: "info",
128+
},
118129
}
119130

120131
_, err := toml.DecodeFile(file, &config)

cmd/osbuild-worker/config_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ type = "aws.ec2"
7575
iam_profile = "osbuild-worker"
7676
key_name = "osbuild-worker"
7777
cloudwatch_group = "osbuild-worker"
78+
79+
[logging]
80+
level = "debug"
81+
format = "text"
7882
`,
7983
want: &workerConfig{
8084
BasePath: "/api/image-builder-worker/v1",
@@ -137,6 +141,10 @@ cloudwatch_group = "osbuild-worker"
137141
ServerURL: "https://example.com/pulp",
138142
},
139143
DeploymentChannel: "local",
144+
Logging: &loggingConfig{
145+
Level: "debug",
146+
Format: "text",
147+
},
140148
},
141149
},
142150
{
@@ -148,6 +156,10 @@ cloudwatch_group = "osbuild-worker"
148156
Type: "host",
149157
},
150158
DeploymentChannel: "local",
159+
Logging: &loggingConfig{
160+
Format: "journal",
161+
Level: "info",
162+
},
151163
},
152164
},
153165
{
@@ -159,6 +171,10 @@ cloudwatch_group = "osbuild-worker"
159171
Type: "host",
160172
},
161173
DeploymentChannel: "staging",
174+
Logging: &loggingConfig{
175+
Format: "journal",
176+
Level: "info",
177+
},
162178
},
163179
},
164180
}

cmd/osbuild-worker/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"errors"
88
"flag"
99
"fmt"
10+
"io"
11+
"log"
1012
"net/url"
1113
"os"
1214
"path"
@@ -16,11 +18,13 @@ import (
1618
slogger "github.com/osbuild/osbuild-composer/pkg/splunk_logger"
1719

1820
"github.com/BurntSushi/toml"
21+
"github.com/coreos/go-systemd/journal"
1922
"github.com/sirupsen/logrus"
2023

2124
"github.com/osbuild/images/pkg/arch"
2225
"github.com/osbuild/images/pkg/dnfjson"
2326
"github.com/osbuild/osbuild-composer/internal/cloud/awscloud"
27+
"github.com/osbuild/osbuild-composer/internal/common"
2428
"github.com/osbuild/osbuild-composer/internal/upload/azure"
2529
"github.com/osbuild/osbuild-composer/internal/upload/koji"
2630
"github.com/osbuild/osbuild-composer/internal/upload/oci"
@@ -167,6 +171,11 @@ func RequestAndRunJob(client *worker.Client, acceptedJobTypes []string, jobImpls
167171
}
168172

169173
func main() {
174+
// Redirect Go standard logger into logrus before it's used by other packages
175+
log.SetOutput(common.Logger())
176+
// Ensure the Go standard logger does not have any prefix or timestamp
177+
log.SetFlags(0)
178+
170179
var unix bool
171180
flag.BoolVar(&unix, "unix", false, "Interpret 'address' as a path to a unix domain socket instead of a network address")
172181

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

200+
logrus.SetReportCaller(true)
201+
logrus.AddHook(&common.BuildHook{})
202+
logrus.SetOutput(os.Stdout)
203+
logLevel, err := logrus.ParseLevel(config.Logging.Level)
204+
if err == nil {
205+
logrus.SetLevel(logLevel)
206+
} else {
207+
logrus.Info("Failed to load loglevel from config:", err)
208+
}
209+
210+
// logger configuration
211+
switch config.Logging.Format {
212+
case "journal", "":
213+
// If we are running under systemd, use the journal. Otherwise,
214+
// fallback to text formatter.
215+
if journal.Enabled() {
216+
logrus.Info("Switching to journal logging mode, use logging.no_discard to keep writing to standard output/error")
217+
logrus.SetFormatter(&logrus.JSONFormatter{})
218+
logrus.AddHook(&common.JournalHook{})
219+
if !config.Logging.NoDiscard {
220+
logrus.SetOutput(io.Discard)
221+
}
222+
} else {
223+
logrus.SetFormatter(&logrus.TextFormatter{})
224+
}
225+
case "text":
226+
logrus.SetFormatter(&logrus.TextFormatter{})
227+
case "json":
228+
logrus.SetFormatter(&logrus.JSONFormatter{})
229+
default:
230+
logrus.Infof("Failed to set logging format from config, '%s' is not a valid option", config.Logging.Format)
231+
}
232+
191233
logrus.Info("Composer configuration:")
192234
encoder := toml.NewEncoder(logrus.StandardLogger().WriterLevel(logrus.InfoLevel))
193235
err = encoder.Encode(&config)

internal/cloudapi/v2/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ func (b binder) Bind(i interface{}, ctx echo.Context) error {
4545
return nil
4646
}
4747

48+
func (h *apiHandlers) GetVersion(ctx echo.Context) error {
49+
spec, err := GetSwagger()
50+
if err != nil {
51+
return HTTPError(ErrorFailedToLoadOpenAPISpec)
52+
}
53+
version := Version{
54+
Version: spec.Info.Version,
55+
BuildCommit: common.ToPtr(common.BuildCommit),
56+
BuildTime: common.ToPtr(common.BuildTime),
57+
}
58+
return ctx.JSON(http.StatusOK, version)
59+
}
60+
4861
func (h *apiHandlers) GetOpenapi(ctx echo.Context) error {
4962
spec, err := GetSwagger()
5063
if err != nil {

0 commit comments

Comments
 (0)