Skip to content

Commit b9224b9

Browse files
committed
Add backup attribute to file resource (close #124)
1 parent 216d1e4 commit b9224b9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

lib/itamae/resource/file.rb

Lines changed: 35 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
@@ -56,6 +59,8 @@ def show_differences
5659
end
5760

5861
def action_create(options)
62+
backup if current.exist
63+
5964
if !current.exist && !@temppath
6065
run_command(["touch", attributes.path])
6166
end
@@ -93,6 +98,8 @@ def action_delete(options)
9398
end
9499

95100
def action_edit(options)
101+
backup if current.exist
102+
96103
if attributes.mode
97104
run_specinfra(:change_file_mode, @temppath, attributes.mode)
98105
else
@@ -171,6 +178,34 @@ def send_tempfile
171178
f.unlink if f
172179
end
173180
end
181+
182+
def backup
183+
return if !attributes.backup || attributes.backup <= 0
184+
185+
savetime = Time.now.strftime('%Y%m%d%H%M%S')
186+
backup_filename = "#{attributes.path}.itamae-#{savetime}"
187+
backup_path = ::File.join(BACKUP_PATH, backup_filename)
188+
backup_directory = ::File.dirname(backup_path)
189+
190+
run_command(['install', '-d', '-m=777', backup_directory])
191+
run_command(['cp', '-p', attributes.path, backup_path])
192+
Itamae.logger.info "#{attributes.path} backed up to #{backup_path}"
193+
194+
basename = ::File.basename(attributes.path)
195+
backup_files = run_command(['ls', '-1', backup_directory])
196+
backup_files = backup_files.stdout.chomp.split("\n").select { |f|
197+
f.match(/\A#{basename}.itamae-[0-9]+\z/)
198+
}.reverse
199+
200+
if backup_files.length > attributes.backup
201+
remainder = backup_files.slice(attributes.backup..-1)
202+
remainder.each do |backup_to_delete|
203+
backup_to_delete = ::File.join(backup_directory, backup_to_delete)
204+
run_specinfra(:remove_file, backup_to_delete)
205+
Itamae.logger.info "#{attributes.path} removed backup at #{backup_to_delete}"
206+
end
207+
end
208+
end
174209
end
175210
end
176211
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)