Skip to content

Commit

Permalink
Merge pull request #71 from plazonic/master
Browse files Browse the repository at this point in the history
Add support for environments.

This closes #54.
  • Loading branch information
skx authored Apr 20, 2020
2 parents 9eb306d + b55cf5a commit 238ea7f
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmd_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func getMetrics() map[string]string {
metrics := make(map[string]string)

// Get the node-states.
data, err := getStates()
data, err := getStates("")
if err != nil {
fmt.Printf("Error getting node states: %s\n", err.Error())
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func runPrune(x pruneCmd) error {
if x.verbose {
fmt.Printf("Pruning 'orphaned' reports from beneath %s\n", ReportPrefix)
}
return (pruneOrphaned(x.prefix, x.verbose))
return (pruneOrphaned("", x.prefix, x.verbose))
}

//
Expand Down
38 changes: 30 additions & 8 deletions cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func APIState(res http.ResponseWriter, req *http.Request) {
//
// Get the nodes.
//
NodeList, err := getIndexNodes()
NodeList, err := getIndexNodes("")
if err != nil {
status = http.StatusInternalServerError
return
Expand Down Expand Up @@ -188,7 +188,7 @@ func RadiatorView(res http.ResponseWriter, req *http.Request) {
//
// Get the state of the nodes.
//
data, err := getStates()
data, err := getStates("")
if err != nil {
status = http.StatusInternalServerError
return
Expand Down Expand Up @@ -447,7 +447,7 @@ func SearchHandler(res http.ResponseWriter, req *http.Request) {
//
// Get all known nodes.
//
NodeList, err := getIndexNodes()
NodeList, err := getIndexNodes("")
if err != nil {
status = http.StatusInternalServerError
return
Expand Down Expand Up @@ -925,20 +925,28 @@ func IndexHandler(res http.ResponseWriter, req *http.Request) {
}
}()

//
// Check if we are filtering by environment
//
vars := mux.Vars(req)
environment := vars["environment"]

//
// Annoying struct to allow us to populate our template
// with both the nodes in the list, and the graph-data
//
type Pagedata struct {
Graph []PuppetHistory
Nodes []PuppetRuns
Urlprefix string
Graph []PuppetHistory
Nodes []PuppetRuns
Environment string
Environments []string
Urlprefix string
}

//
// Get the nodes to show on our front-page
//
NodeList, err := getIndexNodes()
NodeList, err := getIndexNodes(environment)
if err != nil {
status = http.StatusInternalServerError
return
Expand All @@ -947,18 +955,24 @@ func IndexHandler(res http.ResponseWriter, req *http.Request) {
//
// Get the graph-data
//
graphs, err := getHistory()
graphs, err := getHistory(environment)
if err != nil {
status = http.StatusInternalServerError
return
}

//
// Get all environments
environments, err := getEnvironments()

//
// Populate this structure.
//
var x Pagedata
x.Graph = graphs
x.Nodes = NodeList
x.Environment = environment
x.Environments = environments
x.Urlprefix = templateArgs.urlprefix

//
Expand Down Expand Up @@ -1083,6 +1097,9 @@ func serve(settings serveCmd) {
// Handle a display of all known nodes, and their last state.
//
router.HandleFunc("/", IndexHandler).Methods("GET")
// also do it for environments
router.HandleFunc("/environment/{environment}/", IndexHandler).Methods("GET")
router.HandleFunc("/environment/{environment}", IndexHandler).Methods("GET")

//
// Static-Files
Expand Down Expand Up @@ -1179,6 +1196,11 @@ func (p *serveCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
//
SetupDB(p.dbFile)

//
// Check for entries with no environment
//
populateEnvironment(p.prefix)

//
// If autoprune
//
Expand Down
2 changes: 1 addition & 1 deletion cmd_serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestReportView(t *testing.T) {
// The tests
//
tests := []TestCase{
{"text/html", "Report of execution against www.steve.org.uk, at 2017-07-29 23:17:01"},
{"text/html", "Report of execution against www.steve.org.uk in production, at 2017-07-29 23:17:01"},
{"application/json", "\"State\":\"unchanged\","},
{"application/xml", "<State>unchanged</State>"}}

Expand Down
23 changes: 22 additions & 1 deletion data/index.template
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@
<div class="pull-left">
<ul class="nav navbar-nav">
<li class="breadcrumb-item"><a href="{{.Urlprefix }}/"><b>Puppet-Summary</b></a></li>
<li class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Environments
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="{{.Urlprefix}}/">All environments</a></li>
{{range .Environments}}
<li><a class="dropdown-item" href="{{$.Urlprefix}}/environment/{{.}}">{{.}}</a></li>
{{end}}
</ul>
</li>
</ul>
</div>
<div class="pull-right">
Expand All @@ -129,7 +140,7 @@
</nav>
<div class="container">

<h1>Node Summary</h1>
<h1>Node Summary{{if ne .Environment "" }} for environment: {{.Environment}}{{ end }}</h1>
<canvas id="canvas" style="height: 150px; width: 90%; margin-left:5%; margin-right:5%"></canvas>
<div id="fcanvas" ></div>
<p>&nbsp;</p>
Expand All @@ -150,6 +161,7 @@
<thead>
<tr>
<th>Node</th>
<th>Environment</th>
<th>State</th>
<th>Seen</th>
</tr>
Expand All @@ -161,6 +173,7 @@
{{if eq .State "orphaned" }} class="warning" {{ end }}
data-href="{{$.Urlprefix }}/node/{{.Fqdn}}">
<td>{{.Fqdn}}</td>
<td>{{.Environment}}</td>
<td>{{.State}}</td>
<td data-text="{{.Epoch}}" data-sort-value="{{.Epoch}}" title="{{.At}}">{{.Ago}}</td>
</tr>
Expand All @@ -174,6 +187,7 @@
<thead>
<tr>
<th>Node</th>
<th>Environment</th>
<th>State</th>
<th>Seen</th>
</tr>
Expand All @@ -182,6 +196,7 @@
{{if eq .State "failed" }}
<tr class="danger" data-href="{{$.Urlprefix }}/node/{{.Fqdn}}">
<td>{{.Fqdn}}</td>
<td>{{.Environment}}</td>
<td>{{.State}}</td>
<td data-text="{{.Epoch}}" data-sort-value="{{.Epoch}}" title="{{.At}}">{{.Ago}}</td>
</tr>
Expand All @@ -196,6 +211,7 @@
<thead>
<tr>
<th>Node</th>
<th>Environment</th>
<th>State</th>
<th>Seen</th>
</tr>
Expand All @@ -204,6 +220,7 @@
{{if eq .State "changed" }}
<tr class="info" data-href="{{$.Urlprefix }}/node/{{.Fqdn}}">
<td>{{.Fqdn}}</td>
<td>{{.Environment}}</td>
<td>{{.State}}</td>
<td data-text="{{.Epoch}}" data-sort-value="{{.Epoch}}" title="{{.At}}">{{.Ago}}</td>
</tr>
Expand All @@ -218,6 +235,7 @@
<thead>
<tr>
<th>Node</th>
<th>Environment</th>
<th>State</th>
<th>Seen</th>
</tr>
Expand All @@ -226,6 +244,7 @@
{{if eq .State "unchanged" }}
<tr data-href="{{$.Urlprefix }}/node/{{.Fqdn}}">
<td>{{.Fqdn}}</td>
<td>{{.Environment}}</td>
<td>{{.State}}</td>
<td data-text="{{.Epoch}}" data-sort-value="{{.Epoch}}" title="{{.At}}">{{.Ago}}</td>
</tr>
Expand All @@ -240,6 +259,7 @@
<thead>
<tr>
<th>Node</th>
<th>Environment</th>
<th>State</th>
<th>Seen</th>
</tr>
Expand All @@ -248,6 +268,7 @@
{{if eq .State "orphaned" }}
<tr class="warning" data-href="{{$.Urlprefix }}/node/{{.Fqdn}}">
<td>{{.Fqdn}}</td>
<td>{{.Environment}}</td>
<td>{{.State}}</td>
<td data-text="{{.Epoch}}" data-sort-value="{{.Epoch}}" title="{{.At}}">{{.Ago}}</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions data/node.template
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<tr>
<th>ID</th>
<th>Node</th>
<th>Environment</th>
<th>Status</th>
<th>Seen</th>
<th>Failed</th>
Expand All @@ -118,6 +119,7 @@
{{if ne .YamlFile "pruned" }} data-href="{{$.Urlprefix }}/report/{{.ID}}" {{ end }}>
<td id="data_{{incr $i}}">{{incr $i}}</td>
<td>{{.Fqdn}}</td>
<td>{{.Environment}}</td>
<td>{{.State}}</td>
<td title="{{.At}}">{{.Ago}}</td>
<td>{{.Failed}}</td>
Expand Down
2 changes: 1 addition & 1 deletion data/report.template
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<div class="col-sm-1 col-md-1">
</div>
<div class="col-sm-11 col-md-11">
<p>Report of execution against {{ .Report.Fqdn }}, at {{ .Report.At }}:</p>
<p>Report of execution against {{ .Report.Fqdn }} in {{ .Report.Environment }}, at {{ .Report.At }}:</p>
<table class="table table-bordered table-striped table-condensed table-hover">
<tr><td>Changed </td><td>{{ .Report.Changed }}</td></tr>
<tr><td>Skipped </td><td>{{ .Report.Skipped }}</td></tr>
Expand Down
Loading

0 comments on commit 238ea7f

Please sign in to comment.