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,39 +1,82 @@
|
|
|
1
|
-
require 'json'
|
|
2
1
|
require 'pathname'
|
|
3
|
-
require
|
|
4
|
-
require "putpaws/infra_network_config"
|
|
5
|
-
require "putpaws/infra_target_config"
|
|
2
|
+
require 'putpaws/config_file'
|
|
6
3
|
|
|
4
|
+
# An environment (= application). One folder per environment:
|
|
5
|
+
# .putpaws/<environment>/application.json. The folder name is the name, and it
|
|
6
|
+
# is the first argument of every command.
|
|
7
|
+
#
|
|
8
|
+
# The settings are what is needed to operate things that are already running:
|
|
9
|
+
# region / cluster plus prefixes that find tasks, logs and builds.
|
|
10
|
+
# How to start tasks lives in launch.json (Putpaws::LaunchConfig) and when to
|
|
11
|
+
# start them in schedule.json (Putpaws::ScheduleConfig); neither is known here.
|
|
7
12
|
class Putpaws::ApplicationConfig < Struct.new(
|
|
8
13
|
:name, :region,
|
|
9
14
|
:cluster, :service, :task_name_prefix, :ecs_region,
|
|
10
15
|
:log_group_prefix, :log_region,
|
|
11
16
|
:build_project_name_prefix, :build_log_group_prefix, :build_region,
|
|
12
|
-
:schedules,
|
|
13
|
-
:network, :target,
|
|
14
17
|
keyword_init: true)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
FILENAME = 'application.json'
|
|
19
|
+
# Folders right under .putpaws/ that are never environments.
|
|
20
|
+
RESERVED_DIRS = %w[provisioning]
|
|
21
|
+
# Keys of the 0.1.x entries which moved to launch.json / schedule.json.
|
|
22
|
+
REMOVED_KEYS = %i[network target schedules]
|
|
23
|
+
|
|
24
|
+
attr_accessor :path_prefix
|
|
25
|
+
|
|
26
|
+
def self.dir_for(name, path_prefix: Putpaws::ConfigFile.root)
|
|
27
|
+
Pathname.new(path_prefix).join(name.to_s)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Environments are the folders right under .putpaws/ that hold application.json.
|
|
31
|
+
def self.names(path_prefix: Putpaws::ConfigFile.root)
|
|
32
|
+
root = Pathname.new(path_prefix)
|
|
33
|
+
return [] unless root.directory?
|
|
34
|
+
root.children.select(&:directory?)
|
|
35
|
+
.reject{|d| RESERVED_DIRS.include?(d.basename.to_s)}
|
|
36
|
+
.select{|d| d.join(FILENAME).file?}
|
|
37
|
+
.map{|d| d.basename.to_s}
|
|
38
|
+
.sort
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.all(path_prefix: Putpaws::ConfigFile.root)
|
|
42
|
+
Putpaws::ConfigFile.ensure_no_legacy!(path_prefix: path_prefix)
|
|
43
|
+
names(path_prefix: path_prefix).map{|name| build(name, path_prefix)}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.find(name, path_prefix: Putpaws::ConfigFile.root)
|
|
47
|
+
Putpaws::ConfigFile.ensure_no_legacy!(path_prefix: path_prefix)
|
|
48
|
+
return nil unless names(path_prefix: path_prefix).include?(name.to_s)
|
|
49
|
+
build(name, path_prefix)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.build(name, path_prefix)
|
|
53
|
+
file = dir_for(name, path_prefix: path_prefix).join(FILENAME)
|
|
54
|
+
data = Putpaws::ConfigFile.read(file)
|
|
55
|
+
removed = data.keys & REMOVED_KEYS
|
|
56
|
+
unless removed.empty?
|
|
57
|
+
raise Putpaws::ConfigError,
|
|
58
|
+
"#{file} has #{removed.map{|k| "`#{k}`"}.join(', ')}, which application.json no longer holds.\n" +
|
|
59
|
+
"Launches go to launch.json and schedules to schedule.json in the same folder."
|
|
21
60
|
end
|
|
61
|
+
app = new(data.slice(*(members - [:name])).merge(name: name.to_s))
|
|
62
|
+
app.path_prefix = path_prefix
|
|
63
|
+
app
|
|
22
64
|
end
|
|
23
65
|
|
|
24
|
-
def
|
|
25
|
-
|
|
26
|
-
data = v.slice(*self.members)
|
|
27
|
-
new(data.merge(name: k.to_s))
|
|
28
|
-
}
|
|
66
|
+
def dir
|
|
67
|
+
self.class.dir_for(name, path_prefix: path_prefix || Putpaws::ConfigFile.root)
|
|
29
68
|
end
|
|
30
69
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
70
|
+
# Named launches of this environment (launch.json).
|
|
71
|
+
def launches
|
|
72
|
+
require 'putpaws/launch_config'
|
|
73
|
+
Putpaws::LaunchConfig.all(self)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Schedules of this environment (schedule.json).
|
|
77
|
+
def schedules
|
|
78
|
+
require 'putpaws/schedule_config'
|
|
79
|
+
Putpaws::ScheduleConfig.all(self)
|
|
37
80
|
end
|
|
38
81
|
|
|
39
82
|
def ecs_command_params
|
|
@@ -59,20 +102,6 @@ class Putpaws::ApplicationConfig < Struct.new(
|
|
|
59
102
|
}
|
|
60
103
|
end
|
|
61
104
|
|
|
62
|
-
def schedules
|
|
63
|
-
@schedules ||= (self[:schedules] || []).map{|x| Putpaws::ScheduleConfig.find(x)}.compact
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def network
|
|
67
|
-
return nil unless self[:network]
|
|
68
|
-
@network ||= Putpaws::InfraNetworkConfig.find(self[:network])
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def target
|
|
72
|
-
return nil unless self[:target]
|
|
73
|
-
@target ||= Putpaws::InfraTargetConfig.find(self[:target])
|
|
74
|
-
end
|
|
75
|
-
|
|
76
105
|
def codebuild_command_params
|
|
77
106
|
{
|
|
78
107
|
region: build_region || region,
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'pathname'
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
|
|
5
|
+
module Putpaws
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
class ConfigError < Error; end
|
|
8
|
+
class LegacyConfigError < ConfigError; end
|
|
9
|
+
|
|
10
|
+
# Reads and writes the JSON files under .putpaws/.
|
|
11
|
+
#
|
|
12
|
+
# .putpaws/
|
|
13
|
+
# ├── infra.json parts (network / target), shared by all environments
|
|
14
|
+
# ├── <environment>/
|
|
15
|
+
# │ ├── application.json environment settings (operate what is running)
|
|
16
|
+
# │ ├── launch.json named launches (start tasks)
|
|
17
|
+
# │ └── schedule.json schedules (a launch + when to run)
|
|
18
|
+
# └── provisioning/ provisioning inputs and state
|
|
19
|
+
module ConfigFile
|
|
20
|
+
DEFAULT_ROOT = '.putpaws'
|
|
21
|
+
# Files of the 0.1.x layout, which lived right under .putpaws/.
|
|
22
|
+
LEGACY_FILES = %w[application.json schedule.json]
|
|
23
|
+
|
|
24
|
+
def self.root
|
|
25
|
+
@root || DEFAULT_ROOT
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.root=(path)
|
|
29
|
+
@root = path
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.read(path)
|
|
33
|
+
path = Pathname.new(path)
|
|
34
|
+
return {} unless path.exist?
|
|
35
|
+
data = JSON.parse(File.read(path, encoding: 'UTF-8'), symbolize_names: true)
|
|
36
|
+
raise ConfigError, "#{path} must be a JSON object" unless data.is_a?(Hash)
|
|
37
|
+
data
|
|
38
|
+
rescue JSON::ParserError => e
|
|
39
|
+
raise ConfigError, "#{path} is not valid JSON: #{e.message}"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.write(path, data)
|
|
43
|
+
FileUtils.mkdir_p(File.dirname(path.to_s))
|
|
44
|
+
File.write(path.to_s, JSON.pretty_generate(data) + "\n", encoding: 'UTF-8')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.legacy_files(path_prefix: root)
|
|
48
|
+
LEGACY_FILES.map{|f| Pathname.new(path_prefix).join(f)}.select(&:file?)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Top-level keys of the legacy application.json: the names people used to
|
|
52
|
+
# pass as the first argument. Only used to answer them with a migration hint.
|
|
53
|
+
def self.legacy_application_names(path_prefix: root)
|
|
54
|
+
file = Pathname.new(path_prefix).join('application.json')
|
|
55
|
+
return [] unless file.file?
|
|
56
|
+
data = JSON.parse(File.read(file, encoding: 'UTF-8')) rescue nil
|
|
57
|
+
data.is_a?(Hash) ? data.keys.map(&:to_s) : []
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.legacy_message(files)
|
|
61
|
+
"Legacy config found: #{files.map(&:to_s).join(', ')}\n" +
|
|
62
|
+
"putpaws 1.0 reads settings per environment folder " +
|
|
63
|
+
"(.putpaws/<environment>/application.json, launch.json, schedule.json) and no longer reads these files.\n" +
|
|
64
|
+
"Move the settings into the environment folder (`putpaws up` generates application.json and launch.json), " +
|
|
65
|
+
"then delete the legacy files."
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.ensure_no_legacy!(path_prefix: root)
|
|
69
|
+
files = legacy_files(path_prefix: path_prefix)
|
|
70
|
+
raise LegacyConfigError, legacy_message(files) unless files.empty?
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'putpaws/launch_config'
|
|
3
|
+
require 'putpaws/launch_resolver'
|
|
4
|
+
|
|
5
|
+
module Putpaws::Ecs
|
|
6
|
+
# Turns the command-line parameters of `ecs:run` / `ecs:shell` into a
|
|
7
|
+
# resolved launch:
|
|
8
|
+
#
|
|
9
|
+
# launch=seed a saved launch as it is
|
|
10
|
+
# launch=seed cpu=8192 memory=32768 a saved launch, partly overridden
|
|
11
|
+
# target=... network=... cmd='...' assembled on the spot, nothing saved
|
|
12
|
+
# save=seed-small save what was resolved under that name
|
|
13
|
+
# dry=true show what was resolved, start nothing
|
|
14
|
+
class LaunchRequest
|
|
15
|
+
# command-line parameter => launch field
|
|
16
|
+
OVERRIDE_PARAMS = {
|
|
17
|
+
'target' => :target,
|
|
18
|
+
'network' => :network,
|
|
19
|
+
'cmd' => :command,
|
|
20
|
+
'container' => :container,
|
|
21
|
+
'cpu' => :cpu,
|
|
22
|
+
'memory' => :memory,
|
|
23
|
+
'env' => :env,
|
|
24
|
+
'ephemeral_storage' => :ephemeral_storage,
|
|
25
|
+
'task_role' => :task_role,
|
|
26
|
+
'capacity_provider' => :capacity_provider,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# env='NAME=value NAME2=value2' (space separated; values cannot contain spaces)
|
|
30
|
+
def self.parse_env(str)
|
|
31
|
+
str.to_s.split.map{|pair|
|
|
32
|
+
name, value = pair.split('=', 2)
|
|
33
|
+
if name.to_s.empty? || value.nil?
|
|
34
|
+
raise Putpaws::ConfigError, "Invalid env: #{pair.inspect} (Ex) env='NAME=value NAME2=value2'"
|
|
35
|
+
end
|
|
36
|
+
[name, value]
|
|
37
|
+
}.to_h
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.overrides(params)
|
|
41
|
+
OVERRIDE_PARAMS.each_with_object({}) do |(param, field), result|
|
|
42
|
+
value = params[param]
|
|
43
|
+
next if value.nil? || value.strip.empty?
|
|
44
|
+
result[field] = field == :env ? parse_env(value) : value
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
attr_reader :application, :params, :prompt, :io
|
|
49
|
+
def initialize(application:, params: ENV, prompt: nil, io: $stdout)
|
|
50
|
+
@application = application
|
|
51
|
+
@params = params
|
|
52
|
+
@prompt = prompt
|
|
53
|
+
@io = io
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def overrides
|
|
57
|
+
self.class.overrides(params)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def dry?
|
|
61
|
+
present?('dry')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def save_name
|
|
65
|
+
present?('save') ? params['save'].strip : nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def launch_config
|
|
69
|
+
if present?('launch')
|
|
70
|
+
return Putpaws::LaunchConfig.find!(application, params['launch'].strip)
|
|
71
|
+
end
|
|
72
|
+
# Assembled on the spot when target= / network= are given.
|
|
73
|
+
return nil if overrides.key?(:target) || overrides.key?(:network)
|
|
74
|
+
select_launch_config
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def resolve
|
|
78
|
+
Putpaws::LaunchResolver.resolve(
|
|
79
|
+
application: application,
|
|
80
|
+
launch: launch_config,
|
|
81
|
+
overrides: [overrides],
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Save the resolved launch into launch.json of the environment under
|
|
86
|
+
# save=<name>. An existing name is never overwritten without showing the
|
|
87
|
+
# difference and asking. Returns false when the user declined.
|
|
88
|
+
def save!(resolved)
|
|
89
|
+
name = save_name
|
|
90
|
+
return true unless name
|
|
91
|
+
attributes = resolved.to_h
|
|
92
|
+
file = Putpaws::LaunchConfig.path(application)
|
|
93
|
+
existing = Putpaws::LaunchConfig.load(application)[name.to_sym]
|
|
94
|
+
if existing
|
|
95
|
+
current = Putpaws::LaunchConfig.normalize(existing, context: "launch `#{name}` (#{file})")
|
|
96
|
+
if current == attributes
|
|
97
|
+
io.puts "Launch `#{name}` is already saved with the same content."
|
|
98
|
+
return true
|
|
99
|
+
end
|
|
100
|
+
io.puts "=== #{file} : #{name} ==="
|
|
101
|
+
io.puts "--- before"
|
|
102
|
+
io.puts JSON.pretty_generate(current)
|
|
103
|
+
io.puts "+++ after"
|
|
104
|
+
io.puts JSON.pretty_generate(attributes)
|
|
105
|
+
unless prompt.yes?("Launch `#{name}` already exists. Overwrite?", default: false)
|
|
106
|
+
io.puts "Not saved."
|
|
107
|
+
return false
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
Putpaws::LaunchConfig.save!(application, name, attributes)
|
|
111
|
+
io.puts "Saved launch `#{name}` to #{file}"
|
|
112
|
+
true
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def present?(key)
|
|
118
|
+
!params[key].to_s.strip.empty?
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def select_launch_config
|
|
122
|
+
launches = Putpaws::LaunchConfig.all(application)
|
|
123
|
+
if launches.empty?
|
|
124
|
+
raise Putpaws::ConfigError,
|
|
125
|
+
"No launch is defined in #{Putpaws::LaunchConfig.path(application)}\n" +
|
|
126
|
+
"Pass target=<name> network=<name> (names in infra.json), or add a launch (`putpaws up` adds the defaults)."
|
|
127
|
+
end
|
|
128
|
+
choices = launches.map{|l| [self.class.label(l), l.name]}.to_h
|
|
129
|
+
selected = prompt.select("Choose a launch", choices)
|
|
130
|
+
launches.detect{|l| l.name == selected}
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def self.label(launch)
|
|
134
|
+
label = "#{launch.name} (target: #{launch.target}, network: #{launch.network})"
|
|
135
|
+
label += " #{summarize_command(launch.command)}" if launch.command
|
|
136
|
+
label
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def self.summarize_command(command)
|
|
140
|
+
text = command.is_a?(Array) ? command.join(' ') : command
|
|
141
|
+
text.length > 60 ? "#{text[0, 57]}..." : text
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require 'aws-sdk-ecs'
|
|
2
2
|
|
|
3
3
|
module Putpaws::Ecs
|
|
4
|
+
# Starts a temporary task from a resolved launch (Putpaws::ResolvedLaunch).
|
|
4
5
|
class RunCommand
|
|
5
6
|
DEFAULT_TTL = '30m'
|
|
6
7
|
SECONDS = {
|
|
@@ -9,19 +10,6 @@ module Putpaws::Ecs
|
|
|
9
10
|
'h' => 60 * 60,
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
def self.config(config)
|
|
13
|
-
unless config.network && config.target
|
|
14
|
-
raise "Please set `network` and `target` at .putpaws/application.json\n" +
|
|
15
|
-
"They refer to `network` and `target` sections at .putpaws/infra.json\n" +
|
|
16
|
-
"(Ex) \"network\": \"awesome-private-staging\", \"target\": \"awesome-staging\""
|
|
17
|
-
end
|
|
18
|
-
new(
|
|
19
|
-
region: config.ecs_region || config.region,
|
|
20
|
-
network: config.network,
|
|
21
|
-
target: config.target,
|
|
22
|
-
)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
13
|
# "45" and "45m" mean 45 minutes. "90s" and "2h" are also acceptable.
|
|
26
14
|
def self.parse_ttl(ut)
|
|
27
15
|
ut = DEFAULT_TTL if ut.nil? || ut.strip.empty?
|
|
@@ -30,57 +18,47 @@ module Putpaws::Ecs
|
|
|
30
18
|
number.to_i * SECONDS[unit || 'm']
|
|
31
19
|
end
|
|
32
20
|
|
|
33
|
-
attr_reader :
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
@ecs_client =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
21
|
+
attr_reader :launch
|
|
22
|
+
def initialize(launch, ecs_client: nil)
|
|
23
|
+
@launch = launch
|
|
24
|
+
@ecs_client = ecs_client
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def ecs_client
|
|
28
|
+
@ecs_client ||= Aws::ECS::Client.new({region: region})
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def region
|
|
32
|
+
launch.region
|
|
40
33
|
end
|
|
41
34
|
|
|
42
35
|
def cluster_name
|
|
43
|
-
|
|
36
|
+
launch.cluster_name
|
|
44
37
|
end
|
|
45
38
|
|
|
46
39
|
def container_name
|
|
47
|
-
|
|
40
|
+
launch.container_name
|
|
48
41
|
end
|
|
49
42
|
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
overrides = {
|
|
57
|
-
container_overrides: [
|
|
58
|
-
{
|
|
59
|
-
name: container || container_name,
|
|
60
|
-
command: ['/bin/sh', '-c', command],
|
|
61
|
-
}
|
|
62
|
-
]
|
|
63
|
-
}
|
|
64
|
-
overrides[:cpu] = cpu.to_s unless cpu.to_s.strip.empty?
|
|
65
|
-
overrides[:memory] = memory.to_s unless memory.to_s.strip.empty?
|
|
66
|
-
|
|
67
|
-
res = ecs_client.run_task({
|
|
68
|
-
cluster: target.cluster,
|
|
69
|
-
task_definition: target.task_definition,
|
|
43
|
+
# Parameters of ECS RunTask. started_by and group are set so that
|
|
44
|
+
# temporary tasks are identifiable.
|
|
45
|
+
def run_task_params(started_by:, group:)
|
|
46
|
+
params = {
|
|
47
|
+
cluster: launch.cluster,
|
|
48
|
+
task_definition: launch.task_definition,
|
|
70
49
|
count: 1,
|
|
71
|
-
launch_type: 'FARGATE',
|
|
72
50
|
enable_execute_command: true,
|
|
73
51
|
started_by: started_by,
|
|
74
52
|
group: group,
|
|
75
|
-
network_configuration:
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
53
|
+
network_configuration: launch.network_configuration,
|
|
54
|
+
}.merge(launch.placement)
|
|
55
|
+
overrides = launch.task_overrides
|
|
56
|
+
params[:overrides] = overrides unless overrides.empty?
|
|
57
|
+
params
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def run_ecs_task(started_by:, group:)
|
|
61
|
+
res = ecs_client.run_task(run_task_params(started_by: started_by, group: group))
|
|
84
62
|
failure = res.failures.first
|
|
85
63
|
raise "Failed to run task: #{failure.reason} #{failure.detail}" if failure
|
|
86
64
|
res.tasks.first
|
|
@@ -89,7 +67,7 @@ module Putpaws::Ecs
|
|
|
89
67
|
def wait_until_attachable(task_arn, timeout: 600, interval: 5)
|
|
90
68
|
deadline = Time.now + timeout
|
|
91
69
|
loop do
|
|
92
|
-
res = ecs_client.describe_tasks(cluster:
|
|
70
|
+
res = ecs_client.describe_tasks(cluster: launch.cluster, tasks: [task_arn])
|
|
93
71
|
task = res.tasks.first
|
|
94
72
|
if task.nil? || task.last_status == 'STOPPED'
|
|
95
73
|
raise "Task was stopped: #{task && task.stopped_reason}"
|
|
@@ -105,7 +83,7 @@ module Putpaws::Ecs
|
|
|
105
83
|
def wait_until_stopped(task_arn, timeout: 3600, interval: 10)
|
|
106
84
|
deadline = Time.now + timeout
|
|
107
85
|
loop do
|
|
108
|
-
res = ecs_client.describe_tasks(cluster:
|
|
86
|
+
res = ecs_client.describe_tasks(cluster: launch.cluster, tasks: [task_arn])
|
|
109
87
|
task = res.tasks.first
|
|
110
88
|
raise "Task not found: #{task_arn}" unless task
|
|
111
89
|
return task if task.last_status == 'STOPPED'
|
|
@@ -115,7 +93,7 @@ module Putpaws::Ecs
|
|
|
115
93
|
end
|
|
116
94
|
|
|
117
95
|
def stop_ecs_task(task_arn, reason: 'Stopped by putpaws')
|
|
118
|
-
ecs_client.stop_task(cluster:
|
|
96
|
+
ecs_client.stop_task(cluster: launch.cluster, task: task_arn, reason: reason)
|
|
119
97
|
end
|
|
120
98
|
end
|
|
121
99
|
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
require "shellwords"
|
|
2
2
|
require "putpaws/prompt"
|
|
3
3
|
require "putpaws/ecs/task_command"
|
|
4
|
+
require "json"
|
|
4
5
|
require "putpaws/ecs/run_command"
|
|
6
|
+
require "putpaws/ecs/launch_request"
|
|
5
7
|
|
|
6
8
|
namespace :ecs do
|
|
7
9
|
desc "Set ECS task."
|
|
@@ -56,30 +58,28 @@ namespace :ecs do
|
|
|
56
58
|
system(*cmd)
|
|
57
59
|
end
|
|
58
60
|
|
|
59
|
-
desc "Run a command on a temporary ECS task. The task terminates itself when the command finishes. (Ex) cmd='bundle exec rake db:migrate' wait=true cpu=1024 memory=2048"
|
|
61
|
+
desc "Run a command on a temporary ECS task started from a launch. The task terminates itself when the command finishes. (Ex) launch=app cmd='bundle exec rake db:migrate' wait=true cpu=1024 memory=2048 save=name dry=true"
|
|
60
62
|
task :run do
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if command.nil?
|
|
64
|
-
raise "Please specify a command like: cmd='bundle exec rake db:migrate'"
|
|
63
|
+
request = Putpaws::Ecs::LaunchRequest.new(application: fetch(:app), prompt: Putpaws::Prompt.safe)
|
|
64
|
+
launch = request.resolve
|
|
65
|
+
if launch.command.nil? && !request.dry?
|
|
66
|
+
raise Putpaws::Error, "Please specify a command like: cmd='bundle exec rake db:migrate' (or save `command` in the launch)"
|
|
67
|
+
end
|
|
68
|
+
next unless request.save!(launch)
|
|
69
|
+
if request.dry?
|
|
70
|
+
puts JSON.pretty_generate(launch.describe)
|
|
71
|
+
next
|
|
65
72
|
end
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
started_by: 'putpaws-run',
|
|
70
|
-
group: 'putpaws-run',
|
|
71
|
-
container: ENV['container'],
|
|
72
|
-
cpu: ENV['cpu'],
|
|
73
|
-
memory: ENV['memory']
|
|
74
|
-
)
|
|
74
|
+
runner = Putpaws::Ecs::RunCommand.new(launch)
|
|
75
|
+
ecs_task = runner.run_ecs_task(started_by: 'putpaws-run', group: 'putpaws-run')
|
|
75
76
|
task_id = ecs_task.task_arn.split('/').last
|
|
76
77
|
puts "Task started: #{task_id} [putpaws-run]"
|
|
77
78
|
|
|
78
79
|
if ENV['wait']
|
|
79
80
|
puts "Waiting for the task to stop..."
|
|
80
81
|
ecs_task = runner.wait_until_stopped(ecs_task.task_arn)
|
|
81
|
-
|
|
82
|
-
ctn = ecs_task.containers.detect{|c| c.name == ctn_name}
|
|
82
|
+
ctn = ecs_task.containers.detect{|c| c.name == runner.container_name}
|
|
83
83
|
exit_code = ctn && ctn.exit_code
|
|
84
84
|
puts "Task stopped: #{ecs_task.stopped_reason}"
|
|
85
85
|
raise "Command failed with exit code: #{exit_code.inspect}" unless exit_code == 0
|
|
@@ -121,19 +121,22 @@ namespace :ecs do
|
|
|
121
121
|
end
|
|
122
122
|
end
|
|
123
123
|
|
|
124
|
-
desc "
|
|
124
|
+
desc "Start a temporary ECS task from a launch and attach to it. The command is replaced by sleep, so the task terminates itself when ttl passes (default 30m). (Ex) launch=app ttl=45m keep=true cpu=256 memory=512 dry=true"
|
|
125
125
|
task :shell do
|
|
126
|
-
|
|
126
|
+
request = Putpaws::Ecs::LaunchRequest.new(application: fetch(:app), prompt: Putpaws::Prompt.safe)
|
|
127
127
|
ttl = Putpaws::Ecs::RunCommand.parse_ttl(ENV['ttl'])
|
|
128
|
+
resolved = request.resolve
|
|
129
|
+
next unless request.save!(resolved)
|
|
130
|
+
# Everything follows the launch except the command.
|
|
131
|
+
launch = resolved.with(command: "sleep #{ttl}")
|
|
132
|
+
if request.dry?
|
|
133
|
+
puts JSON.pretty_generate(launch.describe)
|
|
134
|
+
next
|
|
135
|
+
end
|
|
128
136
|
|
|
137
|
+
runner = Putpaws::Ecs::RunCommand.new(launch)
|
|
129
138
|
puts "Launching a temporary task which terminates itself after #{ttl} seconds"
|
|
130
|
-
ecs_task = runner.run_ecs_task(
|
|
131
|
-
command: "sleep #{ttl}",
|
|
132
|
-
started_by: 'putpaws-shell',
|
|
133
|
-
group: 'putpaws-shell',
|
|
134
|
-
cpu: ENV['cpu'],
|
|
135
|
-
memory: ENV['memory']
|
|
136
|
-
)
|
|
139
|
+
ecs_task = runner.run_ecs_task(started_by: 'putpaws-shell', group: 'putpaws-shell')
|
|
137
140
|
task_id = ecs_task.task_arn.split('/').last
|
|
138
141
|
puts "Task started: #{task_id} [putpaws-shell]"
|
|
139
142
|
|
|
@@ -146,7 +149,7 @@ namespace :ecs do
|
|
|
146
149
|
cluster: runner.cluster_name
|
|
147
150
|
)
|
|
148
151
|
aws.ecs_task = ecs_task
|
|
149
|
-
cmd = aws.get_attach_command(container:
|
|
152
|
+
cmd = aws.get_attach_command(container: runner.container_name)
|
|
150
153
|
puts Shellwords.join(cmd)
|
|
151
154
|
system(*cmd)
|
|
152
155
|
ensure
|
|
@@ -159,3 +162,23 @@ namespace :ecs do
|
|
|
159
162
|
end
|
|
160
163
|
end
|
|
161
164
|
end
|
|
165
|
+
|
|
166
|
+
namespace :launch do
|
|
167
|
+
desc "List the launches of the environment (.putpaws/<environment>/launch.json). No AWS access."
|
|
168
|
+
task :list do
|
|
169
|
+
app = fetch(:app)
|
|
170
|
+
launches = app.launches
|
|
171
|
+
if launches.empty?
|
|
172
|
+
puts "No launch is defined in #{Putpaws::LaunchConfig.path(app)}"
|
|
173
|
+
next
|
|
174
|
+
end
|
|
175
|
+
launches.each do |launch|
|
|
176
|
+
puts launch.name
|
|
177
|
+
launch.to_h.each do |field, value|
|
|
178
|
+
value = value.map{|k, v| "#{k}=#{v}"}.join(' ') if value.is_a?(Hash)
|
|
179
|
+
value = value.to_json if value.is_a?(Array)
|
|
180
|
+
puts format(" %-18s %s", "#{field}:", value)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
data/lib/putpaws/env.rb
CHANGED
|
@@ -16,6 +16,7 @@ class Putpaws::Env
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def fetch(key, default=nil, &block)
|
|
19
|
+
require_environment! if key == :app && default.nil? && block.nil?
|
|
19
20
|
fetch_for(key, default, &block)
|
|
20
21
|
end
|
|
21
22
|
|
|
@@ -26,4 +27,15 @@ class Putpaws::Env
|
|
|
26
27
|
def delete(key)
|
|
27
28
|
values.delete(key)
|
|
28
29
|
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# The first argument of a command is always the environment.
|
|
34
|
+
def require_environment!
|
|
35
|
+
return if values.key?(:app)
|
|
36
|
+
names = Putpaws::ApplicationConfig.names
|
|
37
|
+
raise Putpaws::Error,
|
|
38
|
+
"Please specify the environment first: putpaws <environment> <task>\n" +
|
|
39
|
+
"Environments: #{names.empty? ? '(none yet. `putpaws up` generates .putpaws/<environment>/application.json)' : names.join(', ')}"
|
|
40
|
+
end
|
|
29
41
|
end
|
|
@@ -104,6 +104,19 @@ module Putpaws
|
|
|
104
104
|
iam_client.delete_policy_version(policy_arn: policy_arn(profile), version_id: oldest.version_id)
|
|
105
105
|
end
|
|
106
106
|
|
|
107
|
+
# Scheduler roles of the targets which the launches of this environment
|
|
108
|
+
# refer to. Falls back to the role name pattern of putpaws provisioning.
|
|
109
|
+
def scheduler_roles
|
|
110
|
+
require "putpaws/infra_target_config"
|
|
111
|
+
path_prefix = app.path_prefix || Putpaws::ConfigFile.root
|
|
112
|
+
roles = app.launches.map{|launch|
|
|
113
|
+
target = Putpaws::InfraTargetConfig.find(launch.target, path_prefix: path_prefix)
|
|
114
|
+
target && target.scheduler_role
|
|
115
|
+
}.compact.uniq
|
|
116
|
+
return "arn:aws:iam::#{account_id}:role/#{app.name}-*" if roles.empty?
|
|
117
|
+
roles.one? ? roles.first : roles
|
|
118
|
+
end
|
|
119
|
+
|
|
107
120
|
def statements_for(group)
|
|
108
121
|
case group
|
|
109
122
|
when 'attach'
|
|
@@ -183,15 +196,15 @@ module Putpaws
|
|
|
183
196
|
]},
|
|
184
197
|
]
|
|
185
198
|
when 'scheduler'
|
|
186
|
-
scheduler_role =
|
|
187
|
-
"arn:aws:iam::#{account_id}:role/#{app.name}-*"
|
|
199
|
+
scheduler_role = scheduler_roles
|
|
188
200
|
[
|
|
189
201
|
{Sid: 'ScheduleList', Effect: 'Allow',
|
|
190
202
|
Action: %w[scheduler:ListSchedules],
|
|
191
203
|
Resource: '*'},
|
|
192
204
|
{Sid: 'ScheduleManage', Effect: 'Allow',
|
|
193
205
|
Action: %w[scheduler:GetSchedule scheduler:CreateSchedule scheduler:UpdateSchedule],
|
|
194
|
-
|
|
206
|
+
# Schedules are deployed as <environment>-<schedule>.
|
|
207
|
+
Resource: "arn:aws:scheduler:#{region}:#{account_id}:schedule/default/#{app.name}-*"},
|
|
195
208
|
{Sid: 'SchedulePassRole', Effect: 'Allow',
|
|
196
209
|
Action: %w[iam:PassRole],
|
|
197
210
|
Resource: scheduler_role},
|