class ChefDK::Policyfile::UndoStack
Constants
- MAX_SIZE
Public Instance Methods
delete(id) { |record| ... }
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 93 def delete(id) undo_file = undo_file_for(id) unless File.exist?(undo_file) raise UndoRecordNotFound, "No undo record for id '#{id}' exists at #{undo_file}" end record = load_undo_record(undo_file) yield record if block_given? File.unlink(undo_file) record end
each_with_id() { |basename, load_undo_record(filename)| ... }
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 50 def each_with_id undo_record_files.each do |filename| yield File.basename(filename), load_undo_record(filename) end end
empty?()
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 42 def empty? size == 0 end
has_id?(id)
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 46 def has_id?(id) File.exist?(undo_file_for(id)) end
pop() { |record| ... }
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 80 def pop file_to_pop = undo_record_files.last if file_to_pop.nil? raise CantUndo, "No undo records exist in #{undo_dir}" end record = load_undo_record(file_to_pop) # if this hits an exception, we skip unlink yield record if block_given? File.unlink(file_to_pop) record end
push(undo_record)
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 60 def push(undo_record) ensure_undo_dir_exists record_id = Time.new.utc.strftime("%Y%m%d%H%M%S") path = File.join(undo_dir, record_id) with_file(path) do |f| f.print(FFI_Yajl::Encoder.encode(undo_record.for_serialization, pretty: true)) end records_to_delete = undo_record_files.size - MAX_SIZE if records_to_delete > 0 undo_record_files.take(records_to_delete).each do |file| File.unlink(file) end end self end
size()
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 38 def size undo_record_files.size end
undo_dir()
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 34 def undo_dir File.join(Helpers.chefdk_home, "undo") end
undo_records()
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 56 def undo_records undo_record_files.map { |f| load_undo_record(f) } end
Private Instance Methods
ensure_undo_dir_exists()
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 120 def ensure_undo_dir_exists return false if File.directory?(undo_dir) FileUtils.mkdir_p(undo_dir) end
load_undo_record(file)
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 111 def load_undo_record(file) data = FFI_Yajl::Parser.parse(IO.read(file)) UndoRecord.new.load(data) end
undo_file_for(id)
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 107 def undo_file_for(id) File.join(undo_dir, id) end
undo_record_files()
click to toggle source
# File lib/chef-dk/policyfile/undo_stack.rb, line 116 def undo_record_files Dir[File.join(undo_dir, "*")].sort end