From 9327fbc722b1ffe82f11e04d10e6e2dc15c40f8b Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:05:07 +0100 Subject: [PATCH 01/10] Don't use custom regex for server validation --- app/models/server.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/models/server.rb b/app/models/server.rb index c1f307f2..a4b9cf18 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'resolv' + class Server < ApplicationRecord belongs_to :responsible, class_name: :User validates :name, :cpu_cores, :ram_gb, :storage_gb, :mac_address, :fqdn, :responsible_id, presence: true @@ -12,21 +14,15 @@ class Server < ApplicationRecord validates :storage_gb, numericality: { greater_than: 0 } validates :ipv4_address, format: { - with: /(^$)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/, - message: 'Not a valid IPv4 address' + with: Resolv::IPv4::Regex, + message: 'is not a valid IPv4 address' } validates :ipv6_address, format: { - with: /(^$)|(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:) - {1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4} - (:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2} - (:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:) - |fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1} - [0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]| - (2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/x, - message: 'Not a valid IPv6 address' + with: Resolv::IPv6::Regex, + message: 'is not a valid IPv6 address' } validates :mac_address, format: { with: /([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/, - message: 'Not a valid MAC address' + message: 'is not a valid MAC address' } end From d8e6f7a05387d7a1ef569bee3f928e481ac62d91 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:05:36 +0100 Subject: [PATCH 02/10] Cache AppSettings.instance --- app/controllers/app_settings_controller.rb | 1 + app/models/app_setting.rb | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/app_settings_controller.rb b/app/controllers/app_settings_controller.rb index 7ef88cf9..ba77c0f3 100644 --- a/app/controllers/app_settings_controller.rb +++ b/app/controllers/app_settings_controller.rb @@ -12,6 +12,7 @@ def edit; end def update respond_to do |format| if @app_setting.update(app_setting_params) + AppSetting.refresh_instance format.html { redirect_to edit_app_setting_path(@app_setting), notice: 'HART Settings were successfully updated.' } format.json { render :edit, status: :ok, location: @app_setting } else diff --git a/app/models/app_setting.rb b/app/models/app_setting.rb index c1ded53e..18d57e29 100644 --- a/app/models/app_setting.rb +++ b/app/models/app_setting.rb @@ -16,8 +16,13 @@ class AppSetting < ApplicationRecord after_commit :apply_settings, on: :update + def refresh_instance + @instance = nil + end + def self.instance - find(1) + @instance = find(1) if @instance.nil + @instance end private From ceeea72c8de98e63df3c5bba16266edac63f6b02 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:05:46 +0100 Subject: [PATCH 03/10] migrate database --- db/schema.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index ee0c1897..0d401e3c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -30,9 +30,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "vsphere_root_folder", default: "" - t.string "puppet_init_path" - t.string "puppet_classes_path" - t.string "puppet_nodes_path" + t.string "puppet_init_path", default: "/" + t.string "puppet_classes_path", default: "/Name" + t.string "puppet_nodes_path", default: "/Node" t.integer "min_cpu_cores" t.integer "max_cpu_cores" t.integer "max_ram_size" @@ -161,12 +161,12 @@ t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.integer "role" - t.string "first_name" - t.string "last_name" t.string "provider" t.string "uid" - t.integer "user_id" t.string "ssh_key" + t.string "first_name" + t.string "last_name" + t.integer "user_id" t.boolean "email_notifications", default: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true @@ -178,6 +178,8 @@ t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["request_id"], name: "index_users_assigned_to_requests_on_request_id" + t.index ["user_id"], name: "index_users_assigned_to_requests_on_user_id" end create_table "users_virtual_machine_configs", id: false, force: :cascade do |t| From a3662f3a5d4ce9609a995e945b646517bf297271 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:06:40 +0100 Subject: [PATCH 04/10] Add VMs path to the VMs show page --- app/api/v_sphere/folder.rb | 11 +++++++++-- app/api/v_sphere/virtual_machine.rb | 16 ++++++++++++++++ app/views/vms/show.html.erb | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/api/v_sphere/folder.rb b/app/api/v_sphere/folder.rb index 7fcd7e66..4358846b 100644 --- a/app/api/v_sphere/folder.rb +++ b/app/api/v_sphere/folder.rb @@ -15,10 +15,17 @@ def initialize(rbvmomi_folder) @folder = rbvmomi_folder end - def subfolders - @folder.children.select { |folder_entry| folder_entry.is_a? RbVmomi::VIM::Folder }.map do |each| + def parent + @folder.parent + end + + def subfolders(recursive: false) + folders = @folder.children.select { |folder_entry| folder_entry.is_a? RbVmomi::VIM::Folder }.map do |each| Folder.new each end + + folders += folders.flat_map { |each| each.subfolders recursive: true } if recursive + folders end def vms(recursive: true) diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index 9e1cde9a..3d8233d1 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -96,6 +96,22 @@ def name @vm.name end + def full_path + path = [name] + parent = parent_folder + until parent.nil? + path << parent.name + parent = parent.parent + end + path.reverse + end + + def parent_folder + root_folder.subfolders(recursive: true).find do |folder| + folder.vms(recursive: false).include? self + end + end + # Guest OS communication def vm_ware_tools? tool_status = @vm&.guest&.toolsStatus diff --git a/app/views/vms/show.html.erb b/app/views/vms/show.html.erb index 04847148..16478bf2 100644 --- a/app/views/vms/show.html.erb +++ b/app/views/vms/show.html.erb @@ -92,7 +92,7 @@ end %> @hardware = {'IP Address' => @vm.ip, 'DNS' => @vm.dns, - 'Heartbeat Status' => @vm.guest_heartbeat_status } + 'Heartbeat Status' => @vm.guest_heartbeat_status} %>
From dde1add8ca0b9039edbd89b20539f3601ec50e69 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:07:47 +0100 Subject: [PATCH 05/10] Fix AppSettings caching --- app/models/app_setting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/app_setting.rb b/app/models/app_setting.rb index 18d57e29..0ce9cb98 100644 --- a/app/models/app_setting.rb +++ b/app/models/app_setting.rb @@ -21,7 +21,7 @@ def refresh_instance end def self.instance - @instance = find(1) if @instance.nil + @instance = find(1) if @instance.nil? @instance end From 206a19bfedb5cc0af60f5c09c08bc87ff799d297 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:25:04 +0100 Subject: [PATCH 06/10] Move path at the top of the page because its too long for the Hardware section --- app/views/vms/show.html.erb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/views/vms/show.html.erb b/app/views/vms/show.html.erb index 16478bf2..efc2b3c4 100644 --- a/app/views/vms/show.html.erb +++ b/app/views/vms/show.html.erb @@ -7,7 +7,13 @@ end %>
-

<%= @vm.name unless @vm.nil? %>

+ + +

<%= @vm.name %>

<% os = @vm.summary&.config&.guestFullName %> <% unless os.nil? || os.downcase.include?('other') %>

<%= "(#{os})"%>

From d2b6d183491ce69b46ff63f43301926a7eeeae16 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:30:26 +0100 Subject: [PATCH 07/10] More caching fixes --- app/models/app_setting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/app_setting.rb b/app/models/app_setting.rb index 0ce9cb98..f5ca91a3 100644 --- a/app/models/app_setting.rb +++ b/app/models/app_setting.rb @@ -16,7 +16,7 @@ class AppSetting < ApplicationRecord after_commit :apply_settings, on: :update - def refresh_instance + def self.refresh_instance @instance = nil end From 75edb0de673e04ca76967644123065ea16bc3e45 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:35:14 +0100 Subject: [PATCH 08/10] Automatically refresh AppSettings instance on commit --- app/controllers/app_settings_controller.rb | 1 - app/models/app_setting.rb | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/app_settings_controller.rb b/app/controllers/app_settings_controller.rb index ba77c0f3..7ef88cf9 100644 --- a/app/controllers/app_settings_controller.rb +++ b/app/controllers/app_settings_controller.rb @@ -12,7 +12,6 @@ def edit; end def update respond_to do |format| if @app_setting.update(app_setting_params) - AppSetting.refresh_instance format.html { redirect_to edit_app_setting_path(@app_setting), notice: 'HART Settings were successfully updated.' } format.json { render :edit, status: :ok, location: @app_setting } else diff --git a/app/models/app_setting.rb b/app/models/app_setting.rb index f5ca91a3..25469a65 100644 --- a/app/models/app_setting.rb +++ b/app/models/app_setting.rb @@ -28,6 +28,7 @@ def self.instance private def apply_settings + self.class.refresh_instance apply_mail_settings GitHelper.reset end From 72a56f041977bbef2843909c766236c935466385 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Sat, 16 Mar 2019 18:40:31 +0100 Subject: [PATCH 09/10] Add parent to folder mocking --- spec/api/v_sphere_api_mocker.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/api/v_sphere_api_mocker.rb b/spec/api/v_sphere_api_mocker.rb index 21878cc4..f96d41ec 100644 --- a/spec/api/v_sphere_api_mocker.rb +++ b/spec/api/v_sphere_api_mocker.rb @@ -42,8 +42,9 @@ def vim_folder_mock(name, subfolders, vms, clusters) # rubocop:disable Metrics/A children = subfolders + vms + clusters children = extract_vim_objects children folder = double - allow(folder).to receive(:name).and_return(name) - allow(folder).to receive(:children).and_return(children) + allow(folder).to receive(:name).and_return name + allow(folder).to receive(:parent).and_return nil + allow(folder).to receive(:children).and_return children allow(folder).to receive(:is_a?).and_return false allow(folder).to receive(:is_a?).with(RbVmomi::VIM::Folder).and_return true allow(folder).to receive_message_chain(:MoveIntoFolder_Task, :wait_for_completion) From a289fcfe9ff51e5b4cda9cf14b39b9b3574b7144 Mon Sep 17 00:00:00 2001 From: Christoph Matthies Date: Mon, 18 Mar 2019 14:45:48 +0100 Subject: [PATCH 10/10] Fix(schema): Revert schema to prev. version. No db changes in PR. --- db/schema.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 0d401e3c..ee0c1897 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -30,9 +30,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "vsphere_root_folder", default: "" - t.string "puppet_init_path", default: "/" - t.string "puppet_classes_path", default: "/Name" - t.string "puppet_nodes_path", default: "/Node" + t.string "puppet_init_path" + t.string "puppet_classes_path" + t.string "puppet_nodes_path" t.integer "min_cpu_cores" t.integer "max_cpu_cores" t.integer "max_ram_size" @@ -161,12 +161,12 @@ 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 "provider" + t.string "uid" t.integer "user_id" + t.string "ssh_key" t.boolean "email_notifications", default: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true @@ -178,8 +178,6 @@ t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["request_id"], name: "index_users_assigned_to_requests_on_request_id" - t.index ["user_id"], name: "index_users_assigned_to_requests_on_user_id" end create_table "users_virtual_machine_configs", id: false, force: :cascade do |t|