Class: Shiprails::Ship::Deploy
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Shiprails::Ship::Deploy
- Includes:
- Thor::Actions
- Defined in:
- lib/shiprails/ship/deploy.rb
Instance Method Summary collapse
- #build_docker_images ⇒ Object
- #check_git_status ⇒ Object
- #done ⇒ Object
- #push_docker_images ⇒ Object
- #tag_docker_images ⇒ Object
- #update_ecs_services ⇒ Object
- #update_ecs_tasks ⇒ Object
Instance Method Details
#build_docker_images ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shiprails/ship/deploy.rb', line 23 def build_docker_images say "Building images..." s3_config_bucket = configuration[:config_s3_bucket].to_s commands = [] configuration[:services].each do |service_name, service| image_name = "#{compose_project_name}_#{service[:image]}" service[:regions].each do |region_name, region| aws_region = region_name.to_s region[:environments].each do |environment_name| next unless args.empty? or args.include?(environment_name) result = `S3_CONFIG_BUCKET=#{s3_config_bucket} bundle exec config list #{environment_name}` s3_config_revision = result.match(/#{environment_name} \(v([0-9]+)\)/)[1] rescue 0 commands << "docker build -t #{image_name}_#{environment_name} --build-arg AWS_ACCESS_KEY_ID='#{aws_access_key_id}' --build-arg AWS_SECRET_ACCESS_KEY='#{aws_access_key_secret}' --build-arg AWS_REGION='#{aws_region}' --build-arg S3_CONFIG_BUCKET='#{s3_config_bucket}' --build-arg S3_CONFIG_ENVIRONMENT='#{environment_name}' --build-arg S3_CONFIG_REVISION='#{s3_config_revision}' -f Dockerfile.production ." end end end commands.uniq! commands.each { |c| run c } # TODO: check that this succeeded say "Build complete", :green end |
#check_git_status ⇒ Object
16 17 18 19 20 21 |
# File 'lib/shiprails/ship/deploy.rb', line 16 def check_git_status if git.status.added.any? or git.status.changed.any? or git.status.deleted.any? say "You have uncommitted changes. Commit and try again.", :red # exit # TESTING end end |
#done ⇒ Object
158 159 160 |
# File 'lib/shiprails/ship/deploy.rb', line 158 def done say "Deploy complete!", :green end |
#push_docker_images ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/shiprails/ship/deploy.rb', line 62 def push_docker_images say "Pushing images..." repository_urls_to_regions = {} configuration[:services].each do |service_name, service| service[:regions].each do |region, values| repository_urls_to_regions[values[:repository_url]] = region end end repository_urls_to_regions.each do |repository_url, region| run "`aws ecr get-login --region #{region}`" run "docker push #{repository_url}:#{git_sha}" # TODO: check that this succeeded end say "Push complete.", :green end |
#tag_docker_images ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/shiprails/ship/deploy.rb', line 44 def tag_docker_images say "Tagging images..." commands = [] configuration[:services].each do |service_name, service| image_name = "#{compose_project_name}_#{service[:image]}" service[:regions].each do |region_name, region| repository_url = region[:repository_url] region[:environments].each do |environment_name| next unless args.empty? or args.include?(environment_name) commands << "docker tag #{image_name}_#{environment_name} #{repository_url}:#{git_sha}" end end end commands.uniq! commands.each { |c| run c } # TODO: check that this succeeded say "Tagging complete.", :green end |
#update_ecs_services ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/shiprails/ship/deploy.rb', line 123 def update_ecs_services say "Updating ECS services..." configuration[:services].each do |service_name, service| service[:regions].each do |region_name, region| ecs = Aws::ECS::Client.new(region: region_name.to_s) region[:environments].each do |environment_name| next unless args.empty? or args.include?(environment_name) cluster_name = "#{project_name}_#{environment_name}" task_name = "#{project_name}_#{service_name}_#{environment_name}" begin task_definition_response = ecs.describe_task_definition({task_definition: task_name}) task_definition = task_definition_response.task_definition.to_hash rescue Aws::ECS::Errors::ClientException => e say "Missing ECS task for #{task_name}!", :red say "Run `ship setup`", :red exit end begin service_response = ecs.update_service({ cluster: cluster_name, service: service_name, task_definition: task_definition_response.task_definition.task_definition_arn }) say "Updated #{service_name}.", :green rescue Aws::ECS::Errors::ServiceNotFoundException, Aws::ECS::Errors::ServiceNotActiveException => e say "Missing ECS service for #{task_name}!", :red say "Run `ship setup`", :red exit end end end end say "ECS services updated.", :green end |
#update_ecs_tasks ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/shiprails/ship/deploy.rb', line 77 def update_ecs_tasks say "Updating ECS tasks..." configuration[:services].each do |service_name, service| image_name = "#{project_name}_#{service_name}" service[:regions].each do |region_name, region| ecs = Aws::ECS::Client.new(region: region_name.to_s) region[:environments].each do |environment_name| next unless args.empty? or args.include?(environment_name) cluster_name = "#{project_name}_#{environment_name}" task_name = "#{project_name}_#{service_name}_#{environment_name}" image_name = "#{region[:repository_url]}:#{git_sha}" begin task_definition_description = ecs.describe_task_definition({task_definition: task_name}) task_definition = task_definition_description.task_definition.to_hash task_definition.delete :task_definition_arn task_definition.delete :revision task_definition.delete :status task_definition.delete :requires_attributes rescue Aws::ECS::Errors::ClientException => e say "Missing ECS task for #{task_name}!", :red say "Run `ship setup`", :red exit end if container = task_definition[:container_definitions].find{ |container| container[:name] == service_name.to_s } container[:cpu] = service[:resources][:cpu_units] container[:image] = image_name container[:memory] = service[:resources][:memory_units] config_s3_version = container[:environment].find{|e| e[:name] == "S3_CONFIG_REVISION" }[:value] container[:environment] = [ { name: "AWS_REGION", value: region_name.to_s }, { name: "GIT_SHA", value: git_sha }, { name: "RACK_ENV", value: environment_name }, { name: "S3_CONFIG_BUCKET", value: config_s3_bucket }, { name: "S3_CONFIG_ENVIRONMENT", value: environment_name }, { name: "S3_CONFIG_REVISION", value: config_s3_version } ] say "Updated #{service_name} container (#{image_name})." end task_definition_response = ecs.register_task_definition(task_definition) say "Updated #{task_name}.", :green end end end say "ECS tasks updated.", :green end |