gcp_scheduler 0.0.1 → 0.1.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: 45a9e2f3c9ca228ac9fa5751a87c9beba2ec73b8e38e0c2b1ef513d766178fc5
4
+ data.tar.gz: 255d18bd2a649d13fc24b1103c209aeaf2c29268f3f887e5720de376192059b8
5
5
  SHA512:
6
- metadata.gz: 7e3431c095b0806934a1288c5f0e781037f38bc0890e394e5b9ed8aabb01ab1573abfa01016474db7ed487d653446044950b12865c2ef4290817581c7388d035
7
- data.tar.gz: 231ed0ea3846239aa1b95b34372325481a18325afa51ef9973479caa9b4c73779a8ee1a2c83e7d911386f4ab501b96328164d38c620dbeb030bc3791c87a5fa1
6
+ metadata.gz: 4a1a0bb5870a8fcbd739b00f394b6be7abb39b62142531383e200e5f8c2b7c2930b9bbadcbe73dc16482400de3e05898c520bdc2449c358d842aff35a4d23ee1
7
+ data.tar.gz: fc388ea4d13736d703a92f7f4098918c41c8d8648b03ac96f3549f78c5a9c3377f7db7e228f9e4cece9d0636a49bc9813fbf68d20bd2574e3060f16fe45aa01c
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.1.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.1.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,44 @@
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(scheduler_file_path)[:jobs].each do |job|
31
+ scheduler_name = "#{prefix}#{job[:name]}"
32
+ puts "Create #{scheduler_name}"
33
+ scheduler.create_job(name: scheduler_name,
34
+ description: job[:description],
35
+ uri: job[:uri],
36
+ schedule: job[:schedule],
37
+ time_zone: job[:time_zone],
38
+ params: job[:params],
39
+ http_method: job[:http_method],
40
+ headers: job[:http_headers])
41
+ puts "Created #{scheduler_name}"
40
42
  end
41
43
  end
42
44
  end
@@ -4,7 +4,7 @@ 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
8
  end
9
9
  end
10
10
 
@@ -24,8 +24,9 @@ module GcpScheduler
24
24
  description:,
25
25
  uri:,
26
26
  schedule:,
27
- secret:, time_zone:, params: {},
28
- http_method: :POST,
27
+ time_zone:,
28
+ params: {},
29
+ http_method:,
29
30
  headers: {})
30
31
 
31
32
  job = {
@@ -37,13 +38,7 @@ module GcpScheduler
37
38
  uri: uri,
38
39
  http_method: http_method,
39
40
  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
- ),
41
+ headers: headers
47
42
  },
48
43
  }
49
44
 
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'] %>"
@@ -2,10 +2,29 @@ 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")
9
28
  end
10
29
  end
11
30
  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.1.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-04-21 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: