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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/shiprails/ship/deploy.rb', line 24 def build_docker_images perform_callbacks :before_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| region[:environments].each do |environment_name| next unless args.empty? or args.include?(environment_name) s3 = Aws::S3::Client.new(region: region_name.to_s) objects = s3.list_objects_v2(bucket: s3_config_bucket, prefix: environment_name) s3_config_revision = objects.contents.map{ |object| object.key[/\d+/].to_i }.max || 0 ecr = Aws::ECR::Client.new({ region: region_name.to_s }) = ecr...first credentials = Base64.decode64(.).split(':') exit(1) unless run "docker login -u #{credentials.first} -p #{credentials.last} #{.proxy_endpoint}" repository_name = region[:repository_url].split('/').drop(1).join('/') images = ecr.describe_images(repository_name: repository_name).image_details = images.map(&:image_tags).flatten shas = git.log(1000).map(&:sha) # get last 1000 commits last_built_sha = nil shas.each do |sha| if .include?(sha) last_built_sha = sha break end end unless last_built_sha.nil? exit(1) unless run "docker pull #{region[:repository_url]}:#{last_built_sha}" end 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='#{region_name.to_s}' --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_path} ." end end end commands.uniq! commands.each do |c| exit(1) unless run c end say "Build complete", :green perform_callbacks :after_build_docker_images end |
#check_git_status ⇒ Object
17 18 19 20 21 22 |
# File 'lib/shiprails/ship/deploy.rb', line 17 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 end end |
#done ⇒ Object
198 199 200 |
# File 'lib/shiprails/ship/deploy.rb', line 198 def done say "Deploy complete!", :green end |
#push_docker_images ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/shiprails/ship/deploy.rb', line 89 def push_docker_images perform_callbacks :before_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| ecr = Aws::ECR::Client.new({ region: region.to_s }) = ecr...first credentials = Base64.decode64(.).split(':') exit(1) unless run "docker login -u #{credentials.first} -p #{credentials.last} #{.proxy_endpoint}" exit(1) unless run "docker push #{repository_url}:#{git_sha}" end say "Push complete.", :green perform_callbacks :after_push_docker_images end |
#tag_docker_images ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/shiprails/ship/deploy.rb', line 67 def tag_docker_images perform_callbacks :before_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 do |c| exit(1) unless run c end say "Tagging complete.", :green perform_callbacks :after_tag_docker_images end |
#update_ecs_services ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/shiprails/ship/deploy.rb', line 161 def update_ecs_services perform_callbacks :before_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 perform_callbacks :after_update_ecs_services end |
#update_ecs_tasks ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 157 158 159 |
# File 'lib/shiprails/ship/deploy.rb', line 109 def update_ecs_tasks perform_callbacks :before_update_ecs_tasks task_arn = nil 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) if service_name == :app task_arn = task_definition_response.task_definition.task_definition_arn end say "Updated #{task_name}.", :green end end end say "ECS tasks updated.", :green perform_callbacks :after_update_ecs_tasks, task_arn end |