diff --git a/nginx_stage/lib/nginx_stage.rb b/nginx_stage/lib/nginx_stage.rb index f5cb71ea59..a74ecf0edb 100644 --- a/nginx_stage/lib/nginx_stage.rb +++ b/nginx_stage/lib/nginx_stage.rb @@ -192,6 +192,21 @@ def self.active_users end.compact end + # List of inactive users. + # @return [Array] the list of inactive users. + def self.inactive_users + Dir[pun_pid_path(user: '*')].map do |v| + name = v[/#{pun_pid_path(user: '(.+)')}/, 1] + User.new(name) + + # return nil here becasue we actually _want_ to rescue. + # i.e., a User is inactive if it _can't_ be instanitated. + nil + rescue ArgumentError => e + name + end.compact + end + # Get a hash of all the staged app configs # @example List of all staged app configs # staged_apps diff --git a/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb b/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb index 60c2ea67ee..5b6e343437 100644 --- a/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb +++ b/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb @@ -79,6 +79,21 @@ class NginxCleanGenerator < Generator $stderr.puts "#{$!.to_s}" end end + + NginxStage.inactive_users.each do |u| + begin + puts "#{u} (disabled)" + pid_path = PidFile.new NginxStage.pun_pid_path(user: u) + + `kill -s TERM #{pid_path.pid}` + FileUtils.rm_rf(Pathname.new(pid_path.to_s).parent) + FileUtils.rm(NginxStage.pun_secret_key_base_path(user: u).to_s) + FileUtils.rm(NginxStage.pun_config_path(user: u).to_s) + + rescue StandardError => e + warn "Error trying to clean up disabled user #{u}: #{e.message}" + end + end end def cleanup_stale_files(pid_path, socket)