Module: ECSUtil::AWS

Included in:
Command
Defined in:
lib/ecsutil/aws.rb

Instance Method Summary collapse

Instance Method Details

#aws_call(service, method, data) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/ecsutil/aws.rb', line 3

def aws_call(service, method, data)
  input = data.is_a?(String) ? data : "--cli-input-json file://#{json_file(data)}" 

  result = `aws #{service} #{method} #{input}`.strip
  unless $?.success?
    fail "#{service} #{method} failed!"
  end
  JSON.load(result)
end

#create_service(config, service_name) ⇒ Object



250
251
252
# File 'lib/ecsutil/aws.rb', line 250

def create_service(config, service_name)
  aws_call("ecs", "create-service", generate_service(config, service_name))
end

#degerister_task_definition(arn) ⇒ Object



140
141
142
# File 'lib/ecsutil/aws.rb', line 140

def degerister_task_definition(arn)
  aws_call("ecs", "deregister-task-definition", "--task-definition #{arn}")
end

#delete_rule(name) ⇒ Object



156
157
158
159
# File 'lib/ecsutil/aws.rb', line 156

def delete_rule(name)
  aws_call("events", "remove-targets", "--rule=#{name} --ids=default")
  aws_call("events", "delete-rule", "--name=#{name}")
end

#delete_service(config, service_name) ⇒ Object



258
259
260
261
# File 'lib/ecsutil/aws.rb', line 258

def delete_service(config, service_name)
  aws_call("ecs", "update-service", "--cluster=#{config["cluster"]} --service=#{service_name} --desired-count 0")
  aws_call("ecs", "delete-service", "--cluster=#{config["cluster"]} --service=#{service_name}")
end

#describe_service(config, service_name) ⇒ Object



240
241
242
243
244
# File 'lib/ecsutil/aws.rb', line 240

def describe_service(config, service_name)
  full_service_name = sprintf("%s-%s-%s", config["app"], config["env"], service_name)
  result = aws_call("ecs", "describe-services", "--cluster=#{config["cluster"]} --services=#{full_service_name}")
  result["services"].first
end

#describe_services(config, names) ⇒ Object



246
247
248
# File 'lib/ecsutil/aws.rb', line 246

def describe_services(config, names)
  aws_call("ecs", "describe-services", "--cluster=#{config.cluster} --services=#{names.join(",")}")["services"]
end

#fetch_parameter_store_keys(prefix, process = true) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/ecsutil/aws.rb', line 175

def fetch_parameter_store_keys(prefix, process = true)
  result = aws_call("ssm", "get-parameters-by-path", "--path=#{prefix} --with-decryption")
  result["Parameters"].map do |p|
    {
      name: p["Name"],
      key: p["Name"].split("/").last,
      value: p["Value"]
    }
  end
end

#generate_event_rule(config) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/ecsutil/aws.rb', line 13

def generate_event_rule(config)
  {
    Name:               config[:name],
    ScheduleExpression: config[:expression],
    State:              config[:enabled] ? "ENABLED" : "DISABLED",
    Tags:               array_hash(config[:tags] || {}, "Key", "Value")
  }
end

#generate_event_target(config, task_name, schedule_name) ⇒ Object



22
23
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
# File 'lib/ecsutil/aws.rb', line 22

def generate_event_target(config, task_name, schedule_name)
  task     = config["tasks"][task_name]
  schedule = config["scheduled_tasks"][schedule_name]
  input    = {}

  if schedule["command"]
    input = {
      "containerOverrides": [
        {
          "name": task_name,
          "command": schedule["command"].split(" ")
        }
      ]
    }
  end

  {
    Rule: schedule["rule_name"],
    Targets: [
      {
        Id: "default",
        Arn: config["cluster"],
        RoleArn: config["roles"]["schedule"],
        Input: JSON.dump(input),
        EcsParameters: {
          TaskDefinitionArn: task["arn"],
          TaskCount: 1,
          LaunchType: "FARGATE",
          PlatformVersion: "LATEST",
          NetworkConfiguration: {
            awsvpcConfiguration: {
              Subnets: [config["subnets"]].flatten,
              SecurityGroups: [task["security_groups"]].flatten,
              AssignPublicIp: "ENABLED"
            }
          }
        }
      }
    ]
  }
end

#generate_service(config, service_name) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ecsutil/aws.rb', line 186

def generate_service(config, service_name)
  service           = config["services"][service_name]
  task              = config["tasks"][service["task"]]
  full_service_name = sprintf("%s-%s-%s", config["app"], config["env"], service_name)
  exists            = service["exists"] == true

  data = {
    cluster: config["cluster"],
    taskDefinition: task["arn"],
    desiredCount: service["desired_count"] || 0,
    deploymentConfiguration: {
      maximumPercent: service["max_percent"] || 100,
      minimumHealthyPercent: service["min_healthy_percent"] || 50
    },
    networkConfiguration: {
      awsvpcConfiguration: {
        subnets: [config["subnets"]].flatten,
        securityGroups: [task["security_groups"]].flatten,
        assignPublicIp: "ENABLED"
      }
    }
  }

  if exists
    data.merge!(
      service: full_service_name,
      forceNewDeployment: service["force_deployment"] == true
    )
  else
    data.merge!(
      serviceName: full_service_name,
      propagateTags: "SERVICE",
      enableECSManagedTags: true,
      schedulingStrategy: "REPLICA",
      launchType: "FARGATE",
    )

    if lb = service["lb"]
      data.merge!(
        loadBalancers: [
          {
            targetGroupArn:   lb["target_group"],
            loadBalancerName: lb["name"],
            containerName:    lb["container_name"],
            containerPort:    lb["container_port"]
          }.compact
        ]
      )
    end
  end
  
  data
end

#generate_task_definition(config, task_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ecsutil/aws.rb', line 64

def generate_task_definition(config, task_name)
  task         = config["tasks"][task_name]
  service_name = config["app"]
  service_env  = config["env"]
  env          = array_hash(task["env"] || {}, :name)

  secrets = (config["secrets_data"] || []).map do |item|
    {
      name:      item[:key],
      valueFrom: item[:name]
    }
  end

  log_config = nil

  if awslogs = task["awslogs"]
    log_config = {
      logDriver: "awslogs",
      options: {
        "awslogs-group": awslogs["group"],
        "awslogs-region": awslogs["region"],
        "awslogs-stream-prefix": awslogs["prefix"] || task_name
      }
    }
  end

  if sumo = task["sumologs"]
    log_config = {
      logDriver: "splunk",
      options: {
        "splunk-url":        sumo["url"],
        "splunk-token":      sumo["token"],
        "splunk-source":     sumo["source"] || "",
        "splunk-sourcetype": sumo["sourcetype"] || "",
      }
    }
  end

  port_mappings = nil
  if ports = [task["ports"]].flatten.compact.uniq
    port_mappings = ports.map do |p|
      {
        containerPort: p,
        hostPort: p,
        protocol: "tcp"
      }
    end
  end

  image = config["repository"].to_s
  unless image.split("/").last.include?(":")
    image << ":#{config["git_commit"]}"
  end

  {
    family: "#{service_name}-#{service_env}-#{task_name}",
    taskRoleArn: config["roles"]["task"],
    executionRoleArn: config["roles"]["execution"],
    networkMode: "awsvpc",
    requiresCompatibilities: ["FARGATE"],
    cpu: (task["cpu"] || "256").to_s,
    memory: (task["memory"] || "512").to_s,
    containerDefinitions: [
      {
        name: task_name,
        command: task["command"] ? task["command"].split(" ") : nil,
        image: image,
        environment: env,
        secrets: secrets,
        logConfiguration: log_config,
        portMappings: port_mappings
      }.compact
    ]
  }
end

#list_active_task_definitionsObject



161
162
163
# File 'lib/ecsutil/aws.rb', line 161

def list_active_task_definitions
  aws_call("ecs", "list-task-definitions", "--status=ACTIVE --max-items=100")["taskDefinitionArns"]
end

#list_rulesObject



171
172
173
# File 'lib/ecsutil/aws.rb', line 171

def list_rules
  aws_call("events", "list-rules", "")["Rules"]
end

#list_services(cluster) ⇒ Object



165
166
167
168
169
# File 'lib/ecsutil/aws.rb', line 165

def list_services(cluster)
  aws_call("ecs", "list-services", "--cluster=#{cluster}")["serviceArns"].map do |s|
    s.split("/", 3).last
  end
end

#put_rule(data) ⇒ Object



148
149
150
# File 'lib/ecsutil/aws.rb', line 148

def put_rule(data)
  aws_call("events", "put-rule", data)
end

#put_targets(data) ⇒ Object



152
153
154
# File 'lib/ecsutil/aws.rb', line 152

def put_targets(data)
  aws_call("events", "put-targets", data)
end

#register_task_definition(data) ⇒ Object



144
145
146
# File 'lib/ecsutil/aws.rb', line 144

def register_task_definition(data)
  aws_call("ecs", "register-task-definition", data)["taskDefinition"]
end

#update_service(config, service_name) ⇒ Object



254
255
256
# File 'lib/ecsutil/aws.rb', line 254

def update_service(config, service_name)
  aws_call("ecs", "update-service", generate_service(config, service_name))
end