Class: Jets::Code

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Util::Logging, Util::Sh
Defined in:
lib/jets/code.rb,
lib/jets/code/user.rb,
lib/jets/code/dummy.rb,
lib/jets/code/stager.rb

Defined Under Namespace

Modules: Copy Classes: Dummy, Stager, User

Constant Summary collapse

@@s3_key =
nil

Instance Method Summary collapse

Methods included from Util::Sh

#capture, #quiet_sh, #sh

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

#initialize(options = {}) ⇒ Code

Returns a new instance of Code.



10
11
12
# File 'lib/jets/code.rb', line 10

def initialize(options = {})
  @options = options
end

Instance Method Details

#s3_keyObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jets/code.rb', line 27

def s3_key
  return @@s3_key if @@s3_key

  timestamp = Time.now.utc.strftime("%Y-%m-%dT%H-%M-%SZ")
  git = Jets::Git::Info.new
  branch, sha, dirty = git.params.values_at(:git_branch, :git_sha, :git_dirty)
  sha = sha[0..6] if sha # short sha
  dirty = dirty ? "dirty" : nil
  key = ["jets/code/#{timestamp}", branch, sha, dirty].compact.join("-")
  key = key.gsub(/[^a-zA-Z0-9\-_:\/]/, "-").squeeze("-") # sanitize
  @@s3_key = key + ".zip"
end

#s3_locationObject



40
41
42
# File 'lib/jets/code.rb', line 40

def s3_location
  "s3://#{s3_bucket}/#{s3_key}"
end

#stageObject



50
51
52
53
54
55
56
# File 'lib/jets/code.rb', line 50

def stage
  if @options[:dummy]
    Dummy.new.build
  else
    Stager.new.build
  end
end

#uploadObject



44
45
46
47
48
# File 'lib/jets/code.rb', line 44

def upload
  s3_resource.bucket(s3_bucket).object(s3_key).upload_file(zip_path)
  logger.debug "Uploaded code to s3://#{s3_bucket}/#{s3_key}"
  s3_location # Important to return the s3_location
end

#zipObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jets/code.rb', line 58

def zip
  # Check if the folder exists
  unless Dir.exist?("#{build_root}/stage/code")
    logger.info "Error: Source folder not found: #{build_root}/stage/code"
    return
  end

  # Setup
  FileUtils.mkdir_p(File.dirname(zip_path))
  FileUtils.rm_f(zip_path) # remove existing zip file. Else files are added to it

  # Zip
  quiet_sh "cd #{build_root}/stage/code && zip --symlinks -rq #{zip_path} ."

  # Check if the zip operation was successful
  unless $?.success?
    logger.info "Error: Failed to zip the folder: #{build_root}/stage/code"
  end

  zip_path
end

#zip_and_uploadObject



14
15
16
17
18
19
20
# File 'lib/jets/code.rb', line 14

def zip_and_upload
  command = ARGV.reject { |s| s.match(/^-/) }.join(":") # remove options
  log.info "Packaging code for #{command}: #{Jets.project.namespace}"
  stage
  zip
  upload
end

#zip_pathObject



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

def zip_path
  "#{build_root}/code/code.zip"
end