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

97/vertical/puppet-scripts-creation #193

Closed
wants to merge 9 commits into from
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,37 @@ def request_params
:port, :application_name, :description, :comment,
:rejection_information, :status, user_ids: [], sudo_user_ids: [])
end

def puppet_node_script(request)

puppet_string =
'class node_vm-%s {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example files given by the admin were different than the US.

Suggested change
'class node_vm-%s {
'class node_$%s {

$admins = [%s]
$users = [%s]

realize(Accounts::Virtual[$admins], Accounts::Sudoroot[$admins])
realize(Accounts::Virtual[$users])
}'
admins = request.users_assigned_to_requests.select(&:sudo).to_a
admins.map!(&:user)
users = request.users.to_a

admins.map! { |user| "\"#{user.first_name << '.' << user.last_name}\"" }
users.map! { |user| "\"#{user.first_name << '.' << user.last_name}\"" }
format(puppet_string, request.name, admins.join(', '), users.join(', '))
end
helper_method :puppet_node_script


def puppet_name_script(request)
puppet_script =
'node \'vm-%s\'{

if defined( node_vm-%s) {
class { node_vm-%s: }
}
}'
format(puppet_script, request.name, request.name,request.name)
end
helper_method :puppet_name_script
end
87 changes: 60 additions & 27 deletions app/views/requests/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,37 +1,70 @@
<p id="notice"><%= notice %></p>
<h1>Requests</h1>

<table class="table table-fixed-columns">
<thead>
<tr>
<th>VM Name</th>
<th style="width: 80px">Cpu-Cores</th>
<th style="width: 80px">MB RAM</th>
<th style="width: 90px">MB Storage</th>
<th>Operating System</th>
<th style="width: 80px">Port</th>
<th>Application Name</th>
<th class="w-25">Comment</th>
<th>Status </th>
</tr>
<tr>
<th>VM Name</th>
<th style="width: 80px">Cpu-Cores</th>
<th style="width: 80px">MB RAM</th>
<th style="width: 90px">MB Storage</th>
<th>Operating System</th>
<th style="width: 80px">Port</th>
<th>Application Name</th>
<th class="w-25">Comment</th>
<th>Status </th>
<th>Puppet Script</th>
</tr>
</thead>

<tbody>
<% @requests.each do |request| %>
<tr>
<td><%= link_to request.name, request_path(request) %></td>
<td><%= request.cpu_cores %></td>
<td><%= request.ram_mb %></td>
<td><%= request.storage_mb %></td>
<td><%= request.operating_system %></td>
<td><%= request.port %></td>
<td><%= request.application_name %></td>
<td><%= request.comment %></td>
<td><%= request.status %></td>
</tr>
<% end %>

<% @requests.each do |request| %>
<tr>
<td><%= link_to request.name, request_path(request) %></td>
<td><%= request.cpu_cores %></td>
<td><%= request.ram_mb %></td>
<td><%= request.storage_mb %></td>
<td><%= request.operating_system %></td>
<td><%= request.port %></td>
<td><%= request.application_name %></td>
<td><%= request.comment %></td>
<td>
<%= link_to request.status,
request_path(request),
class: 'btn btn-primary'
%>
</td>
<%if request.accepted? %>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target=<%= '#puppet-script-' + request.name %>>
show script
</button></td>
<div class="modal fade" id=<%= 'puppet-script-' + request.name %> tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="puppet-scriptTite">Puppet Script</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<pre>
<%= debug puppet_name_script(request) %>
<%= debug puppet_node_script(request) %>
</pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<%else %>
<td></td>
<%end %>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Request', new_request_path, class: 'btn btn-primary' %>
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
t.datetime "updated_at", null: false
t.integer "port"
t.string "application_name"
t.integer "user_id"
t.text "description"
t.integer "user_id"
t.index ["user_id"], name: "index_requests_on_user_id"
end

Expand Down Expand Up @@ -92,12 +92,12 @@
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "role"
t.string "provider"
t.string "uid"
t.string "ssh_key"
Expand Down
29 changes: 29 additions & 0 deletions spec/controllers/requests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,33 @@
expect(response).to redirect_to(requests_url)
end
end

describe 'puppet script helper methods' do
it 'returns correct declaration script for a given request' do
request = Request.create! valid_attributes
script = controller.send(:puppet_name_script, request)
expected_string =
'node \'vm-MyVM\'{

if defined( node_vm-MyVM) {
class { node_vm-MyVM: }
}
}'
expect(script).to eq(expected_string)
end

it 'returns correct initialization script for a given request' do
request = FactoryBot.create(:request_with_users)
script = controller.send(:puppet_node_script, request)
expected_string =
'class node_vm-MyVM {
$admins = []
$users = ["Max.Mustermann", "Max.Mustermann", "Max.Mustermann", "Max.Mustermann"]

realize(Accounts::Virtual[$admins], Accounts::Sudoroot[$admins])
realize(Accounts::Virtual[$users])
}'
expect(script).to eq(expected_string)
end
end
end
10 changes: 10 additions & 0 deletions spec/factories/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@
factory :accepted_request, parent: :request do
status { 'accepted' }
end

factory :request_with_users, parent: :request do
transient do
users_count 4
end

after(:create) do |request, evaluator|
create_list(:user, evaluator.users_count, requests: [request])
end
end
end