Class: Procsd::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/procsd/cli.rb

Defined Under Namespace

Classes: ArgumentError, ConfigurationError

Instance Method Summary collapse

Instance Method Details

#__exec(process_name) ⇒ Object

Raises:



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/procsd/cli.rb', line 206

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

  if options["env"]
    @config[:environment].each { |k, v| @config[:environment][k] = v.to_s }
    exec @config[:environment], start_cmd
  else
    exec start_cmd
  end
end

#__print_versionObject



222
223
224
# File 'lib/procsd/cli.rb', line 222

def __print_version
  puts VERSION
end

#config(name) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/procsd/cli.rb', line 192

def config(name)
  preload!

  case name
  when "sudoers"
    say generate_sudoers_rule(ENV["USER"])
  else
    raise ArgumentError, "Wring type of argument: #{name}"
  end
end

#createObject

Raises:



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
# 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")
    unless Dir.exist?(File.join options["dir"], "public")
      raise ConfigurationError, "Missing public/ folder 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 options["or-restart"]
      restart
    else
      say("App target `#{target_name}` already exists", :red)
    end
  end
end

#destroyObject



44
45
46
47
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
# File 'lib/procsd/cli.rb', line 44

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 configuraion (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

#disableObject



93
94
95
96
97
98
99
100
101
# File 'lib/procsd/cli.rb', line 93

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

#enableObject



82
83
84
85
86
87
88
89
90
# File 'lib/procsd/cli.rb', line 82

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

#listObject



183
184
185
186
187
188
189
# File 'lib/procsd/cli.rb', line 183

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



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/procsd/cli.rb', line 168

def logs(service_name = nil)
  preload!

  command = %w(journalctl --no-pager --no-hostname --all --output short-iso)
  command.push("-n", options.fetch("num", "100"))
  command.push("-f") if options["tail"]
  command.push("--system") if options["system"]
  command.push("--priority", options["priority"]) if options["priority"]
  command.push("--grep", "'" + options["grep"] + "'") if options["grep"]

  command.push("--unit", "#{app_name}-#{service_name}*")
  execute command, type: :exec
end

#restartObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/procsd/cli.rb', line 126

def restart
  preload!
  say_target_not_exists and return unless target_exist?

  # 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

#startObject



104
105
106
107
108
109
110
111
112
# File 'lib/procsd/cli.rb', line 104

def start
  preload!
  say_target_not_exists and return unless target_exist?

  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

#status(service_name = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/procsd/cli.rb', line 148

def status(service_name = nil)
  preload!
  say_target_not_exists and return unless target_exist?

  if options["short"]
    command = %w(systemctl list-units --no-pager --no-legend --all)
  else
    command = %w(systemctl status --no-pager --output short-iso --all)
  end

  command << (options["target"] ? target_name : "#{app_name}-#{service_name}*")
  execute command, type: :exec
end

#stopObject



115
116
117
118
119
120
121
122
123
# File 'lib/procsd/cli.rb', line 115

def stop
  preload!
  say_target_not_exists and return unless target_exist?

  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