Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
implement
Browse files Browse the repository at this point in the history
DSL example:

```ruby
cron_job "example.com" do
  cron_expression "0 0 * * *"
  url "http://example.com"
  email_me 0
  log_output_length 0
end
cron_job "www.example.com" do
  cron_expression "0 0 * * *"
  url "http://www.example.com"
  email_me 0
  log_output_length 0
end
```
  • Loading branch information
Genki Sugawara committed Dec 4, 2016
1 parent 2181c5e commit 941555e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
/*.rb
7 changes: 4 additions & 3 deletions ecman.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
spec.authors = ["Genki Sugawara"]
spec.email = ["sugawara@cookpad.com"]

spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = %q{EasyCron as Code.}
spec.description = %q{EasyCron as Code.}
spec.homepage = "https://github.com/winebarrel/ecman"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
Expand Down Expand Up @@ -41,4 +41,5 @@ Gem::Specification.new do |spec|
#spec.add_dependency 'pp_sort_hash'
spec.add_dependency 'term-ansicolor'
spec.add_dependency 'thor'
spec.add_dependency 'easycron'
end
1 change: 1 addition & 0 deletions lib/ecman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'logger'
require 'singleton'
require 'term/ansicolor'
require 'easycron'

require 'ecman/version'
#require 'ecman/ext/hash_ext'
Expand Down
2 changes: 1 addition & 1 deletion lib/ecman/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Ecman::CLI < Thor
# vi: set ft=ruby :
EOS


class_option :token, default: ENV['EASYCRON_TOKEN']
class_option :target
class_option :color, type: :boolean, default: true
class_option :debug, type: :boolean, default: false
Expand Down
18 changes: 8 additions & 10 deletions lib/ecman/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ class Ecman::Client

def initialize(options = {})
@options = options
# FIXME: create api client
@client = @options[:client] # || YourService::Client.new
@client = @options[:client] || Easycron::Client.new(token: options.fetch(:token))
@driver = Ecman::Driver.new(@client, options)
@exporter = Ecman::Exporter.new(@client, @options)
end

def export
expected = @exporter.export
expected = @exporter.export(without_cron_job_id: true)
Ecman::DSL.convert(expected)
end

Expand All @@ -30,12 +29,8 @@ def apply(file)
private

def walk(expected, actual)
# FIXME:
warn 'FIXME: Client#walk() not implemented'.yellow

# FIXME: this is an example
expected = expected.fetch('server')
actual = actual.fetch('server')
expected = expected.fetch(Ecman::DSL::ROOT_KEY)
actual = actual.fetch(Ecman::DSL::ROOT_KEY)

updated = false

Expand All @@ -45,7 +40,10 @@ def walk(expected, actual)
actual_attrs = actual.delete(name)

if actual_attrs
if expected_attrs != actual_attrs
actual_attrs_without_id = actual_attrs.dup
actual_attrs_without_id.delete('cron_job_id')

if expected_attrs != actual_attrs_without_id
@driver.update(name, expected_attrs, actual_attrs)
updated = true
end
Expand Down
19 changes: 13 additions & 6 deletions lib/ecman/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,34 @@ def create(name, attrs)
log(:info, "Create '#{name}'", color: :cyan)

unless @options[:dry_run]
# FIXME:
warn 'FIXME: Driver#create() not implemented'.yellow
attrs.update('cron_job_name' => name)
@client.add(**symbolize(attrs))
end
end

def delete(name, attrs)
cron_job_id = attrs.delete('cron_job_id')
log(:info, "Delete '#{name}'", color: :red)

unless @options[:dry_run]
# FIXME:
warn 'FIXME: Driver#delete() not implemented'.yellow
@client.delete(id: cron_job_id)
end
end

def update(name, attrs, old_attrs)
cron_job_id = old_attrs.delete('cron_job_id')
log(:info, "Update '#{name}'", color: :green)
log(:info, diff(old_attrs, attrs, color: @options[:color]), color: false)

unless @options[:dry_run]
# FIXME:
warn 'FIXME: Driver#update() not implemented'.yellow
attrs.update('id' => cron_job_id, 'cron_job_name' => name)
@client.edit(**symbolize(attrs))
end
end

private

def symbolize(attrs)
attrs.map {|k, v| [k.to_sym, v] }.to_h
end
end
2 changes: 2 additions & 0 deletions lib/ecman/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Ecman::DSL
ROOT_KEY = 'cron_job'

class << self
def convert(exported)
Dslh.deval(exported, root_identify: true)
Expand Down
30 changes: 17 additions & 13 deletions lib/ecman/dsl/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
# see http://www.kuwata-lab.com/kwalify/ruby/users-guide.html
type: map
mapping:
"server":
"cron_job":
type: seq
sequence:
- type: map
mapping:
"_id":
type: str
required: yes
"middleware":
"cron_expression":
type: str
required: yes
"tag":
type: seq
sequence:
- type: map
mapping:
"key":
type: str
required: true
"value":
type: str
required: true
"url":
type: str
required: yes
"email_me":
type: int
required: yes
"log_output_length":
type: int
required: yes
"cookies":
type: str
"posts":
type: str
"via_tor":
type: int
38 changes: 25 additions & 13 deletions lib/ecman/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,31 @@ def initialize(client, options = {})
@options = options
end

def export
# FIXME:
warn 'FIXME: Exporter#export() not implemented'.yellow
def export(without_cron_job_id: false)
result = {}
cron_jobs = result[Ecman::DSL::ROOT_KEY] = {}

# FIXME: this is an example
{"server"=>
{"web"=>
{"middleware"=>"nginx",
"tag"=>
[{"key"=>"Name", "value"=>"web-001"}, {"key"=>"Role", "value"=>"Web"}]},
"database"=>
{"middleware"=>"mysql",
"tag"=>
[{"key"=>"Name", "value"=>"db-002"}, {"key"=>"Role", "value"=>"DB"}]}}}
@client.list.fetch('cron_jobs').each do |cron_job|
cron_job_name = cron_job.fetch('cron_job_name')
next if cron_job_name.empty?

cron_jobs[cron_job_name] = {
'cron_expression' => cron_job.fetch('cron_expression'),
'url' => cron_job.fetch('url'),
'email_me' => cron_job.fetch('email_me').to_i,
'log_output_length' => cron_job.fetch('log_output_length').to_i,
}

unless without_cron_job_id
cron_jobs[cron_job_name]['cron_job_id'] = cron_job.fetch('cron_job_id').to_i
end

%w(cookies posts via_tor).each do |key|
value = cron_job[key]
cron_jobs[cron_job_name][key] = value if value
end
end

result
end
end

0 comments on commit 941555e

Please sign in to comment.