Class: Jets::TmpLoader

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/jets/tmp_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Constructor Details

#initialize(yaml_path = nil) ⇒ TmpLoader

Returns a new instance of TmpLoader.



9
10
11
12
13
14
15
# File 'lib/jets/tmp_loader.rb', line 9

def initialize(yaml_path=nil)
  yaml_path ||= "#{Jets.root}/handlers/data.yml"
  return unless File.exist?(yaml_path)
  @data = YAML.load_file(yaml_path)
  @s3_bucket = @data['s3_bucket']
  @rack_zip = @data['rack_zip']
end

Class Method Details

.load!Object



5
6
7
# File 'lib/jets/tmp_loader.rb', line 5

def self.load!
  new.load
end

Instance Method Details

#download(key, dest) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/jets/tmp_loader.rb', line 34

def download(key, dest)
  # https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#get_object-instance_method
  # stream object directly to disk
  s3.get_object(response_target: dest,
                bucket: @s3_bucket,
                key: key)
end

#download_and_extract(zip_file, folder_dest) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/jets/tmp_loader.rb', line 26

def download_and_extract(zip_file, folder_dest)
  s3_key = "jets/code/#{zip_file}" # jets/code/rack-checksum.zip
  download_path = "/tmp/#{zip_file}" # /tmp/rack-checksum.zip

  download(s3_key, download_path)
  unzip(download_path, folder_dest)
end

#loadObject



17
18
19
# File 'lib/jets/tmp_loader.rb', line 17

def load
  rack
end

#rackObject



21
22
23
24
# File 'lib/jets/tmp_loader.rb', line 21

def rack
  return unless @rack_zip
  download_and_extract(@rack_zip, '/tmp/rack')
end

#sh(command) ⇒ Object



46
47
48
49
50
51
# File 'lib/jets/tmp_loader.rb', line 46

def sh(command)
  puts "=> #{command}"
  success = system(command)
  raise "Command #{command} failed" unless success
  success
end

#unzip(zipfile, folder_dest) ⇒ Object



42
43
44
# File 'lib/jets/tmp_loader.rb', line 42

def unzip(zipfile, folder_dest)
  sh "unzip -qo #{zipfile} -d #{folder_dest}"
end