Class: KnifeSharp::SharpBackup
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- KnifeSharp::SharpBackup
- Defined in:
- lib/chef/knife/sharp-backup.rb
Instance Method Summary collapse
Instance Method Details
#backup ⇒ Object
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 100 101 102 103 |
# File 'lib/chef/knife/sharp-backup.rb', line 46 def backup = Time.now.to_i unless @cfg["global"]["backupdir"] ui.error "You need to add global/backupdir to your config file" exit 1 end backup_path = @cfg["global"]["backupdir"] + "/backup-#{}" if File.exist?(backup_path) ui.error "Backup path (#{backup_path}) exists. Will not overwrite." exit 1 end ui.msg("Backing up roles") FileUtils.mkdir_p(backup_path + "/roles") Chef::Role.list.keys.each do |role| begin remote_role = Chef::Role.load(role) File.open(backup_path + "/roles/#{role}.json", "w") do |file| file.puts JSON.pretty_generate(remote_role) end rescue Exception => e ui.error "Unable to dump #{role} role (#{e.})" end end ui.msg("Backing up environments") FileUtils.mkdir_p(backup_path + "/environments") Chef::Environment.list.keys.each do |environment| begin env = Chef::Environment.load(environment) File.open(backup_path + "/environments/#{environment}.json", "w") do |file| file.puts JSON.pretty_generate(env) end rescue Exception => e ui.error "Unable to dump #{environment} environment (#{e.})" end end ui.msg("Backing up databags") FileUtils.mkdir_p(backup_path + "/databags") Chef::DataBag.list.keys.each do |bag| Dir.mkdir(backup_path + "/databags/" + bag) Chef::DataBag.load(bag).keys.each do |item| begin data = Chef::DataBagItem.load(bag,item) File.open(backup_path + "/databags/#{bag}/#{item}.json", "w") do |file| file.puts JSON.pretty_generate(data) end rescue Exception => e ui.error "Unable to dump item #{item} from databag #{bag} (#{e.})" end end end end |
#run ⇒ Object
14 15 16 17 |
# File 'lib/chef/knife/sharp-backup.rb', line 14 def run setup() backup() end |
#setup ⇒ Object
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 |
# File 'lib/chef/knife/sharp-backup.rb', line 19 def setup # Sharp config cfg_files = [ "/etc/sharp-config.yml", "~/.chef/sharp-config.yml" ] loaded = false cfg_files.each do |cfg_file| begin @cfg = YAML::load_file(File.(cfg_file)) loaded = true rescue Exception => e ui.error "Error on loading config : #{e.inspect}" if config[:debug] end end unless loaded == true ui.error "config could not be loaded ! Tried the following files : #{cfg_files.join(", ")}" exit 1 end # Knife config if (ENV['HOME'] && File.exist?(File.join(ENV['HOME'], '.chef', 'knife.rb'))) Chef::Config.from_file(File.join(ENV['HOME'], '.chef', 'knife.rb')) else ui.error "Cannot find knife.rb config file" exit 1 end end |