Class: ChefCLI::Policyfile::UndoStack

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/chef-cli/policyfile/undo_stack.rb

Constant Summary collapse

MAX_SIZE =
10

Instance Method Summary collapse

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Instance Method Details

#delete(id) {|record| ... } ⇒ Object

Yields:

  • (record)


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef-cli/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_idObject



50
51
52
53
54
# File 'lib/chef-cli/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?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/chef-cli/policyfile/undo_stack.rb', line 42

def empty?
  size == 0
end

#has_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/chef-cli/policyfile/undo_stack.rb', line 46

def has_id?(id)
  File.exist?(undo_file_for(id))
end

#pop {|record| ... } ⇒ Object

Yields:

  • (record)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef-cli/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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef-cli/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

#sizeObject



38
39
40
# File 'lib/chef-cli/policyfile/undo_stack.rb', line 38

def size
  undo_record_files.size
end

#undo_dirObject



34
35
36
# File 'lib/chef-cli/policyfile/undo_stack.rb', line 34

def undo_dir
  File.join(Helpers.package_home, "undo")
end

#undo_recordsObject



56
57
58
# File 'lib/chef-cli/policyfile/undo_stack.rb', line 56

def undo_records
  undo_record_files.map { |f| load_undo_record(f) }
end