Module: IronChef::Plugin

Defined in:
lib/iron_chef/plugin.rb

Instance Method Summary collapse

Instance Method Details

#applyObject



212
213
214
# File 'lib/iron_chef/plugin.rb', line 212

def apply
  chef(:apply)
end

#cookbooksObject



140
141
142
# File 'lib/iron_chef/plugin.rb', line 140

def cookbooks
  Array(fetch(:chef_cookbooks) { (:chef_cookbooks).select { |path| File.exist?(path) } })
end

#dump_nodes_jsonObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/iron_chef/plugin.rb', line 108

def dump_nodes_json

  nodes_available = nodes_list

  nodes_location  = fetch_chef_nodes_dir

  FileUtils.mkdir_p("./tmp/#{nodes_location}")

  nodes_available.each do |node_name|
    node_config = node(node_name)

    node_dna = {
      :run_list => node_run_list(node_config)
    }.merge(node_config['json'])

    node_dna_json_file = "./tmp/#{nodes_location}/#{node_config['node_name']}.json"
    File.open(node_dna_json_file, 'w') do |f|
      f.puts node_dna.to_json
    end

    puts "Created #{node_dna_json_file}"
  end
end

#env_nodes_listObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/iron_chef/plugin.rb', line 37

def env_nodes_list
  nodes_location  = fetch_chef_nodes_dir
  nodes_available = Dir.glob("./#{nodes_location}/**/*.yml").map { |f| File.basename(f, '.*') }
  env_nodes_available = []
  nodes_available.each do |node_name|
    node_config = node(node_name)
    if node_config['json']
      if node_config['json']['chef_environment']
        node_env = node_config['json']['chef_environment']
        if "#{chef_environment}" == "#{node_env}"
          env_nodes_available << node_config['node_name']
        end
      else
        puts "Node '#{node_config['node_name']}' ['json']['chef_environment'] attribute is empty or missing."
      end
    else
      puts "Node '#{node_config['node_name']}' ['json'] attribute is empty or missing."
    end
  end

  if env_nodes_available.size == 0
    abort "No nodes found for chef environment '#{chef_environment}'. Known nodes are #{nodes_available.join(', ')}."
  end
  env_nodes_available.sort
end

#fetch_chef_environment_dirObject



4
5
6
# File 'lib/iron_chef/plugin.rb', line 4

def fetch_chef_environment_dir
  fetch(:chef_environment_dir, 'environments')
end

#fetch_chef_nodes_dirObject



8
9
10
# File 'lib/iron_chef/plugin.rb', line 8

def fetch_chef_nodes_dir
  fetch(:chef_nodes_dir, 'nodes')
end

#find_node(node_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/iron_chef/plugin.rb', line 12

def find_node(node_path)
  raise "Node YAML file #{node_path} not found" unless node_path && File.exists?(node_path)

  node_name   = File.basename(node_path).gsub('.yml','')
  node_config = IronChef::ERB.read_erb_yaml(node_path)

  node_config['node_name'] = node_name

  node_config
end

#lockObject



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/iron_chef/plugin.rb', line 216

def lock
  if should_lock?
    run <<-CHECK_LOCK
if [ -f #{chef_lock_file} ]; then
stat -c "#{red_text("Chef in progress, #{chef_lock_file} owned by %U since %x")}" #{chef_lock_file} >&2;
exit 1;
fi
    CHECK_LOCK

    run "touch #{chef_lock_file}"
  end
end

#node(node_name) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/iron_chef/plugin.rb', line 23

def node(node_name)
  nodes_location  = fetch_chef_nodes_dir
  unless node_path = Dir.glob("./#{nodes_location}/**/#{node_name}.yml")[0]
    abort "Node '#{node_name}' is unknown. Known nodes are #{nodes_list.join(', ')}."
  end
  find_node(node_path)
end

#node_run_list(node_config) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/iron_chef/plugin.rb', line 132

def node_run_list(node_config)
  run_list = []
  run_list += node_config['roles'].map   { |r| "role[#{r}]"   } if node_config['roles']
  run_list += node_config['recipes'].map { |r| "recipe[#{r}]" } if node_config['recipes']

  run_list
end

#nodes_listObject



31
32
33
34
35
# File 'lib/iron_chef/plugin.rb', line 31

def nodes_list
  nodes_location  = fetch_chef_nodes_dir
  nodes_available = Dir.glob("./#{nodes_location}/**/*.yml").map { |f| File.basename(f, '.*') }
  nodes_available.sort
end

#prepareObject



199
200
201
202
203
204
205
206
# File 'lib/iron_chef/plugin.rb', line 199

def prepare
  run "mkdir -p #{chef_destination}"
  release_chef_client_lock
  run "chown -R $USER: #{chef_destination}"
  run "mkdir -p #{chef_file_cache_dir}"
rescue
  puts cyan_text('Try running `cap node_name chef:clear` to clear chef folders for this node.')
end

#prepare_sudo_cmd(cmd) ⇒ Object



237
238
239
# File 'lib/iron_chef/plugin.rb', line 237

def prepare_sudo_cmd(cmd)
  user == 'root' ? cmd : "sudo -- sh -c '#{cmd}'"
end

#release_chef_client_lockObject



233
234
235
# File 'lib/iron_chef/plugin.rb', line 233

def release_chef_client_lock
  run prepare_sudo_cmd("rm -f #{chef_file_cache_dir}/chef-client-running.pid; true")
end

#rsyncObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/iron_chef/plugin.rb', line 160

def rsync
  IronChef::Util.thread_pool_size = chef_parallel_rsync_pool_size
  servers = IronChef::Util.optionally_async(find_servers_for_task(current_task), chef_parallel_rsync)
  overrides = {}
  overrides[:user] = fetch(:user, ENV['USER'])
  overrides[:port] = fetch(:port) if exists?(:port)

  failed_servers = servers.map do |server|
    rsync_cmd = IronChef::Rsync.command(
      chef_source,
      IronChef::Rsync.remote_address(server.user || fetch(:user, ENV['USER']), server.host, chef_destination),
      :delete => true,
      :excludes => chef_excludes,
      :ssh => ssh_options.merge(server.options[:ssh_options]||{}).merge(overrides)
    )
    logger.debug rsync_cmd

    server.host unless system rsync_cmd
  end.compact

  raise "rsync failed on #{failed_servers.join(',')}" if failed_servers.any?

  upload_chef_solo_config(servers)
end

#tasks_for_env(nodes_names) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/iron_chef/plugin.rb', line 63

def tasks_for_env(nodes_names)

  servers = []

  nodes_names.each do |node_name|
    node_config = node(node_name)
    if node_config['server']
      if node_config['server']['host']

        servers << [node_config['server']['host'], node_config['node_name']]

        task(node_config['node_name']) do
          role :server, node_config['server']['host'], { node_name: node_config['node_name'] }
        end
      else
        puts "Node '#{node_config['node_name']}' ['server']['host'] attribute is empty or missing."
      end
    else
      puts "Node '#{node_config['node_name']}' ['server'] attribute is empty or missing."
    end
  end

  if nodes_names
    task(:all_nodes) do
      servers.each do |server|
        host_name, node_name = server
        role :server, host_name, { node_name: node_name }
      end
    end
  end

end

#unlockObject



229
230
231
# File 'lib/iron_chef/plugin.rb', line 229

def unlock
  run prepare_sudo_cmd("rm -f #{chef_lock_file}; true") if should_lock?
end

#upload_chef_solo_config(servers) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/iron_chef/plugin.rb', line 185

def upload_chef_solo_config(servers)

  servers.map do |server|
    # allows use to use node aliases for Iron Chef
    node_name = server.options[:node_name]

    raise "upload_chef_solo_config failed on #{server.host} with missing node name on role" unless node_name
    upload_node_json(node_name)

    upload_solo_rb
  end

end

#upload_node_json(node_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/iron_chef/plugin.rb', line 96

def upload_node_json(node_name)

  node_config = node(node_name)

  node_dna = {
    :run_list => node_run_list(node_config)
  }.merge(node_config['json'])

  put node_dna.to_json, "#{chef_destination}/node.json", :via => :scp

end

#upload_solo_rbObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/iron_chef/plugin.rb', line 144

def upload_solo_rb
  cookbook_paths = cookbooks.map { |c| "File.join(chef_root, #{c.to_s.inspect})" }.join(', ')
  solo_rb = <<-RUBY
  solo true
  chef_root = File.expand_path(File.dirname(__FILE__))
  file_cache_path '#{chef_file_cache_dir}'
  cookbook_path   [ #{cookbook_paths} ]
  role_path       File.join(chef_root, 'roles')
  data_bag_path   File.join(chef_root, 'data_bags')
  json_attribs    File.join(chef_root, 'node.json')
  log_level "#{chef_log_level}".to_sym
  ssl_verify_mode "#{chef_ssl_verify_mode}".to_sym
  RUBY
  put solo_rb, "#{chef_destination}/solo.rb", :via => :scp
end

#why_runObject



208
209
210
# File 'lib/iron_chef/plugin.rb', line 208

def why_run
  chef(:why_run)
end