Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show unused environments and allow matching nodes to other environments #146

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
38 changes: 0 additions & 38 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Layout/EmptyLineAfterGuardClause:
- 'app/models/hiera_data/config.rb'
- 'app/models/hiera_data/hierarchy.rb'
- 'app/models/hiera_data/yaml_file.rb'
- 'test/support/fake_puppet_db.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
Expand All @@ -66,14 +65,6 @@ Layout/EmptyLinesAroundAccessModifier:
- 'app/controllers/users_controller.rb'
- 'test/models/hiera_data/yaml_file_test.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowAliasSyntax, AllowedMethods.
# AllowedMethods: alias_method, public, protected, private
Layout/EmptyLinesAroundAttributeAccessor:
Exclude:
- 'app/models/hiera_data.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -116,7 +107,6 @@ Layout/HashAlignment:
Exclude:
- 'app/models/user.rb'
- 'test/models/key_test.rb'
- 'test/models/node_test.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -158,13 +148,6 @@ Layout/MultilineMethodCallIndentation:
- 'app/models/hiera_data/data_file.rb'
- 'app/models/hiera_data/hierarchy.rb'
- 'app/models/key.rb'
- 'test/support/fake_puppet_db.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Layout/SpaceAfterComma:
Exclude:
- 'test/support/fake_puppet_db.rb'

# Offense count: 13
# This cop supports safe auto-correction (--auto-correct).
Expand All @@ -176,7 +159,6 @@ Layout/SpaceAroundOperators:
- 'test/models/hiera_data/data_file_test.rb'
- 'test/models/hiera_data/yaml_file_test.rb'
- 'test/models/hiera_data_test.rb'
- 'test/models/node_test.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Expand All @@ -198,8 +180,6 @@ Layout/SpaceInsideHashLiteralBraces:
- 'test/models/hiera_data/yaml_file_test.rb'
- 'test/models/hiera_data_test.rb'
- 'test/models/key_test.rb'
- 'test/models/node_test.rb'
- 'test/support/fake_puppet_db.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -232,7 +212,6 @@ Lint/EmptyFile:
Lint/ScriptPermission:
Exclude:
- 'test/fixtures/files/puppet/environments/eyaml/generate_eyaml_entries.rb'
- 'test/fixtures/files/puppet/generate_nodes.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -345,7 +324,6 @@ Security/YAMLLoad:
- 'test/models/hiera_data/data_file_test.rb'
- 'test/models/hiera_data/yaml_file_test.rb'
- 'test/models/hiera_data_test.rb'
- 'test/support/fake_puppet_db.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
Expand All @@ -357,7 +335,6 @@ Security/YAMLLoad:
Style/BlockDelimiters:
Exclude:
- 'test/fixtures/files/puppet/environments/eyaml/generate_eyaml_entries.rb'
- 'test/fixtures/files/puppet/generate_nodes.rb'

# Offense count: 16
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -387,12 +364,6 @@ Style/CommandLiteral:
Style/Documentation:
Enabled: false

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Style/FileWrite:
Exclude:
- 'test/fixtures/files/puppet/generate_nodes.rb'

# Offense count: 1
# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, IgnoredMethods.
# SupportedStyles: annotated, template, unannotated
Expand All @@ -416,13 +387,6 @@ Style/GuardClause:
- 'app/models/hiera_data/data_file.rb'
- 'app/models/hiera_data/hierarchy.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowSplatArgument.
Style/HashConversion:
Exclude:
- 'test/support/fake_puppet_db.rb'

# Offense count: 1
# Configuration parameters: MinBranchesCount.
Style/HashLikeCase:
Expand Down Expand Up @@ -499,7 +463,6 @@ Style/PreferredHashMethods:
# SupportedStyles: compact, exploded
Style/RaiseArgs:
Exclude:
- 'app/models/hiera_data.rb'
- 'app/models/hiera_data/git_repo.rb'

# Offense count: 1
Expand Down Expand Up @@ -593,7 +556,6 @@ Style/WordArray:
Style/ZeroLengthPredicate:
Exclude:
- 'test/fixtures/files/puppet/environments/eyaml/generate_eyaml_entries.rb'
- 'test/fixtures/files/puppet/generate_nodes.rb'

# Offense count: 23
# This cop supports safe auto-correction (--auto-correct).
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class KeysController < ApplicationController

def index
authorize! :show, Key
@keys = Key.all_for(@node)
@keys = Key.all_for(@node, environment: @environment)
@keys.select! { |k| current_user.may_access?(k) }

add_breadcrumb @environment, environment_nodes_path(@environment)
add_breadcrumb @node, environment_node_keys_path(@environment, @node)
end

def show
@keys = Key.all_for(@node)
@keys = Key.all_for(@node, environment: @environment)
@keys.select! { |k| current_user.may_access?(k) }
@key = Key.new(environment: @environment, name: params[:id])
authorize! :show, @key
Expand Down Expand Up @@ -46,9 +46,9 @@ def destroy
private

def load_nodes
@nodes = Node.all(environment: @environment)
@nodes = Node.all
@nodes.select! { |n| current_user.may_access?(n) }
@node = Node.new(hostname: params[:node_id], environment: @environment)
@node = Node.find(params[:node_id])
authorize! :show, @node
end
end
2 changes: 1 addition & 1 deletion app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class NodesController < ApplicationController

def index
authorize! :index, Node
@nodes = Node.all(environment: @environment)
@nodes = Node.all
@nodes.select! { |n| current_user.may_access?(n) }

add_breadcrumb @environment, environment_nodes_path(@environment)
Expand Down
10 changes: 8 additions & 2 deletions app/javascript/controllers/select_navigation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { Controller } from "@hotwired/stimulus"
import { Turbo } from "@hotwired/turbo-rails"

export default class extends Controller {
connect() {
const selectedOption = this.element.options[this.element.selectedIndex]
this.initialUrl = selectedOption.dataset.url
}

navigate() {
const selectedOption = this.element.options[this.element.selectedIndex];
Turbo.visit(selectedOption.dataset.url);
const selectedOption = this.element.options[this.element.selectedIndex]
const newUrl = selectedOption.dataset.url
if (newUrl != this.initialUrl) Turbo.visit(newUrl)
}
}
33 changes: 30 additions & 3 deletions app/javascript/controllers/slim_select_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@ import { Controller } from "@hotwired/stimulus"
import SlimSelect from "slim-select"

export default class extends Controller {
static targets = ["dropdown"]

connect() {
this.select = new SlimSelect({
select: this.element,
});
select: this.dropdownTarget,
})
this.hideOptions()
}

disconnect() {
this.select.destroy();
this.select.destroy()
}

toggleFilter(event) {
if (event.target.checked) {
this.hideOptions()
} else {
this.showAllOptions()
}
}

hideOptions() {
this.select.setData(this.select.getData().map((o) => {
if (o["data"]["hidable"]) {
o["display"] = false
}
return o
}))
}

showAllOptions() {
this.select.setData(this.select.getData().map((o) => {
o["display"] = true
return o
}))
}
}
17 changes: 11 additions & 6 deletions app/models/environment.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
class Environment < HieraModel
attribute :name, :string

def self.all_names
PuppetDbClient.environments
end
attribute :in_use, :boolean, default: false

def self.all
all_names.map { |e| new(name: e) }
environments_in_use = PuppetDbClient.environments
available_environments = HieraData.environments
unused_environments = available_environments - environments_in_use
environments_in_use.sort.map { |e| new(name: e, in_use: true) } +
unused_environments.sort.map { |e| new(name: e) }
end

def self.find(name)
new(name: name)
all.find { |e| e.name == name }
end

def hierarchies
Hierarchy.all(self)
end

def in_use?
in_use
end

def ==(other)
other.is_a?(Environment) && name == other.name
end
Expand Down
21 changes: 15 additions & 6 deletions app/models/hiera_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ class HieraData
class EnvironmentNotFound < StandardError; end

attr_reader :environment

delegate :hierarchies, to: :config

def self.environments
Pathname.new(config_dir)
.join("environments")
.glob("*/")
.map { |p| p.basename.to_s }
end

def self.config_dir
@config_dir ||= Rails.configuration.hdm.config_dir
end

def initialize(environment)
@environment = environment

raise EnvironmentNotFound.new("Environment '#{environment}' does not exist") unless PuppetDbClient.environments.include?(environment)
raise EnvironmentNotFound, "Environment '#{environment}' does not exist" unless self.class.environments.include?(environment)
end

def all_keys(facts)
Expand Down Expand Up @@ -125,12 +137,9 @@ def lookup_options(facts)

private

def config_dir
@config_dir ||= Rails.configuration.hdm.config_dir
end

def config
@config ||= Config.new(Pathname.new(config_dir).join("environments", environment))
@config ||= Config.new(Pathname.new(self.class.config_dir)
.join("environments", environment))
end

def find_hierarchy(name)
Expand Down
3 changes: 1 addition & 2 deletions app/models/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ class Key < HieraModel
attribute :name, :string
attribute :environment

def self.all_for(node)
def self.all_for(node, environment: node.environment)
facts = node.facts
environment = node.environment
keys = []
HieraData.new(environment.name).all_keys(facts)
.each do |name|
Expand Down
17 changes: 10 additions & 7 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ class Node < HieraModel
attribute :environment
alias name hostname

def self.all_names(environment:)
PuppetDbClient.nodes(environment: environment)
def self.all(environment: nil)
node_records = PuppetDbClient.nodes(environment: environment&.name)
environments = Environment.all.group_by(&:name) unless environment
node_records.map do |node_record|
env = environment || environments[node_record["environment"]].first
new(hostname: node_record["certname"], environment: env)
end
end

def self.all(environment:)
all_names(environment: environment).map do |hostname|
new(hostname: hostname, environment: environment)
end
def self.find(hostname)
all.find { |n| n.hostname == hostname }
end

def ==(other)
Expand All @@ -21,7 +24,7 @@ def ==(other)

def facts
@facts ||=
PuppetDbClient.facts(certname: hostname, environment: environment)
PuppetDbClient.facts(certname: hostname)
end

def to_param
Expand Down
18 changes: 6 additions & 12 deletions app/services/puppet_db_client.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
module PuppetDbClient
module ClassMethods
def nodes(environment:)
response = client.request(
'inventory',
[:'=', 'environment', environment],
{}
)
response.data.map { |x| x.dig("certname") }
def nodes(environment: nil)
query = environment ? [:'=', 'environment', environment] : nil
response = client.request('inventory', query, {})
response.data
rescue
[]
end

def facts(certname:, environment:)
def facts(certname:)
response = client.request(
'facts',
[:and,
[:'=', 'certname', certname],
[:'=', 'environment', environment]
],
[:'=', 'certname', certname],
{}
)

Expand Down
8 changes: 5 additions & 3 deletions app/views/environments/_select_environment.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<%= form_tag "" do |form| %>
<div class="form-group">
<div class="form-group" data-controller="slim-select">
<%= label_tag :environment, "Select environment" %>
<select name="environment" data-controller="slim-select select-navigation" data-action="change->select-navigation#navigate">
<select name="environment" data-controller="select-navigation" data-action="change->select-navigation#navigate" data-slim-select-target="dropdown">
<option value="" <%= "selected" if @environment.nil? %> data-url="<%= environments_path %>"></option>
<% @environments.each do |environment| %>
<option data-url="<%= environment_nodes_path(environment) %>" <%= "selected" if environment == @environment %>><%= environment.name %></option>
<option data-url="<%= environment_nodes_path(environment) %>" <%= "selected" if environment == @environment %>>
<%= environment.name %><%= " (unused)" unless environment.in_use? %>
</option>
<% end %>
</select>
</div>
Expand Down
Loading