populus 0.0.1 → 0.0.3

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
  SHA1:
3
- metadata.gz: 0af5cd919676d03ad1cf32acd295b5f56274d0c6
4
- data.tar.gz: 373393234f80be72ec88e5d546e84a51f3aaa080
3
+ metadata.gz: e1b404b1feb1d96fc1de40bb3806a202f8f2eb37
4
+ data.tar.gz: 1cf98167ae0480f6003f10026b9307c04f58b774
5
5
  SHA512:
6
- metadata.gz: 63e3cd016902a544e7d173c4695218786a10bfd7f5f2cc0830c14a48834dd500635a1f3d341f30fac661ed9b1f43367a5c82cc48a59372726b938699e6c27489
7
- data.tar.gz: 55f5b7ca913d0896f3d26006d96446a68361ccf40f95ae907971af6e06db8a22ecf8155d21e4acd0306a911722b9e55d742a196441d879cfebd31329f9b1edcc
6
+ metadata.gz: afc2e559d6b522b0c9773fe429183fa167d71b8051b44bcbd186673a31a00473bc3a7f67beae154d4f68612cc23d15c6a31aee5d4b958d0dcf9aeb01aa12cddf
7
+ data.tar.gz: 76ae2b3f08db7cfa45b27edbd37d1e5be19dfa8fd133a6ff23096a80aec385e4419fab212460cc6e9eee271902cd410cad9975d9a5b2093053007697d1ec26f4
data/README.md CHANGED
@@ -23,24 +23,40 @@ Or install it yourself as:
23
23
  ### Usages below are just a draft
24
24
 
25
25
  ```ruby
26
- Populus.watch 'node' do
27
- on_receive do |nodes|
28
- if nodes.count < 10
29
- logger.warn "nodes are too short!"
30
- end
26
+ Populus.config do
27
+ set :slack_webhook, "https://hooks.slack.com/services/XXXXXX"
28
+ end
29
+
30
+ Populus.watch :event, name: "echo" do
31
+ cond {|data| data.any?{|d| d.has_key?('Payload')} }
32
+ runs do |data|
33
+ event = data.find{|d| d.has_key?('Payload')}
34
+ Populus.logger.info "From populus!!!"
35
+ Populus.logger.info Base64.decode64(event['Payload'])
36
+ end
37
+ end
38
+
39
+ Populus.watch :event, name: "slack" do
40
+ cond {|data| data.any?{|d| d.has_key?('Payload')} }
41
+ runs do |data|
42
+ event = data.first
43
+
44
+ notify_to_slack("Hello!!!\nConsul event payload:\n>>> `#{event.inspect}`", channel: '#test', username: "Populus")
31
45
  end
32
46
  end
33
47
  ```
34
48
 
35
49
  ```bash
36
- consul watch -type node "bundle exec populus accept sample.popl"
50
+ bundle exec populus watch sample.popl
37
51
  ```
38
52
 
39
53
  ### TODOs
40
54
 
41
- * Deployment switching by node name
42
- * Condition phrase against JSON
43
- * Local/remote execution by specinfra
55
+ * [ ] Deployment switching by node name
56
+ * [x] Condition phrase against JSON
57
+ * [ ] Local/remote execution by specinfra
58
+ * [ ] More helpers
59
+ * [ ] Omnibusify
44
60
 
45
61
  ## Contributing
46
62
 
data/examples/echo.popl CHANGED
@@ -1,11 +1,17 @@
1
1
  # -*- mode: ruby -*-
2
2
  require 'base64'
3
3
 
4
+ Populus.config do
5
+ set :slack_webhook, "https://hooks.slack.com/services/XXXXXX"
6
+ end
7
+
4
8
  Populus.watch :event, name: "sample" do
5
- When {|data| data.any?{|d| d.has_key?('Payload')} }
6
- Runs do |data|
9
+ cond {|data| data.any?{|d| d.has_key?('Payload')} }
10
+ runs do |data|
7
11
  event = data.find{|d| d.has_key?('Payload')}
8
12
  Populus.logger.info "From populus!!!"
9
13
  Populus.logger.info Base64.decode64(event['Payload'])
14
+
15
+ notify_to_slack("Hello!!!\nConsul event payload:\n>>> `#{event.inspect}`", channel: '#bot_test', username: "Populus")
10
16
  end
11
17
  end
@@ -0,0 +1,29 @@
1
+ # -*- mode: ruby -*-
2
+ require 'base64'
3
+
4
+ Populus.watch :event, name: "test001" do
5
+ cond {|data| data.any?{|d| d.has_key?('Payload')} }
6
+ runs do |data|
7
+ event = data.find{|d| d.has_key?('Payload')}
8
+ Populus.logger.info "From populus!!! this is test001"
9
+ Populus.logger.info Base64.decode64(event['Payload'])
10
+ end
11
+ end
12
+
13
+ Populus.watch :event, name: "test002" do
14
+ cond {|data| data.any?{|d| d.has_key?('Payload')} }
15
+ runs do |data|
16
+ event = data.find{|d| d.has_key?('Payload')}
17
+ Populus.logger.info "From populus!!! this is test002"
18
+ Populus.logger.info Base64.decode64(event['Payload'])
19
+ end
20
+ end
21
+
22
+ Populus.watch :event, name: "test003" do
23
+ cond {|data| data.any?{|d| d.has_key?('Payload')} }
24
+ runs do |data|
25
+ event = data.find{|d| d.has_key?('Payload')}
26
+ Populus.logger.info "From populus!!! this is test003"
27
+ Populus.logger.info Base64.decode64(event['Payload'])
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # -*- mode: ruby -*-
2
+ Populus.config do
3
+ set :ssh_options, {
4
+ user: 'operator',
5
+ keys: '~/.ssh/sample.pem'
6
+ }
7
+ end
8
+
9
+ Populus.node 'web001.example.jp'
10
+
11
+ Populus.watch :event, name: "run-command" do
12
+ cond {|_| true }
13
+ runs do |data|
14
+ on 'web001.example.jp' do
15
+ execute "uptime"
16
+ execute "whoami"
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,6 @@
1
+ require 'populus/remote_runner'
2
+ require 'populus/node'
3
+
1
4
  module Populus
2
5
  class Accepter
3
6
  # TODO: validators
@@ -20,13 +23,22 @@ module Populus
20
23
  def accept(data)
21
24
  if condition[data]
22
25
  Populus.logger.debug "Condition judged true: #{data.inspect}"
23
- runner[data]
26
+ instance_exec(data, &runner)
24
27
  return true
25
28
  else
26
29
  Populus.logger.debug "Condition judged false: #{data.inspect}"
27
30
  return false
28
31
  end
29
32
  end
33
+
34
+ def on(hostname, &run_it)
35
+ be = Node.registory[hostname]
36
+ if be
37
+ RemoteRunner.new(be, &run_it)
38
+ else
39
+ Populus.logger.warn "Not found host: #{hostname}. Skip."
40
+ end
41
+ end
30
42
  end
31
43
 
32
44
  class Event < Base
@@ -0,0 +1,35 @@
1
+ require 'populus'
2
+
3
+ module Populus
4
+ class Configuration
5
+ def initialize
6
+ @pool = {}
7
+ end
8
+
9
+ def set(key, value)
10
+ true_value = case value
11
+ when Proc
12
+ value
13
+ else
14
+ lambda { value }
15
+ end
16
+ self.define_singleton_method key do
17
+ @pool[key] || (
18
+ @pool[key] = true_value.call
19
+ )
20
+ end
21
+ end
22
+
23
+ def method_missing(meth, *args)
24
+ raise NameError, "Populus::Configuration value #{meth} is not yet defined"
25
+ end
26
+ end
27
+
28
+ def self.config(&config_block)
29
+ @_config ||= Configuration.new
30
+ if block_given?
31
+ @_config.instance_eval(&config_block)
32
+ end
33
+ @_config
34
+ end
35
+ end
data/lib/populus/dsl.rb CHANGED
@@ -1,25 +1,37 @@
1
1
  require 'populus/pool'
2
+ require 'populus/node'
2
3
  require 'populus/accepter'
3
4
 
4
5
  module Populus
5
6
 
7
+ # Populus.node 'web001.exapmle.jp', 'web002.exapmle.jp'
8
+ #
6
9
  # Populus.watch :event, name: "sample" do
7
- # When {|data| data.has_key?('Payload') }
8
- # Runs do |data|
10
+ # cond {|data| data.has_key?('Payload') }
11
+ # runs do |data|
9
12
  # Populus.logger.info Base64.decode(data['Payload'])
10
13
  # end
11
14
  # end
15
+ #
16
+ # Populus.watch :event, name: "sample2" do
17
+ # cond {|data| data.has_key?('Payload') }
18
+ # runs do |data|
19
+ # on 'web001.exapmle.jp' do
20
+ # execute 'whoami'
21
+ # end
22
+ # end
23
+ # end
12
24
  module DSL
13
25
  class DSLContext
14
26
  def initialize(wrap_obj)
15
27
  @wrap_obj = wrap_obj
16
28
  end
17
29
 
18
- def When(&do_foobar)
30
+ def cond(&do_foobar)
19
31
  @wrap_obj.condition = do_foobar
20
32
  end
21
33
 
22
- def Runs(&do_foobar)
34
+ def runs(&do_foobar)
23
35
  @wrap_obj.runner = do_foobar
24
36
  end
25
37
  end
@@ -30,9 +42,10 @@ module Populus
30
42
  Pool.register_object accepter
31
43
  end
32
44
 
33
- def find_accepter(type)
34
- const = type.gsub(/(^.|_.)/) {|c| c.tr('_', '').upcase }
35
- Accepter.const_get(const)
45
+ def node(*nodes)
46
+ nodes.each do |node|
47
+ Node.register_host(node)
48
+ end
36
49
  end
37
50
 
38
51
  def eval_setting(path)
@@ -41,6 +54,13 @@ module Populus
41
54
  STDERR.puts "Invalid setting format! #{path}", "error is:", e.class, e.message, e.backtrace
42
55
  exit 1
43
56
  end
57
+
58
+ private
59
+
60
+ def find_accepter(type)
61
+ const = type.gsub(/(^.|_.)/) {|c| c.tr('_', '').upcase }
62
+ Accepter.const_get(const)
63
+ end
44
64
  end
45
65
 
46
66
  extend DSL
@@ -0,0 +1,10 @@
1
+ require 'populus/helpers'
2
+ require 'slack-notifier'
3
+
4
+ Populus::Helpers.define_helper :notify_to_slack do |message, options={}|
5
+ notifier = Slack::Notifier.new Populus.config.slack_webhook
6
+ notifier.channel = options[:channel] if options[:channel]
7
+ notifier.username = options[:username] if options[:username]
8
+
9
+ notifier.ping message
10
+ end
@@ -0,0 +1,19 @@
1
+ require 'populus/accepter'
2
+
3
+ module Populus
4
+ module Helpers
5
+ extend self
6
+
7
+ def define_helper(name, &block)
8
+ m = Module.new do
9
+ define_method(name, &block)
10
+ end
11
+ Populus.logger.info "Register helper: #{name}"
12
+ Accepter::Base.send :include, m
13
+ end
14
+ end
15
+ end
16
+
17
+ Dir.glob("#{File.dirname __FILE__}/helpers/*.rb").each do |helper|
18
+ require helper
19
+ end
@@ -0,0 +1,24 @@
1
+ require 'specinfra'
2
+
3
+ module Populus
4
+ class Node
5
+ class << self
6
+ def registory
7
+ @nodes ||= {}
8
+ end
9
+
10
+ def register_host(hostname)
11
+ registory[hostname] = gen_host(hostname)
12
+ end
13
+
14
+ private
15
+ def gen_host(hostname)
16
+ return Specinfra::Backend::Ssh.new(
17
+ host: hostname,
18
+ ssh_options: Populus.config.ssh_options,
19
+ request_pty: true
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'specinfra'
2
+
3
+ module Populus
4
+ class RemoteRunner
5
+ def initialize(backend, &run_it)
6
+ @backend = backend
7
+ instance_exec(&run_it)
8
+ end
9
+
10
+ def execute(*command)
11
+ Populus.logger.info("Running command: %s" % command.inspect)
12
+
13
+ res = @backend.run_command(command.join(" "))
14
+ Populus.logger.debug("stdout:\n%s" % res.stdout)
15
+ Populus.logger.debug("stderr:\n%s" % res.stderr)
16
+ Populus.logger.info("Command exited: %d" % res.exit_status)
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Populus
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/populus.rb CHANGED
@@ -1,12 +1,7 @@
1
1
  module Populus; end
2
2
 
3
3
  require "logger"
4
- require "populus/pool"
5
- require "populus/dsl"
6
- require "populus/daemon"
7
- require "populus/watch_thread"
8
4
  require "populus/default_logger"
9
- require "populus/version"
10
5
 
11
6
  module Populus
12
7
  class << self
@@ -16,3 +11,11 @@ module Populus
16
11
  self.consul_bin = 'consul' # default to count on $PATH
17
12
  self.logger = Populus::DefaultLogger
18
13
  end
14
+
15
+ require "populus/pool"
16
+ require "populus/helpers"
17
+ require "populus/configuration"
18
+ require "populus/dsl"
19
+ require "populus/daemon"
20
+ require "populus/watch_thread"
21
+ require "populus/version"
data/populus.gemspec CHANGED
@@ -14,12 +14,13 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "specinfra"
22
22
  spec.add_dependency "colorize"
23
+ spec.add_dependency "slack-notifier"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.7"
25
26
  spec.add_development_dependency "rake", ">= 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: populus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio, KONDO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-16 00:00:00.000000000 Z
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: specinfra
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: slack-notifier
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -69,7 +83,8 @@ dependencies:
69
83
  description: Consul event definition DSL
70
84
  email:
71
85
  - udzura@udzura.jp
72
- executables: []
86
+ executables:
87
+ - populus
73
88
  extensions: []
74
89
  extra_rdoc_files: []
75
90
  files:
@@ -81,12 +96,19 @@ files:
81
96
  - Rakefile
82
97
  - bin/populus
83
98
  - examples/echo.popl
99
+ - examples/multi-event.popl
100
+ - examples/remote_host.popl
84
101
  - lib/populus.rb
85
102
  - lib/populus/accepter.rb
103
+ - lib/populus/configuration.rb
86
104
  - lib/populus/daemon.rb
87
105
  - lib/populus/default_logger.rb
88
106
  - lib/populus/dsl.rb
107
+ - lib/populus/helpers.rb
108
+ - lib/populus/helpers/slack.rb
109
+ - lib/populus/node.rb
89
110
  - lib/populus/pool.rb
111
+ - lib/populus/remote_runner.rb
90
112
  - lib/populus/version.rb
91
113
  - lib/populus/watch_thread.rb
92
114
  - populus.gemspec