-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add backup attribute to file resource (close #124) #191
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -58,6 +61,8 @@ def show_differences | |
end | ||
|
||
def action_create(options) | ||
backup if current.exist | ||
|
||
if !current.exist && !@temppath | ||
run_command(["touch", attributes.path]) | ||
end | ||
|
@@ -84,6 +89,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 | ||
|
@@ -183,6 +190,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Files backed up should have the same permission. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So, I try to use |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current implementation is good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thank you! |
||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you add tests to check backed-up file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't specify the backed-up file because file name is decided from date. describe command('cat /var/itamae/backup/tmp/file.itamae-*') do
its(:stdout) { should match(/Hello World/) }
its(:exit_status) { should eq(0) }
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
LGTM |
||
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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a concern about permission. For instance, the following situation can confuse users:
/etc/foo.conf
/var/itamae/backup/etc
is mode 755 and owned by user A/etc/bar.conf
/var/itamae/backup/etc
because it is owned by user AWDYT?
I think it is better all directories under
/var/lib/itamae
are755
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryotarai Yes, I agree.
Isn't it mistake with
755
or777
?I try to use
run_command(['install', '-d', '-m=777', backup_directory])
.