Class: VboxTools

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

Overview

require VBoxManage executable binary

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Instance Method Details

#backup_file(machine = nil) ⇒ Object



99
100
101
102
# File 'lib/vbox_tools.rb', line 99

def backup_file( machine = nil )
  return config_file( machine ) +
    '.bak' + Time.now.strftime( "%Y%m%d%H%M%S" ) + '.txt'
end

#backup_files(machine = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/vbox_tools.rb', line 104

def backup_files( machine = nil )
  path = File.dirname( config_file( machine ) )
  return Dir.chdir( path ) {
    Dir.glob( File.basename( config_file( machine ) ) +
              '.bak[0-9]*.txt' ).map { |f|
      File.join( path, f )
    }
  }
end

#clear_backups(machine = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/vbox_tools.rb', line 114

def clear_backups( machine = nil )
  if ( machine )
    backup_files( machine ).map { |e|
      FileUtils.rm( e )
    }
  else
    backup_files.map { |e|
      FileUtils.rm( e )
    }
  end
end

#config_file(machine = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/vbox_tools.rb', line 85

def config_file( machine = nil )
  if ( machine )
    file = File.join( machine_base, machine, "#{machine}.xml" )
  else
    file = File.join( vm_root, 'VirtualBox.xml' )
  end

  return ( File.exist?( file ) and File.file?( file ) ) ? file : nil
end

#create_backup(machine = nil) ⇒ Object



95
96
97
# File 'lib/vbox_tools.rb', line 95

def create_backup( machine = nil )
  return FileUtils.cp( config_file( machine ), backup_file( machine ) )
end

#getextradata(machine) ⇒ Object



166
167
168
# File 'lib/vbox_tools.rb', line 166

def getextradata( machine )
  call4vm( machine, 'getextradata', 'enumerate' )
end

#machine_baseObject



17
18
19
# File 'lib/vbox_tools.rb', line 17

def machine_base
  return File.join( vm_root, 'Machines' )
end

#machinesObject



31
32
33
34
35
36
37
# File 'lib/vbox_tools.rb', line 31

def machines
  if ( !@machines )
    @machines = vm_list( 'vms' ) 
  end

  return @machines
end

#machines_acpi_enabledObject



61
62
63
64
65
# File 'lib/vbox_tools.rb', line 61

def machines_acpi_enabled
  vm_list( 'acpi' ) { |line|
    ( line.match( /\AACPI: (.*)/ ) and $1.strip == 'on' )
  }
end

#machines_need_shutdown_before_verupObject



57
58
59
# File 'lib/vbox_tools.rb', line 57

def machines_need_shutdown_before_verup
  machines_running + machines_saved
end

#machines_runningObject



39
40
41
42
43
44
45
# File 'lib/vbox_tools.rb', line 39

def machines_running
  if ( !@runnings )
    @runnings = vm_list( 'runningvms' )
  end

  return @runnings
end

#machines_savedObject



47
48
49
50
51
52
53
54
55
# File 'lib/vbox_tools.rb', line 47

def machines_saved
  if ( !@saved )
    @saved = vm_list( 'saved' ) { |line|
      ( line.match( /\AState: (.*)\(since/ ) and $1.strip == 'saved' )
    }
  end

  return @saved
end

#managerObject

VBoxManage binary



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vbox_tools.rb', line 70

def manager
  @manager

  if ( !@manager )
    if ( RUBY_PLATFORM =~ /win/ and RUBY_PLATFORM !~ /darwin/ )
      @manager = which( 'VBoxManage.exe',
                        'C:\Program Files\Sun\xVM VirtualBox' )
    else
      @manager = 'VBoxManage'
    end
  end

  return @manager
end

#open(file) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/vbox_tools.rb', line 126

def open( file )
  cmd = open_cmd
  if ( cmd )
    system( cmd, file )
  else
    # for Windows. Cannot open #{Machine}.xml, why ?
    system( 'cmd.exe /c "' + file + '"' )
  end
end

#open_config(machine) ⇒ Object



136
137
138
# File 'lib/vbox_tools.rb', line 136

def open_config( machine )
  open( config_file( machine ) )
end

#open_latest_backup(machine) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/vbox_tools.rb', line 140

def open_latest_backup( machine )
  file = backup_files( machine ).sort.last
  if ( file )
    open( file )
  else
    puts "No backup."
  end
end

#poweroff(machine) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vbox_tools.rb', line 149

def poweroff( machine )
  if ( machines_running.include?( machine ) )
    poweroff_cmd( machine )
  elsif ( machines_saved.include?( machine ) )
    call4vm( machine, 'startvm' )
    timeout = 20
    while ( timeout > 0 )
      if ( poweroff_cmd( machine ) )
        return true
      else
        sleep 1
        timeout -= 1
      end
    end
  end
end

#versionObject



12
13
14
15
# File 'lib/vbox_tools.rb', line 12

def version
  cmdline = "#{manager} --version"
  `#{cmdline}`.strip
end

#vm_rootObject



21
22
23
24
25
26
27
28
29
# File 'lib/vbox_tools.rb', line 21

def vm_root
  home = ENV['HOME']
  ['Library/VirtualBox', '.VirtualBox'].each { |dir|
    d = File.join( home, dir )
    if ( File.exist?( d ) and File.directory?( d ) )
      return d
    end
  }
end