This repository has been archived by the owner on Dec 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
upload.rb
99 lines (87 loc) · 3.23 KB
/
upload.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'json'
require 'tempfile'
require_relative 'base'
module VagrantPlugins
module Berkshelf
module Action
class Upload < Base
def call(env)
if !berkshelf_enabled?(env)
@logger.info "Berkshelf disabled, skipping"
return @app.call(env)
end
if !provision_enabled?(env)
@logger.info "Provisioning disabled, skipping"
return @app.call(env)
end
if !chef_client?(env)
@logger.info "Provisioner does need to upload"
return @app.call(env)
end
upload(env)
@app.call(env)
end
private
# Generate a custom Berkshelf config from the existing Berkshelf
# config on disk, with values in the Vagrantfile taking precedence,
# based on the current provisioner. It is assumed the provisioner is
# the "chef_client" provisioner.
#
# The path to the temporary configuration file is yielded to the
# block. This method ensures the temporary file is cleaned up
# automatically.
#
# @param [Vagrant::Provisioner] provisioner
# @param [Proc] block
def with_provision_berkshelf_config(provisioner, &block)
config = current_berkshelf_config
config[:chef] ||= {}
config[:chef].merge(
chef_server_url: provisioner.config.chef_server_url,
node_name: provisioner.config.node_name,
client_key: provisioner.config.client_key_path,
validation_key: provisioner.config.validation_key_path,
validation_client_name: provisioner.config.validation_client_name,
)
tmpfile = Tempfile.new('config.json')
tmpfile.write(config.to_json)
tmpfile.rewind
yield tmpfile.path
ensure
if defined?(tmpfile) && !tmpfile.nil?
tmpfile.close
tmpfile.unlink
end
end
# The current JSON representation of the Berkshelf config on disk.
#
# @return [Hash<Symbol, Object>]
def current_berkshelf_config
path = File.expand_path(ENV['BERKSHELF_CONFIG'] || '~/.berkshelf/config.json')
if File.exist?(path)
JSON.parse(File.read(path), symbolize_names: true)
else
{}
end
end
# Upload the resolved Berkshelf cookbooks to the target Chef Server
# specified in the Vagrantfile.
#
# @param [Vagrant::Environment] env
def upload(env)
provisioners(:chef_client, env).each do |provisioner|
with_provision_berkshelf_config(provisioner) do |config|
env[:machine].ui.info "Uploading cookbooks to #{provisioner.config.chef_server_url}"
berks("upload",
config: config,
berksfile_path: env[:machine].config.berkshelf.berksfile_path,
force: true,
freeze: false,
)
end
end
end
end
end
end
end