Class: BoringServices::SSHExecutor
- Inherits:
-
Object
- Object
- BoringServices::SSHExecutor
- Includes:
- SSHKit::DSL
- Defined in:
- lib/boring_services/ssh_executor.rb
Constant Summary collapse
- DPKG_LOCK_MAX_WAIT =
300- DPKG_LOCK_INTERVAL =
10
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #capture_on_host(host) ⇒ Object
- #execute_on_host(host) ⇒ Object
- #execute_on_host_for_service(service, &block) ⇒ Object
-
#initialize(config) ⇒ SSHExecutor
constructor
A new instance of SSHExecutor.
- #install_package(package, host = nil) ⇒ Object
- #systemd_disable(service_name, host = nil) ⇒ Object
- #systemd_enable(service_name, host = nil) ⇒ Object
- #systemd_restart(service_name, host = nil) ⇒ Object
- #systemd_start(service_name, host = nil) ⇒ Object
- #systemd_status(service_name, host = nil) ⇒ Object
- #systemd_stop(service_name, host = nil) ⇒ Object
- #uninstall_package(package, host = nil) ⇒ Object
- #upload_template(template_path, destination, context = {}, host = nil) ⇒ Object
- #wait_for_dpkg_lock ⇒ Object
Constructor Details
#initialize(config) ⇒ SSHExecutor
Returns a new instance of SSHExecutor.
15 16 17 18 |
# File 'lib/boring_services/ssh_executor.rb', line 15 def initialize(config) @config = config setup_sshkit end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/boring_services/ssh_executor.rb', line 13 def config @config end |
Instance Method Details
#capture_on_host(host) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/boring_services/ssh_executor.rb', line 155 def capture_on_host(host, *) output = nil execute_on_host(host) do output = capture(*, raise_on_non_zero_exit: false) end output end |
#execute_on_host(host) ⇒ Object
20 21 22 |
# File 'lib/boring_services/ssh_executor.rb', line 20 def execute_on_host(host, &) on(formatted_host(host), &) end |
#execute_on_host_for_service(service, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/boring_services/ssh_executor.rb', line 24 def execute_on_host_for_service(service, &block) hosts = Array(service['hosts'] || service['host']).compact raise Error, "No hosts defined for service #{service['name'] || 'unknown'}" if hosts.empty? hosts.each do |host| on formatted_host(host) do block.call(host) end end end |
#install_package(package, host = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/boring_services/ssh_executor.rb', line 35 def install_package(package, host = nil) if host execute_on_host(host) do wait_for_dpkg_lock execute :sudo, 'apt-get', 'update' execute :sudo, 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', package end else wait_for_dpkg_lock backend.execute :sudo, 'apt-get', 'update' backend.execute :sudo, 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', package end end |
#systemd_disable(service_name, host = nil) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/boring_services/ssh_executor.rb', line 135 def systemd_disable(service_name, host = nil) if host execute_on_host(host) { execute :sudo, 'systemctl', 'disable', service_name } else backend.execute :sudo, 'systemctl', 'disable', service_name end end |
#systemd_enable(service_name, host = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/boring_services/ssh_executor.rb', line 99 def systemd_enable(service_name, host = nil) if host execute_on_host(host) do execute :sudo, 'systemctl', 'daemon-reload' execute :sudo, 'systemctl', 'enable', service_name end else backend.execute :sudo, 'systemctl', 'daemon-reload' backend.execute :sudo, 'systemctl', 'enable', service_name end end |
#systemd_restart(service_name, host = nil) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/boring_services/ssh_executor.rb', line 127 def systemd_restart(service_name, host = nil) if host execute_on_host(host) { execute :sudo, 'systemctl', 'restart', service_name } else backend.execute :sudo, 'systemctl', 'restart', service_name end end |
#systemd_start(service_name, host = nil) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/boring_services/ssh_executor.rb', line 111 def systemd_start(service_name, host = nil) if host execute_on_host(host) { execute :sudo, 'systemctl', 'start', service_name } else backend.execute :sudo, 'systemctl', 'start', service_name end end |
#systemd_status(service_name, host = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/boring_services/ssh_executor.rb', line 143 def systemd_status(service_name, host = nil) if host output = nil execute_on_host(host) do output = capture :sudo, 'systemctl', 'status', service_name, raise_on_non_zero_exit: false end output else backend.capture :sudo, 'systemctl', 'status', service_name, raise_on_non_zero_exit: false end end |
#systemd_stop(service_name, host = nil) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/boring_services/ssh_executor.rb', line 119 def systemd_stop(service_name, host = nil) if host execute_on_host(host) { execute :sudo, 'systemctl', 'stop', service_name } else backend.execute :sudo, 'systemctl', 'stop', service_name end end |
#uninstall_package(package, host = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/boring_services/ssh_executor.rb', line 70 def uninstall_package(package, host = nil) if host execute_on_host(host) do execute :sudo, 'apt-get', 'remove', '-y', package execute :sudo, 'apt-get', 'autoremove', '-y' end else backend.execute :sudo, 'apt-get', 'remove', '-y', package backend.execute :sudo, 'apt-get', 'autoremove', '-y' end end |
#upload_template(template_path, destination, context = {}, host = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/boring_services/ssh_executor.rb', line 82 def upload_template(template_path, destination, context = {}, host = nil) template = File.read(template_path) result = ERB.new(template).result_with_hash(context) if host execute_on_host(host) do upload! StringIO.new(result), destination execute :sudo, 'chown', 'root:root', destination execute :sudo, 'chmod', '644', destination end else backend.upload! StringIO.new(result), destination backend.execute :sudo, 'chown', 'root:root', destination backend.execute :sudo, 'chmod', '644', destination end end |
#wait_for_dpkg_lock ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/boring_services/ssh_executor.rb', line 49 def wait_for_dpkg_lock elapsed = 0 loop do break unless backend.test '[ -f /var/lib/dpkg/lock-frontend ]' processes = backend.capture(:sudo, :fuser, '/var/lib/dpkg/lock-frontend', '2>/dev/null', raise_on_non_zero_exit: false).strip break if processes.empty? if elapsed >= DPKG_LOCK_MAX_WAIT puts " ⚠ Warning: dpkg lock still held after #{DPKG_LOCK_MAX_WAIT}s, proceeding anyway..." break end puts " ⏳ Waiting for dpkg lock to be released (#{elapsed}s elapsed)..." sleep DPKG_LOCK_INTERVAL elapsed += DPKG_LOCK_INTERVAL end end |