Class: Ridley::SandboxUploader Private

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/ridley/sandbox_uploader.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_name, client_key, options = {}) ⇒ SandboxUploader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SandboxUploader.



39
40
41
42
43
# File 'lib/ridley/sandbox_uploader.rb', line 39

def initialize(client_name, client_key, options = {})
  @client_name = client_name
  @client_key  = client_key
  @options     = options
end

Instance Attribute Details

#client_keyObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
# File 'lib/ridley/sandbox_uploader.rb', line 36

def client_key
  @client_key
end

#client_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/ridley/sandbox_uploader.rb', line 35

def client_name
  @client_name
end

#optionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
# File 'lib/ridley/sandbox_uploader.rb', line 37

def options
  @options
end

Class Method Details

.checksum(io, digest = Digest::MD5.new) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the checksum of the contents of the file at the given filepath

Parameters:

  • io (String)

    a filepath or an IO

  • digest (Digest::Base) (defaults to: Digest::MD5.new)

Returns:

  • (String)

    the binary checksum of the contents of the file



13
14
15
16
17
18
# File 'lib/ridley/sandbox_uploader.rb', line 13

def checksum(io, digest = Digest::MD5.new)
  while chunk = io.read(1024 * 8)
    digest.update(chunk)
  end
  digest.hexdigest
end

.checksum64(io) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a base64 encoded checksum of the contents of the given file. This is the expected format of sandbox checksums given to the Chef Server.

Parameters:

  • io (String)

    a filepath or an IO

Returns:

  • (String)

    a base64 encoded checksum



28
29
30
# File 'lib/ridley/sandbox_uploader.rb', line 28

def checksum64(io)
  Base64.encode64([checksum(io)].pack("H*")).strip
end

Instance Method Details

#upload(sandbox, chk_id, file) ⇒ Hash?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Upload one file into the sandbox for the given checksum id

Parameters:

  • sandbox (Ridley::SandboxObject)
  • chk_id (String)

    checksum of the file being uploaded

  • file (String, #read)

    a filepath or an IO

Returns:

  • (Hash, nil)

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ridley/sandbox_uploader.rb', line 57

def upload(sandbox, chk_id, file)
  checksum = sandbox.checksum(chk_id)

  unless checksum[:needs_upload]
    return nil
  end

  io                  = file.respond_to?(:read) ? file : File.new(file, 'rb')
  calculated_checksum = self.class.checksum64(io)
  expected_checksum   = Base64.encode64([chk_id].pack('H*')).strip

  unless calculated_checksum == expected_checksum
    raise Errors::ChecksumMismatch,
      "Error uploading #{chk_id}. Expected #{expected_checksum} but got #{calculated_checksum}"
  end

  headers = {
    'Content-Type' => 'application/x-binary',
    'content-md5' => calculated_checksum
  }

  url         = URI(checksum[:url])
  upload_path = url.path
  url.path    = ""

  # versions prior to OSS Chef 11 will strip the port to upload the file to in the checksum
  # url returned. This will ensure we are uploading to the proper location.
  if sandbox.send(:resource).connection.foss?
    url.port = URI(sandbox.send(:resource).connection.server_url).port
  end

  begin
    io.rewind

    Faraday.new(url, self.options) do |c|
      c.response :chef_response
      c.response :follow_redirects
      c.request :chef_auth, self.client_name, self.client_key
      c.adapter :net_http
    end.put(upload_path, io.read, headers)
  rescue Ridley::Errors::HTTPError => ex
    abort(ex)
  end
end