Skip to content

Commit

Permalink
Merge pull request #383 from controlplaneio/log-error-to-stdout
Browse files Browse the repository at this point in the history
feat: separate logs to STDOUT & STDERR
  • Loading branch information
06kellyjac authored Mar 4, 2024
2 parents df4ee07 + 25afa4b commit f779743
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
9 changes: 6 additions & 3 deletions cmd/container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ func main() {

amiBuilder := tools.Packer{
WorkingDir: packerDir,
Output: os.Stdout,
StdOut: os.Stdout,
StdErr: os.Stderr,
}

infraManager := tools.Terraform{
WorkingDir: terraformWorkspaceDir,
Output: os.Stdout,
StdOut: os.Stdout,
StdErr: os.Stderr,
}

scenarioManager := tools.AnsiblePlaybook{
Expand All @@ -43,7 +45,8 @@ func main() {
// Ansible complains on Windows+WSL that the directory ansible configuration is world writable
// and hence ignore the configuration unless explicitly set using the ANSIBLE_CONFIG environment variable.
Env: []string{"ANSIBLE_CONFIG=" + ansibleConfigPath},
Output: os.Stdout,
StdOut: os.Stdout,
StdErr: os.Stderr,
}

withStateBucketFlag := cli.WithFlag("stateBucket", "", "the name of the S3 bucket to store Terraform state")
Expand Down
8 changes: 5 additions & 3 deletions core/tools/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ type AnsiblePlaybook struct {
WorkingDir string
PlaybookDir string
Env []string
Output io.Writer
StdOut io.Writer
StdErr io.Writer
}

func (p AnsiblePlaybook) Install(ctx context.Context, id string) error {
playbook := fmt.Sprintf("%s.yaml", id)

if err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, p.Env, playbook).Run(ctx, p.Output); err != nil {
err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, p.Env, playbook).Run(ctx, p.StdOut, p.StdErr)
if err != nil {
return fmt.Errorf("failed to execute Ansible Playbook: %w", err)
}

Expand All @@ -39,7 +41,7 @@ func (p AnsiblePlaybook) Uninstall(ctx context.Context, id string) error {
playbook := fmt.Sprintf("%s.yaml", id)

if err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, p.Env, playbook, "state=absent").
Run(ctx, p.Output); err != nil {
Run(ctx, p.StdOut, p.StdErr); err != nil {
return fmt.Errorf("failed to run Ansible Playbook with state=absent: %w", err)
}

Expand Down
7 changes: 4 additions & 3 deletions core/tools/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ type AMIBuilder interface {

type Packer struct {
WorkingDir string
Output io.Writer
StdOut io.Writer
StdErr io.Writer
}

func (p Packer) Build(ctx context.Context, id string) error {
template := fmt.Sprintf("%s.pkr.hcl", id)

if err := packerInitCommand(p.WorkingDir, template).Run(ctx, p.Output); err != nil {
if err := packerInitCommand(p.WorkingDir, template).Run(ctx, p.StdOut, p.StdErr); err != nil {
return fmt.Errorf("failed to initialise packer: %w", err)
}

if err := packerBuildCommand(p.WorkingDir, template).Run(ctx, p.Output); err != nil {
if err := packerBuildCommand(p.WorkingDir, template).Run(ctx, p.StdOut, p.StdErr); err != nil {
return fmt.Errorf("failed to build ami with packer: %w", err)
}

Expand Down
7 changes: 4 additions & 3 deletions core/tools/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ type runner struct {
Arguments []string
}

func (c runner) Run(ctx context.Context, output io.Writer) error {
func (c runner) Run(ctx context.Context, stdOut, stdErr io.Writer) error {
slog.Info("running", "runner", c)

//nolint:gosec
cmd := exec.CommandContext(ctx, string(c.Executable), c.Arguments...)
cmd.Dir = c.WorkingDir
cmd.Stdout = output
cmd.Stderr = output
cmd.Stdout = stdOut
cmd.Stderr = stdErr
cmd.Env = c.Env

err := cmd.Run()
if err != nil {
slog.Error("failed to run runner", "runner", c, "error", err)
return fmt.Errorf("failed to run runner: %w", err)
}

Expand Down
15 changes: 10 additions & 5 deletions core/tools/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ type InfraManager interface {

type Terraform struct {
WorkingDir string
Output io.Writer
StdOut io.Writer
StdErr io.Writer
}

func (t Terraform) Create(ctx context.Context, stateBucket string, stateKey string, name string) error {
backend := backendConfig(stateBucket, stateKey)

if err := terraformInitCommand(t.WorkingDir, backend).Run(ctx, t.Output); err != nil {
err := terraformInitCommand(t.WorkingDir, backend).Run(ctx, t.StdOut, t.StdErr)
if err != nil {
return fmt.Errorf("failed to initialise terraform: %w", err)
}

vars := terraformVars(name)

if err := terraformCommand(t.WorkingDir, TerraformApply, vars).Run(ctx, t.Output); err != nil {
err = terraformCommand(t.WorkingDir, TerraformApply, vars).Run(ctx, t.StdOut, t.StdErr)
if err != nil {
return fmt.Errorf("failed to apply terraform: %w", err)
}

Expand All @@ -45,13 +48,15 @@ func (t Terraform) Create(ctx context.Context, stateBucket string, stateKey stri
func (t Terraform) Destroy(ctx context.Context, stateBucket string, stateKey string, name string) error {
backend := backendConfig(stateBucket, stateKey)

if err := terraformInitCommand(t.WorkingDir, backend).Run(ctx, t.Output); err != nil {
err := terraformInitCommand(t.WorkingDir, backend).Run(ctx, t.StdOut, t.StdErr)
if err != nil {
return fmt.Errorf("failed to initialise terraform: %w", err)
}

vars := terraformVars(name)

if err := terraformCommand(t.WorkingDir, TerraformDestroy, vars).Run(ctx, t.Output); err != nil {
err = terraformCommand(t.WorkingDir, TerraformDestroy, vars).Run(ctx, t.StdOut, t.StdErr)
if err != nil {
return fmt.Errorf("failed to destroy terraform: %w", err)
}

Expand Down

0 comments on commit f779743

Please sign in to comment.