constellation/hack/logcollector/cmd/template.go
Moritz Sanft 60bf770e62
ci: logcollection to OpenSearch in non-debug clusters (#2080)
* refactor `debugd` file structure

* create `hack`-tool to deploy logcollection to non-debug clusters

* integrate changes into CI

* update fields

* update workflow input names

* use `working-directory`

* add opensearch creds to upgrade workflow

* make template func generic

* make templating func generic

* linebreaks

* remove magic defaults

* move `os.Exit` to main package

* make logging index configurable

* make templating generic

* remove excess brace

* update fields

* copy fields

* fix flag name

* fix linter warnings

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>

* remove unused workflow inputs

* remove makefiles

* fix command

* bazel: fix output paths of container

This fixes the output paths of builds within the container by mounting
directories to paths that exist on the host. We also explicitly set the
output path in a .bazelrc to the user specific path. The rc file is
mounted into the container and overrides the host rc.
Also adding automatic stop in case start is called and a containers
is already running.
Sym links like bazel-out and paths bazel outputs should generally work
with this change.

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>

* tabs -> spaces

---------

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
Co-authored-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
2023-08-21 08:01:33 +02:00

124 lines
3.3 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
import (
"fmt"
"github.com/edgelesssys/constellation/v2/hack/logcollector/fields"
"github.com/edgelesssys/constellation/v2/hack/logcollector/internal"
"github.com/spf13/cobra"
)
func newTemplateCmd() *cobra.Command {
templateCmd := &cobra.Command{
Use: "template",
Short: "Templates filebeat and logstash configurations and prepares them for deployment",
Long: `Templates filebeat and logstash configurations and prepares them for deployment by placing them in the specified directory.`,
RunE: runTemplate,
}
templateCmd.Flags().String("dir", "", "Directory to place the templated configurations in (required)")
must(templateCmd.MarkFlagRequired("dir"))
must(templateCmd.MarkFlagDirname("dir"))
templateCmd.Flags().String("username", "", "OpenSearch username (required)")
must(templateCmd.MarkFlagRequired("username"))
templateCmd.Flags().String("password", "", "OpenSearch password (required)")
must(templateCmd.MarkFlagRequired("password"))
templateCmd.Flags().String("index-prefix", "systemd-logs", "Prefix for logging index (e.g. systemd-logs)")
templateCmd.Flags().Int("port", 5045, "Logstash port")
templateCmd.Flags().StringToString("fields", nil, "Additional fields for the Logstash pipeline")
return templateCmd
}
func runTemplate(cmd *cobra.Command, _ []string) error {
flags, err := parseTemplateFlags(cmd)
if err != nil {
return fmt.Errorf("parse template flags: %w", err)
}
if err := flags.extraFields.Check(); err != nil {
return fmt.Errorf("validating extra fields: %w", err)
}
logstashPreparer := internal.NewLogstashPreparer(
flags.extraFields,
flags.username,
flags.password,
flags.indexPrefix,
flags.port,
)
if err := logstashPreparer.Prepare(flags.dir); err != nil {
return fmt.Errorf("prepare logstash: %w", err)
}
filebeatPreparer := internal.NewFilebeatPreparer(
flags.port,
)
if err := filebeatPreparer.Prepare(flags.dir); err != nil {
return fmt.Errorf("prepare filebeat: %w", err)
}
return nil
}
func parseTemplateFlags(cmd *cobra.Command) (templateFlags, error) {
dir, err := cmd.Flags().GetString("dir")
if err != nil {
return templateFlags{}, fmt.Errorf("parse dir string: %w", err)
}
username, err := cmd.Flags().GetString("username")
if err != nil {
return templateFlags{}, fmt.Errorf("parse username string: %w", err)
}
password, err := cmd.Flags().GetString("password")
if err != nil {
return templateFlags{}, fmt.Errorf("parse password string: %w", err)
}
indexPrefix, err := cmd.Flags().GetString("index-prefix")
if err != nil {
return templateFlags{}, fmt.Errorf("parse index-prefix string: %w", err)
}
extraFields, err := cmd.Flags().GetStringToString("fields")
if err != nil {
return templateFlags{}, fmt.Errorf("parse fields map: %w", err)
}
port, err := cmd.Flags().GetInt("port")
if err != nil {
return templateFlags{}, fmt.Errorf("parse port int: %w", err)
}
return templateFlags{
dir: dir,
username: username,
password: password,
indexPrefix: indexPrefix,
extraFields: extraFields,
port: port,
}, nil
}
type templateFlags struct {
dir string
username string
password string
indexPrefix string
extraFields fields.Fields
port int
}
func must(err error) {
if err != nil {
panic(err)
}
}