From 533004e9323955aa1caa9561f7fb56945400188f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Oct 2014 10:31:23 -0700 Subject: [PATCH] core: Package copies private key --- lib/vagrant/action/general/package.rb | 42 +++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/action/general/package.rb b/lib/vagrant/action/general/package.rb index ee7e30ec3..99c0002ec 100644 --- a/lib/vagrant/action/general/package.rb +++ b/lib/vagrant/action/general/package.rb @@ -1,4 +1,5 @@ require 'fileutils' +require "pathname" require 'vagrant/util/safe_chdir' require 'vagrant/util/subprocess' @@ -36,6 +37,9 @@ module Vagrant @app.call(env) + @env[:ui].info I18n.t("vagrant.actions.general.package.compressing", tar_path: tar_path) + copy_include_files + setup_private_key compress end @@ -81,11 +85,6 @@ module Vagrant # Compress the exported file into a package def compress - @env[:ui].info I18n.t("vagrant.actions.general.package.compressing", tar_path: tar_path) - - # Copy over the included files - copy_include_files - # Get the output path. We have to do this up here so that the # pwd returns the proper thing. output_path = tar_path.to_s @@ -100,6 +99,39 @@ module Vagrant end end + # This will copy the generated private key into the box and use + # it for SSH by default. We have to do this because we now generate + # random keypairs on boot, so packaged boxes would stop working + # without this. + def setup_private_key + # If we don't have machine, we do nothing (weird) + return if !@env[:machine] + + # If we don't have a data dir, we also do nothing (base package) + return if !@env[:machine].data_dir + + # If we don't have a generated private key, we do nothing + path = @env[:machine].data_dir.join("private_key") + return if !path.file? + + # Copy it into our box directory + dir = Pathname.new(@env["package.directory"]) + new_path = dir.join("vagrant_private_key") + FileUtils.cp(path, new_path) + + # Append it to the Vagrantfile (or create a Vagrantfile) + vf_path = dir.join("Vagrantfile") + mode = "w+" + mode = "a" if vf_path.file? + vf_path.open(mode) do |f| + f.binmode + f.puts + f.puts %Q[Vagrant.configure("2") do |config|] + f.puts %Q[ config.ssh.private_key_path = File.expand_path("../vagrant_private_key", __FILE__)] + f.puts %Q[end] + end + end + # Path to the final box output file def tar_path File.expand_path(@env["package.output"], FileUtils.pwd)