Class: Broadside::Target

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, LoggingUtils
Defined in:
lib/broadside/target.rb

Constant Summary collapse

CREATE_ONLY_SERVICE_ATTRIBUTES =
%i(
  client_token
  load_balancers
  placement_constraints
  placement_strategy
  role
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Target

Returns a new instance of Target.

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/broadside/target.rb', line 55

def initialize(name, options = {})
  @name = name

  config = options.deep_dup
  @bootstrap_commands     = config.delete(:bootstrap_commands)
  @cluster                = config.delete(:cluster) || Broadside.config.aws.ecs_default_cluster
  @command                = config.delete(:command)
  @docker_image           = config.delete(:docker_image) || Broadside.config.default_docker_image
  @predeploy_commands     = config.delete(:predeploy_commands)
  @scale                  = config.delete(:scale)
  @service_config         = config.delete(:service_config)
  @tag                    = config.delete(:tag)
  @task_definition_config = config.delete(:task_definition_config)

  @env_files = Array.wrap(config.delete(:env_files) || config.delete(:env_file)).map do |env_path|
    env_file = Pathname.new(env_path)
    next env_file if env_file.absolute?

    dir = Broadside.config.config_file ? Pathname.new(Broadside.config.config_file).dirname : Dir.pwd
    env_file.expand_path(dir)
  end

  raise ConfigurationError, errors.full_messages unless valid?
  raise ConfigurationError, "Target #{@name} was configured with invalid options: #{config}" unless config.empty?
end

Instance Attribute Details

#bootstrap_commandsObject (readonly)

Returns the value of attribute bootstrap_commands.



9
10
11
# File 'lib/broadside/target.rb', line 9

def bootstrap_commands
  @bootstrap_commands
end

#clusterObject (readonly)

Returns the value of attribute cluster.



9
10
11
# File 'lib/broadside/target.rb', line 9

def cluster
  @cluster
end

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/broadside/target.rb', line 9

def command
  @command
end

#docker_imageObject (readonly)

Returns the value of attribute docker_image.



9
10
11
# File 'lib/broadside/target.rb', line 9

def docker_image
  @docker_image
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/broadside/target.rb', line 9

def name
  @name
end

#predeploy_commandsObject (readonly)

Returns the value of attribute predeploy_commands.



9
10
11
# File 'lib/broadside/target.rb', line 9

def predeploy_commands
  @predeploy_commands
end

#scaleObject (readonly)

Returns the value of attribute scale.



9
10
11
# File 'lib/broadside/target.rb', line 9

def scale
  @scale
end

#service_configObject (readonly)

Returns the value of attribute service_config.



9
10
11
# File 'lib/broadside/target.rb', line 9

def service_config
  @service_config
end

#tagObject (readonly)

Returns the value of attribute tag.



9
10
11
# File 'lib/broadside/target.rb', line 9

def tag
  @tag
end

#task_definition_configObject (readonly)

Returns the value of attribute task_definition_config.



9
10
11
# File 'lib/broadside/target.rb', line 9

def task_definition_config
  @task_definition_config
end

Instance Method Details

#ecs_env_varsObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/broadside/target.rb', line 81

def ecs_env_vars
  @env_vars ||= @env_files.inject({}) do |env_variables, env_file|
    raise ConfigurationError, "Specified env_file: '#{env_file}' does not exist!" unless env_file.exist?

    begin
      env_variables.merge(Dotenv.load(env_file))
    rescue Dotenv::FormatError => e
      raise e.class, "Error parsing #{env_file}: #{e.message}", e.backtrace
    end
  end.map { |k, v| { 'name' => k, 'value' => v } }
end

#familyObject



93
94
95
# File 'lib/broadside/target.rb', line 93

def family
  "#{Broadside.config.application}_#{@name}"
end

#service_config_for_updateObject



105
106
107
# File 'lib/broadside/target.rb', line 105

def service_config_for_update
  service_config.try(:except, *CREATE_ONLY_SERVICE_ATTRIBUTES)
end

#to_hObject



97
98
99
100
101
102
103
# File 'lib/broadside/target.rb', line 97

def to_h
  {
    Target: @name,
    Image: "#{@docker_image}:#{@tag || 'no_tag_configured'}",
    Cluster: @cluster
  }
end