Skip to content

Commit

Permalink
Merge branch 'dev' into standardize_styling
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisma committed Mar 25, 2019
2 parents a40303a + 6fa25cf commit 3d8f62f
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 206 deletions.
75 changes: 2 additions & 73 deletions app/api/v_sphere/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,82 +311,11 @@ def status

# this method should return all users, including the sudo users
def users
users = []
begin
GitHelper.open_git_repository do
remote_users = Puppetscript.read_node_file(name)
users = remote_users[:users] || []
end
rescue Git::GitExecuteError, RuntimeError => e
Rails.logger.error(e)
end
users
end

def commit_message(git_writer)
if git_writer.added?
'Add ' + name
elsif git_writer.updated?
'Update ' + name
else
''
end
end

def user_name_and_node_script(ids)
all_users = Puppetscript.read_node_file(name)
sudo_users = all_users[:admins]
new_users = User.where(id: ids)
name_script = Puppetscript.name_script(name)
node_script = Puppetscript.node_script(name, sudo_users, new_users)
[name_script, node_script]
end

def users=(ids)
GitHelper.open_git_repository(for_write: true) do |git_writer|
name_script, node_script = user_name_and_node_script(ids)
write_node_and_class_file(git_writer, name_script, node_script)
end
rescue Git::GitExecuteError, RuntimeError => e
Rails.logger.error(e)
end

def sudo_users=(ids)
GitHelper.open_git_repository(for_write: true) do |git_writer|
name_script, node_script = sudo_name_and_node_script(ids)
write_node_and_class_file(git_writer, name_script, node_script)
end
rescue Git::GitExecuteError, RuntimeError => e
logger.error(e)
end

def write_node_and_class_file(git_writer, name_script, node_script)
git_writer.write_file(Puppetscript.class_file_name(name), name_script)
git_writer.write_file(Puppetscript.node_file_name(name), node_script)
message = commit_message(git_writer)
git_writer.save(message)
ensure_config.all_users
end

def sudo_users
admins = []
begin
GitHelper.open_git_repository do
users = Puppetscript.read_node_file(name)
admins = users[:admins] || []
end
rescue Git::GitExecuteError, RuntimeError => e
Rails.logger.error(e)
end
admins
end

def sudo_name_and_node_script(ids)
all_users = Puppetscript.read_node_file(name)
users = all_users[:users]
new_sudo_users = User.where(id: ids)
name_script = Puppetscript.name_script(name)
node_script = Puppetscript.node_script(name, new_sudo_users, users)
[name_script, node_script]
ensure_config.sudo_users
end

# fine to use for a single vm. If you need to check multiple vms for a user, check with user_vms
Expand Down
96 changes: 37 additions & 59 deletions app/controllers/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class VmsController < ApplicationController
attr_reader :vms

include VmsHelper
before_action :assign_vm, except: %i[update_config update edit index]
before_action :assign_config, only: %i[update_config update edit edit_config ]
before_action :authenticate_admin, only: %i[archive_vm edit_config update_config]
before_action :authorize_vm_access, only: %i[show edit update]
before_action :authenticate_root_user_or_admin, only: %i[change_power_state suspend_vm shutdown_guest_os reboot_guest_os reset_vm]
Expand All @@ -17,55 +19,42 @@ def index
filter_vm_categories current_user unless current_user.admin?
end

def show
render(template: 'errors/not_found', status: :not_found) if @vm.nil?
end
def show; end

def edit_config
@vm = VSphere::VirtualMachine.find_by_name params[:id]
@vm.ensure_config
end

def update_config
@config = VirtualMachineConfig.find_by_name params[:id]
if @config
if @config.update(config_params)
redirect_to requests_path, notice: 'Successfully updated configuration'
else
redirect_to requests_path, notice: 'Could not update the configuration'
end
if @vm_config.update(config_params)
redirect_to requests_path, notice: 'Successfully updated configuration'
else
flash[:alert] = 'Configuration could not be found!'
redirect_to controller: :vms, action: 'index'
redirect_to requests_path, notice: 'Could not update the configuration'
end
end

def edit
return render(template: 'errors/not_found', status: :not_found) if @vm.nil?

@sudo_user_ids = @vm.sudo_users.map(&:id)
@non_sudo_user_ids = @vm.users.map(&:id)
@configuration = @vm.ensure_config
@sudo_user_ids = @vm_config.sudo_users.map(&:id)
@non_sudo_user_ids = @vm_config.users.map(&:id)
@configuration = @vm_config
end

def update
prepare_info_params
notify_changed_users(@vm.sudo_users.map(&:id), info_params[:sudo_user_ids].map(&:to_i), true, @vm.name)
notify_changed_users(@vm.users.map(&:id), info_params[:non_sudo_user_ids].map(&:to_i), false, @vm.name)
@vm.sudo_users = info_params[:sudo_user_ids]
@vm.users = info_params[:non_sudo_user_ids]
old_users = @vm_config.all_users
old_sudo_users = @vm_config.sudo_users

unless @vm.ensure_config.update config_params
if @vm_config.update config_params
notify_changed_users(old_sudo_users, @vm_config.sudo_users, true, @vm_config.name)
notify_changed_users(old_users, @vm_config.users, false, @vm_config.name)
redirect_to vm_path(@vm_config.name)
else
flash[:error] = 'Description couldn\'t be saved.'
redirect_to edit_vm_path(@vm.name)
redirect_to edit_vm_path(@vm_config.name)
end

redirect_to vm_path(@vm.name)
end

def request_vm_archivation
@vm = VSphere::VirtualMachine.find_by_name params[:id]
return if !@vm || @vm.archived? || @vm.pending_archivation?
return if @vm.archived? || @vm.pending_archivation?

@vm.users.each do |each|
each.notify("Your VM #{@vm.name} has been requested to be archived",
Expand All @@ -80,8 +69,7 @@ def request_vm_archivation
end

def request_vm_revive
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
return if !@vm || @vm.pending_reviving?
return if @vm.pending_reviving?

User.admin.each do |each|
each.notify("VM #{@vm.name} has been requested to be revived",
Expand All @@ -94,13 +82,11 @@ def request_vm_revive
end

def stop_archiving
@vm = VSphere::VirtualMachine.find_by_name params[:id]
@vm.set_revived
end

def archive_vm
@vm = VSphere::VirtualMachine.find_by_name params[:id]
return if !@vm || @vm.archived?
return if @vm.archived?

@vm.set_archived

Expand All @@ -113,7 +99,6 @@ def archive_vm
end

def revive_vm
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
@vm.set_revived
@vm.power_on

Expand All @@ -125,42 +110,34 @@ def revive_vm
end

def change_power_state
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
@vm.change_power_state
redirect_back(fallback_location: root_path)
end

def suspend_vm
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
@vm.suspend_vm
redirect_back(fallback_location: root_path)
end

def shutdown_guest_os
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
@vm.shutdown_guest_os
redirect_back(fallback_location: root_path)
end

def reboot_guest_os
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
@vm.reboot_guest_os
redirect_back(fallback_location: root_path)
end

def reset_vm
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
@vm.reset_vm
redirect_back(fallback_location: root_path)
end

private

def prepare_info_params
return unless params[:vm_info]

params[:vm_info][:sudo_user_ids] ||= @vm.sudo_users.map(&:id)
params[:vm_info][:non_sudo_user_ids] ||= (@vm.users - @vm.sudo_users).map(&:id)
def config_params
params.require(:vm_info).permit(:description, :ip, :dns, sudo_user_ids: [], user_ids: [])
end

def initialize_vm_categories
Expand All @@ -179,29 +156,30 @@ def filter_vm_categories(user)
end

def authorize_vm_access
@vm = VSphere::VirtualMachine.find_by_name params[:id]
return unless @vm

redirect_to vms_path unless current_user.admin? || @vm.users.include?(current_user)
end
configuration = VirtualMachineConfig.find_by_name params[:id]

def info_params
params.require(:vm_info).permit(sudo_user_ids: [], non_sudo_user_ids: [])
end

def config_params
params.require(:vm_info).permit(:description, :ip, :dns)
redirect_to vms_path unless current_user.admin? || (configuration&.all_users&.include?(current_user))
end

def authenticate_root_user_or_admin
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
return unless @vm
configuration = VirtualMachineConfig.find_by_name params[:id]

redirect_to vms_path unless current_user.admin? || @vm.sudo_users.include?(current_user)
redirect_to vms_path unless current_user.admin? ||
configuration && (configuration.sudo_users.include?(current_user) || configuration.responsible_users.include?(current_user))
end

def not_enough_resources(exception)
redirect_back(fallback_location: root_path)
flash[:alert] = exception.message
end

def assign_vm
@vm = VSphere::VirtualMachine.find_by_name(params[:id])
redirect_back fallback_location: vms_path, alert: 'This Virtual machine could not be found!' if @vm.nil?
end

def assign_config
@vm_config = VirtualMachineConfig.find_by_name params[:id]
redirect_back fallback_location: vms_path, alert: 'This virtual machine could not be found in the database!' if @vm_config.nil?
end
end
12 changes: 6 additions & 6 deletions app/helpers/vms_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def status_for(vm)

def notify_changed_users(old_list, new_list, sudo_lists, vm_name)
removed_users = old_list - new_list
removed_users.each do |user_id|
User.find(user_id).notify('Sudo rights revoked', "Your sudo rights on VM '#{vm_name}' have been revoked.") if sudo_lists
User.find(user_id).notify('User rights revoked', "Your user rights on VM '#{vm_name}' have been revoked.") unless sudo_lists
removed_users.each do |user|
user.notify('Sudo rights revoked', "Your sudo rights on VM '#{vm_name}' have been revoked.") if sudo_lists
user.notify('User rights revoked', "Your user rights on VM '#{vm_name}' have been revoked.") unless sudo_lists
end
added_users = new_list - old_list
added_users.each do |user_id|
User.find(user_id).notify('Sudo rights granted', "You have been made sudo user on VM '#{vm_name}'") if sudo_lists
User.find(user_id).notify('User rights granted', "You have been made user on VM '#{vm_name}'") unless sudo_lists
added_users.each do |user|
user.notify('Sudo rights granted', "You have been made sudo user on VM '#{vm_name}'") if sudo_lists
user.notify('User rights granted', "You have been made user on VM '#{vm_name}'") unless sudo_lists
end
end

Expand Down
Loading

0 comments on commit 3d8f62f

Please sign in to comment.