Module: Virtualman::Interactive::Helper

Extended by:
Helper
Included in:
Helper, REPL
Defined in:
lib/virtualman/interactive/helper.rb

Instance Method Summary collapse

Instance Method Details

#check_chmod_scriptObject



86
87
88
89
90
91
92
93
94
# File 'lib/virtualman/interactive/helper.rb', line 86

def check_chmod_script
  script = "/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh"
  permission = Kernel.sprintf("%o", File.world_readable?(script) )
  if permission != "744"
    puts "Changing file permission of #{script}, to allow it to be executable"
    puts "Thanks to provide your admin password."
    File.chmod(0744, script)
  end
end

#check_conf_fileObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/virtualman/interactive/helper.rb', line 173

def check_conf_file
  puts "Checking conf file..."
  conf_file = "autostart.cfg"
  conf_path = "/etc/vbox/"

  conf_template = %q{
    # Default policy is to deny starting a VM, the other option is "allow".
    default_policy = deny

    # <%= variables[0] %> is allowed to start virtual machines but starting them
    # will be delayed for 10 seconds
    <%= variables[0] %> = {
  allow = true
  startup_delay = 10
    }
    }.gsub(/^  /, '')

  username = `whoami`.strip

  integrate_template(conf_file, conf_path, conf_template, username)
end

#check_plist_fileObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/virtualman/interactive/helper.rb', line 135

def check_plist_file
  puts "Checking plist file..."
  plist_file = "org.virtualbox.vboxautostart.plist"
  plist_path = "/Library/LaunchDaemons/"

  plist_template = %q{
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
<key>Disabled</key>
<false/>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>org.virtualbox.vboxautostart</string>
<key>ProgramArguments</key>
<array>
  <string>/Applications/VirtualBox.app/Contents/MacOS/VBoxAutostartDarwin.sh</string>
  <string>--start</string>
  <string>/etc/vbox/autostart.cfg</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>debug</key>
<true/>
    </dict>
    </plist>
    }    

  if self.integrate_template(plist_file, plist_path, plist_template)
    puts "loading the plist file"
    puts "Thanks to provide your admin password."
    `sudo launchctl load #{plist_path}#{plist_file}`
  end
end

#choose(source) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/virtualman/interactive/helper.rb', line 9

def choose source
  
  list = Array.new
  self.configuration.options[source].each do |option|
    list << option["name"]
  end
  list << "exit"

  choice = Menu.unic_run(list)

  exit 0 if choice == "exit"

  self.configuration.options[source].each_with_index do |option,index|
    if option["name"] == choice
      return self.configuration.options[source][index]
    end
  end

end

#choose_vm(source) ⇒ Object

Source can be either source_vms or clonned_vms



30
31
32
33
34
# File 'lib/virtualman/interactive/helper.rb', line 30

def choose_vm source
  vm_choice = choose source
  return Vm.new("#{vm_choice["name"]}")
   
end

#clone_vmObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/virtualman/interactive/helper.rb', line 36

def clone_vm
  puts "----Clone_VM----"
  puts "Which VM do you want to clone?"

  vm_choice = choose "source_vms"
  vm_to_clone = Vm.new("#{vm_choice["name"]}")

  puts "How do you want to name your freshly spawned VM?"
  vm_name = Menu.ask

  options = "--snapshot \"#{vm_choice["snapshot"]}\" --name \"#{vm_name}\" --register"
  vm_to_clone.manage("clonevm", options)
  vm_cloned = Vm.new(vm_name)
  return vm_cloned
end

#init_bootifyObject



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/virtualman/interactive/helper.rb', line 195

def init_bootify

   #check plist file
	check_plist_file

	#Check chmod of the bash script
   check_chmod_script

	#Check the conf file
   check_conf_file

end

#integrate_template(filename, path, template_string, *variables) ⇒ Object



96
97
98
99
100
101
102
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
131
132
133
# File 'lib/virtualman/interactive/helper.rb', line 96

def integrate_template filename, path, template_string, *variables
  template = ERB.new(template_string, 0, "%<>")

  templated = template.result(binding)

  File.open("/tmp/#{filename}", 'w') { |file| file.write(templated) }

  if File.exist?(path+filename)
    diff = `diff #{path}#{filename} /tmp/#{filename}`
    result = $?
    if result != 0
      puts "We are trying to install the previous file but there are differences"
      puts "here are the differences"
      puts "#{diff}"
      puts "Do you want to override the original?"
      puts "yes/[no]"

      answer = Menu.ask

      if answer != "yes"
        return false
      end
    else
      return true
    end
  end

  puts "Installing the file #{path}#{filename}..."
  puts "Please povide your password :"

  output = `sudo cp /tmp/#{filename} #{path}#{filename}`
  result = $?
  if result == 0
    return true
  else
    Kernel.abort(output)
  end
end

#start_vm(vm) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/virtualman/interactive/helper.rb', line 52

def start_vm(vm)
  if !vm.running?
    puts "Do you want to start the VM? headless?"
    puts "(default is no)"
    puts "yes/headless/[no]"

    answer = Menu.ask

    if answer == "headless"
      option = "--type headless"
      puts "The VM is booting headless"
    elsif answer == "yes"
      option = ""
      puts "The VM is booting"
    else
      puts "The vm will not be started"
      return false
    end

    vm.manage("startvm", option)

    Kernel.print "Waiting for the VM to get an IP"
    while !vm.ip
      Kernel.print "."
      Kernel.sleep (1)
    end
    Kernel.print "\n"
  end

  return vm.running?
end