Class: Veewee::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/veewee/export.rb

Class Method Summary collapse

Class Method Details

.vagrant(boxname, boxdir, definition) ⇒ Object

Shellutil.execute(“vagrant package –base #vmname –include /tmp/Vagrantfile –output /tmp/#vmname.box”, => “on”)



7
8
9
10
11
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
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
62
63
64
# File 'lib/veewee/export.rb', line 7

def self.vagrant(boxname,boxdir,definition)
  
  #Check if box already exists
  vm=VirtualBox::VM.find(boxname)
  if vm.nil?
    puts "#{boxname} is not found, maybe you need to build first?"
    exit
  end
  #We need to shutdown first
  if vm.running?
    puts "Vagrant requires the box to be shutdown, before it can export"
    puts "Sudo also needs to work for user #{definition[:ssh_user]}"
    puts "Performing a clean shutdown now."
    ssh_options={ :user => definition[:ssh_user], :port => definition[:ssh_host_port], :password => definition[:ssh_password],
        :timeout => definition[:ssh_timeout]}       
                
    Veewee::Ssh.execute("localhost","sudo #{definition[:shutdown_cmd]}",ssh_options)

    #Wait for state poweroff
    while (vm.running?) do 
      print '.'
      sleep 1
    end
    puts
    puts "Machine #{boxname} is powered off cleanly"
  end

  #Vagrant requires a relative path for output of boxes
  
  #4.0.x. not using boxes as a subdir
  boxdir=Pathname.new(Dir.pwd)
  
  full_path=File.join(boxdir,boxname+".box")
  path1=Pathname.new(full_path)
  path2=Pathname.new(Dir.pwd)
  box_path=path1.relative_path_from(path2).to_s
  
  if File.exists?("#{box_path}")
    puts "box #{boxname} already exists"
    exit
  end
  
  puts "Excuting vagrant voodoo:"
  export_command="vagrant package --base '#{boxname}' --output '#{box_path}'"
  puts "#{export_command}"
  Veewee::Shell.execute("#{export_command}") #hmm, needs to get the gem_home set?
  puts
  
  #add_ssh_nat_mapping back!!!!      
  
  puts "To import it into vagrant type:"
  puts "vagrant box add '#{boxname}' '#{box_path}'"
  puts ""
  puts "To use it:"
  puts "vagrant init '#{boxname}'"
  puts "vagrant up"
  puts "vagrant ssh"
end