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.



13
14
15
16
17
18
19
20
# File 'lib/xanthus/job.rb', line 13

def initialize
  @iterations = 0
  @tasks = Hash.new
  @outputs = Hash.new
  @inputs = Hash.new
  @pre_instructions = nil
  @post_instructions = nil
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

#post_instructionsObject

Returns the value of attribute post_instructions.



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

def post_instructions
  @post_instructions
end

#pre_instructionsObject

Returns the value of attribute pre_instructions.



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

def pre_instructions
  @pre_instructions
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



96
97
98
99
100
101
# File 'lib/xanthus/job.rb', line 96

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

#execute(config, iteration) ⇒ Object



103
104
105
106
107
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/xanthus/job.rb', line 103

def execute config, iteration
  puts "Running job #{name.to_s}-#{iteration.to_s}..."
  FileUtils.mkdir_p 'tmp'
  Dir.chdir 'tmp' do
    self.host_scripts config
    @tasks.each do |machine, templates|
      self.setup_env machine, templates, config
    end
    self.execute_pre_instructions unless @pre_instructions.nil?
    @tasks.each do |machine, templates|
      self.run machine
    end
    self.execute_post_instructions unless @post_instructions.nil?
    @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?
  config.dataverse_conf.add("#{name.to_s}-#{iteration.to_s}.tar.gz") unless config.dataverse_conf.nil?
  puts "Job #{name.to_s}-#{iteration.to_s} done."
end

#execute_post_instructionsObject



85
86
87
88
# File 'lib/xanthus/job.rb', line 85

def execute_post_instructions
  puts 'Running post instructions...'
  system('sh', './post.sh')
end

#execute_pre_instructionsObject



74
75
76
77
# File 'lib/xanthus/job.rb', line 74

def execute_pre_instructions
  puts 'Running pre instructions...'
  system('sh', './pre.sh')
end

#halt(machine) ⇒ Object



90
91
92
93
94
# File 'lib/xanthus/job.rb', line 90

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

#host_scripts(config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xanthus/job.rb', line 57

def host_scripts config
  puts 'Setting up host scripts...'
  if !@pre_instructions.nil?
    script = Script.new(@pre_instructions, config).to_s
    File.open('pre.sh', 'w+') do |f|
      f.write(script)
    end
  end

  if !@post_instructions.nil?
    script = Script.new(@post_instructions, config).to_s
    File.open('post.sh', 'w+') do |f|
      f.write(script)
    end
  end
end

#output_script(machine, outputs) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/xanthus/job.rb', line 22

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

#run(machine) ⇒ Object



79
80
81
82
83
# File 'lib/xanthus/job.rb', line 79

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

#setup_env(machine, scripts, config) ⇒ Object



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
# File 'lib/xanthus/job.rb', line 30

def setup_env machine, scripts, config
  puts 'Setting up task on machine '+machine.to_s+'...'
  FileUtils.mkdir_p machine.to_s
  Dir.chdir machine.to_s do
    if !@inputs[machine].nil?
      @inputs[machine].each do |name|
        system('cp', '-f', "../../#{name}", "#{name}")
      end
    end
    FileUtils.mkdir_p 'output'
    puts 'Creating provision files...'
    File.open('Vagrantfile', 'w+') do |f|
      f.write(config.vms[machine].to_vagrant)
    end
    script = Script.new(scripts, config).to_s
    script += self.output_script(machine, @outputs[machine]) unless  @outputs[machine].nil?
    File.open('provision.sh', 'w+') do |f|
      f.write(script)
    end
    script = 'echo "nothing to do"'
    File.open('before_halt.sh', 'w+') do |f|
      f.write(script)
    end
    system('chmod', '+x', 'before_halt.sh')
  end
end