Skip to content

Commit

Permalink
Merge branch '510/remove_json_rest' of github.com:hpi-swt2/vm-portal …
Browse files Browse the repository at this point in the history
…into 510/remove_json_rest
  • Loading branch information
chrisma committed Apr 25, 2019
2 parents eaabb27 + 9c0ee3b commit 9bf69c4
Show file tree
Hide file tree
Showing 23 changed files with 353 additions and 249 deletions.
8 changes: 6 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ gem 'omniauth_openid_connect' # https://github.com/m0n9oose/omniauth_openid_conn
gem 'pundit' # https://github.com/varvet/pundit
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false # https://github.com/Shopify/bootsnap
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2' # https://github.com/rails/coffee-rails
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
Expand Down Expand Up @@ -122,6 +120,12 @@ group :development do
gem 'binding_of_caller'
# Continous execution of unit tests
gem 'guard' # https://github.com/guard/guard
# Benchmarking
# Supporting gem for Rails Panel (Google Chrome extension for Rails development)
gem 'meta_request' # https://github.com/dejan/rails_panel
# code profiler for MRI Ruby
gem 'ruby-prof' # https://github.com/ruby-prof/ruby-prof

end

group :test do
Expand Down
18 changes: 10 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ GEM
activemodel (>= 5.0)
builder (3.2.3)
byebug (10.0.2)
callsite (0.0.11)
capybara (3.13.2)
addressable
mini_mime (>= 0.1.3)
Expand All @@ -101,13 +102,6 @@ GEM
xpath (~> 3.2)
clipboard-rails (1.7.1)
coderay (1.1.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.1.4)
coveralls (0.8.22)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -181,6 +175,10 @@ GEM
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
meta_request (0.6.0)
callsite (~> 0.0, >= 0.0.11)
rack-contrib (>= 1.1, < 3)
railties (>= 3.0.0, < 6)
method_source (0.9.2)
mimemagic (0.3.3)
mina (1.2.3)
Expand Down Expand Up @@ -238,6 +236,8 @@ GEM
pundit (2.0.1)
activesupport (>= 3.0.0)
rack (2.0.6)
rack-contrib (2.1.0)
rack (~> 2.0)
rack-oauth2 (1.9.3)
activesupport
attr_required
Expand Down Expand Up @@ -315,6 +315,7 @@ GEM
unicode-display_width (~> 1.4.0)
rubocop-rspec (1.32.0)
rubocop (>= 0.60.0)
ruby-prof (0.17.0)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
sassc (2.0.0)
Expand Down Expand Up @@ -405,7 +406,6 @@ DEPENDENCIES
byebug
capybara (>= 2.15)
clipboard-rails
coffee-rails (~> 4.2)
coveralls
devise
devise-bootstrap-views (~> 1.0)
Expand All @@ -419,6 +419,7 @@ DEPENDENCIES
jquery-datatables
jquery-rails
listen (>= 3.0.5, < 3.2)
meta_request
mina
mina-logs
mina-multistage
Expand All @@ -436,6 +437,7 @@ DEPENDENCIES
rspec-rails (~> 3.8)
rubocop
rubocop-rspec
ruby-prof
sassc-rails
select2-rails
shoulda-matchers (= 4.0.0.rc1)
Expand Down
28 changes: 20 additions & 8 deletions app/api/v_sphere/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ class Folder
# see: https://code.vmware.com/apis/196/vsphere#/doc/vim.Folder.html#createFolder
VSPHERE_FOLDER_NAME_CHARACTER_LIMIT = 79

def initialize(rbvmomi_folder)
def initialize(rbvmomi_folder, parent: nil, name: nil)
@folder = rbvmomi_folder
@parent = parent
@folder_name = name
end

def parent
@folder.parent
def parent(lookup: true)
if @parent.nil? && lookup
parent = @folder.parent
@parent = parent.nil? ? nil : VSphere::Folder.new(parent)
else
@parent
end
end

def subfolders(recursive: false)
folders = @folder.children.select { |folder_entry| folder_entry.is_a? RbVmomi::VIM::Folder }.map do |each|
Folder.new each
Folder.new each, parent: self
end

folders += folders.flat_map { |each| each.subfolders recursive: true } if recursive
Expand All @@ -30,7 +37,7 @@ def subfolders(recursive: false)

def vms(recursive: true)
vms = @folder.children.select { |folder_entry| folder_entry.is_a? RbVmomi::VIM::VirtualMachine }.map do |each|
VSphere::VirtualMachine.new each
VSphere::VirtualMachine.new each, folder: self
end

vms += subfolders.flat_map(&:vms) if recursive
Expand All @@ -53,14 +60,14 @@ def find_vm(name, recursive: true)
end

def name
@folder.name
@folder_name ||= @folder.name
end

# Ensure that a subfolder exists and return it
# folder_name is a string with the name of the subfolder
def ensure_subfolder(folder_name)
subfolder = subfolders.find { |each| each.name == folder_name }
subfolder || VSphere::Folder.new(@folder.CreateFolder(name: folder_name))
subfolder || VSphere::Folder.new(@folder.CreateFolder(name: folder_name), parent: self, name: folder_name)
end

# Ensure that the path relative to this folder is a valid folder and return it
Expand All @@ -72,6 +79,7 @@ def ensure_subfolder_by_path(path)
end

def move_here(folder_entry)
folder_entry.parent_folder = self
managed_entry = folder_entry.instance_exec { managed_folder_entry }
@folder.MoveIntoFolder_Task(list: [managed_entry]).wait_for_completion
end
Expand All @@ -83,7 +91,11 @@ def create_vm(cpu, ram, capacity, name, cluster)

vm_config = creation_config(cpu, ram, capacity, add_prefix(name), cluster.networks.first)
vm = @folder.CreateVM_Task(config: vm_config, pool: cluster.resource_pool).wait_for_completion
VSphere::VirtualMachine.new vm
VSphere::VirtualMachine.new vm, folder: self
end

def eql?(other)
(other.is_a? VSphere::Folder) && other.name == name
end

private
Expand Down
58 changes: 43 additions & 15 deletions app/api/v_sphere/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,21 @@ def self.user_vms(user)
vms
end

def initialize(rbvmomi_vm)
def initialize(rbvmomi_vm, folder: nil, name: nil)
@vm = rbvmomi_vm
@folder = folder
@name = name
end

# handles name format YYYYMMDD_vm-name
def name
if /^\d{8}_vm-/.match? @vm.name
@vm.name[12..-1]
return @name unless @name.nil?

@vsphere_name ||= @vm.name
@name = if /^\d{8}_vm-/.match? @vsphere_name
@vsphere_name[12..-1]
else
@vm.name
@vsphere_name
end
end

Expand All @@ -112,11 +117,15 @@ def full_path
end

def parent_folder
root_folder.subfolders(recursive: true).find do |folder|
@folder ||= root_folder.subfolders(recursive: true).find do |folder|
folder.vms(recursive: false).include? self
end
end

def parent_folder=(folder)
@folder = folder
end

# Guest OS communication
def vm_ware_tools?
tool_status = @vm&.guest&.toolsStatus
Expand Down Expand Up @@ -147,11 +156,11 @@ def reboot_guest_os
# We do not provide a power_state? method which just returns a boolean, because vSphere can internally handle
# more than just two power states and we might later need to respond to more states than just two
def powered_on?
@vm.summary.runtime.powerState == 'poweredOn'
summary.runtime.powerState == 'poweredOn'
end

def powered_off?
@vm.summary.runtime.powerState == 'poweredOff'
summary.runtime.powerState == 'poweredOff'
end

# Power state
Expand All @@ -172,15 +181,15 @@ def change_power_state
end

# Archiving
# The archiving process actually just moves the VM into different folders to communicate their state
# The archiving process actually pending_revivings_folder.vms.any? { |vm| vm.equal? self }just moves the VM into different folders to communicate their state
# Therefore we can check those folders to receive the current VM state
# Archiving then moves a VM into the corresponding folder
def pending_archivation?
pending_archivation_folder.vms.any? { |vm| vm.equal? self }
has_ancestor_folder 'Pending archivings'
end

def archived?
archived_folder.vms.any? { |vm| vm.equal? self }
has_ancestor_folder 'Archived VMs'
end

def set_pending_archivation
Expand Down Expand Up @@ -212,7 +221,7 @@ def set_archived

# Reviving
def pending_reviving?
pending_revivings_folder.vms.any? { |vm| vm.equal? self }
has_ancestor_folder 'Pending revivings'
end

def set_pending_reviving
Expand All @@ -229,7 +238,11 @@ def set_revived
# Config methods
# All the properties that HART saves internally
def config
@config ||= VirtualMachineConfig.find_by_name name
unless @searched_config
@config = VirtualMachineConfig.find_by_name name
@searched_config = true
end
@config
end

def ensure_config
Expand Down Expand Up @@ -266,11 +279,11 @@ def project

# Information about the vm
def boot_time
@vm.summary.runtime.bootTime
summary.runtime.bootTime
end

def summary
@vm.summary
@summary ||= @vm.summary
end

def macs
Expand All @@ -290,7 +303,7 @@ def guest_heartbeat_status
end

def host_name
@vm.summary.runtime.host.name
summary.runtime.host.name
end

def active?
Expand Down Expand Up @@ -357,6 +370,21 @@ def archivation_request
ArchivationRequest.find_by_name(name)
end

def has_ancestor_folder(ancestor_folder_name)
folder = parent_folder
return false if folder.nil?

until folder.name == ancestor_folder_name
folder = folder.parent lookup: false

if folder.nil?
return false
end
end

true
end

def managed_folder_entry
@vm
end
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// Any JavaScript file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
Expand Down
13 changes: 0 additions & 13 deletions app/assets/javascripts/notifications.coffee

This file was deleted.

15 changes: 15 additions & 0 deletions app/assets/javascripts/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var check_notifications = function () {
$.ajax({
url: "/notifications/has_any"
}).done(function (response) {
if (response.has_unread_notifications) {
$('#notification-alert').show();
}
});
};

$(document).on('turbolinks:load', function () {
check_notifications();
setInterval(check_notifications, 60000);
});

4 changes: 3 additions & 1 deletion app/controllers/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def index
filter_vm_categories current_user unless current_user.admin?
end

def show; end
def show
@vm_request = Request.find_by_name params[:id]
end

def edit_config
@vm.ensure_config
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/vms_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def status_for(vm)
end
end

def status_color(vm)
status = vm.status
if status == :archived
'bg-secondary'
else
status == :online ? 'bg-success' : 'bg-danger'
end
end

def notify_changed_users(old_list, new_list, sudo_lists, vm_name)
removed_users = old_list - new_list
removed_users.each do |user|
Expand Down
4 changes: 4 additions & 0 deletions app/models/app_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def self.instance
@instance
end

def github_base_file_url
"https://github.com/#{github_user_name}/#{git_repository_name}/blob/#{git_branch}"
end

private

def apply_settings
Expand Down

0 comments on commit 9bf69c4

Please sign in to comment.