Class: Ridley::SandboxUploader Private

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

Overview

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.

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sandbox) ⇒ 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


77
78
79
# File 'lib/ridley/sandbox_uploader.rb', line 77

def initialize(sandbox)
  @sandbox = sandbox
end

Instance Attribute Details

#sandboxObject (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.


72
73
74
# File 'lib/ridley/sandbox_uploader.rb', line 72

def sandbox
  @sandbox
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


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

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 hte given file. This is the expected format of sandbox checksums given to the Chef Server.

Parameters:

  • path (String)

Returns:

  • (String)

    a base64 encoded checksum


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

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

.checksum64_string(string) ⇒ 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 hte given string. This is the expected format of sandbox checksums given to the Chef Server.

Parameters:

  • content (String)

Returns:

  • (String)

    a base64 encoded checksum


53
54
55
# File 'lib/ridley/sandbox_uploader.rb', line 53

def checksum64_string(string)
  Base64.encode64([Digest::MD5.digest(string)].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)

61
62
63
64
65
66
# File 'lib/ridley/sandbox_uploader.rb', line 61

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

.upload(sandbox, checksums, options = {}) ⇒ Object

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.

Concurrently upload all of the files in the given sandbox and then clean up after ourselves

Parameters:

  • sandbox (Ridley::SandboxResource)
  • checksums (Hash)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :pool_size (Integer) — default: 12

    the amount of concurrent uploads to perform


14
15
16
17
18
19
20
21
22
# File 'lib/ridley/sandbox_uploader.rb', line 14

def upload(sandbox, checksums, options = {})
  options = options.reverse_merge(
    pool_size: 12
  )
  uploader = pool(size: options[:pool_size], args: [sandbox])
  uploader.multi_upload(checksums)
ensure
  uploader.terminate if uploader && uploader.alive?
end

Instance Method Details

#multi_upload(checksums) ⇒ Object

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.

Concurrently upload multiple files into a sandbox

Examples:

uploading multiple checksums


sandbox.multi_upload(
  "e5a0f6b48d0712382295ff30bec1f9cc" => "/Users/reset/code/rbenv-cookbook/recipes/default.rb",
  "de6532a7fbe717d52020dc9f3ae47dbe" => "/Users/reset/code/rbenv-cookbook/recipes/ohai_plugin.rb"
)

Parameters:

  • checksums (Hash)

    a hash of file checksums and file paths or IO objects


92
93
94
95
96
# File 'lib/ridley/sandbox_uploader.rb', line 92

def multi_upload(checksums)
  checksums.collect do |chk_id, path_or_io|
    future.upload(chk_id, path_or_io)
  end.map(&:value)
end

#upload(chk_id, path_or_io) ⇒ 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:

  • chk_id (String)

    checksum of the file being uploaded

  • path_or_io (String, #read)

    path to the file to upload or an IO which returns the content

Returns:

  • (Hash, nil)

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ridley/sandbox_uploader.rb', line 106

def upload(chk_id, path_or_io)
  checksum = self.checksum(chk_id)
  
  unless checksum[:needs_upload]
    return nil
  end

  if path_or_io.respond_to? :read
    io = path_or_io
  elsif path_or_io.kind_of? String
    io = File.open(path_or_io, 'rb')
  else
    raise ArgumentError, "Expected a String or an IO, but got #{path_or_io.inspect}"
  end

  contents = io.read

  headers  = {
    'Content-Type' => 'application/x-binary',
    'content-md5' => self.class.checksum64_string(contents)
  }

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

  begin
    Faraday.new(url, client.options.slice(*Connection::VALID_OPTIONS)) do |c|
      c.response :chef_response
      c.response :follow_redirects
      c.request :chef_auth, client.client_name, client.client_key
      c.adapter :net_http
    end.put(upload_path, contents, headers)
  rescue Ridley::Errors::HTTPError => ex
    abort(ex)
  end
end