Class: VagrantPlugins::ChefZero::Action::Upload
- Inherits:
-
Object
- Object
- VagrantPlugins::ChefZero::Action::Upload
show all
- Includes:
- EnvHelpers, ServerHelpers
- Defined in:
- lib/vagrant-chef-zero/action/upload.rb
Instance Method Summary
collapse
#chef_zero_server_running?, #find_chef_zero_binary, #fork_process, #get_chef_zero_binary, #get_chef_zero_server_pid, #has_chef_zero_binary?, #is_a_chef_zero_server?, #kill_process, #port_open?, #start_chef_zero, #stop_chef_zero, #wait_for_server_to_start
Methods included from EnvHelpers
active_machines, #berkshelf_enabled?, #chef_client?, #chef_zero_enabled?, #get_chef_server_url, #get_fake_key_path, #get_host, #get_key_path, #get_port, #get_validation_client_name, mutex, #provisioners, #rm_knife_config, #server_info, #set_berkshelf_client_key, #set_config, #write_knife_config
Constructor Details
#initialize(app, env) ⇒ Upload
Returns a new instance of Upload.
12
13
14
15
16
17
18
19
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 12
def initialize(app, env)
@conn = nil
@app = app
@env = env
@url = server_info(env)[:host]
@client_name = server_info(env)[:client_name]
@client_key = server_info(env)[:client_key]
end
|
Instance Method Details
#call(env) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 21
def call(env)
unless chef_zero_enabled?(env)
return @app.call(env)
end
setup_connection
upload_cookbooks(env)
upload_environments(env)
upload_roles(env)
upload_nodes(env)
upload_data_bags(env)
@app.call(env)
end
|
#is_valid_cookbook?(path) ⇒ Boolean
170
171
172
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 170
def is_valid_cookbook?(path)
File.exists?("#{path}/metadata.rb") || File.exists?("#{path}/metadata.json")
end
|
#is_valid_cookbook_directory?(path) ⇒ Boolean
161
162
163
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 161
def is_valid_cookbook_directory?(path)
return ! select_valid_cookbooks(path).empty?
end
|
#select_cookbooks(path) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 136
def select_cookbooks(path)
cookbook_paths = []
if path.nil?
cookbook_paths = []
elsif path.respond_to?('empty?') && path.empty?
cookbook_paths = []
elsif path.is_a?(Array)
path.each do |single_path|
if is_valid_cookbook?(single_path)
cookbook_paths << single_path
elsif is_valid_cookbook_directory?(single_path)
cookbook_paths = cookbook_paths + select_valid_cookbooks(single_path)
end
end
elsif path.is_a?(String) && is_valid_cookbook?(path)
cookbook_paths = [path]
elsif path.is_a?(String) && File.directory?(path) && is_valid_cookbook_directory?(path)
cookbook_paths = select_valid_cookbooks(path)
else
@env[:chef_zero].ui.warn("Warning: Unable to normalize #{path}, skipping")
cookbook_paths = []
end
cookbook_paths
end
|
#select_items(path) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 174
def select_items(path)
if path.nil?
path = []
elsif path.respond_to?('empty?') && path.empty?
path = []
elsif path.is_a?(Array)
path
elsif path.is_a?(String) && File.directory?(path)
path = Dir.glob("#{path}/*.json") + Dir.glob("#{path}/*.rb")
elsif path.is_a?(String) && File.exists?(path)
path = [path]
else
@env[:chef_zero].ui.warn("Warning: Unable to normalize #{path}, skipping")
path = []
end
path
end
|
#select_valid_cookbooks(path) ⇒ Object
165
166
167
168
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 165
def select_valid_cookbooks(path)
directories = Dir.glob("#{path}/*")
directories.select{ |s| is_valid_cookbook?(s) }
end
|
#setup_connection ⇒ Object
192
193
194
195
196
197
198
199
200
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 192
def setup_connection
if ! @conn
require 'ridley'
@conn = Ridley.new(server_url: @url,
client_name: @client_name,
client_key: @client_key)
end
@conn
end
|
#upload_cookbooks(env) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 35
def upload_cookbooks(env)
path = env[:machine].config.chef_zero.cookbooks
cookbooks = select_cookbooks(path)
env[:chef_zero].ui.info("Loading cookbooks from #{path}") unless cookbooks.empty?
cookbooks.each do |cookbook|
name = File.basename(cookbook)
env[:chef_zero].ui.info("Uploading Cookbook #{name}")
@conn.cookbook.upload(cookbook, options = { force: true })
end
end
|
#upload_data_bags(env) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 109
def upload_data_bags(env)
path = env[:machine].config.chef_zero.data_bags
if path && path.is_a?(String) && File.directory?(path)
data_bags = Dir.glob("#{path}/*").select{|d| File.directory? d}
data_bags.each do |data_bag_dir|
bag_name = File.basename(data_bag_dir)
data_bag = @conn.data_bag.find(bag_name)
if ! data_bag
env[:chef_zero].ui.info("Creating Data Bag #{bag_name}")
data_bag = @conn.data_bag.create(name: bag_name)
end
Dir.glob("#{data_bag_dir}/*.json").each do |i|
item_as_hash = JSON.parse(IO.read(i)).to_hash
if data_bag.item.find(item_as_hash['id'])
env[:chef_zero].ui.info("Updating item #{item_as_hash['id']} in Data Bag #{bag_name}")
data_bag.item.update(item_as_hash)
else
env[:chef_zero].ui.info("Creating item #{item_as_hash['id']} in Data Bag #{bag_name}")
data_bag.item.create(item_as_hash)
end
end
end
end
end
|
#upload_environments(env) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 63
def upload_environments(env)
path = env[:machine].config.chef_zero.environments
existing_envs = @conn.environment.all
environments = select_items(path)
environments.each do |e|
if e =~ /.rb$/
envrb = ::Chef::Environment.new
envrb.from_file(e)
environment = envrb.to_hash
else
environment = JSON.parse(IO.read(e)).to_hash
end
if ! existing_envs.any?{ |ee| ee['name'] == environment['name'] }
env[:chef_zero].ui.info("Creating Environment #{environment['name']}")
@conn.environment.create(environment)
else
env[:chef_zero].ui.info("Updating Environment #{environment['name']}")
@conn.environment.update(environment)
end
end
end
|
#upload_nodes(env) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 46
def upload_nodes(env)
path = env[:machine].config.chef_zero.nodes
existing_nodes = @conn.node.all
nodes = select_items(path)
nodes.each do |n|
node = JSON.parse(IO.read(n)).to_hash
if ! existing_nodes.any?{ |e| e['name'] == node['name'] }
env[:chef_zero].ui.info("Creating Node #{node['name']}")
@conn.node.create(node)
else
env[:chef_zero].ui.info("Updating Node #{node['name']}")
@conn.node.update(node)
end
end
end
|
#upload_roles(env) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/vagrant-chef-zero/action/upload.rb', line 86
def upload_roles(env)
path = env[:machine].config.chef_zero.roles
existing_roles = @conn.role.all
roles = select_items(path)
roles.each do |r|
if r =~ /.rb$/
rrb = ::Chef::Role.new
rrb.from_file(r)
role = rrb.to_hash
else
role = JSON.parse(IO.read(r)).to_hash
end
if ! existing_roles.any?{ |er| er['name'] == role['name'] }
env[:chef_zero].ui.info("Creating role #{role['name']}")
@conn.role.create(role)
else
env[:chef_zero].ui.info("Updating role #{role['name']}")
@conn.role.update(role)
end
end
end
|