Class: Elevage::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/elevage/environment.rb

Overview

Environment class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Environment

rubocop:disable LineLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elevage/environment.rb', line 18

def initialize(env)
  # Confirm environment has been defined in the platform
  platform = Elevage::Platform.new
  fail(IOError, ERR[:env_not_defined]) unless platform.environments.include?(env)
  # Confirm environment file exists
  envfile = ENV_FOLDER + env.to_s + '.yml'
  fail(IOError, ERR[:no_env_file]) unless env_file_exists?(envfile)
  # Build environment hash from environment and platform defintion files
  environment = build_env(env, YAML.load_file(envfile).fetch('environment'), platform)
  # Populate class variables
  @name = env
  @vcenter = environment['vcenter']
  @components = environment['components']
  @nodenameconvention = platform.nodenameconvention
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



14
15
16
# File 'lib/elevage/environment.rb', line 14

def components
  @components
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/elevage/environment.rb', line 12

def name
  @name
end

#nodenameconventionObject

Returns the value of attribute nodenameconvention.



15
16
17
# File 'lib/elevage/environment.rb', line 15

def nodenameconvention
  @nodenameconvention
end

#vcenterObject

Returns the value of attribute vcenter.



13
14
15
# File 'lib/elevage/environment.rb', line 13

def vcenter
  @vcenter
end

Instance Method Details

#healthy?Boolean

rubocop:disable MethodLength, LineLength, CyclomaticComplexity, PerceivedComplexity

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elevage/environment.rb', line 50

def healthy?
  platform = Elevage::Platform.new
  health = ''
  health += MSG[:invalid_env_vcenter] if @vcenter.nil?
  @components.each do |component, v|
    health += MSG[:invalid_env_network] if v['network'].nil?
    health += MSG[:invalid_env_count] unless (0..POOL_LIMIT).member?(v['count'])
    health += MSG[:invalid_env_compute] if v['compute'].nil?
    health += MSG[:invalid_env_ip] if v['count'] != v['addresses'].size
    if v['addresses'].nil?
      health += MSG[:invalid_env_ip]
    else
      v['addresses'].each { |ip| health += MSG[:invalid_env_ip] unless Resolv::IPv4::Regex.match(ip) }
    end
    health += MSG[:invalid_env_tier] unless platform.tiers.include?(v['tier'])
    health += MSG[:invalid_env_image] if v['image'].nil?
    health += MSG[:invalid_env_port] unless v['port'].is_a?(Integer) || v['port'].nil?
    health += MSG[:invalid_env_runlist] if v['runlist'].nil? || v['runlist'].empty?
    health += MSG[:invalid_env_componentrole] unless v['componentrole'].include?('#') if v['componentrole']
    health += MSG[:env_component_mismatch] unless platform.components.include?(component)
  end
  health += MSG[:env_component_mismatch] unless platform.components.size == @components.size
  if health.length > 0
    puts health + "\n#{health.lines.count} environment offense(s) detected"
    false
  else
    true
  end
end

#list_nodesObject

Public: Environment class method Returns multiline string = IP, fqdn, runlist



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/elevage/environment.rb', line 37

def list_nodes
  nodes =  @vcenter['destfolder'].to_s + "\n"
  @components.each do |component, _config|
    (1..@components[component]['count']).each do |i|
      nodes += @components[component]['addresses'][i - 1].ljust(18, ' ') +
               node_name(component, i) + @vcenter['domain'] + '   ' +
               @components[component]['runlist'].to_s + "\n"
    end
  end
  nodes
end

#provision(type: all, tier: nil, component: nil, instance: nil, options: nil) ⇒ Object

Public: method to request provisioning of all or a portion of the environment rubocop:disable MethodLength, LineLength, CyclomaticComplexity, PerceivedComplexity



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/elevage/environment.rb', line 92

def provision(type: all, tier: nil, component: nil, instance: nil, options: nil)
  # Create the ProvisionerRunQueue to batch up our tasks
  runner = ProvisionerRunQueue.new

  # Modify behavior for dry-run (no concurrency)
  if !options['dry-run']
    runner.max_concurrent = options[:concurrency]
  else
    puts "Dry run requested, forcing concurrency to '1'."
    runner.max_concurrent = 1
  end

  @components.each do |component_name, component_data|
    next unless type.eql?(:all) || component_data['tier'].match(/#{tier}/i) && component_name.match(/#{component}/i)

    1.upto(component_data['addresses'].count) do |component_instance|
      next unless instance == component_instance || instance.nil?

      instance_name = node_name(component_name, component_instance)

      # Create the Provisioner
      provisioner = Elevage::Provisioner.new(instance_name, component_data, component_instance, self, options)

      # Add it to the queue
      runner.provisioners << provisioner

    end
  end

  runner.to_s if options['dry-run']

  # Process the queue
  runner.run
end

#to_sObject

Public: basic class puts string output



82
83
84
85
86
87
# File 'lib/elevage/environment.rb', line 82

def to_s
  puts @name
  puts @vcenter.to_yaml
  puts @components.to_yaml
  puts @nodenameconvention.to_yaml
end