Class: Chef::Knife::EcBackup
Constant Summary
collapse
- PATHS =
%w(chef_repo_path cookbook_path environment_path data_bag_path role_path node_path client_path acl_path group_path container_path)
Instance Method Summary
collapse
Methods included from EcBase
#completion_banner, #configure_chef, #ensure_webui_key_exists!, included, #knife_ec_error_handler, #local_user_list, #org_admin, #remote_user_list, #remote_users, #rest, #server, #set_client_config!, #set_dest_dir_from_args!, #set_skip_user_acl!, #temporary_webui_key, #user_acl_rest, #veil, #veil_config, #warn_on_incorrect_clients_group, #webui_key
Instance Method Details
#chef_fs_copy_pattern(pattern_str, chef_fs_config) ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/chef/knife/ec_backup.rb', line 235
def chef_fs_copy_pattern(pattern_str, chef_fs_config)
ui.msg "Copying #{pattern_str}"
pattern = Chef::ChefFS::FilePattern.new(pattern_str)
Chef::ChefFS::FileSystem.copy_to(pattern, chef_fs_config.chef_fs,
chef_fs_config.local_fs, nil,
config, ui,
proc { |entry| chef_fs_config.format_path(entry) })
rescue Net::HTTPServerException,
Chef::ChefFS::FileSystem::NotFoundError,
Chef::ChefFS::FileSystem::OperationFailedError => ex
knife_ec_error_handler.add(ex)
end
|
#chef_fs_paths(pattern_str, chef_fs_config, exclude = []) ⇒ Object
228
229
230
231
232
233
|
# File 'lib/chef/knife/ec_backup.rb', line 228
def chef_fs_paths(pattern_str, chef_fs_config, exclude=[])
pattern = Chef::ChefFS::FilePattern.new(pattern_str)
list = Chef::ChefFS::FileSystem.list(chef_fs_config.chef_fs, pattern)
list = list.select { |entry| ! exclude.include?(entry.name) } if ! exclude.empty?
list.map { |entry| normalize_path_name(entry.path) }
end
|
#download_org_data(name) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/chef/knife/ec_backup.rb', line 181
def download_org_data(name)
old_config = Chef::Config.save
begin
PATHS.each do |path|
Chef::Config.delete(path.to_sym)
end
Chef::Config.chef_repo_path = "#{dest_dir}/organizations/#{name}"
Chef::Config.versioned_cookbooks = true
Chef::Config.chef_server_url = "#{server.root_url}/organizations/#{name}"
ensure_dir(Chef::Config.chef_repo_path)
chef_fs_config = Chef::ChefFS::Config.new
Chef::Config.node_name = if config[:skip_version]
org_admin
else
server.supports_defaulting_to_pivotal? ? 'pivotal' : org_admin
end
chef_fs_config = Chef::ChefFS::Config.new
top_level_paths = chef_fs_config.chef_fs.children.map { |entry| entry.path }
top_level_paths.each do |path|
chef_fs_copy_pattern(path, chef_fs_config)
end
ensure
Chef::Config.restore(old_config)
end
end
|
#download_org_invitations(name) ⇒ Object
165
166
167
168
169
170
171
172
|
# File 'lib/chef/knife/ec_backup.rb', line 165
def download_org_invitations(name)
ensure_dir("#{dest_dir}/organizations/#{name}")
File.open("#{dest_dir}/organizations/#{name}/invitations.json", 'w') do |file|
file.write(Chef::JSONCompat.to_json_pretty(rest.get("/organizations/#{name}/association_requests")))
end
rescue Net::HTTPServerException => ex
knife_ec_error_handler.add(ex)
end
|
#download_org_members(name) ⇒ Object
156
157
158
159
160
161
162
163
|
# File 'lib/chef/knife/ec_backup.rb', line 156
def download_org_members(name)
ensure_dir("#{dest_dir}/organizations/#{name}")
File.open("#{dest_dir}/organizations/#{name}/members.json", 'w') do |file|
file.write(Chef::JSONCompat.to_json_pretty(rest.get("/organizations/#{name}/users")))
end
rescue Net::HTTPServerException => ex
knife_ec_error_handler.add(ex)
end
|
#download_user(username, url) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/chef/knife/ec_backup.rb', line 117
def download_user(username, url)
File.open("#{dest_dir}/users/#{username}.json", 'w') do |file|
file.write(Chef::JSONCompat.to_json_pretty(rest.get(url)))
end
rescue Net::HTTPServerException => ex
knife_ec_error_handler.add(ex)
end
|
#download_user_acl(username) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/chef/knife/ec_backup.rb', line 125
def download_user_acl(username)
File.open("#{dest_dir}/user_acls/#{username}.json", 'w') do |file|
file.write(Chef::JSONCompat.to_json_pretty(user_acl_rest.get("users/#{username}/_acl")))
end
rescue Net::HTTPServerException => ex
knife_ec_error_handler.add(ex)
end
|
#ensure_dir(dir) ⇒ Object
174
175
176
177
178
|
# File 'lib/chef/knife/ec_backup.rb', line 174
def ensure_dir(dir)
if !File.exist?(dir)
FileUtils.mkdir_p(dir)
end
end
|
#export_from_sql ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/chef/knife/ec_backup.rb', line 133
def export_from_sql
require_relative 'ec_key_export'
Chef::Knife::EcKeyExport.deps
k = Chef::Knife::EcKeyExport.new
k.name_args = ["#{dest_dir}/key_dump.json", "#{dest_dir}/key_table_dump.json"]
k.config[:sql_host] = config[:sql_host]
k.config[:sql_port] = config[:sql_port]
k.config[:sql_db] = config[:sql_db]
k.config[:sql_user] = config[:sql_user]
k.config[:sql_password] = config[:sql_password]
k.config[:skip_users_table] = !config[:with_user_sql]
k.config[:skip_keys_table] = !config[:with_key_sql]
k.run
end
|
#for_each_organization ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/chef/knife/ec_backup.rb', line 92
def for_each_organization
rest.get('/organizations').each_pair do |name, url|
next unless (config[:org].nil? || config[:org] == name)
ui.msg "Downloading organization object for #{name} from #{url}"
begin
org = rest.get(url)
rescue Net::HTTPServerException => ex
ui.error "Failed to find organization '#{name}'."
knife_ec_error_handler.add(ex)
next
end
if org['assigned_at'] || server.version >= Gem::Version.new("12")
yield org
else
ui.msg "Skipping pre-created org #{name}"
end
end
end
|
#for_each_user ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/chef/knife/ec_backup.rb', line 84
def for_each_user
remote_users.each_pair do |name, url|
yield name, url
end
rescue Net::HTTPServerException => ex
knife_ec_error_handler.add(ex)
end
|
#normalize_path_name(path) ⇒ Object
224
225
226
|
# File 'lib/chef/knife/ec_backup.rb', line 224
def normalize_path_name(path)
path=~/\.json\z/ ? path : path<<'.json'
end
|
#purge_users_on_backup ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/chef/knife/ec_backup.rb', line 71
def purge_users_on_backup
return unless config[:purge]
users_for_purge do |user|
ui.msg "Deleting user #{user} from local backup (purge is on)"
begin
::File.delete("#{dest_dir}/users/#{user}.json")
::File.delete("#{dest_dir}/user_acls/#{user}.json")
rescue Errno::ENOENT => e
ui.warn "Failed to find local #{user} data #{e}"
end
end
end
|
#run ⇒ Object
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
|
# File 'lib/chef/knife/ec_backup.rb', line 25
def run
set_dest_dir_from_args!
set_client_config!
set_skip_user_acl!
ensure_webui_key_exists!
ensure_dir("#{dest_dir}/users")
ensure_dir("#{dest_dir}/user_acls") unless config[:skip_useracl]
ui.msg 'Downloading Users'
for_each_user do |username, url|
download_user(username, url)
if config[:skip_useracl]
ui.warn("Skipping user ACL download for #{username}. To download this ACL, remove --skip-useracl.")
else
download_user_acl(username)
end
end
purge_users_on_backup
if config[:with_user_sql] || config[:with_key_sql]
export_from_sql
end
ensure_dir("#{dest_dir}/organizations")
for_each_organization do |org_object|
name = org_object['name']
write_org_object_to_disk(org_object)
download_org_data(name)
download_org_members(name)
download_org_invitations(name)
end
warn_on_incorrect_clients_group(dest_dir, "backup")
completion_banner
end
|
#users_for_purge ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/chef/knife/ec_backup.rb', line 62
def users_for_purge
purge_list = local_user_list - remote_user_list
purge_list -= ['pivotal']
purge_list.each do |user|
yield user
end
end
|
#write_org_object_to_disk(org_object) ⇒ Object
148
149
150
151
152
153
154
|
# File 'lib/chef/knife/ec_backup.rb', line 148
def write_org_object_to_disk(org_object)
name = org_object['name']
ensure_dir("#{dest_dir}/organizations/#{name}")
File.open("#{dest_dir}/organizations/#{name}/org.json", 'w') do |file|
file.write(Chef::JSONCompat.to_json_pretty(org_object))
end
end
|