capistrano-ops 0.2.11 → 0.2.13

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: 2744c35793b74ff96ed5933892458ed3bbb86d33b93b4a9aab119996ab099048
4
- data.tar.gz: 8245cc91a46097474588d94f4b935dc0f2c83090bf1818c7da4e6f37662bfaa0
3
+ metadata.gz: cec4486500e5ddac70640e6f3d074b51b361d649079e98f9ef6d337712c3de23
4
+ data.tar.gz: ac8df454d33822890a673e800db7492bc7d4d2a7f54c0fe91378648fa163a2f8
5
5
  SHA512:
6
- metadata.gz: ddc408fdde37cbc926b36ce1fd3dce989a2aa6c4a73e61a700af581c02b3432274b49837784d182fa1f07ea48cb1ca253e8c6e9647d5aadbe6313d8c319bb57f
7
- data.tar.gz: 14c1223afe87285ced3ce3790ef73b3b515a2912dd62cfd15da8e08ea2bebee3945c0773ba2af423bfc6753756a3dbd3f7d22acebcdf3de5117c6051c0934615
6
+ metadata.gz: c97bd7b0f7b0f9968b30d81a8807da57d690a4700e6674596f64b970727e4d99606af7ac8e192691fae63a0e5cf67ee54936bda0d1d06d935c37e068b3afdf41
7
+ data.tar.gz: aac3a0d13aa1c8f78ab77f5943b2ee5944c561829831c117afd8dc1cfd0e0e638406c9bcda52f92c1cc53b1f8eeef455e9d62e1929ffe984e3408812a22de4eb
data/README.md CHANGED
@@ -96,6 +96,9 @@ require 'capistrano/ops'
96
96
  | `cap <environment> logs:rails` | display server log live |
97
97
  | `cap <environment> whenever:show_crontab` | display server app crontab generated with whenever |
98
98
  | `cap <environment> invoke:rake TASK=<your:task>` | invoke rake task on server |
99
+ | `cap <environment> logrotate:enable` | enable logrotate for logfiles on server |
100
+ | `cap <environment> logrotate:disable` | disable logrotate for logfiles on server |
101
+ | `cap <environment> logrotate:check` | show logrotate status for logfiles on server |
99
102
  | `rake pg:dump` | creates postgres database backup |
100
103
  | `rake pg:remove_old_dumps` | remove old postgres backups |
101
104
  | `rake storage:backup` | creates backup of storage |
@@ -232,6 +235,63 @@ S3_BACKUP_SECRET: '<your-s3-secret>'
232
235
  S3_BACKUP_ENDPOINT: '<your-s3-endpoint>' # optional, used for other S3 compatible services
233
236
  ```
234
237
 
238
+ ## Logrotate
239
+
240
+ Logrotate is a utility designed for administrators who manage servers producing a high volume of log files to help them save some disk space as well as to avoid a potential risk making a system unresponsive due to the lack of disk space.
241
+
242
+ The capistrano-ops gem provides a set of tasks to manage logrotate on your server:
243
+
244
+ - `cap <environment> logrotate:enable` - This task enables logrotate for logfiles on the server.
245
+ - `cap <environment> logrotate:disable` - This task disables logrotate for logfiles on the server.
246
+ - `cap <environment> logrotate:check` - This task shows the logrotate status for logfiles on the server.
247
+
248
+ ### Configuration
249
+
250
+ You can optionally specify the logrotate configuration file path (Defaults to `/etc/logrotate.conf`):
251
+
252
+ ```ruby
253
+ # Defaults to '/etc/logrotate.conf'
254
+ set :logrotate_path, '/path/to/your/logrotate.conf'
255
+ ```
256
+
257
+ ### Usage
258
+
259
+ To use logrotate, you need to have it installed on your server. If it's not installed, you can install it using the package manager of your system. For example, on Ubuntu, you can install it using apt:
260
+
261
+ ```bash
262
+ sudo apt-get install logrotate
263
+ ```
264
+
265
+ Once logrotate is installed, you can use the capistrano-ops tasks to manage it.
266
+
267
+ ## Wkhtmltopdf Setup
268
+
269
+ This script is used to setup `wkhtmltopdf-binary` in your deployment environment. It is designed to work with Capistrano.
270
+
271
+ The main task `setup` is hooked to run after the `deploy:symlink:release` task.
272
+ It performs the following operations:
273
+
274
+ - unzip the necessary binary file
275
+
276
+ - set the binary file permissions
277
+
278
+ The script assumes, that you have a intializer file for `wicked_pdf` gem, which sets the path to the binary file.
279
+ for example:
280
+
281
+ ```ruby
282
+ # config/initializers/wicked_pdf.rb
283
+ WickedPdf.config = {
284
+ exe_path: "#{Bundler.bundle_path}/gems/wkhtmltopdf-binary-0.12.6.6/bin/wkhtmltopdf_ubuntu_18.04_amd64",
285
+ }
286
+ ```
287
+
288
+ To use this script, include it in your Capistrano tasks and it will automatically run during deployment.
289
+
290
+ ```ruby
291
+ # Capfile
292
+ require 'capistrano/ops/wkhtmltopdf'
293
+ ```
294
+
235
295
  ## Contributing
236
296
 
237
297
  1. Fork it ( https://github.com/zauberware/capistrano-ops/fork )
@@ -247,3 +307,7 @@ The gem is available as open source under the terms of the [MIT License](http://
247
307
  ```
248
308
 
249
309
  ```
310
+
311
+ ```
312
+
313
+ ```
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :wkhtmltopdf do
4
+ after 'deploy:symlink:release', 'wkhtmltopdf:setup'
5
+
6
+ desc 'unzip wkhtmltopdf if necessary'
7
+ task :setup do
8
+ on roles(:app) do
9
+ within release_path do
10
+ binary_path, version = binary_path_and_version
11
+ info("setup wkhtmltopdf version #{version}")
12
+ check_file_and_permissions(binary_path, version)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ def binary_path_and_version
18
+ # get the binary path of wkhtmltopdf-binary
19
+ gem_path = capture(:bundle, 'show', 'wkhtmltopdf-binary').strip
20
+ binary_path = "#{gem_path}/bin"
21
+
22
+ # get the use wkhtmltopdf_ubuntu version from the wicked_pdf initializer
23
+ version = capture(:cat, 'config/initializers/wicked_pdf.rb').scan(/wkhtmltopdf_ubuntu_(\d+\.\d+)_amd64/).flatten.first
24
+
25
+ [binary_path, version]
26
+ end
27
+
28
+ def check_file_and_permissions(binary_path, version)
29
+ binary_file = "#{binary_path}/wkhtmltopdf_ubuntu_#{version}_amd64"
30
+
31
+ if test("[ -f #{binary_file} ]")
32
+ info('wkhtmltopdf binary already extracted')
33
+
34
+ if test("[ $(stat -c '%a' #{binary_file}) = '777' ]")
35
+ info('wkhtmltopdf binary has already the right permissions')
36
+ else
37
+ info('adding right permissions to wkhtmltopdf binary')
38
+ execute("chmod 777 #{binary_file}")
39
+ end
40
+ else
41
+ info('extracting wkhtmltopdf binary')
42
+ # extract the binary but keep the gzip file
43
+ execute("cd #{binary_path} && gzip -dk wkhtmltopdf_ubuntu_#{version}_amd64.gz")
44
+ # add execute permission to the binary
45
+ execute("chmod 777 #{binary_file}")
46
+ end
47
+ info('wkhtmltopdf setup finished')
48
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logrotate_helper'
4
+
5
+ namespace :logrotate do
6
+ include LogrotateHelper
7
+
8
+ desc 'show logrotate state and config'
9
+ task :check do
10
+ on roles(:app) do
11
+ within release_path do
12
+ set_config
13
+ if logrotate_enabled
14
+ puts "logrotate running with config\n========================="
15
+ puts capture "cat #{@config_file_path}"
16
+ else
17
+ puts 'logrotate disabled'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logrotate_helper'
4
+
5
+ namespace :logrotate do
6
+ include LogrotateHelper
7
+
8
+ desc 'disable logrotate'
9
+ task :disable do
10
+ on roles(:app) do
11
+ within release_path do
12
+ set_config
13
+ if logrotate_disabled
14
+ puts 'logrotate already disabled'
15
+ else
16
+ whenever 'clear' if logrotate_enabled
17
+ delete_files
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logrotate_helper'
4
+
5
+ namespace :logrotate do
6
+ include LogrotateHelper
7
+ desc 'Enable logrotate for the application'
8
+ task :enable do
9
+ on roles(:app) do
10
+ within release_path do
11
+ set_config
12
+ if logrotate_enabled
13
+ puts "logrotate already enabled:\n========================="
14
+ puts capture "cat #{@config_file_path}"
15
+ else
16
+ make_basepath
17
+ upload! StringIO.new(@config_template), @config_file_path
18
+ upload! StringIO.new(@schedule_template), @schedule_file_path
19
+ whenever 'update' if logrotate_enabled
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LogrotateHelper
4
+ # rubocop:disable Naming/MemoizedInstanceVariableName
5
+ def set_config
6
+ info ''
7
+ @shared_path ||= shared_path
8
+ @log_file_path ||= fetch(:log_file_path) || "#{@shared_path}/log"
9
+ @template_config_filename ||= fetch(:logrotate_template_config_file) || 'templates/logrotate.conf.erb'
10
+ @template_schedule_filename ||= fetch(:logrotate_template_schedule_file) || 'templates/schedule.rb.erb'
11
+ @basepath ||= fetch(:logrotate_basepath) || "#{shared_path}/logrotate"
12
+ @config_filename ||= File.basename(@template_config_filename, '.erb')
13
+ @schedule_filename ||= File.basename(@template_schedule_filename, '.erb')
14
+ @config_file_path ||= "#{@basepath}/#{@config_filename}"
15
+ @schedule_file_path ||= "#{@basepath}/#{@schedule_filename}"
16
+ @config_template ||= config_template
17
+ @schedule_template ||= schedule_template
18
+ end
19
+ # rubocop:enable Naming/MemoizedInstanceVariableName
20
+
21
+ def config
22
+ {
23
+ template_config_file: fetch(:logrotate_template_config_file) || 'templates/logrotate.conf.erb',
24
+ template_schedule_file: fetch(:logrotate_template_schedule_file) || 'templates/schedule.rb.erb'
25
+ }
26
+ end
27
+
28
+ def make_basepath
29
+ puts capture "mkdir -pv #{@basepath}"
30
+ end
31
+
32
+ def delete_files
33
+ puts capture "rm -rfv #{@basepath}"
34
+ end
35
+
36
+ def config_template
37
+ return nil unless @template_config_filename && File.exist?(File.expand_path(@template_config_filename, __dir__))
38
+
39
+ ERB.new(File.read(File.expand_path(@template_config_filename, __dir__))).result(binding)
40
+ end
41
+
42
+ def schedule_template
43
+ return nil unless @template_schedule_filename && File.exist?(File.expand_path(@template_schedule_filename, __dir__))
44
+
45
+ ERB.new(File.read(File.expand_path(@template_schedule_filename, __dir__))).result(binding)
46
+ end
47
+
48
+ def logrotate_enabled
49
+ test("[ -f #{@config_file_path} ]") && test("[ -f #{@schedule_file_path} ]")
50
+ end
51
+
52
+ def logrotate_disabled
53
+ !(test("[ -f #{@config_file_path} ]") && test("[ -f #{@schedule_file_path} ]"))
54
+ end
55
+
56
+ def whenever(type)
57
+ case type
58
+ when 'clear'
59
+ puts capture :bundle, :exec, :whenever, '--clear-crontab',
60
+ "-f #{@schedule_file_path} #{fetch(:whenever_identifier)}_logrotate"
61
+ when 'update'
62
+ puts capture :bundle, :exec, :whenever, '--update-crontab', "-f #{@schedule_file_path}",
63
+ "-i #{fetch(:whenever_identifier)}_logrotate"
64
+ else
65
+ puts 'type not found'
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ <%= @log_file_path %>/*.log {
2
+ daily
3
+ dateext
4
+ missingok
5
+ rotate 52
6
+ compress
7
+ delaycompress
8
+ notifempty
9
+ copytruncate
10
+ }
@@ -0,0 +1,8 @@
1
+ env :PATH, ENV['PATH']
2
+ set :output, -> { '2>&1 | logger -t logrotate' }
3
+
4
+ job_type :logrotate, '/usr/sbin/logrotate -s <%= @shared_path %>/logrotate/status <%= @shared_path %>/logrotate/logrotate.conf :output'
5
+
6
+ every "0 0 * * *" do
7
+ logrotate "logrotate"
8
+ end
@@ -6,10 +6,11 @@ if defined?(Capistrano::VERSION) && Gem::Version.new(Capistrano::VERSION).releas
6
6
  load File.expand_path('capistrano/v3/tasks/whenever.rake', __dir__)
7
7
  load File.expand_path('capistrano/v3/tasks/backup.rake', __dir__)
8
8
  load File.expand_path('capistrano/v3/tasks/figaro_yml.rake', __dir__)
9
- load File.expand_path('capistrano/v3/tasks/logs.rake', __dir__)
10
9
  load File.expand_path('capistrano/v3/tasks/invoke.rake', __dir__)
11
10
  path = File.expand_path(__dir__)
12
11
  Dir.glob("#{path}/capistrano/v3/tasks/backup/**/*.rake").each { |f| load f }
12
+ Dir.glob("#{path}/capistrano/v3/tasks/logrotate/**/*.rake").each { |f| load f }
13
+ Dir.glob("#{path}/capistrano/v3/tasks/logs/**/*.rake").each { |f| load f }
13
14
  else
14
15
  puts 'Capistrano 3 is required to use this gem'
15
16
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Capistrano
4
4
  module Ops
5
- VERSION = '0.2.11'
5
+ VERSION = '0.2.13'
6
6
  end
7
7
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ load File.expand_path('capistrano/tasks/wkhtmltopdf.rake', __dir__)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-ops
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Crusius
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-15 00:00:00.000000000 Z
11
+ date: 2024-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3
@@ -120,6 +120,7 @@ files:
120
120
  - lib/capistrano/ops/backup/s3.rb
121
121
  - lib/capistrano/ops/backup/s3_helper.rb
122
122
  - lib/capistrano/ops/capistrano.rb
123
+ - lib/capistrano/ops/capistrano/tasks/wkhtmltopdf.rake
123
124
  - lib/capistrano/ops/capistrano/v3/tasks/backup.rake
124
125
  - lib/capistrano/ops/capistrano/v3/tasks/backup/backup_helper.rb
125
126
  - lib/capistrano/ops/capistrano/v3/tasks/backup/database/create.rake
@@ -128,7 +129,13 @@ files:
128
129
  - lib/capistrano/ops/capistrano/v3/tasks/backup/storage/pull.rake
129
130
  - lib/capistrano/ops/capistrano/v3/tasks/figaro_yml.rake
130
131
  - lib/capistrano/ops/capistrano/v3/tasks/invoke.rake
131
- - lib/capistrano/ops/capistrano/v3/tasks/logs.rake
132
+ - lib/capistrano/ops/capistrano/v3/tasks/logrotate/check.rake
133
+ - lib/capistrano/ops/capistrano/v3/tasks/logrotate/disable.rake
134
+ - lib/capistrano/ops/capistrano/v3/tasks/logrotate/enable.rake
135
+ - lib/capistrano/ops/capistrano/v3/tasks/logrotate/logrotate_helper.rb
136
+ - lib/capistrano/ops/capistrano/v3/tasks/logrotate/templates/logrotate.conf.erb
137
+ - lib/capistrano/ops/capistrano/v3/tasks/logrotate/templates/schedule.rb.erb
138
+ - lib/capistrano/ops/capistrano/v3/tasks/logs/rails.rake
132
139
  - lib/capistrano/ops/capistrano/v3/tasks/whenever.rake
133
140
  - lib/capistrano/ops/notification.rb
134
141
  - lib/capistrano/ops/notification/api.rb
@@ -141,6 +148,7 @@ files:
141
148
  - lib/capistrano/ops/tasks/storage/backup.rake
142
149
  - lib/capistrano/ops/tasks/storage/remove_old_backups.rake
143
150
  - lib/capistrano/ops/version.rb
151
+ - lib/capistrano/ops/wkhtmltopdf.rb
144
152
  homepage: https://github.com/zauberware/capistrano-ops
145
153
  licenses:
146
154
  - MIT