capun 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2acb35a4d08185c4c6dd27d8eab42b31bce4135d
4
- data.tar.gz: 34d036ad99afff7c5d84a63e8bfcea1f4b36b488
3
+ metadata.gz: c5a8c5d54204799333f6df9f7e2e0a3a3db7976c
4
+ data.tar.gz: 96a37e3ffce577045a497416eccb92fa135527c1
5
5
  SHA512:
6
- metadata.gz: 7d383c87c33c3bccba172ab89edc3234f466a0ad317907ff33d05c5a9fd85d41307d331e598fc2262c1586c5759e6e22a124f55dd3db230b4cd9046d058f96df
7
- data.tar.gz: 4d9c3f29651201dff7f6dc1fd8134dbc6bc7a03f34d0846d070a6f01c33296a47d96c0ce175802155ffc5f1be53633f9680a0e09b69e1d5347c011f991607be8
6
+ metadata.gz: ec5ce547cd29cac3e77ab276adf81aecfb7a51d61b5861dc7ed9bfc25a144e9fe66699024c2823b1498151917783f157f3eb7f68f028c013d5b20e724a791ba2
7
+ data.tar.gz: 29d78668ae200a0c495cfa41806c1d6e0d247b1b8adfe27c04fea684fe0398f6f05840d34af3976f59767e8d23cc7125e8377076d2a179d0be1d4705659766e0
@@ -17,8 +17,10 @@ set :unicorn_config_path, -> { "#{shared_path}/config/unicorn.config.rb" }
17
17
 
18
18
  set :uploads, []
19
19
  set :std_uploads, [
20
- #basic_authenticatable.rb
20
+ #figaro
21
21
  {what: "config/application.yml", where: '#{shared_path}/config/application.yml'},
22
+ #logstash configs
23
+ {what: "config/deploy/logstash.config.erb", where: '#{shared_path}/config/logstash.config'},
22
24
  #basic_authenticatable.rb
23
25
  {what: "config/deploy/basic_authenticatable.rb.erb", where: '#{release_path}/app/controllers/concerns/basic_authenticatable.rb'},
24
26
  #nginx.conf
@@ -34,6 +36,7 @@ set :std_uploads, [
34
36
  set :symlinks, []
35
37
  set :std_symlinks, [
36
38
  {what: "nginx.conf", where: '/etc/nginx/sites-enabled/#{fetch(:application)}'},
39
+ {what: "logstash.config", where: '/etc/logstash/conf.d/#{fetch(:application)}'},
37
40
  {what: "database.yml", where: '#{release_path}/config/database.yml'},
38
41
  {what: "application.yml", where: '#{release_path}/config/application.yml'}
39
42
  ]
@@ -92,16 +95,26 @@ namespace :deploy do
92
95
  end
93
96
 
94
97
  desc 'Restart nginx'
95
- task :restart do
98
+ task :restart_nginx do
96
99
  on roles(:app) do
97
100
  execute :sudo, "service nginx restart"
98
101
  end
99
102
  end
100
103
 
104
+ desc 'Restart logstash'
105
+ task :restart_logstash do
106
+ if fetch(:addELK)
107
+ on roles(:app) do
108
+ execute :sudo, "service logstash restart"
109
+ end
110
+ end
111
+ end
112
+
101
113
  end
102
114
 
103
115
  before "deploy:updating", "deploy:make_dirs"
104
116
  after "deploy:symlink:linked_dirs", "deploy:upload"
105
117
  after "deploy:symlink:linked_dirs", "deploy:add_symlinks"
106
- after "deploy:publishing", "deploy:restart"
118
+ after "deploy:publishing", "deploy:restart_nginx"
119
+ after "deploy:publishing", "deploy:restart_logstash"
107
120
  after "deploy:publishing", "unicorn:restart"
@@ -1,3 +1,3 @@
1
1
  module Capun
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module Capun
@@ -14,6 +15,7 @@ module Capun
14
15
  @username = ask("Basic authentication username [ex.: mike]:")
15
16
  @password = ask("Basic authentication password [ex.: secret]:")
16
17
  end
18
+ @addELK = ask("Would you like to add ELK-compatible logging? [Y/n]").capitalize == 'Y'
17
19
  end
18
20
 
19
21
  def add_stage
@@ -40,12 +42,37 @@ module Capun
40
42
  end
41
43
 
42
44
  def add_secret
43
- secret_token_does_not_exist = Thor::CoreExt::HashWithIndifferentAccess.new(::YAML::load_file("config/secrets.yml"))[singular_name].nil?
44
- if secret_token_does_not_exist
45
- append_to_file "config/secrets.yml", "\n#{singular_name}:\n secret_key_base: #{SecureRandom.hex(64)}"
45
+ if File.exists?("config/secrets.yml")
46
+ secret_token_does_not_exist = Thor::CoreExt::HashWithIndifferentAccess.new(::YAML::load_file("config/secrets.yml"))[singular_name].nil?
47
+ if secret_token_does_not_exist
48
+ append_to_file "config/secrets.yml", "\n#{singular_name}:\n secret_key_base: #{SecureRandom.hex(64)}"
49
+ end
46
50
  end
47
51
  end
48
-
52
+
53
+ def add_ELK
54
+
55
+ if @addELK
56
+ #coping logstash config
57
+ copy_file "logstash.config.erb", "config/deploy/logstash.config.erb"
58
+ #installing required gems
59
+ gem "lograge"
60
+ gem "logstash-event"
61
+ inside Rails.root do
62
+ run "bundle install --quiet"
63
+ end
64
+ #adding lograge configs to relevant environment initializer
65
+ inject_into_file "config/environments/#{singular_name}.rb", File.read(File.expand_path("../templates/lograge_env_config.excerpt", __FILE__)), :before => /^end/
66
+ #adding append_info_to_payload method override to pipe required information to lograge log
67
+ this_line = "class ApplicationController < ActionController::Base\n"
68
+ inject_into_file "app/controllers/application_controller.rb", File.read(File.expand_path("../templates/append_info.excerpt", __FILE__)), :after => this_line
69
+ #coping logstash config
70
+ copy_file "lograge_initializer.rb", "config/initializers/lograge_initializer.rb"
71
+ #adding flag to run 'service logstash restart' during deploy
72
+ append_to_file "config/deploy/#{singular_name}.rb", "\nset :addELK, true"
73
+ end
74
+ end
75
+
49
76
  end
50
77
  end
51
78
  end
@@ -0,0 +1,11 @@
1
+
2
+ def append_info_to_payload(payload)
3
+ super
4
+ #adding ip-address for payload
5
+ payload[:ip] = request.headers['HTTP_X_REAL_IP'] || request.remote_ip
6
+ #adding agent string for payload
7
+ payload[:agentstring] = request.headers['HTTP_USER_AGENT'] || request.user_agent
8
+ #adding user
9
+ payload[:user] = current_user unless defined? (current_user) || current_user.nil?
10
+ end
11
+
@@ -0,0 +1,26 @@
1
+
2
+ #enable lograge for logging
3
+ config.lograge.enabled = true
4
+
5
+ #keep orginal rails log as well
6
+ config.lograge.keep_original_rails_log = true
7
+ config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"
8
+
9
+ #configure lograge options
10
+ config.lograge.custom_options = ->(event) {
11
+ opts = {
12
+ params: event.payload[:params],
13
+ time: %Q('#{event.time}'),
14
+ remote_ip: event.payload[:ip],
15
+ agent_string: event.payload[:agentstring]
16
+ }
17
+ if event.payload[:exception]
18
+ app_trace = Array(event.payload[:stacktrace]).reject { |el| !el.include?('alfaexperience/app') }
19
+ app_trace.map! { |item| item.slice(item.index("app")..-1) }
20
+ opts[:stacktrace] = app_trace.join('\n')
21
+ end
22
+ opts
23
+ }
24
+
25
+ #use logstash output as log formatter
26
+ config.lograge.formatter = Lograge::Formatters::Logstash.new
@@ -0,0 +1,17 @@
1
+ require "active_support/notifications/instrumenter"
2
+
3
+ class ActiveSupport::Notifications::Instrumenter
4
+
5
+ alias_method :instrument_original, :instrument
6
+
7
+ def instrument(name, payload = {})
8
+ instrument_original(name, payload) do
9
+ begin
10
+ yield
11
+ rescue Exception => e
12
+ payload[:stacktrace] = e.backtrace
13
+ raise
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ input {
2
+ file {
3
+ path => "/home/<%= fetch(:user) %>/apps/<%= fetch(:application) %>/shared/log/lograge_*"
4
+ type => "json"
5
+ }
6
+ }
7
+ filter{
8
+ json{
9
+ source => "message"
10
+ }
11
+ useragent {
12
+ source => "agent_string"
13
+ }
14
+ geoip {
15
+ source => "remote_ip"
16
+ }
17
+ }
18
+ output {
19
+ elasticsearch {
20
+ index => "<%= fetch(:application) %>"
21
+ }
22
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Zamylin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,9 +128,13 @@ files:
128
128
  - lib/generators/capun/install_generator.rb
129
129
  - lib/generators/capun/stage_generator.rb
130
130
  - lib/generators/capun/templates/Capfile
131
+ - lib/generators/capun/templates/append_info.excerpt
131
132
  - lib/generators/capun/templates/basic_authenticatable.rb.erb
132
133
  - lib/generators/capun/templates/database.yml.erb
133
134
  - lib/generators/capun/templates/deploy.rb.erb
135
+ - lib/generators/capun/templates/lograge_env_config.excerpt
136
+ - lib/generators/capun/templates/lograge_initializer.rb
137
+ - lib/generators/capun/templates/logstash.config.erb
134
138
  - lib/generators/capun/templates/nginx.conf.erb
135
139
  - lib/generators/capun/templates/stage.rb.erb
136
140
  - lib/generators/capun/templates/unicorn.config.rb.erb