Class: Hygroscope::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/hygroscope/payload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, region, profile) ⇒ Payload

Returns a new instance of Payload.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hygroscope/payload.rb', line 9

def initialize(path, region, profile)
  @path = path
  @region = region
  @profile = profile
  @credentials = Aws::SharedCredentials.new(profile_name: @profile)

  # TODO: This will fail if using root creds or lacking GetUser priv,
  # neither of which should be the case when using hygroscope -- but
  # we should check and error before getting to this point.
  @account_id = Aws::IAM::Client.new(region: @region, credentials: @credentials).get_user.user.arn.split(':')[4]
  @region = ENV['AWS_REGION'] || 'us-east-1'
  @bucket = "hygroscope-payloads-#{@account_id}-#{@region}"
  @name = "payload-#{Time.new.to_i}.zip"

  @client = Aws::S3::Client.new(region: @region, credentials: @credentials)
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



7
8
9
# File 'lib/hygroscope/payload.rb', line 7

def archive
  @archive
end

#bucketObject (readonly)

Returns the value of attribute bucket.



7
8
9
# File 'lib/hygroscope/payload.rb', line 7

def bucket
  @bucket
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/hygroscope/payload.rb', line 7

def key
  @key
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/hygroscope/payload.rb', line 7

def path
  @path
end

#prefixObject



26
27
28
# File 'lib/hygroscope/payload.rb', line 26

def prefix
  @prefix || File.dirname(File.dirname(@path))
end

Instance Method Details

#create_bucketObject



34
35
36
37
# File 'lib/hygroscope/payload.rb', line 34

def create_bucket
  # Returns success if bucket already exists
  @client.create_bucket(bucket: @bucket, acl: 'private')
end

#generate_url(_timeout = 3600) ⇒ Object



63
64
65
66
# File 'lib/hygroscope/payload.rb', line 63

def generate_url(_timeout = 3600)
  signer = Aws::S3::Presigner.new(client: @client)
  signer.presigned_url(:get_object, bucket: @bucket, key: key)
end

#prepareObject



39
40
41
42
43
44
45
# File 'lib/hygroscope/payload.rb', line 39

def prepare
  archive_path = File.join(Dir.tmpdir, @name)
  Archive::Zip.archive(archive_path, "#{@path}/.")

  @archive = File.open(archive_path)
  at_exit { File.unlink(@archive) }
end

#sendObject



47
48
49
50
51
52
53
# File 'lib/hygroscope/payload.rb', line 47

def send
  @client.put_object(
    bucket: @bucket,
    key: key,
    body: @archive
  )
end

#upload!Object



55
56
57
58
59
60
61
# File 'lib/hygroscope/payload.rb', line 55

def upload!
  create_bucket
  prepare
  send

  "s3://#{@bucket}/#{key}"
end