Skip to content

Commit

Permalink
[enhancement] Automatic restore in solo mode
Browse files Browse the repository at this point in the history
When running in solo mode, check remote bucket for existing backup
files. If found, fetch and restore using latest backup files, otherwise
provision as fresh chef server as normal.
  • Loading branch information
chrisroberts committed Jul 29, 2015
1 parent 75a6f59 commit c2cce96
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions recipes/solo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,69 @@
node.default[:chef_server_populator][:default_org] = node[:chef_server_populator][:server_org]
end

include_recipe 'chef-server-populator::org'
# if backup pull files include restore

knife_cmd = "#{node[:chef_server_populator][:knife_exec]}"
knife_opts = "-s https://127.0.0.1/organizations/#{node[:chef_server_populator][:server_org]} -c /etc/opscode/pivotal.rb"

node[:chef_server_populator][:clients].each do |client, pub_key|
execute "create client: #{client}" do
command "#{knife_cmd} client create #{client} --admin -d #{knife_opts} > /dev/null 2>&1"
not_if "#{knife_cmd} client list #{knife_opts}| tr -d ' ' | grep '^#{client}$'"
retries 5
end
if(pub_key && node[:chef_server_populator][:base_path])
pub_key_path = File.join(node[:chef_server_populator][:base_path], pub_key)
execute "remove default public key for #{client}" do
command "chef-server-ctl delete-client-key #{node[:chef_server_populator][:server_org]} #{client} default"
only_if "chef-server-ctl list-client-keys #{node[:chef_server_populator][:server_org]} #{client} | grep 'name: default$'"
if(node[:chef_server_populator][:backup][:remote][:connection])
chef_gem 'miasma'
require 'miasma'
remote_creds = node[:chef_server_populator][:backup][:remote][:connection]
remote_directory = node[:chef_server_populator][:backup][:remote][:directory]
remote = Miasma.api(:provider => remote_creds[:provider].to_s.downcase, :type => 'storage', :credentials => remote_creds)
if(gz_file = remote.bucket(remote_directory).files.get('latest.gz'))
dump_file = remote.bucket(remote_directory).files.get('latest.dump')
local_gz = '/tmp/latest.gz'
local_dump = '/tmp/latest.dump'
File.open(local_gz, 'wb') do |file|
while(data = gz_file.body.readpartial(2048))
file.print data
end
end
execute "set public key for: #{client}" do
command "chef-server-ctl add-client-key #{node[:chef_server_populator][:server_org]} #{client} #{pub_key_path} --key-name populator"
not_if "chef-server-ctl list-client-keys #{node[:chef_server_populator][:server_org]} #{client} | grep 'name: populator$'"
File.open(local_dump, 'wb') do |file|
while(data = dump_file.body.readpartial(2048))
file.print data
end
end
node.set[:chef_server_populator][:restore][:file] = local_dump
node.set[:chef_server_populator][:restore][:data] = local_gz
end
end

execute 'install chef-server-populator cookbook' do
command "#{knife_cmd} cookbook upload chef-server-populator #{knife_opts} -o #{Chef::Config[:cookbook_path].join(':')} --include-dependencies"
only_if do
node[:chef_server_populator][:cookbook_auto_install]
if(local_gz && local_dump)

include_recipe 'chef-server-populator::restore'

else

include_recipe 'chef-server-populator::org'

knife_cmd = "#{node[:chef_server_populator][:knife_exec]}"
knife_opts = "-s https://127.0.0.1/organizations/#{node[:chef_server_populator][:server_org]} -c /etc/opscode/pivotal.rb"

node[:chef_server_populator][:clients].each do |client, pub_key|
execute "create client: #{client}" do
command "#{knife_cmd} client create #{client} --admin -d #{knife_opts} > /dev/null 2>&1"
not_if "#{knife_cmd} client list #{knife_opts}| tr -d ' ' | grep '^#{client}$'"
retries 5
end
if(pub_key && node[:chef_server_populator][:base_path])
pub_key_path = File.join(node[:chef_server_populator][:base_path], pub_key)
execute "remove default public key for #{client}" do
command "chef-server-ctl delete-client-key #{node[:chef_server_populator][:server_org]} #{client} default"
only_if "chef-server-ctl list-client-keys #{node[:chef_server_populator][:server_org]} #{client} | grep 'name: default$'"
end
execute "set public key for: #{client}" do
command "chef-server-ctl add-client-key #{node[:chef_server_populator][:server_org]} #{client} #{pub_key_path} --key-name populator"
not_if "chef-server-ctl list-client-keys #{node[:chef_server_populator][:server_org]} #{client} | grep 'name: populator$'"
end
end
end
retries 5

execute 'install chef-server-populator cookbook' do
command "#{knife_cmd} cookbook upload chef-server-populator #{knife_opts} -o #{Chef::Config[:cookbook_path].join(':')} --include-dependencies"
only_if do
node[:chef_server_populator][:cookbook_auto_install]
end
retries 5
end

end

0 comments on commit c2cce96

Please sign in to comment.