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:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/procsd/cli.rb', line 210

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 options["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_versionObject



227
228
229
# File 'lib/procsd/cli.rb', line 227

def __print_version
  puts VERSION
end

#config(name) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/procsd/cli.rb', line 196

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
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 options["dir"], public_folder_path)
      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



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



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

#enableObject



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

#listObject



187
188
189
190
191
192
193
# File 'lib/procsd/cli.rb', line 187

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



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/procsd/cli.rb', line 172

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/procsd/cli.rb', line 130

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



108
109
110
111
112
113
114
115
116
# File 'lib/procsd/cli.rb', line 108

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



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/procsd/cli.rb', line 152

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



119
120
121
122
123
124
125
126
127
# File 'lib/procsd/cli.rb', line 119

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