Skip to content

Commit

Permalink
Add backup attribute to file resource (close itamae-kitchen#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
hico-horiuchi committed Mar 20, 2016
1 parent 1c1c97c commit 854962a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion itamae.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency "thor"
spec.add_runtime_dependency "specinfra", [">= 2.37.0", "< 3.0.0"]
spec.add_runtime_dependency "specinfra", [">= 2.54.0", "< 3.0.0"]
spec.add_runtime_dependency "hashie"
spec.add_runtime_dependency "ansi"
spec.add_runtime_dependency "schash", "~> 0.1.0"
Expand Down
36 changes: 36 additions & 0 deletions lib/itamae/resource/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
module Itamae
module Resource
class File < Base
BACKUP_PATH = '/var/itamae/backup'.freeze

define_attribute :action, default: :create
define_attribute :path, type: String, default_name: true
define_attribute :content, type: String, default: nil
define_attribute :mode, type: String
define_attribute :owner, type: String
define_attribute :group, type: String
define_attribute :backup, type: [FalseClass, Integer], default: 5
define_attribute :block, type: Proc, default: proc {}

def pre_action
Expand Down Expand Up @@ -56,6 +59,8 @@ def show_differences
end

def action_create(options)
backup if current.exist

if !current.exist && !@temppath
run_command(["touch", attributes.path])
end
Expand Down Expand Up @@ -94,6 +99,8 @@ def action_delete(options)
end

def action_edit(options)
backup if current.exist

if attributes.mode
run_specinfra(:change_file_mode, @temppath, attributes.mode)
else
Expand Down Expand Up @@ -180,6 +187,35 @@ def send_tempfile
f.unlink if f
end
end

def backup
return if !attributes.backup || attributes.backup <= 0

savetime = Time.now.strftime('%Y%m%d%H%M%S')
backup_filename = "#{attributes.path}.itamae-#{savetime}"
backup_path = ::File.join(BACKUP_PATH, backup_filename)
backup_directory = ::File.dirname(backup_path)

run_specinfra(:create_file_as_directory, backup_directory)
run_specinfra(:change_file_mode, backup_directory, '0777')
run_specinfra(:copy_file, attributes.path, backup_path)
Itamae.logger.info "#{attributes.path} backed up to #{backup_path}"

basename = ::File.basename(attributes.path)
backup_files = run_command(['ls', '-1', backup_directory])
backup_files = backup_files.stdout.chomp.split("\n").select { |f|
f.match(/\A#{basename}.itamae-[0-9]+\z/)
}.reverse

if backup_files.length > attributes.backup
remainder = backup_files.slice(attributes.backup..-1)
remainder.each do |backup_to_delete|
backup_to_delete = ::File.join(backup_directory, backup_to_delete)
run_specinfra(:remove_file, backup_to_delete)
Itamae.logger.info "#{attributes.path} removed backup at #{backup_to_delete}"
end
end
end
end
end
end
12 changes: 11 additions & 1 deletion spec/integration/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,19 @@
end
end

describe file('/var/itamae/backup/tmp') do
it { should be_directory }
it { should be_mode 777 }
end

describe command('cat /var/itamae/backup/tmp/file.itamae-*') do
its(:stdout) { should match(/Hello World/) }
its(:exit_status) { should eq(0) }
end

describe file('/tmp/file') do
it { should be_file }
its(:content) { should match(/Hello World/) }
its(:content) { should match(/Hello New World/) }
it { should be_mode 777 }
end

Expand Down
5 changes: 5 additions & 0 deletions spec/integration/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
mode "777"
end

file "/tmp/file" do
content "Hello New World"
mode "777"
end

execute "echo 'Hello Execute' > /tmp/execute"

file "/tmp/never_exist1" do
Expand Down

0 comments on commit 854962a

Please sign in to comment.