Class: Kitchen::Provisioner::Nodes

Inherits:
ChefZero
  • Object
show all
Defined in:
lib/kitchen/provisioner/nodes.rb

Overview

Nodes provisioner for Kitchen.

Author:

Instance Method Summary collapse

Instance Method Details

#active_ips(transport, state) ⇒ Object


136
137
138
139
140
141
142
143
144
# File 'lib/kitchen/provisioner/nodes.rb', line 136

def active_ips(transport, state)
  # inject creds into state for legacy drivers
  [:username, :password].each do |prop|
    state[prop] = instance.driver[prop] if instance.driver[prop]
  end
  ips = Finder.for_transport(transport, state).find_ips
  raise 'Unable to retrieve IPs' if ips.empty?
  ips
end

#chef_environmentObject


89
90
91
92
93
94
95
# File 'lib/kitchen/provisioner/nodes.rb', line 89

def chef_environment
  env = '_default'
  if config[:client_rb] && config[:client_rb][:environment]
    env = config[:client_rb][:environment]
  end
  env
end

#create_nodeObject


39
40
41
42
43
44
45
# File 'lib/kitchen/provisioner/nodes.rb', line 39

def create_node
  FileUtils.mkdir_p(node_dir) unless Dir.exist?(node_dir)
  template_to_write = node_template
  File.open(node_file, 'w') do |out|
    out << JSON.pretty_generate(template_to_write)
  end
end

#create_sandboxObject


32
33
34
35
36
37
# File 'lib/kitchen/provisioner/nodes.rb', line 32

def create_sandbox
  FileUtils.rm(node_file) if File.exist?(node_file)
  create_node
ensure
  super
end

#fqdnObject


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kitchen/provisioner/nodes.rb', line 63

def fqdn
  state = state_file
  begin
    [:username, :password].each do |prop|
      state[prop] = instance.driver[prop] if instance.driver[prop]
    end
    Finder.for_transport(instance.transport, state).find_fqdn
  rescue
    nil
  end
end

#get_reachable_guest_address(state) ⇒ Object


122
123
124
125
126
127
# File 'lib/kitchen/provisioner/nodes.rb', line 122

def get_reachable_guest_address(state)
  active_ips(instance.transport, state).each do |address|
    next if address == '127.0.0.1'
    return address if reachable?(address)
  end
end

#ipaddressObject


54
55
56
57
58
59
60
61
# File 'lib/kitchen/provisioner/nodes.rb', line 54

def ipaddress
  state = state_file

  if %w(127.0.0.1 localhost).include?(state[:hostname])
    return get_reachable_guest_address(state)
  end
  state[:hostname]
end

#node_dirObject

rubocop:enable Metrics/AbcSize


114
115
116
# File 'lib/kitchen/provisioner/nodes.rb', line 114

def node_dir
  config[:nodes_path] || File.join(config[:test_base_path], 'nodes')
end

#node_fileObject


118
119
120
# File 'lib/kitchen/provisioner/nodes.rb', line 118

def node_file
  File.join(node_dir, "#{instance.name}.json")
end

#node_templateObject

rubocop:disable Metrics/AbcSize


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/kitchen/provisioner/nodes.rb', line 98

def node_template
  {
    id: instance.name,
    chef_environment: chef_environment,
    automatic: {
      ipaddress: ipaddress,
      platform: instance.platform.name.split('-')[0].downcase,
      fqdn: fqdn,
      recipes: recipes
    },
    normal: config[:attributes],
    run_list: config[:run_list]
  }
end

#reachable?(address) ⇒ Boolean

Returns:

  • (Boolean)

129
130
131
132
133
134
# File 'lib/kitchen/provisioner/nodes.rb', line 129

def reachable?(address)
  Net::Ping::External.new.ping(address) ||
    Net::Ping::TCP.new(address, 5985).ping ||
    Net::Ping::TCP.new(address, 5986).ping ||
    Net::Ping::TCP.new(address, 22).ping
end

#recipesObject


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kitchen/provisioner/nodes.rb', line 75

def recipes
  rl = config[:run_list].map do |item|
    ::Chef::RunList::RunListItem.new item
  end
  rle = RunListExpansionFromKitchen.new(
    chef_environment,
    rl,
    nil,
    config[:roles_path]
  )
  rle.expand
  rle.recipes
end

#state_fileObject


47
48
49
50
51
52
# File 'lib/kitchen/provisioner/nodes.rb', line 47

def state_file
  @state_file ||= Kitchen::StateFile.new(
    config[:kitchen_root],
    instance.name
  ).read
end