knife-backup 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -33,8 +33,8 @@ knife backup --help
33
33
  Currently the available commands are:
34
34
 
35
35
  ```bash
36
- knife backup export [-d DIR]
37
- knife backup restore [-d DIR]
36
+ knife backup export [-D DIR]
37
+ knife backup restore [-D DIR]
38
38
  ```
39
39
 
40
40
  Note: you should treat this as beta software; I'm using it with success for my needs and hopefully you will find it useful too.
@@ -63,6 +63,8 @@ Pull requests are very welcome! Ideally create a topic branch for every separate
63
63
 
64
64
  Created and maintained by [Marius Ducea][mdxp] (<marius.ducea@gmail.com>)
65
65
 
66
+ Based on the original plugins by [Steven Danna][stevendanna] and [Joshua Timberman][jtimberman]
67
+
66
68
  ## License
67
69
 
68
70
  Apache License, Version 2.0 (see [LICENSE][license])
@@ -30,10 +30,10 @@ module ServerBackup
30
30
  require 'chef/cookbook_loader'
31
31
  end
32
32
 
33
- banner "knife backup export [-d DIR]"
33
+ banner "knife backup export [-D DIR]"
34
34
 
35
35
  option :backup_dir,
36
- :short => "-d DIR",
36
+ :short => "-D DIR",
37
37
  :long => "--backup-directory DIR",
38
38
  :description => "Store backup data in DIR. DIR will be created if it does not already exist.",
39
39
  :default => Chef::Config[:knife][:chef_server_backup_dir] ? Chef::Config[:knife][:chef_server_backup_dir] : File.join(".chef", "chef_server_backup")
@@ -17,7 +17,7 @@
17
17
 
18
18
  class Chef
19
19
  class Knife
20
- class CookbookUpload
20
+ class CookbookUpload < Knife
21
21
  def check_for_dependencies!(cookbook)
22
22
  end
23
23
  end
@@ -34,10 +34,10 @@ module ServerBackup
34
34
  require 'chef/api_client'
35
35
  end
36
36
 
37
- banner "knife backup restore [-d DIR]"
37
+ banner "knife backup restore [-D DIR]"
38
38
 
39
39
  option :backup_dir,
40
- :short => "-d DIR",
40
+ :short => "-D DIR",
41
41
  :long => "--backup-directory DIR",
42
42
  :description => "Restore backup data from DIR.",
43
43
  :default => Chef::Config[:knife][:chef_server_backup_dir] ? Chef::Config[:knife][:chef_server_backup_dir] : File.join(".chef", "chef_server_backup")
@@ -67,17 +67,21 @@ module ServerBackup
67
67
  end
68
68
 
69
69
  def data_bags
70
- ui.msg "Restoring data bags"
70
+ ui.info "=== Restoring data bags ==="
71
71
  loader = Chef::Knife::Core::ObjectLoader.new(Chef::DataBagItem, ui)
72
72
  dbags = Dir.glob(File.join(config[:backup_dir], "data_bags", '*'))
73
73
  dbags.each do |bag|
74
74
  bag_name = File.basename(bag)
75
- ui.msg "Creating data bag #{bag_name}"
76
- rest.post_rest("data", { "name" => bag_name})
75
+ ui.info "Restoring data_bag[#{bag_name}]"
76
+ begin
77
+ rest.post_rest("data", { "name" => bag_name})
78
+ rescue Net::HTTPServerException => e
79
+ handle_error 'data_bag', bag_name, e
80
+ end
77
81
  dbag_items = Dir.glob(File.join(bag, "*"))
78
82
  dbag_items.each do |item_path|
79
83
  item_name = File.basename(item_path, '.json')
80
- ui.msg "Restoring data_bag_item[#{bag_name}::#{item_name}]"
84
+ ui.info "Restoring data_bag_item[#{bag_name}::#{item_name}]"
81
85
  item = loader.load_from("data_bags", bag_name, item_path)
82
86
  dbag = Chef::DataBagItem.new
83
87
  dbag.data_bag(bag_name)
@@ -90,10 +94,10 @@ module ServerBackup
90
94
 
91
95
  def restore_standard(component, klass)
92
96
  loader = Chef::Knife::Core::ObjectLoader.new(klass, ui)
93
- ui.msg "Restoring #{component}"
97
+ ui.info "=== Restoring #{component} ==="
94
98
  files = Dir.glob(File.join(config[:backup_dir], component, "*.json"))
95
99
  files.each do |f|
96
- ui.msg "Updating #{component} from #{f}"
100
+ ui.info "Restoring #{component} from #{f}"
97
101
  updated = loader.load_from(component, f)
98
102
  updated.save
99
103
  end
@@ -101,36 +105,54 @@ module ServerBackup
101
105
 
102
106
  def clients
103
107
  JSON.create_id = "no_thanks"
104
- ui.msg "Restoring clients"
108
+ ui.info "=== Restoring clients ==="
105
109
  clients = Dir.glob(File.join(config[:backup_dir], "clients", "*.json"))
106
110
  clients.each do |file|
107
111
  client = JSON.parse(IO.read(file))
108
112
  begin
109
- rest.post_rest("clients", {
113
+ rest.post_rest("clients", {
110
114
  :name => client['name'],
111
115
  :public_key => client['public_key'],
112
116
  :admin => client['admin']
113
- })
114
- rescue
115
- ui.msg "#{client['name']} already exists; skipping"
116
- end
117
+ })
118
+ rescue Net::HTTPServerException => e
119
+ handle_error 'client', client['name'], e
120
+ end
117
121
  end
118
122
  end
119
123
 
120
124
  def cookbooks
121
- ui.msg "Restoring cookbooks"
125
+ ui.info "=== Restoring cookbooks ==="
122
126
  cookbooks = Dir.glob(File.join(config[:backup_dir], "cookbooks", '*'))
123
127
  cookbooks.each do |cb|
124
128
  full_cb = cb.split("/").last
125
129
  cookbook = full_cb.reverse.split('-',2).last.reverse
126
130
  full_path = File.join(config[:backup_dir], "cookbooks", cookbook)
127
- File.symlink(full_cb, full_path)
128
- cbu = Chef::Knife::CookbookUpload.new
129
- cbu.name_args = [ cookbook ]
130
- cbu.config[:cookbook_path] = File.join(config[:backup_dir], "cookbooks")
131
- puts cbu.name_args
132
- cbu.run
133
- File.unlink(full_path)
131
+
132
+ begin
133
+ File.symlink(full_cb, full_path)
134
+ cbu = Chef::Knife::CookbookUpload.new
135
+ cbu.name_args = [ cookbook ]
136
+ cbu.config[:cookbook_path] = File.join(config[:backup_dir], "cookbooks")
137
+ ui.info "Restoring cookbook #{cbu.name_args}"
138
+ cbu.run
139
+ rescue Net::HTTPServerException => e
140
+ handle_error 'cookbook', full_cb, e
141
+ ensure
142
+ File.unlink(full_path)
143
+ end
144
+ end
145
+ end
146
+
147
+ def handle_error(type, name, error)
148
+ thing = "#{type}[#{name}]"
149
+ case error.response
150
+ when Net::HTTPConflict # 409
151
+ ui.warn "#{thing} already exists; skipping"
152
+ when Net::HTTPClientError # 4xx Catch All
153
+ ui.error "Failed to create #{thing}: #{error.response}; skipping"
154
+ else
155
+ ui.error "Failed to create #{thing}: #{error.response}; skipping"
134
156
  end
135
157
  end
136
158
 
@@ -1,5 +1,5 @@
1
1
  module Knife
2
2
  module Backup
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-27 00:00:00.000000000 Z
12
+ date: 2013-04-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef