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/models/app_setting.rb b/app/models/app_setting.rb index c1ded53e..4db78c00 100644 --- a/app/models/app_setting.rb +++ b/app/models/app_setting.rb @@ -16,13 +16,19 @@ class AppSetting < ApplicationRecord after_commit :apply_settings, on: :update + def self.clear_cache + @instance = nil + end + def self.instance - find(1) + @instance = find(1) if @instance.nil? + @instance end private def apply_settings + self.class.clear_cache apply_mail_settings GitHelper.reset end diff --git a/app/models/server.rb b/app/models/server.rb index c1f307f2..6737e10f 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true +# Part of Ruby stdlib, used for IP regexes +# https://www.rubydoc.info/stdlib/resolv/Resolv +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 +16,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 diff --git a/app/views/vms/show.html.erb b/app/views/vms/show.html.erb index 04847148..efc2b3c4 100644 --- a/app/views/vms/show.html.erb +++ b/app/views/vms/show.html.erb @@ -7,7 +7,13 @@ end %>