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.



45
46
47
48
49
# File 'lib/ridley/sandbox_uploader.rb', line 45

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.



42
43
44
# File 'lib/ridley/sandbox_uploader.rb', line 42

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.



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

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.



43
44
45
# File 'lib/ridley/sandbox_uploader.rb', line 43

def options
  @options
end

Class Method Details

.checksum(path) ⇒ 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:

  • path (String)

    file to checksum

Returns:

  • (String)

    the binary checksum of the contents of the file



12
13
14
# File 'lib/ridley/sandbox_uploader.rb', line 12

def checksum(path)
  File.open(path, 'rb') { |f| checksum_io(f, Digest::MD5.new) }
end

.checksum64(path) ⇒ 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:

  • path (String)

Returns:

  • (String)

    a base64 encoded checksum



23
24
25
# File 'lib/ridley/sandbox_uploader.rb', line 23

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

.checksum_io(io, digest) ⇒ 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.

Parameters:

  • io (String)
  • digest (Object)

Returns:

  • (String)


31
32
33
34
35
36
# File 'lib/ridley/sandbox_uploader.rb', line 31

def checksum_io(io, digest)
  while chunk = io.read(1024 * 8)
    digest.update(chunk)
  end
  digest.hexdigest
end

Instance Method Details

#upload(sandbox, chk_id, path) ⇒ 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

  • path (String)

    path to the file to upload

Returns:

  • (Hash, nil)


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
# File 'lib/ridley/sandbox_uploader.rb', line 60

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

  unless checksum[:needs_upload]
    return nil
  end

  headers  = {
    'Content-Type' => 'application/x-binary',
    'content-md5' => self.class.checksum64(path)
  }
  contents = File.open(path, 'rb') { |f| f.read }

  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
    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, contents, headers)
  rescue Ridley::Errors::HTTPError => ex
    abort(ex)
  end
end