Module: Fog::Proxmox::CpuHelper

Defined in:
lib/fog/proxmox/helpers/cpu_helper.rb

Overview

module Cpu mixins

Constant Summary collapse

CPU_REGEXP =
/(\bcputype=)?(?<cputype>[\w-]+),?(\bflags=)?(?<flags>[[+-][\w-]+;?]*)/
FLAGS =
{ spectre: 'spec-ctrl', pcid: 'pcid', ssbd: 'ssbd', ibpb: 'ibpb', virt_ssbd: 'virt-ssbd',
amd_ssbd: 'amd-ssbd', amd_no_ssb: 'amd-no-ssb', md_clear: 'md-clear', pdpe1gb: 'pdpe1gb', hv_tlbflush: 'hv-tlbflush', aes: 'aes', hv_evmcs: 'hv-evmcs' }

Class Method Summary collapse

Class Method Details

.extract(cpu, name) ⇒ Object



31
32
33
34
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 31

def self.extract(cpu, name)
  captures_h = cpu ? CPU_REGEXP.match(cpu.to_s) : { cputype: '', flags: '' }
  captures_h[name]
end

.extract_cputype(cpu) ⇒ Object



36
37
38
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 36

def self.extract_cputype(cpu)
  extract(cpu, :cputype)
end

.extract_flags(cpu) ⇒ Object



40
41
42
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 40

def self.extract_flags(cpu)
  extract(cpu, :flags)
end

.flag_value(cpu, flag_key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 44

def self.flag_value(cpu, flag_key)
  flag_value = '0'
  raw_values = extract_flags(cpu).split(';').select { |flag| ['+' + flag_key, '-' + flag_key].include?(flag) }
  unless raw_values.empty?
    flag_value = if raw_values[0].start_with?('+')
                   '+1'
                 else
                   raw_values[0].start_with?('-') ? '-1' : '0'
                 end
  end
  flag_value
end

.flagsObject



27
28
29
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 27

def self.flags
  FLAGS
end

.flatten(cpu_h) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 70

def self.flatten(cpu_h)
  return '' unless cpu_h['cpu_type']

  cpu_type = "cputype=#{cpu_h['cpu_type']}"
  num_flags = 0
  FLAGS.each_key { |flag_key| num_flags += 1 if hash_has_no_default_flag?(cpu_h, flag_key.to_s) }
  cpu_type += ',flags=' if num_flags > 0
  flags_with_no_default_value = FLAGS.select do |flag_key, _flag_value|
    hash_has_no_default_flag?(cpu_h, flag_key.to_s)
  end
  flags_with_no_default_value.each_with_index do |(flag_key, flag_value), index|
    cpu_type += hash_flag(cpu_h, flag_key.to_s) + flag_value if hash_has_no_default_flag?(cpu_h, flag_key.to_s)
    cpu_type += ';' if num_flags > index + 1
  end
  cpu_type
end

.hash_flag(cpu_h, flag_name) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 61

def self.hash_flag(cpu_h, flag_name)
  flag = ''
  if cpu_h.key?(flag_name)
    flag = '+' if cpu_h[flag_name] == '+1'
    flag = '-' if cpu_h[flag_name] == '-1'
  end
  flag
end

.hash_has_no_default_flag?(cpu_h, flag_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fog/proxmox/helpers/cpu_helper.rb', line 57

def self.hash_has_no_default_flag?(cpu_h, flag_name)
  cpu_h.key?(flag_name) && ['-1', '+1'].include?(cpu_h[flag_name])
end