Skip to content

Commit 02b3d42

Browse files
committed
Add backup attribute to file resource (close #124)
1 parent 8c44072 commit 02b3d42

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

itamae.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.require_paths = ["lib"]
1919

2020
spec.add_runtime_dependency "thor"
21-
spec.add_runtime_dependency "specinfra", [">= 2.37.0", "< 3.0.0"]
21+
spec.add_runtime_dependency "specinfra", [">= 2.54.0", "< 3.0.0"]
2222
spec.add_runtime_dependency "hashie"
2323
spec.add_runtime_dependency "ansi"
2424
spec.add_runtime_dependency "schash", "~> 0.1.0"

lib/itamae/resource/file.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
module Itamae
44
module Resource
55
class File < Base
6+
BACKUP_PATH = '/var/itamae/backup'.freeze
7+
68
define_attribute :action, default: :create
79
define_attribute :path, type: String, default_name: true
810
define_attribute :content, type: String, default: nil
911
define_attribute :mode, type: String
1012
define_attribute :owner, type: String
1113
define_attribute :group, type: String
14+
define_attribute :backup, type: [FalseClass, Integer], default: 5
1215
define_attribute :block, type: Proc, default: proc {}
1316

1417
def pre_action
@@ -58,6 +61,8 @@ def show_differences
5861
end
5962

6063
def action_create(options)
64+
backup if current.exist
65+
6166
if !current.exist && !@temppath
6267
run_command(["touch", attributes.path])
6368
end
@@ -84,6 +89,8 @@ def action_delete(options)
8489
end
8590

8691
def action_edit(options)
92+
backup if current.exist
93+
8794
if attributes.mode
8895
run_specinfra(:change_file_mode, @temppath, attributes.mode)
8996
else
@@ -183,6 +190,35 @@ def send_tempfile
183190
f.unlink if f
184191
end
185192
end
193+
194+
def backup
195+
return if !attributes.backup || attributes.backup <= 0
196+
197+
savetime = Time.now.strftime('%Y%m%d%H%M%S')
198+
backup_filename = "#{attributes.path}.itamae-#{savetime}"
199+
backup_path = ::File.join(BACKUP_PATH, backup_filename)
200+
backup_directory = ::File.dirname(backup_path)
201+
202+
run_specinfra(:create_file_as_directory, backup_directory)
203+
run_specinfra(:change_file_mode, backup_directory, '0777')
204+
run_specinfra(:copy_file, attributes.path, backup_path)
205+
Itamae.logger.info "#{attributes.path} backed up to #{backup_path}"
206+
207+
basename = ::File.basename(attributes.path)
208+
backup_files = run_command(['ls', '-1', backup_directory])
209+
backup_files = backup_files.stdout.chomp.split("\n").select { |f|
210+
f.match(/\A#{basename}.itamae-[0-9]+\z/)
211+
}.reverse
212+
213+
if backup_files.length > attributes.backup
214+
remainder = backup_files.slice(attributes.backup..-1)
215+
remainder.each do |backup_to_delete|
216+
backup_to_delete = ::File.join(backup_directory, backup_to_delete)
217+
run_specinfra(:remove_file, backup_to_delete)
218+
Itamae.logger.info "#{attributes.path} removed backup at #{backup_to_delete}"
219+
end
220+
end
221+
end
186222
end
187223
end
188224
end

spec/integration/default_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@
5151
end
5252
end
5353

54+
describe file('/var/itamae/backup/tmp') do
55+
it { should be_directory }
56+
it { should be_mode 777 }
57+
end
58+
59+
describe command('cat /var/itamae/backup/tmp/file.itamae-*') do
60+
its(:stdout) { should match(/Hello World/) }
61+
its(:exit_status) { should eq(0) }
62+
end
63+
5464
describe file('/tmp/file') do
5565
it { should be_file }
56-
its(:content) { should match(/Hello World/) }
66+
its(:content) { should match(/Hello New World/) }
5767
it { should be_mode 777 }
5868
end
5969

spec/integration/recipes/default.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@
148148
mode "777"
149149
end
150150

151+
file "/tmp/file" do
152+
content "Hello New World"
153+
mode "777"
154+
end
155+
151156
execute "echo 'Hello Execute' > /tmp/execute"
152157

153158
file "/tmp/never_exist1" do

0 commit comments

Comments
 (0)