Class: Procsd::CLI
- Inherits:
-
Thor
- Object
- Thor
- Procsd::CLI
- Defined in:
- lib/procsd/cli.rb
Defined Under Namespace
Classes: ArgumentError, ConfigurationError
Instance Method Summary collapse
- #__exec(process_name) ⇒ Object
- #__print_version ⇒ Object
- #config(name) ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
- #list ⇒ Object
- #logs(service_name = nil) ⇒ Object
- #restart(service_name = nil) ⇒ Object
- #start(service_name = nil) ⇒ Object
- #status(service_name = nil) ⇒ Object
- #stop(service_name = nil) ⇒ Object
Instance Method Details
#__exec(process_name) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/procsd/cli.rb', line 248 def __exec(process_name) preload! start_cmd = @config[:processes].dig(process_name, "commands", "ExecStart") raise ArgumentError, "Process is not defined: #{process_name}" unless start_cmd process_env = @config[:environment].each { |k, v| @config[:environment][k] = v.to_s } if ["dev"] dev_env = @config[:dev_environment].each { |k, v| @config[:dev_environment][k] = v.to_s } process_env.merge!(dev_env) end exec process_env, start_cmd end |
#__print_version ⇒ Object
265 266 267 |
# File 'lib/procsd/cli.rb', line 265 def __print_version puts VERSION end |
#config(name) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/procsd/cli.rb', line 219 def config(name) preload! = { "user" => ENV["USER"], "dir" => ENV["PWD"], "path" => `/bin/bash -ilc 'echo $PATH'`.strip } generator = Generator.new(@config, ) case name when "sudoers" puts generator.generate_sudoers(["user"], has_reload: has_reload?) when "services" return unless () services = generator.generate_units services.each do |service_name, service_data| puts "Service: #{service_name} (size: #{service_data[:size]}):" puts "---\n\n" puts service_data[:content] puts "---\n\n" end when "certbot_command" puts get_certbot_command.join(' ') else raise ArgumentError, "Wrong type of argument: #{name}" end end |
#create ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/procsd/cli.rb', line 15 def create raise ConfigurationError, "Can't find systemctl executable available" unless in_path?("systemctl") preload! if @config[:nginx] raise ConfigurationError, "Can't find nginx executable available" unless in_path?("nginx") public_folder_path = @config[:nginx]["public_folder_path"] || "public" unless Dir.exist?(File.join ["dir"], public_folder_path) raise ConfigurationError, "Missing public folder path to use with Nginx" end unless @config.dig(:environment, "PORT") raise ConfigurationError, "Please provide PORT environment variable in procsd.yml to use with Nginx" end if @config[:nginx]["ssl"] raise ConfigurationError, "Can't find certbot executable available" unless in_path?("certbot") end end if !target_exist? perform_create else if ["or-restart"] restart else say("App target `#{target_name}` already exists", :red) end end end |
#destroy ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/procsd/cli.rb', line 48 def destroy preload! if target_exist? stop disable units.each do |filename| path = File.join(systemd_dir, filename) execute %W(sudo rm #{path}) and say "Deleted: #{path}" if File.exist?(path) end if execute %w(sudo systemctl daemon-reload) say("Reloaded configuration (daemon-reload)", :green) end say("App services were stopped, disabled and removed", :green) sudoers_file_path = "#{SUDOERS_DIR}/#{app_name}" if system "sudo", "test", "-e", sudoers_file_path say("Sudoers file removed", :green) if execute %W(sudo rm #{sudoers_file_path}) end if @config[:nginx] enabled_path = File.join(NGINX_DIR, "sites-enabled", app_name) available_path = File.join(NGINX_DIR, "sites-available", app_name) [enabled_path, available_path].each do |path| execute %W(sudo rm #{path}) and say "Deleted: #{path}" if File.exist?(path) end execute %w(sudo systemctl reload-or-restart nginx) say("Nginx config removed and daemon reloaded", :green) end else say_target_not_exists end end |
#disable ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/procsd/cli.rb', line 97 def disable preload! say_target_not_exists and return unless target_exist? say "Note: app target #{target_name} already disabled" if !target_enabled? if execute %W(sudo systemctl disable #{target_name}) say("Disabled app target #{target_name}", :green) end end |
#enable ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/procsd/cli.rb', line 86 def enable preload! say_target_not_exists and return unless target_exist? say "Note: app target #{target_name} already enabled" if target_enabled? if execute %W(sudo systemctl enable #{target_name}) say("Enabled app target #{target_name}", :green) end end |
#list ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/procsd/cli.rb', line 210 def list preload! say_target_not_exists and return unless target_exist? command = %W(systemctl list-dependencies #{target_name}) execute command, type: :exec end |
#logs(service_name = nil) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/procsd/cli.rb', line 195 def logs(service_name = nil) preload! command = %w(journalctl --no-pager --no-hostname --all --output short-iso) command.push("-n", .fetch("num", "100")) command.push("-f") if ["tail"] command.push("--system") if ["system"] command.push("--priority", ["priority"]) if ["priority"] command.push("--grep", "'" + ["grep"] + "'") if ["grep"] command.push("--unit", "#{app_name}-#{service_name}*") execute command, type: :exec end |
#restart(service_name = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/procsd/cli.rb', line 146 def restart(service_name = nil) preload! say_target_not_exists and return unless target_exist? if service_name full_name = to_full_name(service_name) if execute %W(sudo systemctl reload-or-restart #{full_name} --all) say("Restarted app service (#{full_name})", :green) end else # If one of the child services of a target has `ExecReload` and `ReloadPropagatedFrom` # options defined, then use `reload-or-restart` to call all services (not the main target) # because of systemd bug https://github.com/systemd/systemd/issues/10638 success = if has_reload? execute %W(sudo systemctl reload-or-restart #{app_name}-* --all) else execute %W(sudo systemctl restart #{target_name}) end if success say("Restarted app services (#{target_name})", :green) end end end |
#start(service_name = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/procsd/cli.rb', line 108 def start(service_name = nil) preload! say_target_not_exists and return unless target_exist? if service_name full_name = to_full_name(service_name) say "Note: app service #{full_name} already started/active" if service_active?(full_name) if execute %W(sudo systemctl start #{full_name} --all) say("Started app service (#{full_name})", :green) end else say "Note: app target #{target_name} already started/active" if target_active? if execute %W(sudo systemctl start #{target_name}) say("Started app services (#{target_name})", :green) end end end |
#status(service_name = nil) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/procsd/cli.rb', line 175 def status(service_name = nil) preload! say_target_not_exists and return unless target_exist? if ["short"] command = %w(systemctl list-units --no-pager --no-legend --all) else command = %w(systemctl status --no-pager --output short-iso --all) end command << (["target"] ? target_name : to_full_name(service_name)) execute command, type: :exec end |
#stop(service_name = nil) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/procsd/cli.rb', line 127 def stop(service_name = nil) preload! say_target_not_exists and return unless target_exist? if service_name full_name = to_full_name(service_name) say "Note: app service #{full_name} already stopped/inactive" if !service_active?(full_name) if execute %W(sudo systemctl stop #{full_name} --all) say("Stopped app service (#{full_name})", :green) end else say "Note: app target #{target_name} already stopped/inactive" if !target_active? if execute %W(sudo systemctl stop #{target_name}) say("Stopped app services (#{target_name})", :green) end end end |