Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: [enhancement] Automatic restore in solo mode #45

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
default[:chef_server_populator][:backup][:filename] = 'chef-server-full'
default[:chef_server_populator][:backup][:remote][:connection] = {}
default[:chef_server_populator][:backup][:remote][:directory] = nil
default[:chef_server_populator][:backup][:remote][:file_prefix] = nil
default[:chef_server_populator][:backup][:schedule] = {
:minute => '33',
:hour => '3'
Expand All @@ -54,4 +55,4 @@

default[:chef_server_populator][:server_org] = 'inception_llc'
#If this is set to nil, the configurator recipe will set it to the server_org.
default[:chef_server_populator][:default_org] = nil
default[:chef_server_populator][:default_org] = nil
2 changes: 1 addition & 1 deletion recipes/backups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
node[:chef_server_populator][:backup][:schedule].each do |k,v|
send(k,v)
end
path "$PATH:/opt/chef/embedded/bin/"
path "/opt/chef/embedded/bin/:$PATH"
end
15 changes: 15 additions & 0 deletions recipes/restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,28 @@
creates '/etc/opscode/restore.json'
end

execute 'restore chef server wait for postgresql' do
command 'sleep 10'
creates '/etc/opscode/restore.json'
end

#Drop and Restore entire chef database from file
execute 'restoring chef data' do
command "/opt/opscode/embedded/bin/psql -f #{file} postgres"
user 'opscode-pgsql'
creates '/etc/opscode/restore.json'
end

execute 'shutdown postgresql after restore' do
command 'chef-server-ctl stop postgresql'
creates '/etc/opscode/restore.json'
end

execute 'wait for postgresql to stop' do
command 'sleep 20'
creates '/etc/opscode/restore.json'
end

execute 'remove existing data' do
command "rm -rf /var/opt/opscode /etc/opscode"
creates '/etc/opscode/restore.json'
Expand Down
82 changes: 59 additions & 23 deletions recipes/solo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,70 @@
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[:credentials])
remote_bucket = remote.buckets.get(remote_directory)
if(remote_bucket && gz_file = remote_bucket.files.get(File.join(node[:chef_server_populator][:backup][:remote][:file_prefix], 'latest.tgz')))
dump_file = remote_bucket.files.get(File.join(node[:chef_server_populator][:backup][:remote][:file_prefix], 'latest.dump'))
local_gz = '/tmp/latest.tgz'
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
14 changes: 11 additions & 3 deletions templates/default/chef-server-backup.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ server_manifest = MultiJson.load(

prefix = [
Time.now.to_i,
"ver_#{server_manifest[:version]}",
"ver_#{server_manifest[:build_version]}",
config[:filename]
].join('-')

Expand Down Expand Up @@ -75,14 +75,22 @@ remote_directory = [:remote, :directory].inject(config) do |memo, key|
memo[key] || break
end

remote_prefix = [:remote, :file_prefix].inject(config) do |memo, key|
memo[key] || break
end

if(remote_creds)
if(remote_directory)
remote = Miasma.api(:provider => remote_creds[:provider].to_s.downcase, :type => 'storage', :credentials => remote_creds)
remote = Miasma.api(:provider => remote_creds[:provider].to_s.downcase, :type => 'storage', :credentials => remote_creds[:credentials])
directory = remote.buckets.get(remote_directory)
[db_file, data_file].each do |file|
[ :date_stamped, :latest ].each do |type|
remote_file = Miasma::Models::Storage::File.new(directory)
remote_file.name = type == :date_stamped ? File.basename(file) : "latest#{File.extname(file)}"
remote_file.name = File.join(*[
remote_prefix,
type == :date_stamped ? File.basename(file) : "latest#{File.extname(file)}"
].flatten.compact
)
remote_file.body = File.open(file, 'r')
remote_file.save
end
Expand Down