Method: Vagrant::CopyBox::Command#execute

Defined in:
lib/vagrant-commit/command.rb

#executeObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'lib/vagrant-commit/command.rb', line 117

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")
  provider = box.["provider"]

  # 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(provider.to_s)
  existing_box_found = false
  if File.exist?(new_box_dir)
    existing_box_found = true
    ans = @env.ui.ask("Box #{target_boxname} exists. Recreate? [y/n] ")
    return 1 if ans.strip.downcase != "y"
    FileUtils.rm_rf(new_box_dir)
  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)
  if existing_box_found
    @env.ui.info("Box recreated. You probably want to run vagrant fulldestroy to use the new box.")
  end
end