capistrano-cable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b6b994fb2900c73d5d4afe2a12e7a7432e2a53611fd37ad2ac4534031a3bbd65
4
+ data.tar.gz: bc7ee8750a36a2d613eaf7c9e8379da3d4d21cf5d69dffb40c5347eb0e8a8b07
5
+ SHA512:
6
+ metadata.gz: 78ddf453aeeadee09e1bfe8418f7435fbc8565fdfbb9131f47b4e8d8a1d06f6f57f4245ee0cf0d8b77450aab013a8ab9f9b46e82095572843e961bfe701dc7d8
7
+ data.tar.gz: c094d0a7acda838ad3c219a29deee9226765213099a41a03d0176a98a75a0782ce6e57ae0f902904f4d71998abea30afea634a16d7e94a9a7645180790435f5b
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-07-05
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at brice@codeur.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Codeur SAS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Capistrano::Cable
2
+
3
+ **Capistrano::Cable** helps to deploy standalone ActionCable server with Puma over `systemd`.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add capistrano-cable
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install capistrano-cable
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ # Capfile
19
+
20
+ require 'capistrano/cable'
21
+ install_plugin Capistrano::Cable::Systemd
22
+ ```
23
+
24
+ To prevent loading the hooks of the plugin, add false to the load_hooks param.
25
+ ```ruby
26
+ # Capfile
27
+
28
+ install_plugin Capistrano::Cable, load_hooks: false # Default cable tasks without hooks
29
+ ```
30
+
31
+ To make it work with rvm, rbenv and chruby, install the plugin after corresponding library inclusion.
32
+ ```ruby
33
+ # Capfile
34
+
35
+ require 'capistrano/rbenv'
36
+ require 'capistrano/cable'
37
+ install_plugin Capistrano::Cable
38
+ ```
39
+
40
+ ### Config
41
+
42
+ ```ruby
43
+ # config/deploy.rb or config/deploy/<stage>.rb
44
+ set :cable_role, :web
45
+ set :cable_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
46
+ set :cable_access_log, -> { File.join(shared_path, 'log', 'cable.log') }
47
+ set :cable_error_log, -> { File.join(shared_path, 'log', 'cable.log') }
48
+
49
+ set :cable_systemctl_bin, -> { fetch(:systemctl_bin, '/bin/systemctl') }
50
+ set :cable_service_unit_name, -> { "#{fetch(:application)}_cable_#{fetch(:stage)}" }
51
+ set :cable_enable_socket_service, false
52
+ # set :cable_bind, ... # Example: ->{ "unix:/path/to/cable.sock" }
53
+
54
+ set :cable_service_unit_env_files, -> { fetch(:service_unit_env_files, []) }
55
+ set :cable_service_unit_env_vars, -> { fetch(:service_unit_env_vars, []) }
56
+
57
+ set :cable_systemctl_user, -> { fetch(:systemctl_user, :user) }
58
+ set :cable_enable_lingering, -> { fetch(:cable_systemctl_user) != :system }
59
+ set :cable_lingering_user, -> { fetch(:lingering_user, fetch(:user)) }
60
+
61
+ set :cable_service_templates_path, fetch(:service_templates_path, 'config/deploy/templates')
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codeur/capistrano-cable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/codeur/capistrano-cable/blob/main/CODE_OF_CONDUCT.md).
73
+
74
+ Largely inspired from [capistrano-puma](https://github.com/seuros/capistrano-puma) gem.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
79
+
80
+ ## Code of Conduct
81
+
82
+ Everyone interacting in the Capistrano::Cable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/codeur/capistrano-cable/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,35 @@
1
+ module Capistrano
2
+ module Cable
3
+ class Bind < Struct.new(:full_address, :kind, :address)
4
+ def unix?
5
+ kind == :unix
6
+ end
7
+
8
+ def ssl?
9
+ kind == :ssl
10
+ end
11
+
12
+ def tcp
13
+ kind == :tcp || ssl?
14
+ end
15
+
16
+ def local
17
+ if unix?
18
+ self
19
+ else
20
+ self.class.new(
21
+ localize_address(full_address),
22
+ kind,
23
+ localize_address(address)
24
+ )
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def localize_address(address)
31
+ address.gsub(/0\.0\.0\.0(.+)/, "127.0.0.1\\1")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,165 @@
1
+ require "capistrano/plugin"
2
+ require_relative "bind"
3
+
4
+ module Capistrano
5
+ module Cable
6
+ class Systemd < Capistrano::Plugin
7
+ def register_hooks
8
+ after "deploy:finished", "cable:smart_restart"
9
+ end
10
+
11
+ def define_tasks
12
+ eval_rakefile File.expand_path("../../tasks/systemd.rake", __FILE__)
13
+ end
14
+
15
+ def set_defaults
16
+ set_if_empty :cable_role, :web
17
+ set_if_empty :cable_port, 29292
18
+ set_if_empty :cable_dir, -> { File.join(release_path, "cable") }
19
+ set_if_empty :cable_pidfile, -> { File.join(shared_path, "tmp", "pids", "cable.pid") }
20
+ set_if_empty :cable_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
21
+ set_if_empty :cable_access_log, -> { File.join(shared_path, "log", "cable.access.log") }
22
+ set_if_empty :cable_error_log, -> { File.join(shared_path, "log", "cable.error.log") }
23
+
24
+ # Chruby, Rbenv and RVM integration
25
+ append :chruby_map_bins, "puma", "pumactl" if fetch(:chruby_map_bins)
26
+ append :rbenv_map_bins, "puma", "pumactl" if fetch(:rbenv_map_bins)
27
+ append :rvm_map_bins, "puma", "pumactl" if fetch(:rvm_map_bins)
28
+
29
+ # Bundler integration
30
+ append :bundle_bins, "puma", "pumactl"
31
+
32
+ set_if_empty :cable_systemctl_bin, -> { fetch(:systemctl_bin, "/bin/systemctl") }
33
+ set_if_empty :cable_service_unit_name, -> { "#{fetch(:application)}_cable_#{fetch(:stage)}" }
34
+ set_if_empty :cable_enable_socket_service, false
35
+ set_if_empty :cable_socket_unit_name, -> { "#{fetch(:application)}_cable_#{fetch(:stage)}.socket" }
36
+ # set_if_empty :cable_bind, -> { "unix:/tmp/#{fetch(:app_domain)}.sock" }
37
+
38
+ set_if_empty :cable_service_unit_env_files, -> { fetch(:service_unit_env_files, []) }
39
+ set_if_empty :cable_service_unit_env_vars, -> { fetch(:service_unit_env_vars, []) }
40
+
41
+ set_if_empty :cable_systemctl_user, -> { fetch(:systemctl_user, :user) }
42
+ set_if_empty :cable_enable_lingering, -> { fetch(:cable_systemctl_user) != :system }
43
+ set_if_empty :cable_lingering_user, -> { fetch(:lingering_user, fetch(:user)) }
44
+
45
+ set_if_empty :cable_service_templates_path, fetch(:service_templates_path, "config/deploy/templates")
46
+ end
47
+
48
+ def expanded_bundle_command
49
+ backend.capture(:echo, SSHKit.config.command_map[:bundle]).strip
50
+ end
51
+
52
+ def fetch_systemd_unit_path
53
+ if fetch(:cable_systemctl_user) == :system
54
+ "/etc/systemd/system/"
55
+ else
56
+ home_dir = backend.capture :pwd
57
+ File.join(home_dir, ".config", "systemd", "user")
58
+ end
59
+ end
60
+
61
+ def systemd_command(*args)
62
+ command = [fetch(:cable_systemctl_bin)]
63
+
64
+ unless fetch(:cable_systemctl_user) == :system
65
+ command << "--user"
66
+ end
67
+
68
+ command + args
69
+ end
70
+
71
+ def sudo_if_needed(*command)
72
+ if fetch(:cable_systemctl_user) == :system
73
+ backend.sudo command.map(&:to_s).join(" ")
74
+ else
75
+ backend.execute(*command)
76
+ end
77
+ end
78
+
79
+ def execute_systemd(*args)
80
+ sudo_if_needed(*systemd_command(*args))
81
+ end
82
+
83
+ # From Common
84
+
85
+ def cable_switch_user(role, &block)
86
+ user = cable_user(role)
87
+ if user == role.user
88
+ block.call
89
+ else
90
+ backend.as user do
91
+ block.call
92
+ end
93
+ end
94
+ end
95
+
96
+ def cable_user(role)
97
+ properties = role.properties
98
+ properties.fetch(:cable_user) || # local property for cable only
99
+ fetch(:cable_user) ||
100
+ properties.fetch(:run_as) || # global property across multiple capistrano gems
101
+ role.user
102
+ end
103
+
104
+ def cable_bind
105
+ Array(fetch(:cable_bind)).collect do |bind|
106
+ "bind '#{bind}'"
107
+ end.join("\n")
108
+ end
109
+
110
+ def service_unit_type
111
+ ## Jruby don't support notify
112
+ return "simple" if RUBY_ENGINE == "jruby"
113
+ fetch(:cable_service_unit_type,
114
+ ## Check if sd_notify is available in the bundle
115
+ Gem::Specification.find_all_by_name("sd_notify").any? ? "notify" : "simple")
116
+ end
117
+
118
+ def puma_options
119
+ options = []
120
+ options << "--no-config"
121
+ # options << "--dir #{fetch(:cable_dir)}" if fetch(:cable_dir) # or change WorkingDirectory in cable.service?
122
+ if fetch(:cable_ssl_certificate) && fetch(:cable_ssl_certificate_key)
123
+ options << "--bind 'ssl://0.0.0.0:#{fetch(:cable_port)}?key=#{fetch(:cable_ssl_certificate_key)}&cert=#{fetch(:cable_ssl_certificate)}'"
124
+ # Can use: &verify_mode=none&ca=...
125
+ end
126
+ options << "--environment #{fetch(:cable_env)}"
127
+ # options << "--port #{fetch(:cable_port)}"
128
+ options << "--pidfile #{fetch(:cable_pidfile)}" if fetch(:cable_pidfile)
129
+ options << "--threads #{fetch(:cable_threads)}" if fetch(:cable_threads)
130
+ options << "--workers #{fetch(:cable_workers)}" if fetch(:cable_workers)
131
+ options.join(" ")
132
+ end
133
+
134
+ def compiled_template_cable(from, role)
135
+ @role = role
136
+ file = [
137
+ "lib/capistrano/templates/#{from}-#{role.hostname}-#{fetch(:stage)}.rb",
138
+ "lib/capistrano/templates/#{from}-#{role.hostname}.rb",
139
+ "lib/capistrano/templates/#{from}-#{fetch(:stage)}.rb",
140
+ "lib/capistrano/templates/#{from}.rb.erb",
141
+ "lib/capistrano/templates/#{from}.rb",
142
+ "lib/capistrano/templates/#{from}.erb",
143
+ "config/deploy/templates/#{from}.rb.erb",
144
+ "config/deploy/templates/#{from}.rb",
145
+ "config/deploy/templates/#{from}.erb",
146
+ File.expand_path("../../templates/#{from}.erb", __FILE__),
147
+ File.expand_path("../../templates/#{from}.rb.erb", __FILE__)
148
+ ].detect { |path| File.file?(path) }
149
+ erb = File.read(file)
150
+ StringIO.new(ERB.new(erb, trim_mode: "-").result(binding))
151
+ end
152
+
153
+ def upload_template_cable(from, to, role)
154
+ backend.upload! compiled_template_cable(from, role), to
155
+ end
156
+
157
+ def cable_binds
158
+ Array(fetch(:cable_bind)).map do |m|
159
+ etype, address = /(tcp|unix|ssl):\/{1,2}(.+)/.match(m).captures
160
+ Bind.new(m, etype.to_sym, address)
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capistrano
4
+ module Cable
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "capistrano/cable/systemd"
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ git_plugin = self
4
+
5
+ namespace :cable do
6
+ desc "Install Cable systemd service"
7
+ task :install do
8
+ on roles(fetch(:cable_role)) do |role|
9
+ upload_compiled_template = lambda do |template_name, unit_filename|
10
+ git_plugin.upload_template_cable template_name, "#{fetch(:tmp_dir)}/#{unit_filename}", role
11
+ systemd_path = fetch(:cable_systemd_conf_dir, git_plugin.fetch_systemd_unit_path)
12
+ if fetch(:cable_systemctl_user) == :system
13
+ sudo "mv #{fetch(:tmp_dir)}/#{unit_filename} #{systemd_path}"
14
+ else
15
+ execute :mkdir, "-p", systemd_path
16
+ execute :mv, "#{fetch(:tmp_dir)}/#{unit_filename}", systemd_path.to_s
17
+ end
18
+ end
19
+
20
+ upload_compiled_template.call("cable.service", "#{fetch(:cable_service_unit_name)}.service")
21
+
22
+ if fetch(:cable_enable_socket_service)
23
+ upload_compiled_template.call("cable.socket", fetch(:cable_socket_unit_name))
24
+ end
25
+
26
+ # Reload systemd
27
+ git_plugin.execute_systemd("daemon-reload")
28
+ invoke "cable:enable"
29
+ end
30
+ end
31
+
32
+ desc "Uninstall Cable systemd service"
33
+ task :uninstall do
34
+ invoke "cable:disable"
35
+ on roles(fetch(:cable_role)) do |role|
36
+ systemd_path = fetch(:cable_systemd_conf_dir, git_plugin.fetch_systemd_unit_path)
37
+ if fetch(:cable_systemctl_user) == :system
38
+ sudo "rm -f #{systemd_path}/#{fetch(:cable_service_unit_name)}*"
39
+ else
40
+ execute :rm, "-f", "#{systemd_path}/#{fetch(:cable_service_unit_name)}*"
41
+ end
42
+ git_plugin.execute_systemd("daemon-reload")
43
+ end
44
+ end
45
+
46
+ desc "Enable Cable systemd service"
47
+ task :enable do
48
+ on roles(fetch(:cable_role)) do
49
+ git_plugin.execute_systemd("enable", fetch(:cable_service_unit_name))
50
+ git_plugin.execute_systemd("enable", fetch(:cable_socket_unit_name)) if fetch(:cable_enable_socket_service)
51
+
52
+ if fetch(:cable_systemctl_user) != :system && fetch(:cable_enable_lingering)
53
+ execute :loginctl, "enable-linger", fetch(:cable_lingering_user)
54
+ end
55
+ end
56
+ end
57
+
58
+ desc "Disable Cable systemd service"
59
+ task :disable do
60
+ on roles(fetch(:cable_role)) do
61
+ git_plugin.execute_systemd("disable", fetch(:cable_service_unit_name))
62
+ git_plugin.execute_systemd("disable", fetch(:cable_socket_unit_name)) if fetch(:cable_enable_socket_service)
63
+ end
64
+ end
65
+
66
+ desc "Start Cable service via systemd"
67
+ task :start do
68
+ on roles(fetch(:cable_role)) do
69
+ git_plugin.execute_systemd("start", fetch(:cable_service_unit_name))
70
+ end
71
+ end
72
+
73
+ desc "Stop Cable service via systemd"
74
+ task :stop do
75
+ on roles(fetch(:cable_role)) do
76
+ git_plugin.execute_systemd("stop", fetch(:cable_service_unit_name))
77
+ end
78
+ end
79
+
80
+ desc "Stop Cable socket via systemd"
81
+ task :stop_socket do
82
+ on roles(fetch(:cable_role)) do
83
+ git_plugin.execute_systemd("stop", fetch(:cable_socket_unit_name))
84
+ end
85
+ end
86
+
87
+ desc "Restarts or reloads Cable service via systemd"
88
+ task :smart_restart do
89
+ if fetch(:cable_phased_restart)
90
+ invoke "cable:reload"
91
+ else
92
+ invoke "cable:restart"
93
+ end
94
+ end
95
+
96
+ desc "Restart Cable service via systemd"
97
+ task :restart do
98
+ on roles(fetch(:cable_role)) do
99
+ git_plugin.execute_systemd("restart", fetch(:cable_service_unit_name))
100
+ end
101
+ end
102
+
103
+ desc "Restart Cable socket via systemd"
104
+ task :restart_socket do
105
+ on roles(fetch(:cable_role)) do
106
+ git_plugin.execute_systemd("restart", fetch(:cable_socket_unit_name))
107
+ end
108
+ end
109
+
110
+ desc "Reload Cable service via systemd"
111
+ task :reload do
112
+ on roles(fetch(:cable_role)) do
113
+ service_ok = if fetch(:cable_systemctl_user) == :system
114
+ execute("#{fetch(:cable_systemctl_bin)} status #{fetch(:cable_service_unit_name)} > /dev/null", raise_on_non_zero_exit: false)
115
+ else
116
+ execute("#{fetch(:cable_systemctl_bin)} --user status #{fetch(:cable_service_unit_name)} > /dev/null", raise_on_non_zero_exit: false)
117
+ end
118
+ cmd = "reload"
119
+ unless service_ok
120
+ cmd = "restart"
121
+ end
122
+ if fetch(:cable_systemctl_user) == :system
123
+ sudo "#{fetch(:cable_systemctl_bin)} #{cmd} #{fetch(:cable_service_unit_name)}"
124
+ else
125
+ execute fetch(:cable_systemctl_bin).to_s, "--user", cmd, fetch(:cable_service_unit_name)
126
+ end
127
+ end
128
+ end
129
+
130
+ desc "Get Cable service status via systemd"
131
+ task :status do
132
+ on roles(fetch(:cable_role)) do
133
+ git_plugin.execute_systemd("status", fetch(:cable_service_unit_name))
134
+ git_plugin.execute_systemd("status", fetch(:cable_socket_unit_name)) if fetch(:cable_enable_socket_service)
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,45 @@
1
+ # This file tells systemd how to run Cable as a 24/7 long-running daemon.
2
+ #
3
+ # Customize this file based on your bundler location, app directory, etc.
4
+ # Customize and copy this into /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu).
5
+ # Then run:
6
+ # - systemctl enable <%= fetch(:cable_service_unit_name) %>
7
+ # - systemctl {start,stop,restart} <%= fetch(:cable_service_unit_name) %>
8
+ #
9
+ #
10
+ # Use `journalctl -u <%= fetch(:cable_service_unit_name) %> -rn 100` to view the last 100 lines of log output.
11
+ #
12
+ [Unit]
13
+ Description=Cable HTTP Server for <%= "#{fetch(:application)} (#{fetch(:stage)})" %>
14
+ <%= "Requires=#{fetch(:cable_socket_unit_name)}" if fetch(:cable_enable_socket_service) %>
15
+ After=syslog.target network.target
16
+
17
+ [Service]
18
+ Type=<%= service_unit_type %>
19
+ WatchdogSec=30
20
+ <%="User=#{cable_user(@role)}" if fetch(:cable_systemctl_user) == :system %>
21
+ <%="PIDFile=#{fetch(:cable_pidfile)}" if fetch(:cable_pidfile) %>
22
+ WorkingDirectory=<%= current_path %>
23
+ ExecStart=<%= expanded_bundle_command %> exec puma <%= puma_options %> cable/config.ru
24
+ ExecReload=/bin/kill -USR1 $MAINPID
25
+ Environment='RAILS_ENV=<%= fetch(:stage) %>'
26
+ <% fetch(:default_env).reject{ |k, _| k.to_s == 'path' }.each do |variable, value| -%>
27
+ Environment='<%= variable.to_s.upcase %>=<%= value.to_s %>'
28
+ <% end -%>
29
+ <%- Array(fetch(:cable_service_unit_env_files)).each do |file| %>
30
+ <%="EnvironmentFile=#{file}" -%>
31
+ <% end -%>
32
+ <% Array(fetch(:cable_service_unit_env_vars)).each do |environment_variable| %>
33
+ <%="Environment=\"#{environment_variable}\"" -%>
34
+ <% end -%>
35
+
36
+ # if we crash, restart
37
+ RestartSec=1
38
+ Restart=always
39
+
40
+ <%="StandardOutput=append:#{fetch(:cable_access_log)}" if fetch(:cable_access_log) %>
41
+ <%="StandardError=append:#{fetch(:cable_error_log)}" if fetch(:cable_error_log) %>
42
+
43
+ SyslogIdentifier=<%= fetch(:cable_service_unit_name) %>
44
+ [Install]
45
+ WantedBy=<%=(fetch(:cable_systemctl_user) == :system) ? "multi-user.target" : "default.target"%>
@@ -0,0 +1,22 @@
1
+ [Unit]
2
+ Description=Cable Puma HTTP Server Accept Sockets for <%= "#{fetch(:application)} (#{fetch(:stage)})" %>
3
+
4
+ [Socket]
5
+ <% cable_binds.each do |bind| -%>
6
+ <%= "ListenStream=#{bind.local.address}" %>
7
+ <% end -%>
8
+
9
+ # Don't let systemd accept the request, wait for Cable to do that.
10
+ # Systemd will start the cable service upon first request if it wasn't started.
11
+ #
12
+ # You might also want to set your Nginx upstream to have a fail_timeout large enough to accomodate your app's
13
+ # startup time.
14
+ Accept=no
15
+ <%= "NoDelay=true" if fetch(:cable_systemctl_user) == :system %>
16
+ ReusePort=true
17
+ Backlog=1024
18
+
19
+ SyslogIdentifier=<%= fetch(:cable_socket_unit_name) %>
20
+
21
+ [Install]
22
+ WantedBy=sockets.target
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-cable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brice TEXIER
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: ActionCable integration for Capistrano using Puma and Systemd
28
+ email:
29
+ - brice@codeur.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".standard.yml"
35
+ - CHANGELOG.md
36
+ - CODE_OF_CONDUCT.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/capistrano/cable.rb
41
+ - lib/capistrano/cable/bind.rb
42
+ - lib/capistrano/cable/systemd.rb
43
+ - lib/capistrano/cable/version.rb
44
+ - lib/capistrano/tasks/systemd.rake
45
+ - lib/capistrano/templates/cable.service.erb
46
+ - lib/capistrano/templates/cable.socket.erb
47
+ homepage: https://github.com/codeur/capistrano-cable#readme
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/codeur/capistrano-cable#readme
52
+ source_code_uri: https://github.com/codeur/capistrano-cable
53
+ changelog_uri: https://github.com/codeur/capistrano-cable/releases
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.3.26
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: ActionCable integration for Capistrano
73
+ test_files: []