Skip to content

Commit

Permalink
add deployment_id option
Browse files Browse the repository at this point in the history
  • Loading branch information
katjacresanti committed Sep 3, 2024
1 parent f1722a8 commit c452e91
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ group :development do
gem 'pry'
gem 'guard'
gem 'guard-minitest'
gem 'aws-sdk-codedeploy'
end
18 changes: 16 additions & 2 deletions lib/egads/command/release.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# rubocop:disable Style/Documentation, Style/RaiseArgs, Style/RescueModifier
# frozen_string_literal: true

require 'aws-sdk-codedeploy'

module Egads
class Release < Group
include Egads::LocalHelpers
include Thor::Actions

desc "[remote] Symlinks SHA to current and restarts services. If needed, stages SHA"
class_option :force, type: :boolean, default: false, banner: "Overwrite existing release"
desc '[remote] Symlinks SHA to current and restarts services. If needed, stages SHA'
class_option :force, type: :boolean, default: false, banner: 'Overwrite existing release'
class_option :deployment_id, type: :boolean, default: false, banner: 'Use the CodeDeploy deployment ID in the release directory'
argument :sha, type: :string, required: true, desc: 'git SHA to stage'

def setup_environment
RemoteConfig.setup_environment
end
Expand All @@ -15,11 +23,13 @@ def stage

def run_before_release_hooks
return unless should_release?

inside(dir) { run_hooks_for(:release, :before) }
end

def symlink_release
return unless should_release?

atomic_symlink(dir, release_to)
end

Expand All @@ -44,6 +54,7 @@ def trim
end

protected

def dir
RemoteConfig.release_dir(sha)
end
Expand All @@ -66,6 +77,7 @@ def should_release?
# Use an extra temporary symlink for atomicity (equivalent to `mv -T`)
def atomic_symlink(src, dest)
raise ArgumentError.new("#{src} is not a directory") unless File.directory?(src)

say_status :symlink, "from #{src} to #{dest}"
tmp = "#{dest}-new-#{rand(2**32)}"
# Make a temporary symlink
Expand All @@ -75,3 +87,5 @@ def atomic_symlink(src, dest)
end
end
end

# rubocop:enable Style/Documentation, Style/RaiseArgs, Style/RescueModifier
27 changes: 21 additions & 6 deletions lib/egads/command/stage.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# rubocop:disable Style/Documentation, Style/GuardClause
# frozen_string_literal: true

require 'aws-sdk-codedeploy'

module Egads
class Stage < Group
include Egads::LocalHelpers
include Thor::Actions


desc "[remote] Readies SHA for release. If needed, generates URL for SHA and extracts"
class_option :force, type: :boolean, default: false, banner: "Overwrite existing files"
desc '[remote] Readies SHA for release. If needed, generates URL for SHA and extracts'
class_option :force, type: :boolean, default: false, banner: 'Overwrite existing files'
class_option :deployment_id, type: :string, default: false, banner: 'Include deployment ID in release directory'
argument :sha, type: :string, required: true, desc: 'git SHA to stage'

def setup_environment
Expand All @@ -17,19 +23,21 @@ def extract

def run_before_hooks
return unless should_stage?
inside(dir){ run_hooks_for(:stage, :before) }

inside(dir) { run_hooks_for(:stage, :before) }
end

def bundle
return unless should_stage?

inside(dir) do
run_with_code("bundle install #{RemoteConfig.bundler_options}", stream: true) if File.readable?("Gemfile")
run_with_code("bundle install #{RemoteConfig.bundler_options}", stream: true) if File.readable?('Gemfile')
end
end

def symlink_system_paths
return unless should_stage? && shared_path

symlink_directory File.join(shared_path, 'system'), File.join(dir, 'public', 'system')
symlink_directory File.join(shared_path, 'log'), File.join(dir, 'log')
end
Expand All @@ -49,6 +57,7 @@ def symlink_config_files

def run_after_stage_hooks
return unless should_stage?

inside(dir) { run_hooks_for(:stage, :after) }
end

Expand All @@ -57,8 +66,12 @@ def mark_as_staged
end

protected

def dir
RemoteConfig.release_dir(sha)
base_dir = RemoteConfig.release_dir(sha)
deployment_id_value = deployment_id if options[:deployment_id]

deployment_id_value ? "#{base_dir}_#{deployment_id_value}" : base_dir
end

def stage_flag_path
Expand All @@ -74,3 +87,5 @@ def shared_path
end
end
end

# rubocop:enable Style/Documentation, Style/GuardClause
29 changes: 28 additions & 1 deletion lib/egads/local_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# frozen_string_literal: true

# rubocop:disable Metrics/MethodLength

require 'aws-sdk-codedeploy'

module Egads
# Some helper methods for all local commands
module LocalHelpers
Expand All @@ -6,12 +12,33 @@ def sha
end

def short_sha
sha[0,7]
sha[0, 7]
end

def tarball
@tarball ||= S3Tarball.new(sha, seed: options[:seed])
end

def deployment_id
environment = ENV['EC2_ENVIRONMENT'] || ''
application = ENV['EC2_SERVICE'] || ''

return unless environment && application

deployment = CodeDeploy::Client.new.list_deployments(
application_name: application,
deployment_group_name: environment,
include_only_statuses: %w[InProgress Created Queued]
).deployments.first

return nil if deployment.empty?

deployment
rescue Aws::CodeDeploy::Errors::ServiceError => e
puts "Error fetching deployment: #{e.message}"
nil
end
end
end

# rubocop:enable Metrics/MethodLength
2 changes: 1 addition & 1 deletion lib/egads/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Egads
VERSION = '5.1.2'
VERSION = '5.2.0'
end

0 comments on commit c452e91

Please sign in to comment.