Class: NiseBOSHVagrant::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/nise-bosh-vagrant/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nise-bosh-vagrant/runner.rb', line 12

def initialize(opts)
  opts[:nise].nil? ? @nise_path = nil : @nise_path = File.expand_path(opts[:nise])
  @release_path = opts[:release]
  @vagrantfile_path = File.join(@release_path, "Vagrantfile")
  @scripts_path = File.join(File.dirname(File.expand_path(__FILE__)), "../../scripts")
  @manifest_file = opts[:manifest]
  @preinstall_file = opts[:preinstall]
  @postinstall_file = opts[:postinstall]
  @memory = opts[:memory]
  @ip_address = opts[:address]
  @bridge = opts[:bridge]

  copy_file_prefix = '.nise-bosh'
  @copy_name = {
    :manifest       => "#{copy_file_prefix}-manifest.yml",
    :preinstall     => "#{copy_file_prefix}-preinstall",
    :postinstall    => "#{copy_file_prefix}-postinstall",
    :install_script => "#{copy_file_prefix}-install.sh",
  }

  if @bridge
    @bridge_if = NetworkInterface.find(@ip_address)
    raise "No available interface found" if @bridge_if.nil?
  end
end

Instance Attribute Details

#copy_nameObject (readonly)

Returns the value of attribute copy_name.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def copy_name
  @copy_name
end

#install_script_pathObject (readonly)

Returns the value of attribute install_script_path.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def install_script_path
  @install_script_path
end

#manifest_copy_pathObject (readonly)

Returns the value of attribute manifest_copy_path.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def manifest_copy_path
  @manifest_copy_path
end

#manifest_fileObject (readonly)

Returns the value of attribute manifest_file.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def manifest_file
  @manifest_file
end

#memoryObject (readonly)

Returns the value of attribute memory.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def memory
  @memory
end

#nise_pathObject (readonly)

Returns the value of attribute nise_path.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def nise_path
  @nise_path
end

#release_pathObject (readonly)

Returns the value of attribute release_path.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def release_path
  @release_path
end

#scripts_pathObject (readonly)

Returns the value of attribute scripts_path.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def scripts_path
  @scripts_path
end

#vagrantfile_pathObject (readonly)

Returns the value of attribute vagrantfile_path.



9
10
11
# File 'lib/nise-bosh-vagrant/runner.rb', line 9

def vagrantfile_path
  @vagrantfile_path
end

Instance Method Details

#copy_hook_scripts(output_dir = @release_path, preinstall_file = @preinstall_file, postinstall_file = @postinstall_file) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nise-bosh-vagrant/runner.rb', line 50

def copy_hook_scripts(output_dir=@release_path, preinstall_file=@preinstall_file, postinstall_file=@postinstall_file)
  @preinstall_copy_path = File.join(output_dir, @copy_name[:preinstall])
  if preinstall_file
    FileUtils.cp(preinstall_file, @preinstall_copy_path)
  else
    FileUtils.rm_rf(@preinstall_copy_path)
  end
  @postinstall_copy_path = File.join(output_dir, @copy_name[:postinstall])
  if postinstall_file
    FileUtils.cp(postinstall_file, @postinstall_copy_path)
  else
    FileUtils.rm_rf(@postinstall_copy_path)
  end
end

#copy_manifest(output_dir = @release_path, manifest_file = @manifest_file) ⇒ Object



45
46
47
48
# File 'lib/nise-bosh-vagrant/runner.rb', line 45

def copy_manifest(output_dir=@release_path, manifest_file=@manifest_file)
  @manifest_copy_path = File.join(output_dir, @copy_name[:manifest])
  FileUtils.cp(manifest_file, @manifest_copy_path)
end

#exec(cmd) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nise-bosh-vagrant/runner.rb', line 96

def exec(cmd)
  begin
    PTY.spawn (cmd) do |stdout, stdin, pid|
      begin
        stdout.each { |line| print line }
      rescue Errno::EIO
      end
    end
  rescue PTY::ChildExited
  end
end

#generate_install_script(output_dir = @release_path, manifest_file = @manifest_copy_path) ⇒ Object



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/nise-bosh-vagrant/runner.rb', line 65

def generate_install_script(output_dir=@release_path, manifest_file=@manifest_copy_path)
  manifest = YAML.load_file manifest_file
  jobs = []
  manifest['jobs'].each do |job|
    jobs << job['name']
  end

  puts "Job list: #{jobs}"

  install_script_template = File.open(File.join(File.dirname(File.expand_path(__FILE__)), '../../resources/install_release.sh.erb'), "rb") { |f| f.read }
  install_script_erb = ERB.new install_script_template

  install_script = "#!/bin/bash\n\n"
  install_script += "(\n"
  install_script += "    cd /home/vagrant/nise_bosh\n"


  jobs.each do |job|
    manifest_path = "/home/vagrant/release/#{@copy_name[:manifest]}"
    job_name = job
    install_entry = install_script_erb.result(binding)
    install_script += "#{install_entry}\n"
  end

  install_script += ")"

  @install_script_path = File.join(output_dir, @copy_name[:install_script])
  File.open(@install_script_path, "wb") { |f| f.write(install_script) }
  FileUtils.chmod 0755, @install_script_path
end

#generate_vagrantfile(output_path = @vagrantfile_path) ⇒ Object



38
39
40
41
42
43
# File 'lib/nise-bosh-vagrant/runner.rb', line 38

def generate_vagrantfile(output_path=@vagrantfile_path)
  vagrantfile_template = File.open(File.join(File.dirname(File.expand_path(__FILE__)), '../../resources/Vagrantfile.erb'), "rb") { |f| f.read }
  template = ERB.new vagrantfile_template
  vagrantfile = template.result(binding)
  File.open(output_path, "wb") { |f| f.write(vagrantfile) }
end

#install_release(release_path = @release_path) ⇒ Object



119
120
121
122
# File 'lib/nise-bosh-vagrant/runner.rb', line 119

def install_release(release_path=@release_path)
  install_cmd = "cd #{release_path} ; vagrant ssh -c \"/home/vagrant/install_release.sh\""
  self.exec(install_cmd)
end

#prepare_vm(release_path = @release_path) ⇒ Object



114
115
116
117
# File 'lib/nise-bosh-vagrant/runner.rb', line 114

def prepare_vm(release_path=@release_path)
  prepare_cmd = "cd #{release_path} ; vagrant ssh -c \"/home/vagrant/scripts/prepare.sh\""
  self.exec(prepare_cmd)
end

#run_postinstall_release(release_path = @release_path) ⇒ Object



129
130
131
132
# File 'lib/nise-bosh-vagrant/runner.rb', line 129

def run_postinstall_release(release_path=@release_path)
  hook_cmd = "cd #{release_path} ; vagrant ssh -c \"/home/vagrant/postinstall_release\""
  self.exec(hook_cmd)
end

#run_preinstall_release(release_path = @release_path) ⇒ Object



124
125
126
127
# File 'lib/nise-bosh-vagrant/runner.rb', line 124

def run_preinstall_release(release_path=@release_path)
  hook_cmd = "cd #{release_path} ; vagrant ssh -c \"/home/vagrant/preinstall_release\""
  self.exec(hook_cmd)
end

#start_release(release_path = @release_path) ⇒ Object



134
135
136
137
# File 'lib/nise-bosh-vagrant/runner.rb', line 134

def start_release(release_path=@release_path)
  start_cmd = "cd #{release_path} ; vagrant ssh -c \"/home/vagrant/start.sh\""
  self.exec(start_cmd)
end

#start_vm(release_path = @release_path) ⇒ Object



108
109
110
111
112
# File 'lib/nise-bosh-vagrant/runner.rb', line 108

def start_vm(release_path=@release_path)
  up_cmd = "cd #{release_path} ; vagrant up"
  self.exec(up_cmd)
  
end