Class: Toaster::ToasterApp

Inherits:
Thor
  • Object
show all
Defined in:
lib/toaster/api.rb

Constant Summary collapse

CMD =
"toaster"
ROOT_DIR =
File.join(File.dirname(File.expand_path(__FILE__)), "..", "..")
CHEF_TMP_DIR =
"/tmp/toaster.chef"
CHEF_TMP_SOLO_FILE =
"#{CHEF_TMP_DIR}/solo.rb"
CHEF_TMP_NODE_FILE =
"#{CHEF_TMP_DIR}/node.json"

Instance Method Summary collapse

Instance Method Details

#agentObject



263
264
265
266
# File 'lib/toaster/api.rb', line 263

def agent()
  require "toaster/toaster_app_service"
  ToasterAppService.start_service()
end

#chefsolo(node_file) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/toaster/api.rb', line 131

def chefsolo(node_file)
  require "toaster/chef/chef_util"
  require "toaster/util/util"
  solo_file = CHEF_TMP_SOLO_FILE
  Toaster::ChefUtil.create_chef_config(solo_file)
  Toaster::ChefUtil.run_chef(solo_file, node_file)
end

#cleanObject



270
271
272
273
# File 'lib/toaster/api.rb', line 270

def clean()
  require "toaster/util/lxc"
  Toaster::LXC.clean()
end

#download(cookbook, prototype_name, version = "latest", recipes = ["default"]) ⇒ Object

option :version, :desc => “Cookbook version to download”, :default => “latest” option :recipes, :type => :array, :desc => “List of recipes”, :default => [“default”]



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/toaster/api.rb', line 206

def download(cookbook, prototype_name, version="latest", recipes=["default"])

  require "toaster/chef/chef_util"
  require "toaster/util/lxc"

  recipes = parse_recipes(recipes)
  prototype_name_full = prototype_name
  prototype_name_full = "prototype_#{prototype_name_full}" if !prototype_name_full.match(/^prototype_/)
  start_stop_container("start", prototype_name_full)

  lxc = Toaster::LXC.prototype_container(prototype_name)
  puts "INFO: Ensuring existence of latest cookbooks and node file in LXC container '#{lxc['lxc_id']}'"
  Toaster::ChefUtil.download_cookbook_version_in_lxc(lxc, cookbook, version)
  cookbook_dir = Toaster::ChefUtil.lxc_cookbook_dir(lxc)
  # clean cookbooks
  Toaster::ChefUtil.fix_known_bugs_in_recipes(cookbook_dir, "before")
  # dowload dependencies for cookbook
  Toaster::ChefUtil.download_dependencies(cookbook, nil, cookbook_dir, true)
  # additionally, check dependencies for each recipe
  recipes.each do |recipe|
    Toaster::ChefUtil.download_dependencies(cookbook, recipe, cookbook_dir, false)
  end
  # fix cookbook bugs, encodings, etc.
  Toaster::ChefUtil.fix_known_bugs_in_recipes(cookbook_dir, "after")

  start_stop_container("stop", prototype_name_full)

  # if we are using docker.io tools, the changes made to 
  # the container need to be saved (committed) explicitly... 
  Docker.save_container_changes(prototype_name_full)
end

#proto(prototype_name, operating_system = "ubuntu") ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/toaster/api.rb', line 36

def proto(prototype_name, operating_system="ubuntu")

  prototype_name = "prototype_#{prototype_name}" if !prototype_name.match(/^prototype_.*/)

  #operating_system = options[:os]
  write(CHEF_TMP_NODE_FILE, "{\n  \"run_list\": [\"recipe[lxc::init_proto]\"],\n  \"lxc\": {\n    \"proto\": {\n      \"name\": \"\#{prototype_name}\"\n    },\n    \"bare_os\": {\n      \"distribution\": \"\#{operating_system}\"\n    }\n  }\n}\n  EOF\n  )\n  run_chef()\n\nend\n"

#runchef(lxc_name, lxc_ip, run_list, cfg = "") ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/toaster/api.rb', line 105

def runchef(lxc_name, lxc_ip, run_list, cfg="")

  if cfg != ""
    cfg = ", #{cfg}"
    cfg = cfg.gsub(/"/, '\\\\"')
  end
  if Dir.exist?("/lxc/#{lxc_name}/rootfs/")
    tmpf = `mktemp`
    tmpf1 = `mktemp`
    tmpf.strip!
    tmpf1.strip!
    tmpfile = "/lxc/#{lxc_name}/rootfs/#{tmpf}"
    tmpfile1 = "/lxc/#{lxc_name}/rootfs/#{tmpf1}"
    write(tmpfile, "{\"run_list\": [\"#{run_list}\"] #{cfg}}")
    `cp #{CHEF_TMP_SOLO_FILE} #{tmpfile1}`
    puts "INFO: Created config file #{tmpfile}: #{`cat #{tmpfile}`}"
    system("ssh root@#{lxc_ip} chef-solo -j #{tmpf} -c #{tmpf1}")
    `rm -f #{tmpfile} #{tmpfile1}`
  else
    puts "ERROR: container directory /lxc/#{lxc_name}/rootfs/ does not exist."
  end
end

#runtests(test_suite_uuid, idem_N = (1..100).to_a, skip_N = [], skip_N_succ = [], combine_N = [], combine_N_succ = []) ⇒ Object

option :idemN, :type => :array, :desc => “idempotence for task sequences of length N (N is an array)” option :skipN, :type => :array, :desc => “skip N tasks (N is an array)” option :skipNsucc, :desc => “skip N successive tasks (N is an array)” option :combineN, :desc => “combine N tasks (N is an array)” option :combineNsucc, :desc => “combine N successive tasks (N is an array)”



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/toaster/api.rb', line 158

def runtests(test_suite_uuid, idem_N=(1..100).to_a, 
    skip_N=[], skip_N_succ=[], combine_N=[], combine_N_succ=[])

  require "toaster/test/test_suite"
  require "toaster/test/test_coverage"
  require "toaster/test/test_orchestrator"

  #idem_N = options[:idemN]
  #skip_N = options[:skipN]
  #skip_N_succ = options[:skipNsucc]
  #combine_N = options[:combineN]
  #combine_N_succ = options[:combineNsucc]
  if idem_N
    if idem_N.include?("..")
      idem_N = eval("#{idem_N}").to_a.join(",")
    elsif !idem_N.kind_of?(Array)
      idem_N = [idem_N.to_i]
    end
  end

  init_db_connection()
  test_suites = Toaster::TestSuite.find({"uuid" => test_suite_uuid})
  if !test_suites || test_suites.empty?
    puts "ERROR: Invalid test suite id specified: '#{test_suite_uuid}'"
  else
    puts "INFO: Running/continuing tests for test suite '#{test_suite_uuid}'"
    test_suite = test_suites[0]
#        test_suite.coverage_goal.idempotence = idem_N if idem_N
#        test_suite.coverage_goal.combinations[Toaster::CombinationCoverage::SKIP_N] = skip_N if skip_N
#        test_suite.coverage_goal.combinations[Toaster::CombinationCoverage::SKIP_N_SUCCESSIVE] = skip_N_succ if skip_N_succ
#        test_suite.coverage_goal.combinations[Toaster::CombinationCoverage::COMBINE_N] = combine_N if combine_N
#        test_suite.coverage_goal.combinations[Toaster::CombinationCoverage::COMBINE_N_SUCCESSIVE] = combine_N_succ if combine_N_succ
#        test_suite.save()
    orch = Toaster::TestOrchestrator.new
#        orch.generate_tests_for_suite(test_suite)
    if Config.get("testing.test_hosts").kind_of?(Array)
      Config.get("testing.test_hosts").each do |test_host|
        orch.add_host(test_host)
      end
    end
    orch.distribute_tests(test_suite)
  end
end

#setupObject



23
24
25
26
27
28
29
30
31
# File 'lib/toaster/api.rb', line 23

def setup()
  write(CHEF_TMP_NODE_FILE, "  {\n    \"run_list\": [\"recipe[lxc::setup_host]\"]\n  }\n  EOF\n  )\n  run_chef()\nend\n"

#spawn(lxc_name, prototype_name) ⇒ Object



62
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
95
96
97
98
99
100
# File 'lib/toaster/api.rb', line 62

def spawn(lxc_name, prototype_name)

  prototype_name = "prototype_#{prototype_name}"

  prototype_dir = "/lxc/#{prototype_name}"
  if !Dir.exist?(prototype_dir)
    raise "Prototype directory '#{prototype_dir}' does not exist."
  end

  ip_num = ""
  # test if LXC name is "lxc[0-9]+"
  if lxc_name.match(/^lxc[0-9]+$/)
    ip_num = lxc_name.gsub(/lxc([0-9]+)/, '\1').to_i
    ip_num += 2
  end
  if ip_num == ""
    Docker.get_container_names().size() + 3
    ip_num = num_lxcs.to_i + 3
  end
  guest_ip = "192.168.100.#{ip_num}"

  content = "  {\n    \"run_list\": [\"recipe[lxc::create_lxc]\"],\n    \"lxc\": {\n      \"proto\": {\n        \"name\": \"\#{prototype_name}\"\n      },\n      \"cont\": {\n        \"name\": \"\#{lxc_name}\",\n        \"ip_address\": \"\#{guest_ip}\"\n      }\n    }\n  }\n  EOF\n  write(CHEF_TMP_NODE_FILE, content)\n  run_chef()\n\nend\n"

#start(lxc_name) ⇒ Object



141
142
143
# File 'lib/toaster/api.rb', line 141

def start(lxc_name)
  start_stop_container("start", lxc_name)
end

#stop(lxc_name) ⇒ Object



147
148
149
# File 'lib/toaster/api.rb', line 147

def stop(lxc_name)
  start_stop_container("stop", lxc_name)
end

#testinit(chef_node, recipes, prototype, destroy_container = true) ⇒ Object

option :destroy, :type => :boolean, :desc => “Destroy container after execution”, :default => true



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/toaster/api.rb', line 242

def testinit(chef_node, recipes, prototype, destroy_container=true)

  require "toaster/test_manager"

  #destroy_container = options[:destroy]
  recipes = parse_recipes(recipes)
  puts "INFO: Ensuring existence of automation #{chef_node}, recipe list '#{recipes}', prototype #{prototype} (destroy container: #{destroy_container})"

  init_db_connection()
  do_output = true
  suite = Toaster::TestManager.init_test(chef_node, recipes, nil, prototype, destroy_container, do_output)

  if suite && suite.uuid
    puts "INFO: New test suite UUID: #{suite.uuid}"
  else
    puts "WARN: Could not initialize test suite, test manager returned empty test suite UUID."
  end
end

#versionObject



277
278
279
280
281
282
# File 'lib/toaster/api.rb', line 277

def version()
  file = File.join(File.dirname(__FILE__), "..", "..", "VERSION")
  version = File.read(file)
  puts version
  return "#{version}".strip
end

#web(detached = false) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/toaster/api.rb', line 287

def web(detached=false)
  puts "INFO: Starting Web application on port 8080"
  dir = File.join(File.dirname(__FILE__), "..", "..")
  cmd = "cd \"#{dir}\" && #{dir}/webapp/bin/rails server thin"
  detached ||= options[:detached]
  if detached
    Kernel::exec("screen -d -m bash -c '#{cmd}'")
  else
    Kernel::exec("#{cmd}")
  end
end