Skip to content

Commit

Permalink
store and display init ouptut for debuging (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
asiyani authored May 1, 2024
1 parent 0b8b4f4 commit 1fdef1b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
1 change: 1 addition & 0 deletions api/v1beta1/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Run struct {
CommitMsg string `json:"commitMsg,omitempty"`
DiffDetected bool `json:"diffDetected,omitempty"`
Applied bool `json:"applied,omitempty"`
InitOutput string `json:"initOutput,omitempty"`
Output string `json:"output,omitempty"`
}

Expand Down
3 changes: 2 additions & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,15 @@ func (r *Runner) runTF(
return false
}

_, err := te.init(ctx, backendConf)
initOut, err := te.init(ctx, backendConf)
if err != nil {
msg := fmt.Sprintf("unable to init module: err:%s", err)
// tf err contains new lines not suitable logging
log.Error("unable to init module", "err", fmt.Sprintf("%q", err))
r.setFailedStatus(run, module, tfaplv1beta1.ReasonInitialiseFailed, msg, r.Clock.Now())
return false
}
run.InitOutput = initOut

log.Info("Initialised successfully")
r.Recorder.Event(module, corev1.EventTypeNormal, tfaplv1beta1.ReasonInitialised, "Initialised successfully")
Expand Down
6 changes: 4 additions & 2 deletions webserver/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ func createNamespaceMap(ctx context.Context, modules []tfaplv1beta1.Module, redi

// sort runs by StartedAt DESC
slices.SortFunc(module.Runs, func(a *tfaplv1beta1.Run, b *tfaplv1beta1.Run) int {
if a != nil && b != nil {
if a != nil && b != nil &&
a.StartedAt != nil && b.StartedAt != nil {
return b.StartedAt.Compare(a.StartedAt.Time)
}
return 0
})
// remove duplicate runs (scenario when last run is also a apply run)
module.Runs = slices.CompactFunc(module.Runs, func(a *tfaplv1beta1.Run, b *tfaplv1beta1.Run) bool {
if a != nil && b != nil {
if a != nil && b != nil &&
a.StartedAt != nil && b.StartedAt != nil {
return a.Request.ID == b.Request.ID &&
a.StartedAt.Compare(b.StartedAt.Time) == 0
}
Expand Down
88 changes: 88 additions & 0 deletions webserver/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,44 @@ func Test_ExecuteTemplate(t *testing.T) {
Duration: 60 * time.Second,
CommitHash: "abcccf2a0f758ba0d8e88a834a2acdba5885577c",
CommitMsg: `initial commit (john)`,
InitOutput: `{
"terraform_version": "1.8.2",
"platform": "linux_amd64",
"provider_selections": {},
"terraform_outdated": false
}
Initializing the backend...
Successfully configured the backend "local"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Finding latest version of hashicorp/null...
- Using hashicorp/null v3.2.2 from the shared cache directory
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Warning: Incomplete lock file information for providers
Due to your customized provider installation methods, Terraform was forced to
calculate lock file checksums locally for the following providers:
- hashicorp/null
The current .terraform.lock.hcl file only includes checksums for linux_amd64,
so Terraform running on another platform will fail to install these
providers.
To calculate additional checksums for another platform, run:
terraform providers lock -platform=linux_amd64
(where linux_amd64 is the platform to generate)
Terraform has been successfully initialized!
`,
Output: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
Expand Down Expand Up @@ -160,6 +198,56 @@ Plan: 7 to add, 0 to change, 0 to destroy.`,
CommitHash: "abcccf2a0f758ba0d8e88a834a2acdba5885577c",
CommitMsg: `initial commit (john)`,
Applied: true,
InitOutput: `
{
"terraform_version": "1.8.2",
"platform": "linux_amd64",
"provider_selections": {},
"terraform_outdated": false
}
Initializing the backend...
Successfully configured the backend "local"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Finding latest version of hashicorp/google-beta...
- Finding latest version of okta/okta...
- Finding latest version of hashicorp/aws...
- Finding latest version of hashicorp/google...
- Finding latest version of hashicorp/null...
- Using hashicorp/google v5.26.0 from the shared cache directory
- Using hashicorp/null v3.2.2 from the shared cache directory
- Using hashicorp/google-beta v5.26.0 from the shared cache directory
- Using okta/okta v4.8.1 from the shared cache directory
- Using hashicorp/aws v5.47.0 from the shared cache directory
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Warning: Incomplete lock file information for providers
Due to your customized provider installation methods, Terraform was forced to
calculate lock file checksums locally for the following providers:
- hashicorp/aws
- hashicorp/google
- hashicorp/google-beta
- hashicorp/null
- okta/okta
The current .terraform.lock.hcl file only includes checksums for linux_amd64,
so Terraform running on another platform will fail to install these
providers.
To calculate additional checksums for another platform, run:
terraform providers lock -platform=linux_amd64
(where linux_amd64 is the platform to generate)
Terraform has been successfully initialized!`,
Output: `
null_resource.echo: Creating...
null_resource.env3: Creating...
Expand Down
29 changes: 21 additions & 8 deletions webserver/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h6>
<span class="fs-5 fw-semibold text-dark">Module Info</span>
</a>
</div>
<div class="card-body">
<div class="card-body p-2">
<div id="module-info-list" class="scrollarea">
{{range $k, $v := . }}
{{ range $i, $m := $v.Modules }}
Expand Down Expand Up @@ -130,8 +130,9 @@ <h4 class="text-center">Refresh for updates and check the status and logs for th
<!-- modules -->
{{define "module"}}
{{$m := .}}
<div id="{{sanitizedUniqueName .Module.NamespacedName}}" class="d-none">
<div class='card {{if eq .Module.Status.CurrentState "Running"}}border-info{{end}}'>
<div id="{{sanitizedUniqueName .Module.NamespacedName}}">
<div class='card {{if eq .Module.Status.CurrentState "Running"}}border-info{{end}}' class="d-none"
style="max-height: 90vh;">
<div class="card-header bg-transparent">
<div href="#{{ sanitizedUniqueName .Module.NamespacedName}}-info">
<div class="d-flex justify-content-between align-items-center mb-1">
Expand Down Expand Up @@ -211,10 +212,8 @@ <h3 class="fw-bold" style="color: #550091;">{{.Module.Name}}
</dl>
</div>
</div>
<div id="{{sanitizedUniqueName .Module.NamespacedName}}-info">
<div id="{{sanitizedUniqueName .Module.NamespacedName}}-info" class="overflow-auto">
<div class="card-body">
<div>
</div>
<div>
<nav>
<div class="nav nav-tabs" role="tablist">
Expand All @@ -237,7 +236,7 @@ <h3 class="fw-bold" style="color: #550091;">{{.Module.Name}}
{{ range $i, $run := .Runs }}
<div class="tab-pane fade {{if eq $i 0}}active show{{end}}"
id="{{sanitizedUniqueName .Module}}-{{$i}}" role="tabpanel">
<dl class="p-2">
<dl class="p-2 mb-2">
<div class="row">
<div class="col-4">
<dt>Status</dt>
Expand All @@ -264,10 +263,24 @@ <h3 class="fw-bold" style="color: #550091;">{{.Module.Name}}
</div>
</div>
</dl>
<pre class="py-1">
<div class="mh-100 overflow-auto">
<!-- init output -->
<a href="#{{sanitizedUniqueName .Module}}-{{$i}}-init" data-bs-toggle="collapse"
class="px-2" role="button">
Toggle Init Output
</a>
<div class="collapse out" id="{{sanitizedUniqueName .Module}}-{{$i}}-init">
<pre class="py-1">
<!-- below <code> element lacking indenting whitespace because it is significant and creates left margin on first line -->
<code class="language-hcl">{{$run.InitOutput}}</code>
</pre>
</div>
<!-- run output -->
<pre class="py-1">
<!-- below <code> element lacking indenting whitespace because it is significant and creates left margin on first line -->
<code class="language-hcl">{{$run.Output}}</code>
</pre>
</div>
</div>
{{end}}
</div>
Expand Down

0 comments on commit 1fdef1b

Please sign in to comment.