Module: Cucumber::Chef::Utility

Included in:
Cucumber::Chef
Defined in:
lib/cucumber/chef/utility.rb

Instance Method Summary collapse

Instance Method Details

#artifacts_dirObject



122
123
124
125
126
# File 'lib/cucumber/chef/utility.rb', line 122

def artifacts_dir
  artifacts_dir = File.join(Cucumber::Chef.home_dir, Cucumber::Chef::Config.provider.to_s, "artifacts")
  FileUtils.mkdir_p(File.dirname(artifacts_dir))
  artifacts_dir
end

#boot(name = nil) ⇒ Object

BOOT



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/cucumber/chef/utility.rb', line 262

def boot(name=nil)
  if !in_chef_repo?
    message = "It does not look like you are inside a chef-repo!  Please relocate to one and execute your command again!"
    logger.fatal { message }
    raise message
  end
  name and logger.info { "loading #{name}" }
  logger.info { "boot(#{Cucumber::Chef.config_rb})" }
  Cucumber::Chef::Config.load
  Cucumber::Chef::Labfile.load(Cucumber::Chef.labfile)
end

#bootstrap_identityObject



185
186
187
188
189
# File 'lib/cucumber/chef/utility.rb', line 185

def bootstrap_identity
  bootstrap_identity = Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:identity_file]
  File.exists?(bootstrap_identity) && File.chmod(0400, bootstrap_identity)
  bootstrap_identity
end

#bootstrap_userObject

Bootstraping SSH Helpers



176
177
178
# File 'lib/cucumber/chef/utility.rb', line 176

def bootstrap_user
  Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:bootstrap_user]
end

#bootstrap_user_home_dirObject



180
181
182
183
# File 'lib/cucumber/chef/utility.rb', line 180

def bootstrap_user_home_dir
  user = Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:bootstrap_user]
  ((user == "root") ? "/root" : "/home/#{user}")
end

#build_command(name, *args) ⇒ Object



253
254
255
256
# File 'lib/cucumber/chef/utility.rb', line 253

def build_command(name, *args)
  executable = (Cucumber::Chef.locate(:file, "bin", name) rescue "/usr/bin/env #{name}")
  [executable, args].flatten.compact.join(" ")
end

#chef_identityObject



166
167
168
169
170
# File 'lib/cucumber/chef/utility.rb', line 166

def chef_identity
  chef_identity = File.join(Cucumber::Chef.home_dir, Cucumber::Chef::Config.provider.to_s, "#{chef_user}.pem")
  FileUtils.mkdir_p(File.dirname(chef_identity))
  chef_identity
end

#chef_pre_11Object



89
90
91
92
# File 'lib/cucumber/chef/utility.rb', line 89

def chef_pre_11
  return false if (Cucumber::Chef::Config.chef[:version].downcase == "latest")
  (Cucumber::Chef::Config.chef[:version].to_f < 11.0)
end

#chef_repoObject

Path Helpers



98
99
100
# File 'lib/cucumber/chef/utility.rb', line 98

def chef_repo
  (Cucumber::Chef.locate_parent(".chef") rescue nil)
end

#chef_userObject



162
163
164
# File 'lib/cucumber/chef/utility.rb', line 162

def chef_user
  Cucumber::Chef::Config.user
end

#config_rbObject



138
139
140
141
142
# File 'lib/cucumber/chef/utility.rb', line 138

def config_rb
  config_rb = File.join(Cucumber::Chef.home_dir, "config.rb")
  FileUtils.mkdir_p(File.dirname(config_rb))
  config_rb
end

#external_ipObject



83
84
85
# File 'lib/cucumber/chef/utility.rb', line 83

def external_ip
  %x(wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//').chomp.strip
end

#generate_do_not_edit_warning(message = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/cucumber/chef/utility.rb', line 71

def generate_do_not_edit_warning(message=nil)
  warning = Array.new
  warning << "#"
  warning << "# WARNING: Automatically generated file; DO NOT EDIT!"
  warning << [ "# Cucumber-Chef v#{Cucumber::Chef::VERSION}", message ].compact.join(" ")
  warning << "# Generated on #{Time.now.utc.to_s}"
  warning << "#"
  warning.join("\n")
end

#home_dirObject



114
115
116
117
118
# File 'lib/cucumber/chef/utility.rb', line 114

def home_dir
  home_dir = (ENV['CUCUMBER_CHEF_HOME'] || File.join(Cucumber::Chef.locate_parent(".chef"), ".cucumber-chef"))
  FileUtils.mkdir_p(File.dirname(home_dir))
  home_dir
end

#in_chef_repo?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/cucumber/chef/utility.rb', line 102

def in_chef_repo?
  ((chef_repo && File.exists?(chef_repo) && File.directory?(chef_repo)) ? true : false)
end

#is_rc?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/cucumber/chef/utility.rb', line 31

def is_rc?
  ((Cucumber::Chef::VERSION =~ /rc/) || (Cucumber::Chef::VERSION =~ /pre/))
end

#lab_hostname_fullObject



222
223
224
# File 'lib/cucumber/chef/utility.rb', line 222

def lab_hostname_full
  "#{lab_hostname_short}.#{Cucumber::Chef::Config.test_lab[:tld]}"
end

#lab_hostname_shortObject



218
219
220
# File 'lib/cucumber/chef/utility.rb', line 218

def lab_hostname_short
  Cucumber::Chef::Config.test_lab[:hostname]
end

#lab_identityObject



204
205
206
207
208
# File 'lib/cucumber/chef/utility.rb', line 204

def lab_identity
  lab_identity = File.join(Cucumber::Chef.home_dir, Cucumber::Chef::Config.provider.to_s, "id_rsa-#{lab_user}")
  File.exists?(lab_identity) && File.chmod(0400, lab_identity)
  lab_identity
end

#lab_ipObject



210
211
212
# File 'lib/cucumber/chef/utility.rb', line 210

def lab_ip
  Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:ssh][:lab_ip]
end

#lab_ssh_portObject



214
215
216
# File 'lib/cucumber/chef/utility.rb', line 214

def lab_ssh_port
  Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:ssh][:lab_port]
end

#lab_userObject

Test Lab SSH Helpers



195
196
197
# File 'lib/cucumber/chef/utility.rb', line 195

def lab_user
  Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:lab_user]
end

#lab_user_home_dirObject



199
200
201
202
# File 'lib/cucumber/chef/utility.rb', line 199

def lab_user_home_dir
  user = Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:lab_user]
  ((user == "root") ? "/root" : "/home/#{user}")
end

#labfileObject



146
147
148
149
150
# File 'lib/cucumber/chef/utility.rb', line 146

def labfile
  labfile = File.join(Cucumber::Chef.chef_repo, "Labfile")
  FileUtils.mkdir_p(File.dirname(labfile))
  labfile
end

#locate(type, *args) ⇒ Object

Raises:



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

def locate(type, *args)
  pwd = Dir.pwd.split(File::SEPARATOR)
  (pwd.length - 1).downto(0) do |i|
    candidate = File.join(pwd[0..i], args)
    case type
    when :file
      if (File.exists?(candidate) && !File.directory?(candidate))
        return File.expand_path(candidate)
      end
    when :directory
      if (File.exists?(candidate) && File.directory?(candidate))
        return File.expand_path(candidate)
      end
    when :any
      if File.exists?(candidate)
        return File.expand_path(candidate)
      end
    end
  end

  message = "Could not locate #{type} '#{File.join(args)}'."
  raise UtilityError, message
end

#locate_parent(child) ⇒ Object

Raises:



63
64
65
66
67
# File 'lib/cucumber/chef/utility.rb', line 63

def locate_parent(child)
  parent = (locate(:any, child).split(File::SEPARATOR) rescue nil)
  raise UtilityError, "Could not locate parent of '#{child}'." unless parent
  File.expand_path(File.join(parent[0..(parent.length - 2)]))
end

#log_fileObject



130
131
132
133
134
# File 'lib/cucumber/chef/utility.rb', line 130

def log_file
  log_file = File.join(Cucumber::Chef.home_dir, "cucumber-chef.log")
  FileUtils.mkdir_p(File.dirname(log_file))
  log_file
end

#loggerObject



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/cucumber/chef/utility.rb', line 276

def logger
  if (!defined?($logger) || $logger.nil?)
    $logger = ZTK::Logger.new(Cucumber::Chef.log_file)
    Cucumber::Chef.is_rc? and ($logger.level = ZTK::Logger::DEBUG)

    dependencies = {
      "cucumber_chef_version" => Cucumber::Chef::VERSION.inspect,
      "fog_version" => ::Fog::VERSION.inspect,
      "ruby_version" => RUBY_VERSION.inspect,
      "ruby_patchlevel" => RUBY_PATCHLEVEL.inspect,
      "ruby_platform" => RUBY_PLATFORM.inspect,
      "ztk_version" => ::ZTK::VERSION.inspect
    }
    if RUBY_VERSION >= "1.9"
      dependencies.merge!("ruby_engine" => RUBY_ENGINE.inspect)
    end

    details = {
      "program" => $0.to_s.inspect,
      "uname" => %x(uname -a).chomp.strip.inspect,
      "chef_repo" => chef_repo.inspect,
      "log_file" => log_file.inspect,
      "config_rb" => config_rb.inspect,
      "labfile" => labfile.inspect
    }

    max_key_length = [dependencies.keys.collect{ |key| key.to_s.length }.max, details.keys.collect{ |key| key.to_s.length }.max].max + 2

    $logger.info { ("=" * 80) }
    details.sort.each do |key, value|
      $logger.info { " %s%s: %s" % [ key.upcase, '.' * (max_key_length - key.length), value.to_s ] }
    end
    $logger.info { ("-" * (max_key_length * 2)) }
    dependencies.sort.each do |key, value|
      $logger.info { " %s%s: %s" % [ key.upcase, '.' * (max_key_length - key.length), value.to_s ] }
    end
    $logger.info { ("-" * (max_key_length * 2)) }

  end

  $logger
end

#lxc_identityObject



239
240
241
242
243
# File 'lib/cucumber/chef/utility.rb', line 239

def lxc_identity
  lxc_identity = File.join(Cucumber::Chef.home_dir, Cucumber::Chef::Config.provider.to_s, "id_rsa-#{lxc_user}")
  File.exists?(lxc_identity) && File.chmod(0400, lxc_identity)
  lxc_identity
end

#lxc_userObject

Container SSH Helpers



230
231
232
# File 'lib/cucumber/chef/utility.rb', line 230

def lxc_user
  Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:lxc_user]
end

#lxc_user_home_dirObject



234
235
236
237
# File 'lib/cucumber/chef/utility.rb', line 234

def lxc_user_home_dir
  user = Cucumber::Chef::Config[Cucumber::Chef::Config.provider][:lxc_user]
  ((user == "root") ? "/root" : "/home/#{user}")
end

#root_dirObject



108
109
110
# File 'lib/cucumber/chef/utility.rb', line 108

def root_dir
  File.expand_path(File.join(File.dirname(__FILE__), "..", "..", ".."), File.dirname(__FILE__))
end

#tag(name = nil) ⇒ Object



247
248
249
# File 'lib/cucumber/chef/utility.rb', line 247

def tag(name=nil)
  [ name, "v#{Cucumber::Chef::VERSION}" ].compact.join(" ")
end