Class: Chef::Checksum

Inherits:
Object show all
Defined in:
lib/chef/checksum.rb,
lib/chef/checksum/storage/filesystem.rb

Overview

Chef::Checksum

Checksum for an individual file; e.g., used for sandbox/cookbook uploading to track which files the system already manages.

Defined Under Namespace

Classes: Storage

Constant Summary collapse

DESIGN_DOCUMENT =
{
  "version" => 1,
  "language" => "javascript",
  "views" => {
    "all" => {
      "map" => "function(doc) {\nif (doc.chef_type == \"checksum\") {\nemit(doc.checksum, doc);\n}\n}\n"
    },
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(checksum = nil, couchdb = nil) ⇒ Checksum

Creates a new Chef::Checksum object.

Arguments

checksum:

the MD5 content hash of the file

couchdb:

An instance of Chef::CouchDB

Returns

object<Chef::Checksum>

Duh. :)


61
62
63
64
65
66
# File 'lib/chef/checksum.rb', line 61

def initialize(checksum=nil, couchdb=nil)
  @create_time = Time.now.iso8601
  @checksum = checksum
  @original_committed_file_location = nil
  @storage = Storage::Filesystem.new(Chef::Config.checksum_path, checksum)
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum


27
28
29
# File 'lib/chef/checksum.rb', line 27

def checksum
  @checksum
end

#couchdb_idObject

Returns the value of attribute couchdb_id


28
29
30
# File 'lib/chef/checksum.rb', line 28

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev


28
29
30
# File 'lib/chef/checksum.rb', line 28

def couchdb_rev
  @couchdb_rev
end

#create_timeObject

Returns the value of attribute create_time


27
28
29
# File 'lib/chef/checksum.rb', line 27

def create_time
  @create_time
end

#original_committed_file_locationObject (readonly)

When a Checksum commits a sandboxed file to its final home in the checksum repo, this attribute will have the original on-disk path where the file was stored; it will be used if the commit is reverted to restore the sandbox to the pre-commit state.


36
37
38
# File 'lib/chef/checksum.rb', line 36

def original_committed_file_location
  @original_committed_file_location
end

#storageObject (readonly)

Returns the value of attribute storage


30
31
32
# File 'lib/chef/checksum.rb', line 30

def storage
  @storage
end

Class Method Details

.cdb_all_checksums(couchdb = nil) ⇒ Object


141
142
143
144
# File 'lib/chef/checksum.rb', line 141

def self.cdb_all_checksums(couchdb = nil)
  rs = (couchdb || Chef::CouchDB.new).list("checksums", true)
  rs["rows"].inject({}) { |hash_result, r| hash_result[r['key']] = 1; hash_result }
end

.cdb_list(inflate = false, couchdb = nil) ⇒ Object


135
136
137
138
139
# File 'lib/chef/checksum.rb', line 135

def self.cdb_list(inflate=false, couchdb=nil)
  rs = (couchdb || Chef::CouchDB.new).list("checksums", inflate)
  lookup = (inflate ? "value" : "key")
  rs["rows"].collect { |r| r[lookup] }        
end

.cdb_load(checksum, couchdb = nil) ⇒ Object


146
147
148
149
# File 'lib/chef/checksum.rb', line 146

def self.cdb_load(checksum, couchdb=nil)
  # Probably want to look for a view here at some point
  (couchdb || Chef::CouchDB.new).load("checksum", checksum)
end

.create_design_document(couchdb = nil) ⇒ Object

Couchdb


131
132
133
# File 'lib/chef/checksum.rb', line 131

def self.create_design_document(couchdb=nil)
  (couchdb || Chef::CouchDB.new).create_design_document("checksums", DESIGN_DOCUMENT)
end

.json_create(o) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chef/checksum.rb', line 81

def self.json_create(o)
  checksum = new(o['checksum'])
  checksum.create_time = o['create_time']

  if o.has_key?('_rev')
    checksum.couchdb_rev = o["_rev"]
    o.delete("_rev")
  end
  if o.has_key?("_id")
    checksum.couchdb_id = o["_id"]
    o.delete("_id")
  end
  checksum
end

Instance Method Details

#cdb_destroy(couchdb = nil) ⇒ Object


151
152
153
# File 'lib/chef/checksum.rb', line 151

def cdb_destroy(couchdb=nil)
  (couchdb || Chef::CouchDB.new).delete("checksum", checksum, @couchdb_rev)
end

#cdb_save(couchdb = nil) ⇒ Object


155
156
157
# File 'lib/chef/checksum.rb', line 155

def cdb_save(couchdb=nil)
  @couchdb_rev = (couchdb || Chef::CouchDB.new).store("checksum", checksum, self)["rev"]
end

#commit_sandbox_file(sandbox_file) ⇒ Object

Moves the given sandbox_file into the checksum repo using the path given by file_location and saves the Checksum to the database


98
99
100
101
102
103
# File 'lib/chef/checksum.rb', line 98

def commit_sandbox_file(sandbox_file)
  @original_committed_file_location = sandbox_file
  Chef::Log.info("Commiting sandbox file: move #{sandbox_file} to #{@storage}")
  @storage.commit(sandbox_file)
  cdb_save
end

#purgeObject

Removes the on-disk file backing this checksum object, then removes it from the database


122
123
124
125
# File 'lib/chef/checksum.rb', line 122

def purge
  purge_file
  cdb_destroy
end

#revert_sandbox_file_commitObject

Moves the checksum file back to its pre-commit location and deletes the checksum object from the database, effectively undoing commit_sandbox_file. Raises Chef::Exceptions::IllegalChecksumRevert if the original file location is unknown, which is will be the case if commit_sandbox_file was not previously called


110
111
112
113
114
115
116
117
118
# File 'lib/chef/checksum.rb', line 110

def revert_sandbox_file_commit
  unless original_committed_file_location
    raise Chef::Exceptions::IllegalChecksumRevert, "Checksum #{self.inspect} cannot be reverted because the original sandbox file location is not known"
  end

  Chef::Log.warn("Reverting sandbox file commit: moving #{@storage} back to #{original_committed_file_location}")
  @storage.revert(original_committed_file_location)
  cdb_destroy
end

#to_json(*a) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/checksum.rb', line 68

def to_json(*a)
  result = {
    :checksum => checksum,
    :create_time => create_time,
    :json_class => self.class.name,
    :chef_type => 'checksum',

    # For Chef::CouchDB (id_to_name, name_to_id)
    :name => checksum
  }
  result.to_json(*a)
end