Skip to content

Commit 854962a

Browse files
committed
Add backup attribute to file resource (close #124)
1 parent 1c1c97c commit 854962a

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
@@ -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
@@ -94,6 +99,8 @@ def action_delete(options)
9499
end
95100

96101
def action_edit(options)
102+
backup if current.exist
103+
97104
if attributes.mode
98105
run_specinfra(:change_file_mode, @temppath, attributes.mode)
99106
else
@@ -180,6 +187,35 @@ def send_tempfile
180187
f.unlink if f
181188
end
182189
end
190+
191+
def backup
192+
return if !attributes.backup || attributes.backup <= 0
193+
194+
savetime = Time.now.strftime('%Y%m%d%H%M%S')
195+
backup_filename = "#{attributes.path}.itamae-#{savetime}"
196+
backup_path = ::File.join(BACKUP_PATH, backup_filename)
197+
backup_directory = ::File.dirname(backup_path)
198+
199+
run_specinfra(:create_file_as_directory, backup_directory)
200+
run_specinfra(:change_file_mode, backup_directory, '0777')
201+
run_specinfra(:copy_file, attributes.path, backup_path)
202+
Itamae.logger.info "#{attributes.path} backed up to #{backup_path}"
203+
204+
basename = ::File.basename(attributes.path)
205+
backup_files = run_command(['ls', '-1', backup_directory])
206+
backup_files = backup_files.stdout.chomp.split("\n").select { |f|
207+
f.match(/\A#{basename}.itamae-[0-9]+\z/)
208+
}.reverse
209+
210+
if backup_files.length > attributes.backup
211+
remainder = backup_files.slice(attributes.backup..-1)
212+
remainder.each do |backup_to_delete|
213+
backup_to_delete = ::File.join(backup_directory, backup_to_delete)
214+
run_specinfra(:remove_file, backup_to_delete)
215+
Itamae.logger.info "#{attributes.path} removed backup at #{backup_to_delete}"
216+
end
217+
end
218+
end
183219
end
184220
end
185221
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)