Skip to content

Commit

Permalink
Create Puppetscript init (#324)
Browse files Browse the repository at this point in the history
* Change puppetscript init when a user is created

* Fix tests refactor git stub and add simple test

* Refactor githelper and tests

* Rubocop auto-correct
  • Loading branch information
Adrian-St authored Jan 28, 2019
1 parent 33d186c commit 1d0536d
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 93 deletions.
2 changes: 1 addition & 1 deletion app/models/archivation_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ def can_be_executed?
three_days = 60 * 60 * 24 * 3
Time.now >= created_at + three_days
end
end
end
17 changes: 15 additions & 2 deletions app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,29 @@ def push_to_git
path = File.join Rails.root, 'public', 'puppet_script_temp'

begin
message = GitHelper.write_to_repository(path, name) do |git_writer|
notice = ''
GitHelper.write_to_repository(path) 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)
message, notice = commit_and_notice_message(git_writer)
git_writer.save(message)
end
{ notice: message }
{ notice: notice }
rescue Git::GitExecuteError
{ alert: 'Could not push to git. Please check that your ssh key and environment variables are set.' }
end
end

def commit_and_notice_message(git_writer)
if git_writer.added?
['Add ' + name, 'Added file and pushed to git.']
elsif git_writer.updated?
['Update ' + name, 'Changed file and pushed to git.']
else
['', 'Already up to date.']
end
end

def generate_puppet_node_script
admin_users = users_assigned_to_requests.select(&:sudo).to_a
admin_users.map!(&:user)
Expand Down
22 changes: 21 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable

after_create :set_user_id
after_create :set_user_id, :update_repository
after_initialize :set_default_role, if: :new_record?

devise :database_authenticatable, :registerable,
Expand Down Expand Up @@ -98,6 +98,26 @@ def set_user_id
end
end

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

begin
GitHelper.write_to_repository(path) do |git_writer|
git_writer.write_file('init.pp', generate_puppet_init_script)
message = if git_writer.added?
'Create init.pp'
else
"Add #{name}"
end
git_writer.save(message)
end
end
end

def generate_puppet_init_script
Puppetscript.init_scrit(User.all)
end

def valid_ssh_key?
!ssh_key? || SSHKey.valid_ssh_public_key?(ssh_key)
end
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20190121210719_create_archivation_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CreateArchivationRequest < ActiveRecord::Migration[5.2]
def change
create_table :archivation_requests do |t|
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20190123102046_create_virtual_machine_configs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CreateVirtualMachineConfigs < ActiveRecord::Migration[5.2]
def change
create_table :virtual_machine_configs do |t|
Expand Down
6 changes: 0 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@
t.index ["user_id"], name: "index_requests_on_user_id"
end

create_table "requests_responsible_users", id: false, force: :cascade do |t|
t.integer "request_id", null: false
t.integer "user_id", null: false
t.index ["request_id", "user_id"], name: "index_requests_responsible_users_on_request_id_and_user_id"
end

create_table "responsible_users", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "project_id", null: false
Expand Down
21 changes: 5 additions & 16 deletions lib/git_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
# frozen_string_literal: true

module GitHelper
def self.write_to_repository(path, name)
def self.write_to_repository(path)
FileUtils.mkdir_p(path) unless File.exist?(path)
git_writer = GitWriter.new(path, name)
git_writer = GitWriter.new(path)
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)
def initialize(path)
@path = path
@name = name
@git = setup_git(path)
end

Expand All @@ -35,16 +32,8 @@ 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
def save(message)
commit_and_push(message) if added? || updated?
end

private
Expand Down
29 changes: 29 additions & 0 deletions lib/puppetscript.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# frozen_string_literal: true

module Puppetscript
def self.init_scrit(users)
users_string = ''.dup
users.each do |user|
# " " for identation
users_string << user_script(user)
end
format(generic_init_script, users_string)
end

def self.node_script(name, admin_users, users)
puppet_string = generic_node_script
admins_string = generate_user_array(admin_users)
Expand All @@ -13,6 +22,26 @@ def self.name_script(name)
format(puppet_script, name, name, name)
end

def self.generic_init_script
<<~INIT_SCRIPT
class accounts {
%s
}
INIT_SCRIPT
end

def self.user_script(user)
<<-USER_SCRIPT
@accounts::virtual { '#{user.first_name}.#{user.last_name}':
uid => #{user.user_id},
realname => '#{user.first_name} #{user.last_name}',
sshkeytype => 'ssh-rsa',
sshkey => '#{user.ssh_key.try(:sub!, 'ssh-rsa ', '')}'
}
USER_SCRIPT
end

def self.generic_node_script
<<~NODE_SCRIPT
class node_%s {
Expand Down
2 changes: 1 addition & 1 deletion spec/api/v_sphere_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
end

it 'VirtualMachine.all finds all vms that not have a special status' do
vms = (mock_root_folder_vms).map { |each| VSphere::VirtualMachine.new each }
vms = mock_root_folder_vms.map { |each| VSphere::VirtualMachine.new each }
expect(VSphere::VirtualMachine.rest).to match_array vms
end

Expand Down
18 changes: 8 additions & 10 deletions spec/controllers/vms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
@request.env['devise.mapping'] = Devise.mappings[:user]
sign_in current_user
end

before do
allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock([vm1, vm2], [], [], [], [])
end

describe 'GET #index' do



context 'when the current user is a user' do
it 'returns http success' do
get :index
Expand Down Expand Up @@ -123,27 +121,27 @@

context 'when user is not associated to vm' do
it 'redirects' do
get :show, params: { id: vm1.name }
expect(response).to have_http_status :redirect
get :show, params: { id: vm1.name }
expect(response).to have_http_status :redirect
end
end
end

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

context 'when user is associated to vm' do
it 'renders show page' do
expect(get(:show, params: { id: vm2.name })).to render_template('vms/show')
end
end

context 'when user is not associated to vm' do
it 'renders show page' do
expect(get(:show, params: { id: vm1.name })).to render_template('vms/show')
expect(get(:show, params: { id: vm1.name })).to render_template('vms/show')
end
end
end
end
end

context 'when no vm found' do
Expand All @@ -154,7 +152,7 @@
it 'returns http status not found when no vm found' do
get :show, params: { id: 5 }
expect(response).to have_http_status(:not_found)
end
end

it 'renders not found page when no vm found' do
expect(get(:show, params: { id: vm1.name })).to render_template('errors/not_found')
Expand Down
73 changes: 17 additions & 56 deletions spec/models/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,51 +193,28 @@ class node_MyVM {
let(:request) { FactoryBot.build :request }

before do
@git = double
@status = double
allow(@git).to receive(:config).with('user.name', 'test_user_name')
allow(@git).to receive(:config).with('user.email', 'test_user_email')
allow(@git).to receive(:status) { @status }
allow(@git).to receive(:add)
allow(@git).to receive(:commit_all)
allow(@git).to receive(:push)

@path = File.join Rails.root, 'public', 'puppet_script_temp', ENV['GIT_REPOSITORY_NAME']
node_path = File.join @path, 'Node'
name_path = File.join @path, 'Name'

git_class = class_double('Git')
.as_stubbed_const(transfer_nested_constants: true)

allow(git_class).to receive(:clone) do
FileUtils.mkdir_p(@path) unless File.exist?(@path)
FileUtils.mkdir_p(node_path) unless File.exist?(node_path)
FileUtils.mkdir_p(name_path) unless File.exist?(name_path)
@git
end
@git_stub = create_git_stub
end

after do
FileUtils.rm_rf(@path) if File.exist?(@path)
@git_stub.delete
end

it 'correctly calls git' do
expect(@git_stub.git).to receive(:config).with('user.name', 'test_user_name')
expect(@git_stub.git).to receive(:config).with('user.email', 'test_user_email')
request.push_to_git
end

context 'with a new puppet script' do
before do
allow(@status).to receive(:changed).and_return([])
allow(@status).to receive(:added).and_return(['added_file'])
allow(@git_stub.status).to receive(:changed).and_return([])
allow(@git_stub.status).to receive(:added).and_return(['added_file'])
end

it 'correctly calls git' do
expect(@git).to receive(:config).with('user.name', 'test_user_name')
expect(@git).to receive(:config).with('user.email', 'test_user_email')
expect(@git).to(receive(:status).once) { @status }
expect(@status).to receive(:added).once
expect(@status).not_to receive(:changed)

expect(@git).to receive(:add)
expect(@git).to receive(:commit_all)
expect(@git).to receive(:push)
request.push_to_git
expect(@git_stub.git).to have_received(:commit_all).with('Add MyVM')
end

it 'returns a success message' do
Expand All @@ -247,20 +224,12 @@ class node_MyVM {

context 'with a changed puppet script' do
before do
allow(@status).to receive(:changed).and_return(['changed_file'])
allow(@status).to receive(:added).and_return([])
allow(@git_stub.status).to receive(:changed).and_return(['changed_file'])
allow(@git_stub.status).to receive(:added).and_return([])
end

it 'correctly calls git' do
expect(@git).to receive(:config).with('user.name', 'test_user_name')
expect(@git).to receive(:config).with('user.email', 'test_user_email')
expect(@git).to(receive(:status).twice) { @status }
expect(@status).to receive(:added).once
expect(@status).to receive(:changed).once

expect(@git).to receive(:add)
expect(@git).to receive(:commit_all)
expect(@git).to receive(:push)
expect(@git_stub.git).to receive(:commit_all).with('Update MyVM')
request.push_to_git
end

Expand All @@ -271,20 +240,12 @@ class node_MyVM {

context 'without any changes' do
before do
allow(@status).to receive(:changed).and_return([])
allow(@status).to receive(:added).and_return([])
allow(@git_stub.status).to receive(:changed).and_return([])
allow(@git_stub.status).to receive(:added).and_return([])
end

it 'correctly calls git' do
expect(@git).to receive(:config).with('user.name', 'test_user_name')
expect(@git).to receive(:config).with('user.email', 'test_user_email')
expect(@git).to(receive(:status).twice) { @status }
expect(@status).to receive(:added).once
expect(@status).to receive(:changed).once

expect(@git).to receive(:add)
expect(@git).not_to receive(:commit_all)
expect(@git).not_to receive(:push)
expect(@git_stub.git).not_to receive(:commit_all)
request.push_to_git
end

Expand Down
16 changes: 16 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
end

context 'when the user does not exist' do
before do
@git_stub = create_git_stub
end

after do
@git_stub.delete
end

it 'creates a new user' do
expect { user }.to change(User, :count).by(1)
end
Expand All @@ -70,6 +78,14 @@
it 'sets the users email' do
expect(user.email).to eq(mail)
end

it 'adds the users to puppet_script init' do
allow(@git_stub.status).to receive(:changed).and_return(['init.pp'])
allow(@git_stub.status).to receive(:added).and_return([])

user
expect(@git_stub.git).to have_received(:commit_all).with('Add First Last')
end
end

context 'when the user already exists' do
Expand Down
Loading

0 comments on commit 1d0536d

Please sign in to comment.