Sha256: 31d2950442d9b6a32fe75480d03102ae987c987bf22f2a292d7d88e5159878be

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

module Veewee
  module Provider
    module Virtualbox
      module BoxCommand

        def destroy(option={})

          unless self.exists?
            ui.error "Error:: You tried to destroy a non-existing box '#{name}'"
            exit -1
          end

          # If it has a save state,remove that first

          if self.running?
            # Poweroff
            self.poweroff
            # Wait for it to happen
            sleep 2
          end

          command="#{@vboxcmd} unregistervm  \"#{name}\" --delete"
          ui.info command
          ui.info "Deleting vm #{name}"

          #Exec and system stop the execution here
          shell_exec("#{command}",{:mute => true})
          sleep 1

          #if the disk was not attached when the machine was destroyed we also need to delete the disk
          pattern= File::SEPARATOR+name+"."
          #+definition.disk_format.downcase
          found=false
          command="#{@vboxcmd} list hdds -l"
          hdds=shell_exec("#{command}",{:mute => true}).stdout.split(/\n\n/)

          hdds.each do |hdd_text|
            location=hdd_text.split(/\n/).grep(/^Location/).first.split(':')[1].strip
            if location.match(/#{pattern}/)

              if File.exists?(location)
                command="#{@vboxcmd} closemedium disk \"#{location}\" --delete"
              else
                command="#{@vboxcmd} closemedium disk \"#{location}\""
              end

              ui.info "Deleting disk #{location}"
              ui.info "#{command}"

              shell_exec("#{command}",{:mute => true})

              if File.exists?(location)
                ui.info "We tried to delete the disk file via virtualbox '#{location} but failed"
                ui.info "Removing it manually"
                FileUtils.rm(location)
                exit -1
              end
              break
            end
          end
        end

      end #Module
    end #Module
  end #Module
end #Module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
veewee-0.3.0.alpha8 lib/veewee/provider/virtualbox/box/destroy.rb
veewee-0.3.0.alpha7 lib/veewee/provider/virtualbox/box/destroy.rb