Class: Xanthus::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/xanthus/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJob

Returns a new instance of Job.



11
12
13
14
15
# File 'lib/xanthus/job.rb', line 11

def initialize
  @iterations = 0
  @tasks = Hash.new
  @outputs = Hash.new
end

Instance Attribute Details

#inputsObject

Returns the value of attribute inputs.



9
10
11
# File 'lib/xanthus/job.rb', line 9

def inputs
  @inputs
end

#iterationsObject

Returns the value of attribute iterations.



6
7
8
# File 'lib/xanthus/job.rb', line 6

def iterations
  @iterations
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/xanthus/job.rb', line 5

def name
  @name
end

#outputsObject

Returns the value of attribute outputs.



8
9
10
# File 'lib/xanthus/job.rb', line 8

def outputs
  @outputs
end

#tasksObject

Returns the value of attribute tasks.



7
8
9
# File 'lib/xanthus/job.rb', line 7

def tasks
  @tasks
end

Instance Method Details

#destroy(machine) ⇒ Object



75
76
77
78
79
80
# File 'lib/xanthus/job.rb', line 75

def destroy machine
  Dir.chdir machine.to_s do
    system('vagrant', 'destroy', '-f')
    system('rm', '-rf', '.vagrant')
  end
end

#execute(config, iteration) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xanthus/job.rb', line 82

def execute config, iteration
  puts "Running job #{name.to_s}-#{iteration.to_s}..."
  FileUtils.mkdir_p 'tmp'
  Dir.chdir 'tmp' do
    @tasks.each do |machine, templates|
      self.setup_env machine, templates, config
    end
    @tasks.each do |machine, templates|
      self.run machine
    end
    @tasks.each do |machine, templates|
      self.halt machine
    end
    @tasks.each do |machine, templates|
      self.destroy machine
    end
  end
  system('mv', 'tmp', "#{name.to_s}-#{iteration.to_s}")
  system('tar', '-czvf', "#{name.to_s}-#{iteration.to_s}.tar.gz", "#{name.to_s}-#{iteration.to_s}")
  system('rm', '-rf', "#{name.to_s}-#{iteration.to_s}")
  config.github_conf.add("#{name.to_s}-#{iteration.to_s}.tar.gz") unless config.github_conf.nil?
  config.github_conf.push unless config.github_conf.nil?
  puts "Job #{name.to_s}-#{iteration.to_s} done."
end

#halt(machine) ⇒ Object



69
70
71
72
73
# File 'lib/xanthus/job.rb', line 69

def halt machine
  Dir.chdir machine.to_s do
    system('vagrant', 'halt')
  end
end

#output_script(outputs) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/xanthus/job.rb', line 17

def output_script outputs
  script = ''
  outputs.each do |name, path|
    script += "cp -f #{path} /vagrant/output/#{name}.data\n"
  end
  return script
end

#run(machine) ⇒ Object



63
64
65
66
67
# File 'lib/xanthus/job.rb', line 63

def run machine
  Dir.chdir machine.to_s do
    system('vagrant', 'up')
  end
end

#setup_env(machine, scripts, config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
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/xanthus/job.rb', line 25

def setup_env machine, scripts, config
  puts 'Setting up task on machine '+machine.to_s+'...'
  script = ''
  scripts.each do |t|
    v = eval(config.scripts[t])
    if v.kind_of?(Array)
      v.each do |w|
        script+=w+"\n"
      end
    else
      script+=v
    end
  end
  script += self.output_script(@outputs[machine]) unless  @outputs[machine].nil?

  script_to_clean = script
  script = ''
  script_to_clean.each_line do |s|
    script += s.strip + "\n" unless s=="\n"
  end
  script = script.gsub "\n\n", "\n"

  FileUtils.mkdir_p machine.to_s
  Dir.chdir machine.to_s do
    @inputs[machine].each do |name|
      system('cp', '-f', "../../#{name}", "#{name}")
    end
    FileUtils.mkdir_p 'output'
    puts 'Creating provision files...'
    File.open('Vagrantfile', 'w+') do |f|
      f.write(config.vms[machine].to_vagrant)
    end
    File.open('provision.sh', 'w+') do |f|
      f.write(script)
    end
  end
end