class Ufo::Tasks::Register

Public Class Methods

new(template_definition_path, options={}) click to toggle source
# File lib/ufo/tasks/register.rb, line 18
def initialize(template_definition_path, options={})
  @template_definition_path = template_definition_path
  @options = options
end
register(task_name, options={}) click to toggle source
# File lib/ufo/tasks/register.rb, line 9
def self.register(task_name, options={})
  Dir.glob("#{Ufo.root}/.ufo/output/*").each do |path|
    if task_name == :all or path.include?(task_name)
      task_register = Tasks::Register.new(path, options)
      task_register.register
    end
  end
end

Public Instance Methods

register() click to toggle source

aws ecs register-task-definition –cli-input-json file://.ufo/output/demo-web-prod.json

# File lib/ufo/tasks/register.rb, line 24
def register
  data = JSON.parse(IO.read(@template_definition_path))
  data = rubyize_format(data)

  message = "#{data[:family]} task definition registered."
  if @options[:noop]
    message = "NOOP: #{message}"
  else
    register_task_definition(data)
  end

  unless @options[:mute]
    puts "Equivalent aws cli command:"
    file_path = "file://#{@template_definition_path.sub(/^\.\//,'')}"
    puts "  aws ecs register-task-definition --cli-input-json #{file_path}".color(:green)
    puts message
  end
end
register_task_definition(data) click to toggle source
# File lib/ufo/tasks/register.rb, line 43
def register_task_definition(data)
  if ENV["UFO_SHOW_REGISTER_TASK_DEFINITION"]
    puts "Registering task definition with:"
    display_params(data)
  end

  ecs.register_task_definition(data)
rescue Aws::ECS::Errors::ClientException => e
  if e.message =~ /No Fargate configuration exists for given values/
    puts "ERROR: #{e.message}".color(:red)
    puts "Configured values are: cpu #{data[:cpu]} memory #{data[:memory]}"
    puts "Check that the cpu and memory values are a supported combination by Fargate."
    puts "More info: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html"
    exit 1
  else
    raise
  end
end
rubyize_format(original_data) click to toggle source

The ruby aws-sdk expects symbols for keys and AWS docs for the task definition uses json camelCase for the keys. This method transforms the keys to the expected ruby aws-sdk format.

One quirk is that the logConfiguration options casing should not be transformed.

# File lib/ufo/tasks/register.rb, line 68
def rubyize_format(original_data)
  data = original_data.to_snake_keys.deep_symbolize_keys

  definitions = data[:container_definitions]
  definitions.each_with_index do |definition, i|
    next unless definition[:log_configuration] || definition[:firelens_configuration]
    { log_configuration: 'logConfiguration',
      firelens_configuration: 'firelensConfiguration' }.each_pair do |key, value|

      next unless definition.dig(key, :options)

      # LogConfiguration and firelensConfiguration options do not get transformed and
      # keep their original structure:
      #   https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/ECS/Types/ContainerDefinition.html
      original_definition = original_data["containerDefinitions"][i]
      definition[key][:options] = original_definition[value]["options"]
    end
  end

  data
end