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

100/vertical/git puppet scripts #234

Merged
merged 35 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ebe1d9e
Add puppet script functionality to request index
NiklasKoehnecke Dec 13, 2018
44fb8e7
Merge branch 'dev' into 97/frontend/puppet-scripts
NiklasKoehnecke Jan 8, 2019
06f8461
Fix name generation, create puppet scripts in tmp
NiklasKoehnecke Jan 8, 2019
e9b1b50
Undo file creation, fix modal ids
NiklasKoehnecke Jan 8, 2019
992f528
Change method names.
henleo Jan 8, 2019
c32269f
Add puppet name script
NiklasKoehnecke Jan 10, 2019
f106ec5
Add Git gem
Jan 10, 2019
927a2fd
Add logic to push created puppet script to git
Jan 10, 2019
b1d3255
Add button and route to push puppetscripts to git and fix code
Jan 13, 2019
48c3836
Use environment variables for github repository and user
Jan 13, 2019
1c91d43
Add simple test for push_to_git route
Jan 13, 2019
1acf637
Extract logic to request model
Jan 14, 2019
f6dbfc3
Add testing environment variables to example.yml and travis.yml
Jan 14, 2019
68868e3
Add notifications for different git actions
Jan 14, 2019
3d3c33b
Change travis.yml to not trigger multiple jobs
Jan 14, 2019
d7c8c40
Add more tests
Jan 14, 2019
9e1d7b4
Add puppet_name_file to git push
Jan 14, 2019
5487950
Apply Rubocop autofix
Jan 14, 2019
76fa886
Refactor request model
Jan 14, 2019
0512f89
Refactor tests
Jan 14, 2019
cc4de04
Fix tests
Jan 14, 2019
f56dd9b
merge dev
Jan 14, 2019
5ce1be8
Fix tests
Jan 14, 2019
c92ff82
Add tests for puppet script creation.
henleo Jan 14, 2019
63b9c09
Merge dev
Jan 19, 2019
9a64944
Fix rubocop issues
Jan 19, 2019
b0664f0
Fix tests
Jan 19, 2019
532faec
Fix controller linting issues
Jan 19, 2019
a2e8ec6
Extract request model logic into lib
Jan 19, 2019
7a2935a
Unfix controller linting
Jan 20, 2019
13b0b4d
Merge branch 'dev' into 100/vertical/git-puppet-scripts
Jan 20, 2019
622df08
Change puppetscript and fix bugs
Jan 20, 2019
518b84e
Update tests to puppetscript change
Jan 20, 2019
4dace34
Uncomment git rescue
Jan 22, 2019
43082e4
Merge dev
Jan 25, 2019
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ coverage
# Ignore locks
.lock*

config/credentials.yml.enc
config/credentials.yml.enc

# Ignore environment variables
/config/environment_variables.yml
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ script:
after_success:
# https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
- curl --data "commit=$TRAVIS_COMMIT" --data "branch=$TRAVIS_BRANCH" --data "eventtype=$TRAVIS_EVENT_TYPE" http://hart-deploy.epic-hpi.de/

env:
- GIT_REPOSITORY_URL=test_repository_url GIT_REPOSITORY_NAME=test_repository_name GITHUB_USER_NAME=test_user_name GITHUB_USER_EMAIL=test_user_email
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ gem 'omniauth_openid_connect'
# Allow locks on database
gem 'with_advisory_lock'

# Ruby Git
gem 'git'

# Mina for deployment
# Have a look in the tutorial:
# https://github.com/lnikell/wiki/wiki/Deploy-rails-application-with-Mina,-Nginx-and-Puma
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ GEM
font-awesome-rails (4.7.0.4)
railties (>= 3.2, < 6.0)
formatador (0.2.5)
git (1.5.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
guard (2.15.0)
Expand Down Expand Up @@ -447,6 +448,7 @@ DEPENDENCIES
factory_bot_rails
faker!
font-awesome-rails
git
guard
jbuilder (~> 2.5)
jquery-rails
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class RequestsController < ApplicationController
include OperatingSystemsHelper
include RequestsHelper
before_action :set_request, only: %i[show edit update destroy request_state_change]
before_action :set_request, only: %i[show edit update push_to_git destroy request_state_change]
before_action :authenticate_employee
before_action :authenticate_state_change, only: %i[request_change_state]

Expand Down Expand Up @@ -100,6 +100,12 @@ def destroy
end
end

# Creates puppet files for request and pushes the created files into a git repository
def push_to_git
response = @request.push_to_git
redirect_to requests_path, response
end

private

def host_url
Expand Down Expand Up @@ -158,4 +164,14 @@ def request_params
def rejection_params
params.require(:request).permit(:rejection_information)
end

def puppet_node_script(request)
request.generate_puppet_node_script
end
helper_method :puppet_node_script

def puppet_name_script(request)
request.generate_puppet_name_script
end
helper_method :puppet_name_script
end
24 changes: 24 additions & 0 deletions app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ def create_vm
folder.create_vm(cpu_cores, ram_mb, storage_mb, name, clusters.first) if clusters.first
end

def push_to_git
path = File.join Rails.root, 'public', 'puppet_script_temp'

begin
message = GitHelper.write_to_repository(path, name) do |git_writer|
git_writer.write_file('Node/' + "node_#{name}.pp", generate_puppet_node_script)
git_writer.write_file('Name/' + "#{name}.pp", generate_puppet_name_script)
end
{ notice: message }
rescue Git::GitExecuteError
{ alert: 'Could not push to git. Please check that your ssh key and environment variables are set.' }
end
end

def generate_puppet_node_script
admin_users = users_assigned_to_requests.select(&:sudo).to_a
admin_users.map!(&:user)
Puppetscript.node_script(name, admin_users, users.to_a)
end

def generate_puppet_name_script
Puppetscript.name_script(name)
end

private

def url(host_name)
Expand Down
34 changes: 32 additions & 2 deletions app/views/requests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<th>Application Name</th>
<th>Comment</th>
<th>Status </th>
<th>Puppet Script</th>
</tr>
</thead>

Expand All @@ -50,10 +51,39 @@
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_node_script(request) %>
<%= debug puppet_name_script(request) %>
</pre>
</div>
<div class="modal-footer">
<%= link_to "Push to git", push_to_git_request_path(request), method: :post, class: "btn btn-primary" %>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<%else %>
<td></td>
<%end %>
</tr>
<% end %>
<%end %>
</tbody>
</table>
<% if @requests.length == 0 %>
<p class="empty-table-label">No requests created yet</p>
<p class="empty-table-label">No requests created yet</p>
<% end %>
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2

# Load files in lib
config.eager_load_paths << Rails.root.join('lib')

# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
Expand Down
19 changes: 19 additions & 0 deletions config/environment_variables.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copy and set environment variable values in a new environment_variables.yml

development:
GIT_REPOSITORY_URL: GIT_REPOSITORY_URL
GIT_REPOSITORY_NAME: GIT_REPOSITORY_NAME
GITHUB_USER_NAME: GITHUB_USER_NAME
GITHUB_USER_EMAIL: GITHUB_USER_EMAIL

production:
GIT_REPOSITORY_URL: GIT_REPOSITORY_URL
GIT_REPOSITORY_NAME: GIT_REPOSITORY_NAME
GITHUB_USER_NAME: GITHUB_USER_NAME
GITHUB_USER_EMAIL: GITHUB_USER_EMAIL

test:
GIT_REPOSITORY_URL: test_repository_url
GIT_REPOSITORY_NAME: test_repository_name
GITHUB_USER_NAME: test_user_name
GITHUB_USER_EMAIL: test_user_email
15 changes: 15 additions & 0 deletions config/initializers/environment_variables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module EnvironmentVariablesExample
class Application < Rails::Application
config.before_configuration do
env_file = Rails.root.join('config', 'environment_variables.yml').to_s

if File.exist?(env_file)
YAML.load_file(env_file)[Rails.env].each do |key, value|
ENV[key.to_s] = value
end
end
end
end
end
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
resources :operating_systems, path: '/vms/requests/operating_systems', except: :show

resources :request_templates, path: '/vms/request_templates', except: :show

patch '/vms/requests/reject', to: 'requests#reject', as: 'reject'
resources :requests, path: '/vms/requests'
resources :requests, path: '/vms/requests' do
post :push_to_git, on: :member
end

resources :notifications, only: %i[index new create destroy] do
get :mark_as_read, on: :member
Expand Down
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@
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"
t.string "first_name"
t.string "last_name"
t.string "ssh_key"
t.string "provider"
t.string "uid"
t.integer "user_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
Expand Down
68 changes: 68 additions & 0 deletions lib/git_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

module GitHelper
def self.write_to_repository(path, name)
FileUtils.mkdir_p(path) unless File.exist?(path)
git_writer = GitWriter.new(path, name)
begin
yield git_writer
message = git_writer.save
message
ensure
FileUtils.rm_rf(path) if File.exist?(path)
end
end

class GitWriter
def initialize(path, name)
@path = path
@name = name
@git = setup_git(path)
end

def write_file(file_name, file_content)
path = File.join @path, ENV['GIT_REPOSITORY_NAME'], file_name
File.delete(path) if File.exist?(path)
File.open(path, 'w') { |f| f.write(file_content) }
@git.add(path)
end

def added?
@git.status.added.any?
end

def updated?
@git.status.changed.any?
end

def save
if added?
commit_and_push('Add ' + @name)
'Added file and pushed to git.'
elsif updated?
commit_and_push('Update ' + @name)
'Changed file and pushed to git.'
else
'Already up to date.'
end
end

private

def setup_git(path)
# Dir.mkdir(dir) unless File.exists?(dir)
uri = ENV['GIT_REPOSITORY_URL']
name = ENV['GIT_REPOSITORY_NAME']

git = Git.clone(uri, name, path: path)
git.config('user.name', ENV['GITHUB_USER_NAME'])
git.config('user.email', ENV['GITHUB_USER_EMAIL'])
git
end

def commit_and_push(message)
@git.commit_all(message)
@git.push
end
end
end
43 changes: 43 additions & 0 deletions lib/puppetscript.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Puppetscript
def self.node_script(name, admin_users, users)
puppet_string = generic_node_script
admins_string = generate_user_array(admin_users)
users_string = generate_user_array(users)
format(puppet_string, name, admins_string, users_string)
end

def self.name_script(name)
puppet_script = generic_name_script
format(puppet_script, name, name, name)
end

def self.generic_node_script
<<~NODE_SCRIPT
class node_$%s {
$admins = [%s]
$users = [%s]

realize(Accounts::Virtual[$admins], Accounts::Sudoroot[$admins])
realize(Accounts::Virtual[$users])
}
NODE_SCRIPT
end

def self.generic_name_script
<<~NAME_SCRIPT
node \'$%s\'{

if defined( node_$%s) {
class { node_$%s: }
}
}
NAME_SCRIPT
end

def self.generate_user_array(users)
users.map! { |user| "\"#{user.first_name << '.' << user.last_name}\"" }
users.join(', ')
end
end
1 change: 1 addition & 0 deletions spec/controllers/request_templates_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@

context 'when the current_user is an admin' do
let(:current_user) { FactoryBot.create :admin }

describe 'GET #new' do
it 'returns a success response' do
get :new, params: {}, session: valid_session
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/requests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,20 @@
expect(response).to redirect_to(requests_url)
end
end

describe 'POST #push_to_git' do
before do
request = Request.create! valid_attributes
allow(request).to receive(:push_to_git).and_return(notice: 'Successfully pushed to git.')
request_class = class_double('Request')
.as_stubbed_const(transfer_nested_constants: true)

expect(request_class).to receive(:find) { request }
end

it 'redirects with a success message' do
post :push_to_git, params: { id: request.to_param }
expect(response).to redirect_to(requests_url)
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
Loading