Class: Vagrant::CopyBox::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-commit/command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



131
132
133
134
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
171
172
173
# File 'lib/vagrant-commit/command.rb', line 131

def execute
  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant copybox <source> <target>"
  end

  # Parse the options
  argv = parse_options(opts)
  src_boxname, target_boxname = argv
  if !src_boxname || !target_boxname
    raise Vagrant::Errors::CLIInvalidUsage,
          help: opts.help.chomp
  end
  collection = Vagrant::BoxCollection.new(@env.boxes_path)
  box = collection.find(src_boxname, :libvirt, "> 0")
  if !box
    @env.ui.error("Box #{src_boxname} not found")
    return 1
  end
  # create necessary files in the boxes path
  root_dir = @env.boxes_path
  version = "0"
  new_box_dir = root_dir.join(target_boxname).join(version).join("libvirt")
  if File.exist?(new_box_dir)
    ans = @env.ui.ask("Box #{target_boxname} exists. What to do? [use/remove/exit] ")
    if ans.strip.downcase == "use"
      return 0
    elsif ans.strip.downcase == "remove"
      FileUtils.rm_rf(new_box_dir)
      @env.ui.info("Box removed. You probably want to run vagrant fulldestroy.")
    end
    return 1
  end
  new_box_dir.mkpath
  new_box_dir.join("metadata.json").open("w") do |f|
    f.write(())
  end
  new_box_dir.join("Vagrantfile").open("w") do |f|
    f.write(Vagrantfile())
  end
  src_path = box.directory.join("box.img")
  target_path = new_box_dir.join("box.img")
  FileUtils.cp(src_path, target_path)
end

#metadata_jsonObject



105
106
107
108
109
110
111
112
113
# File 'lib/vagrant-commit/command.rb', line 105

def 
  <<-EOF
    {
      "provider": "libvirt",
      "format": "qcow2",
      "virtual_size": 32
    }
  EOF
end

#VagrantfileObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vagrant-commit/command.rb', line 115

def Vagrantfile
  <<-EOF
    Vagrant.configure("2") do |config|
      config.vm.provider :libvirt do |libvirt|
        libvirt.driver = "kvm"
        libvirt.host = ""
        libvirt.connect_via_ssh = false
        libvirt.storage_pool_name = "default"
      end
    end

    user_vagrantfile = File.expand_path('../_include/Vagrantfile', __FILE__)
    load user_vagrantfile if File.exists?(user_vagrantfile)
  EOF
end