|
| 1 | +require 'archive/tar/minitar' |
| 2 | + |
| 3 | +module VagrantAWS |
| 4 | + class Action |
| 5 | + class PrepareProvisioners |
| 6 | + def initialize(app, env) |
| 7 | + @app = app |
| 8 | + @env = env |
| 9 | + @env["provision.enabled"] = true if !@env.has_key?("provision.enabled") |
| 10 | + @provisioner_configs = [] |
| 11 | + |
| 12 | + load_provisioner_configs if provisioning_enabled? |
| 13 | + end |
| 14 | + |
| 15 | + def call(env) |
| 16 | + @provisioner_configs.each do |provisioner_config| |
| 17 | + if provisioner_config.is_a?(Vagrant::Provisioners::ChefSolo::Config) |
| 18 | + env.ui.info I18n.t("vagrant.plugins.aws.actions.prepare_provisioners.uploading_chef_resources") |
| 19 | + ChefSolo.prepare(provisioner_config) |
| 20 | + end |
| 21 | + end |
| 22 | + @app.call(env) |
| 23 | + end |
| 24 | + |
| 25 | + def provisioning_enabled? |
| 26 | + !@env["config"].vm.provisioners.empty? && @env["provision.enabled"] |
| 27 | + end |
| 28 | + |
| 29 | + def load_provisioner_configs |
| 30 | + @env["config"].vm.provisioners.each do |provisioner| |
| 31 | + @provisioner_configs << provisioner.config |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + class ChefSolo |
| 36 | + |
| 37 | + def self.prepare(config) |
| 38 | + my_preparer = new(config) |
| 39 | + my_preparer.bootstrap_if_needed |
| 40 | + my_preparer.chown_provisioning_folder |
| 41 | + my_preparer.copy_and_update_paths |
| 42 | + end |
| 43 | + |
| 44 | + def initialize(config) |
| 45 | + @config = config |
| 46 | + end |
| 47 | + |
| 48 | + def bootstrap_if_needed |
| 49 | + begin |
| 50 | + @config.env.vm.ssh.execute do |ssh| |
| 51 | + ssh.sudo!("which chef-solo") |
| 52 | + end |
| 53 | + rescue Vagrant::Errors::VagrantError |
| 54 | + # Bootstrap chef-solo |
| 55 | + @config.env.ui.info I18n.t("vagrant.plugins.aws.actions.prepare_provisioners.chef_not_detected", :binary => "chef-solo") |
| 56 | + @config.env.vm.system.bootstrap_chef |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + |
| 61 | + def chown_provisioning_folder |
| 62 | + @config.env.vm.ssh.execute do |ssh| |
| 63 | + ssh.sudo!("mkdir -p #{@config.provisioning_path}") |
| 64 | + ssh.sudo!("chown #{@config.env.config.ssh.username} #{@config.provisioning_path}") |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def copy_and_update_paths |
| 69 | + # Copy relevant host paths to remote instance and update provisioner config |
| 70 | + # to point to new "vm" paths for cookbooks, etc. |
| 71 | + %w{ cookbooks_path roles_path data_bags_path }.each do |path| |
| 72 | + copy_host_paths(@config.send(path), path) |
| 73 | + @config.send "#{path}=", strip_host_paths(@config.send(path)).push([:vm, path]) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def copy_host_paths(paths, target_directory) |
| 78 | + archive = tar_host_folder_paths(paths) |
| 79 | + |
| 80 | + target_d = "#{@config.provisioning_path}/#{target_directory}" |
| 81 | + target_f = target_d + '.tar' |
| 82 | + |
| 83 | + @config.env.vm.ssh.upload!(archive.path, target_f) |
| 84 | + @config.env.vm.ssh.execute do |ssh| |
| 85 | + ssh.sudo!([ |
| 86 | + "mkdir -p #{target_d}", |
| 87 | + "chown #{@config.env.config.ssh.username} #{target_d}", |
| 88 | + "tar -C #{target_d} -xf #{target_f}" |
| 89 | + ]) |
| 90 | + end |
| 91 | + |
| 92 | + target_directory |
| 93 | + end |
| 94 | + |
| 95 | + def tar_host_folder_paths(paths) |
| 96 | + tarf = Tempfile.new(['vagrant-chef-solo','.tar']) |
| 97 | + Archive::Tar::Minitar::Output.open(tarf) do |outp| |
| 98 | + host_folder_paths(paths).each do |full_path| |
| 99 | + Dir.chdir(full_path) do |ignored| |
| 100 | + Dir.glob("**#{File::SEPARATOR}**") { |entry| Archive::Tar::Minitar.pack_file(entry, outp) } |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + tarf |
| 105 | + end |
| 106 | + |
| 107 | + |
| 108 | + def host_folder_paths(paths) |
| 109 | + paths = [paths] if paths.is_a?(String) || paths.first.is_a?(Symbol) |
| 110 | + paths.inject([]) do |acc, path| |
| 111 | + path = [:host, path] if !path.is_a?(Array) |
| 112 | + type, path = path |
| 113 | + acc << File.expand_path(path, @config.env.root_path) if type == :host |
| 114 | + acc |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + def strip_host_paths(paths) |
| 119 | + paths = [paths] if paths.is_a?(String) || paths.first.is_a?(Symbol) |
| 120 | + paths.delete_if { |path| !path.is_a?(Array) || path[0] == :host } |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments