Class: Ridley::Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/ridley/resources/sandbox.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id, checksums) ⇒ Sandbox

Returns a new instance of Sandbox.



51
52
53
54
55
# File 'lib/ridley/resources/sandbox.rb', line 51

def initialize(connection, id, checksums)
  @connection = connection
  @sandbox_id = id
  @checksums = checksums
end

Instance Attribute Details

#checksumsObject (readonly)

Returns the value of attribute checksums.



49
50
51
# File 'lib/ridley/resources/sandbox.rb', line 49

def checksums
  @checksums
end

#sandbox_idObject (readonly)

Returns the value of attribute sandbox_id.



48
49
50
# File 'lib/ridley/resources/sandbox.rb', line 48

def sandbox_id
  @sandbox_id
end

Class Method Details

.checksum(path) ⇒ String

Checksum the file at the given filepath for a Chef API.

Parameters:

  • path (String)

Returns:

  • (String)


22
23
24
# File 'lib/ridley/resources/sandbox.rb', line 22

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

.checksum64(path) ⇒ String

Checksum and encode the file at the given filepath for uploading

Parameters:

  • path (String)

Returns:

  • (String)

    a base64 encoded checksum



32
33
34
# File 'lib/ridley/resources/sandbox.rb', line 32

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

.checksum_io(io, digest) ⇒ String

Parameters:

  • io (String)
  • digest (Object)

Returns:

  • (String)


40
41
42
43
44
45
# File 'lib/ridley/resources/sandbox.rb', line 40

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

.create(connection, checksums = []) ⇒ Ridley::Sandbox

Parameters:

Returns:



8
9
10
11
12
13
14
15
# File 'lib/ridley/resources/sandbox.rb', line 8

def create(connection, checksums = [])
  sumhash = { checksums: Hash.new }.tap do |chks|
    Array(checksums).each { |chk| chks[:checksums][chk] = nil }
  end

  attrs = connection.post("sandboxes", sumhash.to_json).body
  new(connection, attrs[:sandbox_id], attrs[:checksums])
end

Instance Method Details

#checksum(chk_id) ⇒ Object



57
58
59
# File 'lib/ridley/resources/sandbox.rb', line 57

def checksum(chk_id)
  checksums.fetch(chk_id.to_sym)
end

#commitObject



97
98
99
# File 'lib/ridley/resources/sandbox.rb', line 97

def commit
  connection.put("sandboxes/#{sandbox_id}", { is_completed: true }.to_json).body
end

#to_sObject



101
102
103
# File 'lib/ridley/resources/sandbox.rb', line 101

def to_s
  "#{sandbox_id}: #{checksums}"
end

#upload(chk_id, path) ⇒ Hash?

Parameters:

  • chk_id (String)
  • path (String)

Returns:

  • (Hash, nil)


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
# File 'lib/ridley/resources/sandbox.rb', line 65

def upload(chk_id, path)
  checksum = self.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 }

  # Hosted Chef returns Amazon S3 URLs for where to upload
  # checksums to. OSS Chef Server and Hosted Chef return URLs
  # to the same Chef API that the Sandbox creation request was
  # sent to.
  #
  # The connection object is duplicated to ensure all of our connection
  # settings persist, but the scheme, host, and port are set to the
  # value of the given checksum.
  conn = connection.send(:conn).dup

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

  conn.url_prefix = url.to_s

  conn.put(upload_path, contents, headers)
end