putpaws 0.1.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +268 -61
- data/lib/putpaws/ai/guide.rb +35 -21
- data/lib/putpaws/all.rb +3 -0
- data/lib/putpaws/application.rb +11 -0
- data/lib/putpaws/application_config.rb +66 -37
- data/lib/putpaws/config_file.rb +73 -0
- data/lib/putpaws/ecs/launch_request.rb +144 -0
- data/lib/putpaws/ecs/run_command.rb +33 -55
- data/lib/putpaws/ecs/tasks/ecs_task.rake +48 -25
- data/lib/putpaws/env.rb +12 -0
- data/lib/putpaws/iam/grant_command.rb +16 -3
- data/lib/putpaws/infra_network_config.rb +12 -18
- data/lib/putpaws/infra_target_config.rb +26 -19
- data/lib/putpaws/launch_config.rb +141 -0
- data/lib/putpaws/launch_resolver.rb +104 -0
- data/lib/putpaws/provision/config_writer.rb +95 -34
- data/lib/putpaws/provision/runner.rb +4 -2
- data/lib/putpaws/provision/tasks/provision.rake +1 -1
- data/lib/putpaws/resolved_launch.rb +142 -0
- data/lib/putpaws/schedule_config.rb +75 -41
- data/lib/putpaws/scheduler/schedule_command.rb +50 -34
- data/lib/putpaws/scheduler/tasks/scheduler_task.rake +32 -12
- data/lib/putpaws/setup.rb +17 -5
- data/lib/putpaws/tasks/info.rake +14 -16
- data/lib/putpaws/version.rb +1 -1
- metadata +6 -1
|
@@ -1,31 +1,25 @@
|
|
|
1
|
-
require 'json'
|
|
2
1
|
require 'pathname'
|
|
2
|
+
require 'putpaws/config_file'
|
|
3
3
|
|
|
4
|
+
# A network part (.putpaws/infra.json: network.<name>).
|
|
5
|
+
# Parts do not belong to an environment; they are shared by all of them.
|
|
4
6
|
class Putpaws::InfraNetworkConfig < Struct.new(
|
|
5
7
|
:name, :subnets, :security_groups, :assign_public_ip,
|
|
6
8
|
keyword_init: true)
|
|
7
|
-
def self.load(path_prefix:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
data = File.exist?(path) ?
|
|
11
|
-
JSON.parse(File.read(path), symbolize_names: true).to_h :
|
|
12
|
-
{}
|
|
13
|
-
data[:network] || {}
|
|
14
|
-
end
|
|
9
|
+
def self.load(path_prefix: Putpaws::ConfigFile.root)
|
|
10
|
+
data = Putpaws::ConfigFile.read(Pathname.new(path_prefix).join("infra.json"))
|
|
11
|
+
data[:network] || {}
|
|
15
12
|
end
|
|
16
13
|
|
|
17
|
-
def self.all
|
|
18
|
-
load.map{|k,v|
|
|
19
|
-
|
|
20
|
-
new(data.merge(name: k.to_s))
|
|
14
|
+
def self.all(path_prefix: Putpaws::ConfigFile.root)
|
|
15
|
+
load(path_prefix: path_prefix).map{|k, v|
|
|
16
|
+
new(v.slice(*members).merge(name: k.to_s))
|
|
21
17
|
}
|
|
22
18
|
end
|
|
23
19
|
|
|
24
|
-
def self.find(name)
|
|
25
|
-
|
|
26
|
-
data = infra_network_data[name.to_sym]
|
|
20
|
+
def self.find(name, path_prefix: Putpaws::ConfigFile.root)
|
|
21
|
+
data = load(path_prefix: path_prefix)[name.to_sym]
|
|
27
22
|
return nil unless data
|
|
28
|
-
data
|
|
29
|
-
new(data.merge({name: name.to_s}))
|
|
23
|
+
new(data.slice(*members).merge(name: name.to_s))
|
|
30
24
|
end
|
|
31
25
|
end
|
|
@@ -1,35 +1,42 @@
|
|
|
1
|
-
require 'json'
|
|
2
1
|
require 'pathname'
|
|
2
|
+
require 'putpaws/config_file'
|
|
3
3
|
|
|
4
|
+
# A target part (.putpaws/infra.json: target.<name>): which task definition
|
|
5
|
+
# to start on which cluster. Parts do not belong to an environment.
|
|
4
6
|
class Putpaws::InfraTargetConfig < Struct.new(
|
|
5
|
-
:name,
|
|
7
|
+
:name,
|
|
6
8
|
:scheduler_role,
|
|
7
9
|
:cluster,
|
|
8
10
|
:task_definition,
|
|
9
11
|
:container_name,
|
|
10
12
|
keyword_init: true)
|
|
11
|
-
def self.load(path_prefix:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
data = File.exist?(path) ?
|
|
15
|
-
JSON.parse(File.read(path), symbolize_names: true).to_h :
|
|
16
|
-
{}
|
|
17
|
-
data[:target] || {}
|
|
18
|
-
end
|
|
13
|
+
def self.load(path_prefix: Putpaws::ConfigFile.root)
|
|
14
|
+
data = Putpaws::ConfigFile.read(Pathname.new(path_prefix).join("infra.json"))
|
|
15
|
+
data[:target] || {}
|
|
19
16
|
end
|
|
20
17
|
|
|
21
|
-
def self.all
|
|
22
|
-
load.map{|k,v|
|
|
23
|
-
|
|
24
|
-
new(data.merge(name: k.to_s))
|
|
18
|
+
def self.all(path_prefix: Putpaws::ConfigFile.root)
|
|
19
|
+
load(path_prefix: path_prefix).map{|k, v|
|
|
20
|
+
new(v.slice(*members).merge(name: k.to_s))
|
|
25
21
|
}
|
|
26
22
|
end
|
|
27
23
|
|
|
28
|
-
def self.find(name)
|
|
29
|
-
|
|
30
|
-
data = infra_target_data[name.to_sym]
|
|
24
|
+
def self.find(name, path_prefix: Putpaws::ConfigFile.root)
|
|
25
|
+
data = load(path_prefix: path_prefix)[name.to_sym]
|
|
31
26
|
return nil unless data
|
|
32
|
-
data
|
|
33
|
-
|
|
27
|
+
new(data.slice(*members).merge(name: name.to_s))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def cluster_arn?
|
|
31
|
+
cluster.to_s.start_with?('arn:')
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Region of the cluster ARN. nil when `cluster` is a bare name.
|
|
35
|
+
def cluster_region
|
|
36
|
+
cluster_arn? ? cluster.to_s.split(':')[3] : nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def cluster_name
|
|
40
|
+
cluster.to_s.split('/').last
|
|
34
41
|
end
|
|
35
42
|
end
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
require 'putpaws/config_file'
|
|
2
|
+
|
|
3
|
+
# A named launch (.putpaws/<environment>/launch.json): which task definition
|
|
4
|
+
# to start on which network, with which overrides. `ecs:run`, `ecs:shell` and
|
|
5
|
+
# the scheduler all refer to the same launches by name.
|
|
6
|
+
#
|
|
7
|
+
# Only what ECS lets you decide when starting a task can be written here.
|
|
8
|
+
# Image, sidecars and log destination belong to the task definition.
|
|
9
|
+
class Putpaws::LaunchConfig
|
|
10
|
+
FILENAME = 'launch.json'
|
|
11
|
+
FIELDS = %i[
|
|
12
|
+
target network
|
|
13
|
+
command container env
|
|
14
|
+
cpu memory ephemeral_storage
|
|
15
|
+
task_role capacity_provider
|
|
16
|
+
]
|
|
17
|
+
REQUIRED = %i[target network]
|
|
18
|
+
INTEGER_FIELDS = %i[cpu memory ephemeral_storage]
|
|
19
|
+
STRING_FIELDS = %i[target network container task_role]
|
|
20
|
+
CAPACITY_PROVIDERS = %w[FARGATE FARGATE_SPOT]
|
|
21
|
+
NAME_PATTERN = /\A[A-Za-z0-9][A-Za-z0-9_\-]*\z/
|
|
22
|
+
|
|
23
|
+
attr_reader :name, :attributes
|
|
24
|
+
|
|
25
|
+
def self.path(application)
|
|
26
|
+
application.dir.join(FILENAME)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.load(application)
|
|
30
|
+
Putpaws::ConfigFile.read(path(application))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.all(application)
|
|
34
|
+
file = path(application)
|
|
35
|
+
load(application).map{|name, attrs|
|
|
36
|
+
new(name: name, attributes: attrs, context: "launch `#{name}` (#{file})")
|
|
37
|
+
}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.find(application, name)
|
|
41
|
+
attrs = load(application)[name.to_sym]
|
|
42
|
+
return nil unless attrs
|
|
43
|
+
new(name: name, attributes: attrs, context: "launch `#{name}` (#{path(application)})")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.find!(application, name)
|
|
47
|
+
find(application, name) or raise Putpaws::ConfigError,
|
|
48
|
+
"Launch not found: #{name} (#{path(application)})\n" +
|
|
49
|
+
"Available: #{names(application).join(', ')}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.names(application)
|
|
53
|
+
load(application).keys.map(&:to_s)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Save one launch under the given name. Other launches are kept as they are.
|
|
57
|
+
# Overwrites silently: ask the user first when the name already exists.
|
|
58
|
+
def self.save!(application, name, attributes)
|
|
59
|
+
unless name.to_s.match?(NAME_PATTERN)
|
|
60
|
+
raise Putpaws::ConfigError, "Invalid launch name: #{name} (letters, numbers, `-` and `_` only)"
|
|
61
|
+
end
|
|
62
|
+
attrs = normalize(attributes, context: "launch `#{name}`", required: true)
|
|
63
|
+
file = path(application)
|
|
64
|
+
data = load(application)
|
|
65
|
+
data[name.to_sym] = attrs
|
|
66
|
+
Putpaws::ConfigFile.write(file, data)
|
|
67
|
+
file
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Validate and normalize launch fields. Returns a Hash in FIELDS order
|
|
71
|
+
# without blank values. Unknown fields are an error: a setting that is
|
|
72
|
+
# silently ignored would look like it works.
|
|
73
|
+
def self.normalize(attributes, context:, required: false)
|
|
74
|
+
unless attributes.is_a?(Hash)
|
|
75
|
+
raise Putpaws::ConfigError, "#{context} must be a JSON object"
|
|
76
|
+
end
|
|
77
|
+
attrs = attributes.map{|k, v| [k.to_sym, v]}.to_h
|
|
78
|
+
unknown = attrs.keys - FIELDS
|
|
79
|
+
unless unknown.empty?
|
|
80
|
+
raise Putpaws::ConfigError,
|
|
81
|
+
"Unknown field #{unknown.map{|k| "`#{k}`"}.join(', ')} in #{context}. Available: #{FIELDS.join(', ')}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
normalized = {}
|
|
85
|
+
FIELDS.each do |field|
|
|
86
|
+
value = attrs[field]
|
|
87
|
+
next if blank?(value)
|
|
88
|
+
normalized[field] = normalize_field(field, value, context)
|
|
89
|
+
end
|
|
90
|
+
if required
|
|
91
|
+
missing = REQUIRED - normalized.keys
|
|
92
|
+
unless missing.empty?
|
|
93
|
+
raise Putpaws::ConfigError, "#{context} needs #{missing.map{|k| "`#{k}`"}.join(' and ')}"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
normalized
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def self.normalize_field(field, value, context)
|
|
100
|
+
if STRING_FIELDS.include?(field)
|
|
101
|
+
raise Putpaws::ConfigError, "`#{field}` must be a string in #{context}" unless value.is_a?(String)
|
|
102
|
+
value.strip
|
|
103
|
+
elsif INTEGER_FIELDS.include?(field)
|
|
104
|
+
unless value.is_a?(Integer) || value.to_s.match?(/\A\d+\z/)
|
|
105
|
+
raise Putpaws::ConfigError, "`#{field}` must be an integer in #{context}: #{value.inspect}"
|
|
106
|
+
end
|
|
107
|
+
value.to_i
|
|
108
|
+
elsif field == :command
|
|
109
|
+
valid = value.is_a?(String) || (value.is_a?(Array) && !value.empty? && value.all?{|x| x.is_a?(String)})
|
|
110
|
+
raise Putpaws::ConfigError, "`command` must be a string or an array of strings in #{context}" unless valid
|
|
111
|
+
value
|
|
112
|
+
elsif field == :env
|
|
113
|
+
raise Putpaws::ConfigError, "`env` must be an object like {\"NAME\": \"value\"} in #{context}" unless value.is_a?(Hash)
|
|
114
|
+
value.map{|k, v| [k.to_s, v.to_s]}.to_h
|
|
115
|
+
elsif field == :capacity_provider
|
|
116
|
+
provider = value.to_s.upcase
|
|
117
|
+
unless CAPACITY_PROVIDERS.include?(provider)
|
|
118
|
+
raise Putpaws::ConfigError,
|
|
119
|
+
"`capacity_provider` must be one of #{CAPACITY_PROVIDERS.join(' / ')} in #{context}: #{value.inspect}"
|
|
120
|
+
end
|
|
121
|
+
provider
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def self.blank?(value)
|
|
126
|
+
value.nil? || (value.respond_to?(:empty?) && value.empty?) || (value.is_a?(String) && value.strip.empty?)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def initialize(name:, attributes:, context: nil)
|
|
130
|
+
@name = name.to_s
|
|
131
|
+
@attributes = self.class.normalize(attributes, context: context || "launch `#{name}`", required: true)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
FIELDS.each do |field|
|
|
135
|
+
define_method(field){ attributes[field] }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def to_h
|
|
139
|
+
attributes.dup
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
require 'putpaws/config_file'
|
|
2
|
+
require 'putpaws/launch_config'
|
|
3
|
+
require 'putpaws/resolved_launch'
|
|
4
|
+
require 'putpaws/infra_network_config'
|
|
5
|
+
require 'putpaws/infra_target_config'
|
|
6
|
+
|
|
7
|
+
# Builds the launch that actually gets started. Later layers win, field by field:
|
|
8
|
+
#
|
|
9
|
+
# 1. defaults of the task definition (putpaws passes nothing)
|
|
10
|
+
# 2. the named launch of the environment (launch.json)
|
|
11
|
+
# 3. for a schedule: the overrides written in schedule.json
|
|
12
|
+
# 4. for a manual run: the command-line parameters
|
|
13
|
+
#
|
|
14
|
+
# `env` is merged name by name; every other field is replaced as a whole.
|
|
15
|
+
# `ecs:run`, `ecs:shell` and the scheduler all go through here.
|
|
16
|
+
class Putpaws::LaunchResolver
|
|
17
|
+
def self.resolve(application:, launch: nil, overrides: [])
|
|
18
|
+
new(application: application, launch: launch, overrides: overrides).resolve
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
attr_reader :application, :launch, :overrides
|
|
22
|
+
|
|
23
|
+
# launch: Putpaws::LaunchConfig or nil (assemble on the spot)
|
|
24
|
+
# overrides: a Hash, or an Array of Hashes applied in order
|
|
25
|
+
def initialize(application:, launch: nil, overrides: [])
|
|
26
|
+
@application = application
|
|
27
|
+
@launch = launch
|
|
28
|
+
@overrides = overrides.is_a?(Hash) ? [overrides] : overrides.compact
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def path_prefix
|
|
32
|
+
application.path_prefix || Putpaws::ConfigFile.root
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def resolve
|
|
36
|
+
attributes = merged_attributes
|
|
37
|
+
missing = Putpaws::LaunchConfig::REQUIRED - attributes.keys
|
|
38
|
+
unless missing.empty?
|
|
39
|
+
raise Putpaws::ConfigError,
|
|
40
|
+
"The launch has no #{missing.map{|k| "`#{k}`"}.join(' and ')}. " +
|
|
41
|
+
"Pass launch=<name> (see `launch:list`), or both target=<name> and network=<name>."
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
target = Putpaws::InfraTargetConfig.find(attributes[:target], path_prefix: path_prefix)
|
|
45
|
+
unless target
|
|
46
|
+
raise Putpaws::ConfigError, "target `#{attributes[:target]}` is not defined in #{infra_path}\n" +
|
|
47
|
+
"Available: #{Putpaws::InfraTargetConfig.all(path_prefix: path_prefix).map(&:name).join(', ')}"
|
|
48
|
+
end
|
|
49
|
+
network = Putpaws::InfraNetworkConfig.find(attributes[:network], path_prefix: path_prefix)
|
|
50
|
+
unless network
|
|
51
|
+
raise Putpaws::ConfigError, "network `#{attributes[:network]}` is not defined in #{infra_path}\n" +
|
|
52
|
+
"Available: #{Putpaws::InfraNetworkConfig.all(path_prefix: path_prefix).map(&:name).join(', ')}"
|
|
53
|
+
end
|
|
54
|
+
check_environment!(target)
|
|
55
|
+
|
|
56
|
+
Putpaws::ResolvedLaunch.new(
|
|
57
|
+
application: application,
|
|
58
|
+
attributes: attributes,
|
|
59
|
+
target: target,
|
|
60
|
+
network: network,
|
|
61
|
+
)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def merged_attributes
|
|
65
|
+
layers = []
|
|
66
|
+
layers << launch.attributes if launch
|
|
67
|
+
overrides.each do |layer|
|
|
68
|
+
layers << Putpaws::LaunchConfig.normalize(layer, context: "the overrides")
|
|
69
|
+
end
|
|
70
|
+
merged = layers.reduce({}) do |result, layer|
|
|
71
|
+
env = (result[:env] || {}).merge(layer[:env] || {})
|
|
72
|
+
result = result.merge(layer)
|
|
73
|
+
result[:env] = env unless env.empty?
|
|
74
|
+
result
|
|
75
|
+
end
|
|
76
|
+
# Keep a stable field order whatever the layers looked like.
|
|
77
|
+
Putpaws::LaunchConfig::FIELDS.select{|f| merged.key?(f)}.map{|f| [f, merged[f]]}.to_h
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def infra_path
|
|
83
|
+
File.join(path_prefix.to_s, 'infra.json')
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Parts do not know environments, so a launch could point at the parts of
|
|
87
|
+
# another environment by mistake. Stop here when the cluster of the target
|
|
88
|
+
# is not the cluster of this environment.
|
|
89
|
+
def check_environment!(target)
|
|
90
|
+
if target.cluster.to_s.strip.empty?
|
|
91
|
+
raise Putpaws::ConfigError, "target `#{target.name}` has no `cluster` (#{infra_path})"
|
|
92
|
+
end
|
|
93
|
+
env_region = application.ecs_region || application.region
|
|
94
|
+
env_cluster = application.cluster.to_s.split('/').last
|
|
95
|
+
region = target.cluster_region
|
|
96
|
+
return if (region.nil? || region == env_region) && target.cluster_name == env_cluster
|
|
97
|
+
|
|
98
|
+
raise Putpaws::ConfigError,
|
|
99
|
+
"target `#{target.name}` does not belong to environment `#{application.name}`.\n" +
|
|
100
|
+
" target cluster: #{target.cluster}\n" +
|
|
101
|
+
" environment cluster: #{env_cluster} (region: #{env_region})\n" +
|
|
102
|
+
"Check `target` of the launch, or `region` / `cluster` in #{application.dir.join('application.json')}"
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
require 'json'
|
|
2
2
|
require 'fileutils'
|
|
3
3
|
require 'pathname'
|
|
4
|
+
require 'putpaws/config_file'
|
|
5
|
+
require 'putpaws/application_config'
|
|
6
|
+
require 'putpaws/launch_config'
|
|
4
7
|
require 'putpaws/provision/util'
|
|
5
8
|
|
|
6
9
|
module Putpaws
|
|
7
10
|
module Provision
|
|
8
|
-
# Reflects provisioned resources into
|
|
9
|
-
# .putpaws
|
|
10
|
-
# Shows a diff and asks for confirmation before writing.
|
|
11
|
+
# Reflects provisioned resources into the operation settings under
|
|
12
|
+
# .putpaws/. The service name is the environment name.
|
|
11
13
|
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
14
|
+
# `up` writes only what it knows the right answer for, and never overrides
|
|
15
|
+
# a choice made by the user:
|
|
16
|
+
#
|
|
17
|
+
# <service_name>/application.json generated / updated (the keys putpaws knows; others are kept)
|
|
18
|
+
# infra.json network.<service_name> generated / updated
|
|
19
|
+
# infra.json target.<family> generated / updated, one per task definition of the preset
|
|
20
|
+
# <service_name>/launch.json a launch per task definition is ADDED when its name is absent;
|
|
21
|
+
# existing launches are never rewritten
|
|
22
|
+
# <service_name>/schedule.json never touched
|
|
23
|
+
#
|
|
24
|
+
# Updates show a diff and ask for confirmation before writing.
|
|
20
25
|
class ConfigWriter
|
|
21
26
|
attr_reader :config, :state, :prompt, :io
|
|
22
27
|
def initialize(config:, state:, prompt:, io: $stdout)
|
|
@@ -26,18 +31,17 @@ module Putpaws
|
|
|
26
31
|
@io = io
|
|
27
32
|
end
|
|
28
33
|
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
# Operating what is running: one set per environment. Prefixes are the
|
|
35
|
+
# service name so that tasks of every task definition (-app / -web ...) are found.
|
|
36
|
+
def application_entry
|
|
31
37
|
{
|
|
32
38
|
region: config.region,
|
|
33
39
|
cluster: config.cluster_name,
|
|
34
|
-
service:
|
|
35
|
-
task_name_prefix:
|
|
40
|
+
service: config.service_name,
|
|
41
|
+
task_name_prefix: config.service_name,
|
|
36
42
|
log_group_prefix: config.log_group,
|
|
37
|
-
build_log_group_prefix: config.build_log_group,
|
|
38
43
|
build_project_name_prefix: config.build_project_name,
|
|
39
|
-
|
|
40
|
-
target: family,
|
|
44
|
+
build_log_group_prefix: config.build_log_group,
|
|
41
45
|
}
|
|
42
46
|
end
|
|
43
47
|
|
|
@@ -60,45 +64,102 @@ module Putpaws
|
|
|
60
64
|
}
|
|
61
65
|
end
|
|
62
66
|
|
|
67
|
+
# The default launch of a task definition, named after its suffix.
|
|
68
|
+
def launch_entry(entry)
|
|
69
|
+
{
|
|
70
|
+
target: config.task_definition_family(entry[:suffix]),
|
|
71
|
+
network: config.service_name,
|
|
72
|
+
}
|
|
73
|
+
end
|
|
74
|
+
|
|
63
75
|
def apply!
|
|
76
|
+
write_application
|
|
64
77
|
write_key(path('infra.json'), [:network, config.service_name.to_sym], network_entry)
|
|
65
78
|
config.preset.task_definitions.each do |entry|
|
|
66
79
|
key = config.task_definition_family(entry[:suffix]).to_sym
|
|
67
|
-
write_key(path('application.json'), [key], application_entry(entry))
|
|
68
80
|
write_key(path('infra.json'), [:target, key], target_entry(entry))
|
|
69
81
|
end
|
|
82
|
+
add_missing_launches
|
|
70
83
|
end
|
|
71
84
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
85
|
+
# Files of the 0.1.x layout are neither read nor deleted. Just say so.
|
|
86
|
+
def notify_legacy
|
|
87
|
+
ConfigFile.legacy_files(path_prefix: config.path_prefix).each do |file|
|
|
88
|
+
io.puts "[notice] #{file} is a legacy (0.1.x) file and is no longer read. Please delete it."
|
|
89
|
+
end
|
|
76
90
|
end
|
|
77
91
|
|
|
92
|
+
private
|
|
93
|
+
|
|
78
94
|
def path(basename)
|
|
79
95
|
Pathname.new(config.path_prefix).join(basename)
|
|
80
96
|
end
|
|
81
97
|
|
|
98
|
+
def environment_path(basename)
|
|
99
|
+
ApplicationConfig.dir_for(config.service_name, path_prefix: config.path_prefix).join(basename)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Only the keys putpaws knows are compared and written, so optional keys
|
|
103
|
+
# added by hand (ecs_region / log_region / build_region) survive.
|
|
104
|
+
def write_application
|
|
105
|
+
file = environment_path(ApplicationConfig::FILENAME)
|
|
106
|
+
current = ConfigFile.read(file)
|
|
107
|
+
desired = current.merge(application_entry)
|
|
108
|
+
return if current == desired
|
|
109
|
+
|
|
110
|
+
show_diff(file, nil, current.empty? ? nil : current, desired)
|
|
111
|
+
return unless confirm(file)
|
|
112
|
+
backup(file)
|
|
113
|
+
ConfigFile.write(file, desired)
|
|
114
|
+
io.puts "Updated #{file}"
|
|
115
|
+
end
|
|
116
|
+
|
|
82
117
|
def write_key(file, keys, entry)
|
|
83
|
-
data =
|
|
118
|
+
data = ConfigFile.read(file)
|
|
84
119
|
current = keys.reduce(data){|d, k| d.is_a?(Hash) ? d[k] : nil}
|
|
85
120
|
return if current == entry
|
|
86
121
|
|
|
87
|
-
|
|
122
|
+
show_diff(file, keys, current, entry)
|
|
123
|
+
return unless confirm(file)
|
|
124
|
+
backup(file)
|
|
125
|
+
parent = keys[0..-2].reduce(data){|d, k| d[k] ||= {}}
|
|
126
|
+
parent[keys.last] = entry
|
|
127
|
+
ConfigFile.write(file, data)
|
|
128
|
+
io.puts "Updated #{file}"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Adding never overrides anything, so it is done without asking.
|
|
132
|
+
def add_missing_launches
|
|
133
|
+
file = environment_path(LaunchConfig::FILENAME)
|
|
134
|
+
data = ConfigFile.read(file)
|
|
135
|
+
added = []
|
|
136
|
+
config.preset.task_definitions.each do |entry|
|
|
137
|
+
name = entry[:suffix].to_sym
|
|
138
|
+
next if data.key?(name)
|
|
139
|
+
data[name] = launch_entry(entry)
|
|
140
|
+
added << name
|
|
141
|
+
end
|
|
142
|
+
return if added.empty?
|
|
143
|
+
ConfigFile.write(file, data)
|
|
144
|
+
io.puts "Added launch #{added.map{|n| "`#{n}`"}.join(', ')} to #{file}"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def show_diff(file, keys, current, entry)
|
|
148
|
+
io.puts keys ? "=== #{file} : #{keys.join('.')} ===" : "=== #{file} ==="
|
|
88
149
|
io.puts "--- before"
|
|
89
150
|
io.puts current ? JSON.pretty_generate(current) : "(none)"
|
|
90
151
|
io.puts "+++ after"
|
|
91
152
|
io.puts JSON.pretty_generate(entry)
|
|
92
|
-
|
|
93
|
-
io.puts "Skipped updating #{file}"
|
|
94
|
-
return
|
|
95
|
-
end
|
|
153
|
+
end
|
|
96
154
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
155
|
+
def confirm(file)
|
|
156
|
+
return true if prompt.yes?("Update #{file}?")
|
|
157
|
+
io.puts "Skipped updating #{file}"
|
|
158
|
+
false
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def backup(file)
|
|
162
|
+
FileUtils.cp(file, "#{file}.bak") if File.exist?(file)
|
|
102
163
|
end
|
|
103
164
|
end
|
|
104
165
|
end
|
|
@@ -152,7 +152,9 @@ module Putpaws
|
|
|
152
152
|
apply_resources(service_resources)
|
|
153
153
|
state.mark_step!(:service, 'done')
|
|
154
154
|
|
|
155
|
-
ConfigWriter.new(config: config, state: state, prompt: prompt, io: io)
|
|
155
|
+
writer = ConfigWriter.new(config: config, state: state, prompt: prompt, io: io)
|
|
156
|
+
writer.apply!
|
|
157
|
+
writer.notify_legacy
|
|
156
158
|
|
|
157
159
|
if missing_devops_requirements.any?
|
|
158
160
|
io.puts "[stop point] Devops step is not ready. Fill in: #{missing_devops_requirements.join(', ')}"
|
|
@@ -166,7 +168,7 @@ module Putpaws
|
|
|
166
168
|
ConfigWriter.new(config: config, state: state, prompt: prompt, io: io).apply!
|
|
167
169
|
|
|
168
170
|
io.puts "All steps completed."
|
|
169
|
-
io.puts "Verify with: bundle exec putpaws #{config.service_name} ecs:run cmd='echo ok' wait=true"
|
|
171
|
+
io.puts "Verify with: bundle exec putpaws #{config.service_name} ecs:run launch=#{config.preset.service_task_definition} cmd='echo ok' wait=true"
|
|
170
172
|
end
|
|
171
173
|
|
|
172
174
|
private
|
|
@@ -8,7 +8,7 @@ namespace :provision do
|
|
|
8
8
|
prompt = Putpaws::Prompt.safe
|
|
9
9
|
util = Putpaws::Provision::Util
|
|
10
10
|
|
|
11
|
-
service_name = ENV['service'] || prompt.ask("
|
|
11
|
+
service_name = ENV['service'] || prompt.ask("Service name, which becomes the environment name (Ex: awesome-api-staging):") do |q|
|
|
12
12
|
q.required(true)
|
|
13
13
|
q.validate(/\A[a-z0-9][a-z0-9\-]*\z/, "lowercase letters, numbers and hyphens only")
|
|
14
14
|
end
|