pushyd 0.6.0 → 0.7.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
  SHA1:
3
- metadata.gz: 5f089c97e7a6a3ef7b973c85d0ed46441095cd65
4
- data.tar.gz: d663be735567c62e089e4037969bff5dc303c502
3
+ metadata.gz: de49b896a3b942b2babca81aa547804008fe7e39
4
+ data.tar.gz: b31568f729c9bf37c5b423f578c94a68ffaef429
5
5
  SHA512:
6
- metadata.gz: b9bebfd5975a91d43c6bc0bd8cf8f1d3382571712483d7d75abe87ea1da1fb6e9c97f4fb43a111f6b31ced941f26406be20f5f94e918a97ee4f96e9e5cec51e1
7
- data.tar.gz: 7e16e42759376aa51ef81fb3df3910dc35726d9241761446e1e16e83a91ec8b0c14c4e09d43ec8666d1335b718891633c6834d61063fdec28d0b033344fecc2e
6
+ metadata.gz: 73bafb1821c8c3d52f59c5202f24ee18c367c472eb2b80c082e5b81a5b7f2fa1b7568a882b46c1b4d9558b013cb99de8090dc2a30a054f57e685a08454148a77
7
+ data.tar.gz: bcd83b4f5351c6eee60798bf37fcf2d85f5c9ada8a7d0bcce64907aa32cf8ec347140f2e8cc2de56f3f055685eedd354e6c4b17c726bf2a0911514b92934f304
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pushyd (0.6.0)
4
+ pushyd (0.7.0)
5
+ api-auth
5
6
  bunny (~> 2.3)
6
7
  chamber (~> 2.9)
7
8
  daemons
@@ -15,6 +16,7 @@ GEM
15
16
  specs:
16
17
  addressable (2.4.0)
17
18
  amq-protocol (2.0.1)
19
+ api-auth (2.0.0)
18
20
  ast (2.3.0)
19
21
  bunny (2.4.0)
20
22
  amq-protocol (>= 2.0.1)
@@ -38,7 +40,7 @@ GEM
38
40
  json (1.8.3)
39
41
  mime-types (2.99.2)
40
42
  netrc (0.11.0)
41
- newrelic_rpm (3.15.2.317)
43
+ newrelic_rpm (3.16.0.318)
42
44
  parser (2.3.1.2)
43
45
  ast (~> 2.2)
44
46
  powerpack (0.1.1)
data/bin/pushyd CHANGED
@@ -23,13 +23,14 @@ begin
23
23
  Conf.app_env = "production"
24
24
 
25
25
  # Parse options and check compliance
26
- OptionParser.new do |opts|
26
+ parser = OptionParser.new do |opts|
27
27
  opts.banner = "Usage: #{File.basename $PROGRAM_NAME} [options] start|stop"
28
28
  opts.on("-l", "--log LOGFILE") { |path| cmd_logfile = File.expand_path(path)}
29
29
  opts.on("-c", "--config CONFIGFILE") { |path| cmd_config = File.expand_path(path)}
30
30
  opts.on("-e", "--environment ENV") { |env| Conf.app_env = env }
31
31
  opts.on("", "--dev") { Conf.app_env = "development" }
32
- end.order!(ARGV)
32
+ end
33
+ parser.order!(ARGV)
33
34
 
34
35
  # Load Chamber-based configuration
35
36
  Conf.prepare config: cmd_config, logfile: cmd_logfile
@@ -53,17 +54,20 @@ puts "Environment \t #{Conf.app_env}"
53
54
  puts "Config files \t #{Conf.files}"
54
55
  puts "Started at \t #{Conf.app_started}"
55
56
  puts "Loging to file \t #{Conf[:log][:file]}" if Conf[:log].is_a? Enumerable
56
- puts "Process name \t #{Conf.gen_process_name}"
57
+ puts "Process name \t #{Conf.generate(:process_name)}"
57
58
  puts
58
59
  puts Conf.dump
59
60
 
60
61
  # Run daemon
61
62
  run_options = {
63
+ monitor: false,
62
64
  ontop: false,
63
- :backtrace => true,
64
- :multiple => false
65
+ backtrace: true,
66
+ multiple: false,
67
+ dir_mode: :normal,
68
+ dir: '/tmp/'
65
69
  }
66
- Daemons.run_proc(Conf.gen_process_name, run_options) do
70
+ Daemons.run_proc(Conf.generate(:process_name), run_options) do
67
71
  # Load code
68
72
  puts "--- load code"
69
73
  require_relative "../lib/pushyd"
data/lib/pushyd/proxy.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'api_auth'
1
2
  require 'rest_client'
2
3
  require 'terminal-table'
3
4
 
@@ -47,7 +48,6 @@ module PushyDaemon
47
48
  def handle_message rule, delivery_info, metadata, payload
48
49
  # Prepare data
49
50
  rule_name = rule[:name]
50
- rule_relay = rule[:relay]
51
51
  msg_exchange = delivery_info.exchange
52
52
  msg_rkey = delivery_info.routing_key.force_encoding('UTF-8')
53
53
  msg_headers = metadata.headers || {}
@@ -63,7 +63,7 @@ module PushyDaemon
63
63
  }
64
64
 
65
65
  # Build notification payload
66
- post_body = {
66
+ propagate_data = {
67
67
  exchange: msg_exchange,
68
68
  route: msg_rkey,
69
69
  sent_at: msg_headers['sent_at'],
@@ -72,25 +72,63 @@ module PushyDaemon
72
72
  }
73
73
 
74
74
  # Propagate data if needed
75
- propagate rule_relay, post_body if rule_relay
75
+ propagate rule, propagate_data
76
76
  end
77
77
 
78
- def propagate relay_url, post_body
78
+ def propagate rule, data
79
79
  # Nothing more to do if no relay
80
- return if relay_url.nil? || relay_url.empty?
80
+ return if rule[:relay].nil? || rule[:relay].empty?
81
81
 
82
- # Generate a unique identifier
82
+ # Prepare stuff
83
+ relay_auth = rule[:auth].to_s
84
+ relay_uri = URI(rule[:relay])
85
+ relay_url = relay_uri.to_s
83
86
  id = identifier(6)
87
+ # log_info "propagate: user[#{relay_uri.user}] url[#{relay_url}]"
88
+
89
+ # Build POST body and log message
90
+ post_body = JSON.pretty_generate(data)
91
+ log_message WAY_PROP, id, relay_url, data
92
+
93
+ # Prepare request
94
+ request = RestClient::Request.new url: relay_url,
95
+ method: :post,
96
+ payload: post_body,
97
+ headers: {
98
+ content_type: :json,
99
+ accept: :json,
100
+ user_agent: Conf.generate(:user_agent),
101
+ }
102
+
103
+ # Accordong to auth type
104
+ # log_info "propagate: auth[#{relay_auth}] class[#{relay_auth.class}]"
105
+
106
+ if !rule["hmac-user"]
107
+ # log_info "propagate: normal"
108
+
109
+ elsif !rule["hmac-secret"]
110
+ log_error "propagate: hmac: missing secret"
111
+ return
112
+ else
113
+ log_info "propagate: hmac: signing request"
114
+ request = ApiAuth.sign!(request, rule["hmac-user"].to_s, rule["hmac-secret"].to_s)
115
+ end
84
116
 
85
- # Log message details
86
- log_message WAY_PROP, id, relay_url, post_body
87
-
88
- # Push message to URL
89
- response = RestClient.post relay_url.to_s, JSON.pretty_generate(post_body), :content_type => :json
90
- log_info "#{id}: #{response.body}"
91
-
117
+ # Send request
118
+ log_info "propagate: url", request.headers
119
+ response = request.execute
120
+
121
+ # Handle exceptions
122
+ rescue RestClient::ExceptionWithResponse, URI::InvalidURIError => e
123
+ log_error "propagate: rest-client: #{e.message}"
124
+ rescue RestClient::InternalServerError => e
125
+ log_error "propagate: rest-client: #{e.message}"
126
+ rescue ApiAuth::ApiAuthError, ApiAuth::UnknownHTTPRequest => e
127
+ log_error "propagate: api-auth: #{e.message}"
128
+ rescue Errno::ECONNREFUSED => e
129
+ log_error "propagate: connection refused: #{e.message}"
92
130
  rescue StandardError => e
93
- log_error "propagate: #{e.message}"
131
+ log_error "propagate: unknown: #{e.message}, #{e.inspect}", e.backtrace
94
132
  end
95
133
 
96
134
  def parse payload, content_type #, fields = []
data/lib/shared/conf.rb CHANGED
@@ -10,6 +10,7 @@ module Shared
10
10
 
11
11
  class Conf
12
12
  extend Chamber
13
+ PIDFILE_DIR = "/tmp/"
13
14
 
14
15
  class << self
15
16
  attr_accessor :app_env
@@ -21,7 +22,6 @@ module Shared
21
22
  attr_reader :app_spec
22
23
  attr_reader :files
23
24
  attr_reader :host
24
-
25
25
  end
26
26
 
27
27
  def self.init app_root
@@ -51,11 +51,12 @@ module Shared
51
51
  fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver
52
52
 
53
53
  # Now we know app_name, initalize app_libs
54
- @app_libs = File.expand_path( @app_root + "/lib/#{@app_name}/" )
54
+ @app_libs = File.expand_path("lib/#{@app_name}/", @app_root)
55
55
 
56
56
  # Add other config files
57
- add_default_config
58
- add_etc_config
57
+ #add_default_config
58
+ add_config generate(:config_defaults)
59
+ add_config generate(:config_etc)
59
60
 
60
61
  # Return something
61
62
  return @app_name
@@ -64,17 +65,20 @@ module Shared
64
65
  def self.prepare args = {}
65
66
  ensure_init
66
67
 
67
- # Add extra config file
68
- add_extra_config args[:config]
69
-
70
- # Load configuration files
71
- load_files
68
+ # Add extra config file and load them all
69
+ add_config args[:config]
70
+ reload!
72
71
 
73
72
  # Set Rack env
74
73
  ENV["RACK_ENV"] = @app_env.to_s
75
74
 
75
+ # Set up encodings
76
+ Encoding.default_internal = "utf-8"
77
+ Encoding.default_external = "utf-8"
78
+
76
79
  # Init New Relic
77
- prepare_newrelic self[:newrelic], self.at(:logs, :newrelic)
80
+ newrelic_logfile = File.expand_path(Conf[:logs][:newrelic], Conf[:logs][:path])
81
+ prepare_newrelic self[:newrelic], newrelic_logfile
78
82
 
79
83
  # Try to access any key to force parsing of the files
80
84
  self[:dummy]
@@ -104,57 +108,55 @@ module Shared
104
108
 
105
109
  def self.newrelic_enabled?
106
110
  ensure_init
107
- !!self[:newrelic]
111
+ self[:newrelic] && self[:newrelic][:licence]
108
112
  end
109
113
 
110
114
  # Defaults generators
111
- def self.gen_pidfile
115
+ def self.generate what
112
116
  ensure_init
113
- "/tmp/#{@app_name}-#{@host}-#{Process.pid}.pid"
114
- end
115
- def self.gen_process_name
116
- ensure_init
117
- "#{@app_name}/#{@app_env}/#{Process.pid}"
118
- end
119
- def self.gen_config_etc
120
- ensure_init
121
- "/etc/#{@app_name}.yml"
122
- end
123
- def self.gen_config_sample
124
- ensure_init
125
- "#{@app_root}/#{@app_name}.sample.yml"
126
- end
127
- def self.gen_config_message
128
- config_etc = gen_config_etc
129
- config_sample = gen_config_sample
130
- return "
131
- A default configuration is available here: #{config_sample}.
132
- You should copy it to the default location: #{config_etc}.
133
- sudo cp #{config_sample} #{config_etc}
134
- "
117
+ return case what
118
+
119
+ when :user_agent
120
+ "#{@app_name}/#{@app_ver}" if @app_name && @app_ver
121
+
122
+ when :config_defaults
123
+ "#{@app_root}/defaults.yml" if @app_root
124
+
125
+ when :config_etc
126
+ "/etc/#{@app_name}.yml" if @app_name
127
+
128
+ when :process_name
129
+ parts = [@app_name, @app_env]
130
+ parts << self[:port] if self[:port]
131
+ parts.join('-')
132
+
133
+ when :pidfile
134
+ process_name = self.generate(:process_name)
135
+ File.expand_path "#{process_name}.pid", PIDFILE_DIR
136
+
137
+ when :config_message
138
+ config_defaults = self.generate(:config_defaults)
139
+ config_etc = self.generate(:config_etc)
140
+
141
+ "A default configuration is available (#{config_defaults}) and can be copied to the default location (#{config_etc}): \n sudo cp #{config_defaults} #{config_etc}"
142
+
143
+ end
135
144
  end
136
145
 
146
+
137
147
  protected
138
148
 
139
149
  def self.load_files
140
150
  load files: @files, namespaces: { environment: @app_env }
141
151
  end
142
152
 
143
- def self.add_default_config
144
- @files << "#{@app_root}/defaults.yml" if @app_root
145
- end
146
-
147
- def self.add_etc_config
148
- @files << File.expand_path("/etc/#{@app_name}.yml") if @app_name
149
- end
150
-
151
- def self.add_extra_config path
152
- @files << File.expand_path(path) if path
153
+ def self.add_config path
154
+ @files << File.expand_path(path) if path && File.readable?(path)
153
155
  end
154
156
 
155
157
  def self.prepare_newrelic section, logfile
156
158
  # Disable NewRelic if no config present
157
- unless section.is_a?(Hash) && section[:licence]
159
+ unless self.newrelic_enabled?
158
160
  ENV["NEWRELIC_AGENT_ENABLED"] = "false"
159
161
  return
160
162
  end
data/pushyd.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  # Project version
4
- spec.version = "0.6.0"
4
+ spec.version = "0.7.0"
5
5
 
6
6
  # Project description
7
7
  spec.name = "pushyd"
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_runtime_dependency "json"
34
34
  spec.add_runtime_dependency "bunny", "~> 2.3"
35
35
  spec.add_runtime_dependency "rest-client", "~> 1.8"
36
+ spec.add_runtime_dependency "api-auth"
36
37
  spec.add_runtime_dependency "terminal-table"
37
38
  spec.add_runtime_dependency "newrelic_rpm"
38
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pushyd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno MEDICI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-27 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '1.8'
153
+ - !ruby/object:Gem::Dependency
154
+ name: api-auth
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: terminal-table
155
169
  requirement: !ruby/object:Gem::Requirement