putpaws 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ac7d1e3c9a05d7cabf6ed3ddb76b68eebf5e238ee14cd2d220bfeaf88226570
4
- data.tar.gz: cbcff987bb94042328f0b65d5f693d8aabd75231db1766456d4fe4ed803433e8
3
+ metadata.gz: c9b025bc939b88f486c24a29bee4a1e0d8d7d3fbd1dee2e074d747d591eef94c
4
+ data.tar.gz: 4bc2a5ea2528e771e9b50af5e2452c49de9b1f0b3fb564bcb018328a3c7db380
5
5
  SHA512:
6
- metadata.gz: 76acfa6f6d0b7a514c31a0f63819cc6e1ea587a48a07a465a321afc6743bdc2b645a1d1f22aa2eb3c7c460e44cdf11d51abbc6bc11f5efbc222950a4231ae56e
7
- data.tar.gz: daeea4af00b61ebd35b17daf562343b8839791d94dbb5ae356635d3f39d8d09d754ef971b325e979a24c846c1ea0efe878b0c910c54459a507306759a4448d47
6
+ metadata.gz: ca49348b366c8edf1b7956918a127226aff23955f603f8f3a5fbb20a79599df8c6b6c6a05e463e83afd835c7f1fa7966f692b9dff40874159e871a0a760cadc4
7
+ data.tar.gz: da8783fe46689817420ab7e7258add92c3fb2131ae8d55afa3940102efc12860037a6b4d95523a8dac78dc8fbd859f2a1255755cac2fd02cd10ed061c662c04c
data/lib/Putpawsfile CHANGED
@@ -3,4 +3,5 @@ require "putpaws/setup"
3
3
 
4
4
  require "putpaws/hello"
5
5
  require "putpaws/ecs/ecs_task"
6
- require "putpaws/cloud_watch/log_task"
6
+ require "putpaws/cloud_watch/log_task"
7
+ require "putpaws/scheduler/scheduler_task"
data/lib/putpaws/all.rb CHANGED
@@ -5,6 +5,7 @@ require "putpaws/version"
5
5
  require "putpaws/application"
6
6
 
7
7
  require "putpaws/application_config"
8
+ require "putpaws/schedule_config"
8
9
 
9
10
  module Putpaws
10
11
  end
@@ -1,10 +1,13 @@
1
1
  require 'json'
2
2
  require 'pathname'
3
+ require "putpaws/schedule_config"
3
4
 
4
5
  class Putpaws::ApplicationConfig < Struct.new(
5
6
  :name, :region,
6
7
  :cluster, :service, :task_name_prefix, :ecs_region,
7
8
  :log_group_prefix, :log_region,
9
+ :build_project, :build_log_group_prefix, :build_region,
10
+ :schedules,
8
11
  keyword_init: true)
9
12
  def self.load(path_prefix: '.putpaws')
10
13
  @application_data ||= begin
@@ -45,4 +48,15 @@ class Putpaws::ApplicationConfig < Struct.new(
45
48
  log_group_prefix: log_group_prefix,
46
49
  }
47
50
  end
51
+
52
+ def build_log_command_params
53
+ {
54
+ region: build_region || region,
55
+ log_group_prefix: build_log_group_prefix,
56
+ }
57
+ end
58
+
59
+ def schedules
60
+ @schedules ||= (self[:schedules] || []).map{|x| Putpaws::ScheduleConfig.find(x)}
61
+ end
48
62
  end
@@ -10,8 +10,12 @@ module Putpaws::CloudWatch
10
10
  'w' => 60 * 60 * 24 * 7,
11
11
  }
12
12
 
13
- def self.config(config)
14
- new(config.log_command_params)
13
+ def self.config(config, type: "default")
14
+ if type == "build"
15
+ new(config.build_log_command_params)
16
+ else
17
+ new(config.log_command_params)
18
+ end
15
19
  end
16
20
 
17
21
  def self.parse_unit_time(ut)
@@ -5,16 +5,23 @@ require "putpaws/cloud_watch/default_log_formatter"
5
5
  namespace :log do
6
6
  desc "Set Log Group."
7
7
  task :set_log_group do
8
- aws = Putpaws::CloudWatch::LogCommand.config(fetch(:app))
8
+ app = fetch(:app)
9
+ prompt = TTY::Prompt.new
10
+ log_type = if app.build_log_group_prefix
11
+ prompt.select("Choose log type", ["default", "build"])
12
+ else
13
+ "default"
14
+ end
15
+ aws = Putpaws::CloudWatch::LogCommand.config(app, type: log_type)
16
+
9
17
  log_groups = aws.list_log_groups.map{|a|
10
18
  [a.log_group_name, a]
11
19
  }.to_h
12
20
  raise "Log group not found on your permission" if log_groups.empty?
13
21
 
14
22
  log_group = if log_groups.length == 1
15
- log_groups.first
23
+ log_groups.values.first
16
24
  else
17
- prompt = TTY::Prompt.new
18
25
  selected = prompt.select("Choose a log_group you're going to operate", log_groups.keys)
19
26
  log_groups[selected]
20
27
  end
@@ -40,6 +40,8 @@ module Putpaws::Ecs
40
40
  task: ecs_task.task_arn,
41
41
  })
42
42
 
43
+ ssm_region = ENV['AWS_REGION_SSM'] || @region
44
+
43
45
  # https://github.com/aws/aws-cli/blob/2a6136010d8656a605d41d1e7b5fdab3c2930cad/awscli/customizations/ecs/executecommand.py#L105
44
46
  session_json = {
45
47
  "SessionId" => res.session.session_id,
@@ -56,7 +58,7 @@ module Putpaws::Ecs
56
58
  "StartSession",
57
59
  'test',
58
60
  target_json.dump,
59
- "https://ssm.ap-northeast-1.amazonaws.com"
61
+ "https://ssm.#{ssm_region}.amazonaws.com"
60
62
  ]
61
63
  cmd.join(' ')
62
64
  end
@@ -17,7 +17,7 @@ namespace :ecs do
17
17
  set :ecs_task, ecs_task
18
18
  end
19
19
 
20
- desc "Attach on ECS task. You need to enable ECS Exec on a specified task."
20
+ desc "Attach on ECS task. You need to enable ECS Exec on a specified task and also install session-manager-plugin."
21
21
  task attach: :set_task do
22
22
  ecs_task = fetch(:ecs_task)
23
23
  aws = Putpaws::Ecs::TaskCommand.config(fetch(:app))
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+ require 'pathname'
3
+
4
+ class Putpaws::InfraNetworkConfig < Struct.new(
5
+ :name, :subnets, :security_groups, :assign_public_ip,
6
+ keyword_init: true)
7
+ def self.load(path_prefix: '.putpaws')
8
+ @infra_network_data ||= begin
9
+ path = Pathname.new(path_prefix).join("infra.json").to_s
10
+ data = File.exists?(path) ?
11
+ JSON.parse(File.read(path), symbolize_names: true).to_h :
12
+ {}
13
+ data[:network] || {}
14
+ end
15
+ end
16
+
17
+ def self.all
18
+ load.map{|k,v|
19
+ data = v.slice(*self.members)
20
+ new(data.merge(name: k.to_s))
21
+ }
22
+ end
23
+
24
+ def self.find(name)
25
+ infra_network_data = load
26
+ data = infra_network_data[name.to_sym]
27
+ return nil unless data
28
+ data = data.slice(*self.members)
29
+ new(data.merge({name: name.to_s}))
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+ require 'pathname'
3
+
4
+ class Putpaws::InfraTargetConfig < Struct.new(
5
+ :name,
6
+ :scheduler_role,
7
+ :cluster,
8
+ :task_definition,
9
+ :container_name,
10
+ keyword_init: true)
11
+ def self.load(path_prefix: '.putpaws')
12
+ @infra_target_data ||= begin
13
+ path = Pathname.new(path_prefix).join("infra.json").to_s
14
+ data = File.exists?(path) ?
15
+ JSON.parse(File.read(path), symbolize_names: true).to_h :
16
+ {}
17
+ data[:target] || {}
18
+ end
19
+ end
20
+
21
+ def self.all
22
+ load.map{|k,v|
23
+ data = v.slice(*self.members)
24
+ new(data.merge(name: k.to_s))
25
+ }
26
+ end
27
+
28
+ def self.find(name)
29
+ infra_target_data = load
30
+ data = infra_target_data[name.to_sym]
31
+ return nil unless data
32
+ data = data.slice(*self.members)
33
+ new(data.merge({name: name.to_s}))
34
+ end
35
+ end
@@ -0,0 +1,54 @@
1
+ require 'json'
2
+ require 'pathname'
3
+ require 'putpaws/infra_network_config'
4
+ require 'putpaws/infra_target_config'
5
+
6
+ class Putpaws::ScheduleConfig < Struct.new(
7
+ :name, :region, :network, :target, :command, :args,
8
+ keyword_init: true)
9
+ def self.load(path_prefix: '.putpaws')
10
+ @schedule_data ||= begin
11
+ path = Pathname.new(path_prefix).join("schedule.json").to_s
12
+ data = File.exists?(path) ?
13
+ JSON.parse(File.read(path), symbolize_names: true).to_h :
14
+ {}
15
+ end
16
+ end
17
+
18
+ def self.build_args(name, config_args)
19
+ region = config_args.delete(:region)
20
+ network = config_args.delete(:network)
21
+ target = config_args.delete(:target)
22
+ command = config_args.delete(:command)
23
+ {
24
+ name: name.to_s,
25
+ region: region,
26
+ network: network,
27
+ target: target,
28
+ command: command,
29
+ }.merge(args: config_args)
30
+ end
31
+
32
+ def self.all
33
+ load.map{|k,v|
34
+ new(build_args(k, v))
35
+ }
36
+ end
37
+
38
+ def self.find(name)
39
+ schedule_data = load
40
+ data = schedule_data[name.to_sym]
41
+ return nil unless data
42
+ new(build_args(name, data))
43
+ end
44
+
45
+ def network
46
+ return nil unless self[:network]
47
+ @network ||= Putpaws::InfraNetworkConfig.find(self[:network])
48
+ end
49
+
50
+ def target
51
+ return nil unless self[:target]
52
+ @target ||= Putpaws::InfraTargetConfig.find(self[:target])
53
+ end
54
+ end
@@ -0,0 +1,62 @@
1
+ require 'aws-sdk-scheduler'
2
+
3
+ module Putpaws::Scheduler
4
+ class ScheduleCommand
5
+ def self.config(config)
6
+ new(config.log_command_params)
7
+ end
8
+
9
+ attr_reader :client
10
+ attr_reader :config, :network, :target
11
+
12
+ def initialize(schedule_config)
13
+ @config = schedule_config
14
+ @network = config.network
15
+ @target = config.target
16
+ @client = Aws::Scheduler::Client.new({region: config.region})
17
+ end
18
+
19
+ def generated_params
20
+ {
21
+ name: config.name,
22
+ flexible_time_window: {mode: "OFF"},
23
+ target: {
24
+ arn: target.cluster,
25
+ role_arn: target.scheduler_role,
26
+ retry_policy: {maximum_retry_attempts: 0},
27
+ input: {
28
+ containerOverrides: [
29
+ {
30
+ name: target.container_name,
31
+ command: config.command,
32
+ }
33
+ ]
34
+ }.to_json,
35
+ ecs_parameters: {
36
+ launch_type: "FARGATE",
37
+ network_configuration: {
38
+ awsvpc_configuration: {
39
+ subnets: network.subnets,
40
+ security_groups: network.security_groups,
41
+ assign_public_ip: network.assign_public_ip,
42
+ }
43
+ },
44
+ task_definition_arn: target.task_definition,
45
+ task_count: 1, }
46
+ }
47
+ }
48
+ end
49
+
50
+ def deploy!
51
+ # TODO: Use deep merge here
52
+ args = generated_params.merge(config.args)
53
+
54
+ begin
55
+ res = client.get_schedule(name: config.name)
56
+ client.update_schedule(args)
57
+ rescue Aws::Scheduler::Errors::ResourceNotFoundException => e
58
+ client.create_schedule(args)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1 @@
1
+ load File.expand_path("../tasks/scheduler_task.rake", __FILE__)
@@ -0,0 +1,29 @@
1
+ require "tty-prompt"
2
+ require "putpaws/schedule_config"
3
+ require "putpaws/scheduler/schedule_command"
4
+
5
+ namespace :scheduler do
6
+ desc "Select Schedule to Manage."
7
+ task :set_schedule_config do
8
+ app = fetch(:app)
9
+ schedules = (app.schedules && !app.schedules.empty?) ? app.schedules : []
10
+ if schedules.empty?
11
+ schedule_options = Putpaws::ScheduleConfig.all.map(&:name)
12
+ raise "Please set `schedules` at .putpaws/schedule.json\nFollowing schedules available:\n#{schedule_options.to_json}\n"
13
+ end
14
+
15
+ prompt = TTY::Prompt.new
16
+ selected = prompt.select("Choose a task you're going to operate", schedules.map(&:name))
17
+ schedule_config = schedules.find{|x| x.name == selected}
18
+
19
+ set :schedule_config, schedule_config
20
+ end
21
+
22
+ desc "Deploy schedule."
23
+ task deploy: :set_schedule_config do
24
+ schedule_config = fetch(:schedule_config)
25
+ # pp schedule_config
26
+ aws = Putpaws::Scheduler::ScheduleCommand.new(schedule_config)
27
+ aws.deploy!
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Putpaws
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: putpaws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - metheglin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-26 00:00:00.000000000 Z
11
+ date: 2023-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -28,44 +28,58 @@ dependencies:
28
28
  name: tty-prompt
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.0'
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: aws-sdk-ecs
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.0'
47
+ version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: aws-sdk-cloudwatchlogs
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '1.0'
61
+ version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk-scheduler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '1.0'
82
+ version: '0'
69
83
  description: aws fargate based infra management
70
84
  email: pigmybank@gmail.com
71
85
  executables:
@@ -89,6 +103,12 @@ files:
89
103
  - lib/putpaws/ecs/tasks/ecs_task.rake
90
104
  - lib/putpaws/env.rb
91
105
  - lib/putpaws/hello.rb
106
+ - lib/putpaws/infra_network_config.rb
107
+ - lib/putpaws/infra_target_config.rb
108
+ - lib/putpaws/schedule_config.rb
109
+ - lib/putpaws/scheduler/schedule_command.rb
110
+ - lib/putpaws/scheduler/scheduler_task.rb
111
+ - lib/putpaws/scheduler/tasks/scheduler_task.rake
92
112
  - lib/putpaws/setup.rb
93
113
  - lib/putpaws/tasks/hello.rake
94
114
  - lib/putpaws/version.rb