Class: JetsGemLayer::TaskHelper

Inherits:
Object
  • Object
show all
Includes:
Jets::AwsServices, Rake::DSL
Defined in:
lib/jets_gem_layer/task_helper.rb

Overview

All methods that run in the app environment, including rake tasks

Constant Summary collapse

INPUT_FILES =

Files used to generate the gem layer

Rake::FileList.new(
  %w[Gemfile Gemfile.lock].map { |f| File.join(Jets.root, f) }
)
OUTPUT_DIR =
File.expand_path(File.join(Jets.root, 'tmp/jets_gem_layer/'))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arnObject



26
27
28
# File 'lib/jets_gem_layer/task_helper.rb', line 26

def self.arn
  instance.arn
end

.installObject



19
20
21
22
23
24
# File 'lib/jets_gem_layer/task_helper.rb', line 19

def self.install
  return if @installed

  instance.install
  @installed = true
end

.instanceObject



15
16
17
# File 'lib/jets_gem_layer/task_helper.rb', line 15

def self.instance
  @instance ||= new
end

Instance Method Details

#all_layer_versionsObject

paginate through all layer versions to get them all (but it’s unlikely there will be more than 1 page)



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/jets_gem_layer/task_helper.rb', line 143

def all_layer_versions
  all_versions = []
  marker = nil
  loop do
    page = aws_lambda.list_layer_versions(layer_name:, max_items: 50, marker:)
    all_versions.concat page.layer_versions
    marker = page.next_marker
    break if marker.nil?
  end
  all_versions
end

#arnObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets_gem_layer/task_helper.rb', line 30

def arn
  # We do not want to do any of this when running in the lambda environment
  # as it is only required for deployment.
  @arn ||= if ENV['LAMBDA_TASK_ROOT'] || ENV['JETS_NO_INTERNET']
             'no-op-while-running-in-lambda-or-test'
           else
             published_arn
           end

  @arn ||= 'no-matching-gem-layer-found'
end

#build_layerObject



101
102
103
104
105
106
107
108
109
# File 'lib/jets_gem_layer/task_helper.rb', line 101

def build_layer
  FileUtils.mkdir_p(inputs_dir)
  FileUtils.cp(INPUT_FILES.existing, inputs_dir)
  puts 'Running docker to build layer'
  command = docker_run_cmd
  puts command.join(' ')
  system(*docker_run_cmd) or raise $CHILD_STATUS.to_s
  zip_layer
end

#clean_working_dirObject



120
121
122
# File 'lib/jets_gem_layer/task_helper.rb', line 120

def clean_working_dir
  FileUtils.rm_rf(working_dir)
end

#cleanup_publishedObject



124
125
126
127
128
129
130
131
132
# File 'lib/jets_gem_layer/task_helper.rb', line 124

def cleanup_published
  all_layer_versions.each_with_index do |layer_version, i|
    next if i.zero? # skips the current version

    aws_lambda.delete_layer_version(layer_name:, version_number: layer_version.version)
    puts "Deleted #{layer_version.layer_version_arn}"
  end
  puts 'Deleted all prior versions!'
end

#delete_all_publishedObject



134
135
136
137
138
139
140
# File 'lib/jets_gem_layer/task_helper.rb', line 134

def delete_all_published
  all_layer_versions.each do |layer_version|
    aws_lambda.delete_layer_version(layer_name:, version_number: layer_version.version)
    puts "Deleted #{layer_version.layer_version_arn}"
  end
  puts 'Deleted all published versions!'
end

#installObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets_gem_layer/task_helper.rb', line 42

def install
  namespace :gem_layer do
    install_clean
    install_build
    install_publish
    install_build_and_publish
    install_cleanup_published
    install_delete_all_published
  end
  true
end

#install_buildObject



66
67
68
69
70
71
# File 'lib/jets_gem_layer/task_helper.rb', line 66

def install_build
  desc 'Build a gem layer zip file'
  task build: :clean do
    build_layer
  end
end

#install_build_and_publishObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jets_gem_layer/task_helper.rb', line 54

def install_build_and_publish
  desc 'Build and publish a gem layer version, if necessary'
  task :build_and_publish do
    if published?
      puts "#{layer_name} already published for #{layer_version_description}. Not doing anything!"
      next
    end
    Rake::Task['gem_layer:build'].invoke
    Rake::Task['gem_layer:publish'].invoke
  end
end

#install_cleanObject



80
81
82
83
84
85
# File 'lib/jets_gem_layer/task_helper.rb', line 80

def install_clean
  desc 'Clean jets_gem_layer tmp files'
  task :clean do
    clean_working_dir
  end
end

#install_cleanup_publishedObject



87
88
89
90
91
92
# File 'lib/jets_gem_layer/task_helper.rb', line 87

def install_cleanup_published
  desc 'Delete old layer versions from AWS (for use after deployment)'
  task :cleanup_published do
    cleanup_published
  end
end

#install_delete_all_publishedObject



94
95
96
97
98
99
# File 'lib/jets_gem_layer/task_helper.rb', line 94

def install_delete_all_published
  desc 'Delete all published versions of the gem layer from AWS'
  task :delete_all_published do
    delete_all_published
  end
end

#install_publishObject



73
74
75
76
77
78
# File 'lib/jets_gem_layer/task_helper.rb', line 73

def install_publish
  desc 'Publish the already built layer zip file'
  task :publish do
    publish_layer
  end
end

#publish_layerObject



111
112
113
114
115
116
117
118
# File 'lib/jets_gem_layer/task_helper.rb', line 111

def publish_layer
  aws_lambda.publish_layer_version(
    layer_name:, # required
    description: layer_version_description,
    content: { zip_file: File.read(zip_file_path) }
  )
  puts "#{layer_name} published for #{layer_version_description}!"
end

#published?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/jets_gem_layer/task_helper.rb', line 159

def published?
  published_layer_version&.description == layer_version_description
end

#published_arnObject



163
164
165
166
167
168
169
# File 'lib/jets_gem_layer/task_helper.rb', line 163

def published_arn
  return nil unless published?

  published_layer_version.layer_version_arn
rescue StandardError
  'error-fetching-gem-layer-arn'
end

#published_layer_versionObject



155
156
157
# File 'lib/jets_gem_layer/task_helper.rb', line 155

def published_layer_version
  @published_layer_version ||= aws_lambda.list_layer_versions(layer_name:, max_items: 1).layer_versions.first
end