gcp_scheduler 0.0.1 → 0.2.0

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: 62cbc3423620cda69674fa0d2b0d84eba999a2b9b7c4644770461721d798e6b7
4
- data.tar.gz: 237263736fd1e7786d65749bf0a3d4b297ee045ad37371a704ac08ae23c0de59
3
+ metadata.gz: f8ac6e8722729145a8554094fd36c0f6fbca375ceee2dbb22dabb8000da4fff5
4
+ data.tar.gz: dac2f9c3a07478d3eb8e234dad0c1f0049a7ae022689150c1e3b1ad6339cc777
5
5
  SHA512:
6
- metadata.gz: 7e3431c095b0806934a1288c5f0e781037f38bc0890e394e5b9ed8aabb01ab1573abfa01016474db7ed487d653446044950b12865c2ef4290817581c7388d035
7
- data.tar.gz: 231ed0ea3846239aa1b95b34372325481a18325afa51ef9973479caa9b4c73779a8ee1a2c83e7d911386f4ab501b96328164d38c620dbeb030bc3791c87a5fa1
6
+ metadata.gz: 44a5afe8aecd424d1ece6110bb9c86fd19eb0c768da90a2644197a194d2c3ce1c3eb48c6e31f6fae912e1639cb1585adcf9ba8273c3a892ab5cdb54183a09cf1
7
+ data.tar.gz: d5f9cc18e76b649b9a5581b4a67964dd181b37e72d7aa175c6514a6f019fc90f355d8e1f794807037d9848888fb1b7f96741e20392f7eec0e4fc1de3bafc1fbe
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gcp_scheduler (0.0.1)
4
+ gcp_scheduler (0.2.0)
5
5
  activesupport
6
6
  google-cloud-scheduler-v1 (>= 0.7.0)
7
7
  thor
data/README.md CHANGED
@@ -22,15 +22,12 @@ export GOOGLE_APPLICATION_CREDENTIALS=your_credential.json
22
22
  ### ジョブの作成
23
23
 
24
24
  ```
25
- gcp_scheduler create --gcp_project your_project_name --scheduler_file path/to/scheduler.yml --uri https://your_domain.example.com --prefix development- --region asia-northeast1 --secret your_secret_token
25
+ gcp_scheduler create --gcp_project your_project_name --scheduler_file path/to/scheduler.yml --prefix development- --region asia-northeast1
26
26
  ```
27
27
 
28
28
  - scheduler_fileを元にジョブを作成します。
29
29
  - prefixは、ジョブ名に付与されるプレフィックスです。
30
30
  - regionは、Cloud Schedulerのリージョンです。
31
- - uriは、スケジューラーがHTTP POSTを実行するURLです。
32
- - すべてのジョブは一つのURLにapplication/jsonでリクエストパラメータのjob_nameにscheduler_fileのjob名が入ります。
33
- - secretは、Http Authorization Header Bearer tokenです。
34
31
 
35
32
  ### ジョブの一覧表示
36
33
 
@@ -52,18 +49,26 @@ gcp_scheduler delete --gcp_project your_project_name --prefix development- --reg
52
49
  |---|-------------------------------------------------------------------------------------------|
53
50
  | `--gcp_project` | Google Cloud project name |
54
51
  | `--scheduler_file` | Path to the scheduler configuration file (used with `create` command) |
55
- | `--uri` | URL of the endpoint to be executed by the job (used with `create` command) |
56
52
  | `--prefix` | Prefix for the job name (used with `create`, `list`, and `delete` commands) |
57
53
  | `--region` | Cloud Scheduler region |
58
- | `--secret` | Http Authorization Header Bearer token to be sent by the job (used with `create` command) |
59
54
 
60
55
  ## scheduler.yml file
61
56
 
62
- scheduler.ymlファイルは、ジョブのスケジュール情報を定義するために使用されます。以下は、scheduler.ymlファイルのサンプルです。
57
+ `--scheduler_file`オプションでyamlファイルを指定してください。スケジュールを定義するために使用されます。以下は、scheduler.ymlファイルのサンプルです。
63
58
 
64
59
  ```yaml
65
- weekly_job:
66
- cron: '0 9 * * 1'
60
+ jobs:
61
+ - name: WeeklyJob
62
+ description: 'Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
63
+ schedule: '0 9 * * *'
64
+ time_zone: Asia/Tokyo
65
+ uri: https://yourdomain.example.com/api/v1/jobs
66
+ http_method: POST
67
+ params:
68
+ job_name: weekly_job
69
+ http_headers:
70
+ Content-Type: application/json
71
+ Authorization: 'Bearer <%= ENV["SECRET"] %>'
67
72
  ```
68
73
 
69
74
  ## Contributing
@@ -2,13 +2,13 @@ $LOAD_PATH.push File.expand_path("lib", __dir__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "gcp_scheduler"
5
- s.version = "0.0.1"
5
+ s.version = "0.2.0"
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Akira Kusumoto"]
8
8
  s.email = ["akirakusumo10@gmail.com"]
9
9
  s.homepage = "https://github.com/bluerabbit/gcp_scheduler"
10
10
  s.summary = "A command-line interface for managing Google Cloud Scheduler jobs with ease"
11
- s.description = "GCP Scheduler CLI is a Ruby gem that provides a simple command-line interface for managing Google Cloud Scheduler jobs. With this tool, you can create, list, and delete Cloud Scheduler jobs using intuitive commands. It streamlines job management tasks by allowing you to define job schedules in a YAML file and supports custom job prefixes, regions, and authentication tokens."
11
+ s.description = "GCP Scheduler is a Ruby gem that provides a simple command-line interface for managing Google Cloud Scheduler jobs. With this tool, you can create, list, and delete Cloud Scheduler jobs using intuitive commands. It streamlines job management tasks by allowing you to define job schedules in a YAML file."
12
12
 
13
13
  s.files = `git ls-files`.split("\n")
14
14
  s.require_paths = ["lib"]
@@ -34,20 +34,14 @@ module GcpScheduler
34
34
 
35
35
  desc "create", "Create schedules from scheduler.yml"
36
36
  method_option :gcp_project, type: :string, required: true
37
- method_option :region, type: :string, default: "asia-northeast1"
38
- method_option :prefix, type: :string, default: ""
37
+ method_option :region, type: :string, required: true
39
38
  method_option :scheduler_file, type: :string, required: true
40
- method_option :uri, type: :string, required: true
41
- method_option :secret, type: :string, default: ""
42
- method_option :time_zone, type: :string, default: "Etc/UTC"
39
+ method_option :prefix, type: :string, default: ""
43
40
  def create
44
41
  GcpScheduler.create(gcp_project: options[:gcp_project],
45
42
  region: options[:region],
46
43
  prefix: options[:prefix],
47
- scheduler_file_path: options[:scheduler_file],
48
- uri: options[:uri],
49
- secret: options[:secret],
50
- time_zone: options[:time_zone])
44
+ scheduler_file_path: options[:scheduler_file])
51
45
  end
52
46
 
53
47
  desc "version", "Show Version"
@@ -1,42 +1,43 @@
1
1
  module GcpScheduler
2
2
  class Command
3
- class << self
4
- def list(gcp_project:, region:, prefix: "")
5
- scheduler = Scheduler.new(project: gcp_project, location: region)
6
- scheduler.jobs.each do |job|
7
- scheduler_name = job.name.split("/").last
8
- next unless scheduler_name.start_with?(prefix)
3
+ attr_reader :scheduler
9
4
 
10
- puts "- name:#{scheduler_name} description:#{job.description} schedule:#{job.schedule}"
11
- end
5
+ def initialize(gcp_project:, region:)
6
+ @scheduler = Scheduler.new(project: gcp_project, location: region)
7
+ end
8
+
9
+ def list(prefix: "")
10
+ scheduler.jobs.each do |job|
11
+ scheduler_name = job.name.split("/").last
12
+ next unless scheduler_name.start_with?(prefix)
13
+
14
+ puts "- name:#{scheduler_name} description:#{job.description} schedule:#{job.schedule}"
12
15
  end
16
+ end
13
17
 
14
- def delete(gcp_project:, region:, prefix: "")
15
- scheduler = Scheduler.new(project: gcp_project, location: region)
16
- scheduler.jobs.each do |job|
17
- scheduler_name = job.name.split("/").last
18
- next unless scheduler_name.start_with?(prefix)
18
+ def delete(prefix: "")
19
+ scheduler.jobs.each do |job|
20
+ scheduler_name = job.name.split("/").last
21
+ next unless scheduler_name.start_with?(prefix)
19
22
 
20
- puts "Delete #{scheduler_name}"
21
- scheduler.delete_job(scheduler_name)
22
- puts "Deleted #{scheduler_name}"
23
- end
23
+ puts "Delete #{scheduler_name}"
24
+ scheduler.delete_job(scheduler_name)
25
+ puts "Deleted #{scheduler_name}"
24
26
  end
27
+ end
25
28
 
26
- def create(gcp_project:, region:, scheduler_file_path:, uri:, secret:, time_zone:, prefix: "")
27
- scheduler = Scheduler.new(project: gcp_project, location: region)
28
- Scheduler.scheduler_config(scheduler_file_path).each do |name, h|
29
- scheduler_name = "#{prefix}#{name}"
30
- puts "Create #{scheduler_name}"
31
- scheduler.create_job(name: scheduler_name,
32
- description: "#{h["class"]} CreatedAt:#{Time.current.strftime("%Y/%m/%d %-H:%M")}",
33
- uri: uri,
34
- schedule: h["cron"],
35
- params: { job_name: name },
36
- secret: secret,
37
- time_zone: time_zone)
38
- puts "Created #{scheduler_name}"
39
- end
29
+ def create(scheduler_file_path:, prefix: "")
30
+ Scheduler.scheduler_config_jobs(file_path: scheduler_file_path, job_name_prefix: prefix).each do |job|
31
+ puts "Create #{job[:name]}"
32
+ scheduler.create_job(name: job[:name],
33
+ description: job[:description],
34
+ uri: job[:uri],
35
+ schedule: job[:schedule],
36
+ time_zone: job[:time_zone],
37
+ params: job[:params],
38
+ http_method: job[:http_method],
39
+ headers: job[:http_headers])
40
+ puts "Created #{job[:name]}"
40
41
  end
41
42
  end
42
43
  end
@@ -4,7 +4,14 @@ module GcpScheduler
4
4
 
5
5
  class << self
6
6
  def scheduler_config(file_path)
7
- YAML.load(ERB.new(File.read(file_path)).result).with_indifferent_access
7
+ YAML.safe_load(ERB.new(File.read(file_path)).result, aliases: true).with_indifferent_access
8
+ end
9
+
10
+ def scheduler_config_jobs(file_path:, job_name_prefix: "")
11
+ config = scheduler_config(file_path)
12
+ config[:jobs].map do |job|
13
+ (config[:defaults] || {}).merge(job.merge(name: "#{job_name_prefix}#{job[:name]}")).with_indifferent_access
14
+ end
8
15
  end
9
16
  end
10
17
 
@@ -24,8 +31,9 @@ module GcpScheduler
24
31
  description:,
25
32
  uri:,
26
33
  schedule:,
27
- secret:, time_zone:, params: {},
28
- http_method: :POST,
34
+ time_zone:,
35
+ params: {},
36
+ http_method:,
29
37
  headers: {})
30
38
 
31
39
  job = {
@@ -36,17 +44,15 @@ module GcpScheduler
36
44
  http_target: {
37
45
  uri: uri,
38
46
  http_method: http_method,
39
- body: params.to_json,
40
- headers: headers.merge(
41
- {
42
- "Authorization" => "Bearer #{secret}",
43
- "Content-Type" => "application/json",
44
- "User-Agent" => "Google-Cloud-Scheduler",
45
- },
46
- ),
47
+ body: params,
48
+ headers: headers
47
49
  },
48
50
  }
49
51
 
52
+ if params.present? && headers["Content-Type"] == "application/json"
53
+ job[:http_target][:body] = params.to_json
54
+ end
55
+
50
56
  client.create_job parent: parent, job: job
51
57
  end
52
58
 
data/lib/gcp_scheduler.rb CHANGED
@@ -10,21 +10,18 @@ module GcpScheduler
10
10
 
11
11
  class << self
12
12
  def list(gcp_project:, region:, prefix: "")
13
- GcpScheduler::Command.list(gcp_project: gcp_project, prefix: prefix, region: region)
13
+ command = GcpScheduler::Command.new(gcp_project: gcp_project, region: region)
14
+ command.list(prefix: prefix)
14
15
  end
15
16
 
16
17
  def delete(gcp_project:, region:, prefix: "")
17
- GcpScheduler::Command.delete(gcp_project: gcp_project, prefix: prefix, region: region)
18
+ command = GcpScheduler::Command.new(gcp_project: gcp_project, region: region)
19
+ command.delete(prefix: prefix)
18
20
  end
19
21
 
20
- def create(gcp_project:, region:, scheduler_file_path:, uri:, secret:, time_zone:, prefix: "")
21
- GcpScheduler::Command.create(gcp_project: gcp_project,
22
- region: region,
23
- prefix: prefix,
24
- scheduler_file_path: scheduler_file_path,
25
- uri: uri,
26
- secret: secret,
27
- time_zone: time_zone)
22
+ def create(gcp_project:, region:, scheduler_file_path:, prefix: "")
23
+ command = GcpScheduler::Command.new(gcp_project: gcp_project, region: region)
24
+ command.create(prefix: prefix, scheduler_file_path: scheduler_file_path)
28
25
  end
29
26
  end
30
27
  end
@@ -1,2 +1,12 @@
1
- weekly_job:
2
- cron: '0 9 * * 1'
1
+ jobs:
2
+ - name: WeeklyJob
3
+ description: 'Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
4
+ schedule: '0 9 * * *'
5
+ time_zone: Asia/Tokyo
6
+ uri: https://yourdomain.example.com/api/v1/jobs
7
+ http_method: POST
8
+ params:
9
+ job_name: weekly_job
10
+ http_headers:
11
+ Content-Type: application/json
12
+ Authorization: 'Bearer <%= ENV["SECRET"] %>'
@@ -0,0 +1,19 @@
1
+ defaults:
2
+ uri: https://yourdomain.example.com/api/v1/jobs
3
+ http_method: POST
4
+ time_zone: Asia/Tokyo
5
+ http_headers:
6
+ Authorization: 'Bearer <%= ENV["SECRET"] %>'
7
+ Content-Type: application/json
8
+
9
+ jobs:
10
+ - name: WeeklyJob
11
+ description: 'Runs every week at 9:00 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
12
+ schedule: '0 9 * * *'
13
+ params:
14
+ job_name: weekly_job
15
+ - name: DailyJob
16
+ description: 'Runs every day at 10:30 a.m. Created:<%= Time.now.strftime("%Y/%m/%d %-H:%M") %>'
17
+ schedule: '30 10 * * *'
18
+ params:
19
+ job_name: daily_job
@@ -2,10 +2,85 @@ require "spec_helper"
2
2
 
3
3
  describe GcpScheduler::Scheduler do
4
4
  describe "#scheduler_config" do
5
+ before do
6
+ travel_to Time.parse("2023/4/1 10:00")
7
+ ENV["SECRET"] = "secret_key"
8
+ end
9
+
10
+ after do
11
+ travel_back
12
+ end
13
+
5
14
  it do
6
- path = fixture_path("scheduler.yml")
15
+ path = fixture_path("scheduler.yml")
7
16
  config = GcpScheduler::Scheduler.scheduler_config(path)
8
- expect(config).to eq({ "weekly_job" => { "cron" => "0 9 * * 1" } })
17
+ jobs = config[:jobs]
18
+ expect(jobs.size).to eq(1)
19
+ job = jobs.first
20
+ expect(job[:params]).to eq({ "job_name" => "weekly_job" })
21
+ expect(job[:time_zone]).to eq("Asia/Tokyo")
22
+ expect(job[:description]).to eq("Runs every week at 9:00 a.m. Created:2023/04/01 10:00")
23
+ expect(job[:http_headers]).to eq("Authorization" => "Bearer secret_key", "Content-Type" => "application/json")
24
+ expect(job[:http_method]).to eq("POST")
25
+ expect(job[:name]).to eq("WeeklyJob")
26
+ expect(job[:schedule]).to eq("0 9 * * *")
27
+ expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
28
+ end
29
+ end
30
+
31
+ describe "#scheduler_config_jobs" do
32
+ before do
33
+ travel_to Time.parse("2023/4/1 11:00")
34
+ ENV["SECRET"] = "secret_key"
35
+ end
36
+
37
+ after do
38
+ travel_back
39
+ end
40
+
41
+ it "Configuration file with default settings" do
42
+ path = fixture_path("scheduler_with_defaults.yml")
43
+ jobs = GcpScheduler::Scheduler.scheduler_config_jobs(file_path: path,
44
+ job_name_prefix: "rspec-")
45
+
46
+ expect(jobs.size).to eq(2)
47
+ weekly_job = jobs.first
48
+ expect(weekly_job[:name]).to eq("rspec-WeeklyJob")
49
+ expect(weekly_job[:params]).to eq({ "job_name" => "weekly_job" })
50
+ expect(weekly_job[:description]).to eq("Runs every week at 9:00 a.m. Created:2023/04/01 11:00")
51
+ expect(weekly_job[:schedule]).to eq("0 9 * * *")
52
+
53
+ daily_job = jobs.last
54
+ expect(daily_job[:name]).to eq("rspec-DailyJob")
55
+ expect(daily_job[:params]).to eq({ "job_name" => "daily_job" })
56
+ expect(daily_job[:description]).to eq("Runs every day at 10:30 a.m. Created:2023/04/01 11:00")
57
+ expect(daily_job[:schedule]).to eq("30 10 * * *")
58
+
59
+ # defaults
60
+ jobs.each do |job|
61
+ expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
62
+ expect(job[:http_method]).to eq("POST")
63
+ expect(job[:time_zone]).to eq("Asia/Tokyo")
64
+ http_headers = { "Authorization" => "Bearer secret_key", "Content-Type" => "application/json" }
65
+ expect(job[:http_headers]).to eq(http_headers)
66
+ end
67
+ end
68
+
69
+ it "Configuration file without default settings" do
70
+ path = fixture_path("scheduler.yml")
71
+ jobs = GcpScheduler::Scheduler.scheduler_config_jobs(file_path: path,
72
+ job_name_prefix: "rspec-")
73
+
74
+ expect(jobs.size).to eq(1)
75
+ job = jobs.first
76
+ expect(job[:name]).to eq("rspec-WeeklyJob")
77
+ expect(job[:description]).to eq("Runs every week at 9:00 a.m. Created:2023/04/01 11:00")
78
+ expect(job[:schedule]).to eq("0 9 * * *")
79
+ expect(job[:time_zone]).to eq("Asia/Tokyo")
80
+ expect(job[:uri]).to eq("https://yourdomain.example.com/api/v1/jobs")
81
+ expect(job[:http_method]).to eq("POST")
82
+ expect(job[:params]).to eq({ "job_name" => "weekly_job" })
83
+ expect(job[:http_headers]).to eq("Authorization" => "Bearer secret_key", "Content-Type" => "application/json")
9
84
  end
10
85
  end
11
86
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
2
2
  require "pry"
3
3
  require "rspec"
4
-
4
+ require "active_support/testing/time_helpers"
5
5
  require "gcp_scheduler"
6
6
  require_relative "support/path"
7
7
 
8
8
  RSpec.configure do |config|
9
9
  config.include Spec::Path
10
+ config.include ActiveSupport::Testing::TimeHelpers
10
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcp_scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Kusumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-20 00:00:00.000000000 Z
11
+ date: 2023-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -80,11 +80,10 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.9'
83
- description: GCP Scheduler CLI is a Ruby gem that provides a simple command-line interface
83
+ description: GCP Scheduler is a Ruby gem that provides a simple command-line interface
84
84
  for managing Google Cloud Scheduler jobs. With this tool, you can create, list,
85
85
  and delete Cloud Scheduler jobs using intuitive commands. It streamlines job management
86
- tasks by allowing you to define job schedules in a YAML file and supports custom
87
- job prefixes, regions, and authentication tokens.
86
+ tasks by allowing you to define job schedules in a YAML file.
88
87
  email:
89
88
  - akirakusumo10@gmail.com
90
89
  executables:
@@ -107,6 +106,7 @@ files:
107
106
  - lib/gcp_scheduler/command.rb
108
107
  - lib/gcp_scheduler/scheduler.rb
109
108
  - spec/fixtures/scheduler.yml
109
+ - spec/fixtures/scheduler_with_defaults.yml
110
110
  - spec/gcp_scheduler/scheduler_spec.rb
111
111
  - spec/gcp_scheduler_spec.rb
112
112
  - spec/spec_helper.rb