Have Quay lookup the sbin/my_init PID to kill

We changed the entry point in Quay to be a shell script that calls `my_init`, which means the init no longer has PID 1. We therefore need to look up the correct PID to kill it.

Fixes https://jira.coreos.com/browse/QS-74
This commit is contained in:
Joseph Schorr 2017-12-01 14:04:43 -05:00
parent c383ac1f9d
commit 874a7b0c41

View file

@ -3,6 +3,7 @@
import logging
import os
import signal
import subprocess
from flask import abort
@ -136,6 +137,21 @@ class SuperUserSetupDatabase(ApiResource):
abort(403)
# From: https://stackoverflow.com/a/44712205
def get_process_id(name):
"""Return process ids found by (partial) name or regex.
>>> get_process_id('kthreadd')
[2]
>>> get_process_id('watchdog')
[10, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61] # ymmv
>>> get_process_id('non-existent process')
[]
"""
child = subprocess.Popen(['pgrep', name], stdout=subprocess.PIPE, shell=False)
response = child.communicate()[0]
return [int(pid) for pid in response.split()]
@resource('/v1/superuser/shutdown')
@internal_only
@ -154,7 +170,7 @@ class SuperUserShutdown(ApiResource):
if app.config.get('DEBUGGING') == True:
return {}
os.kill(1, signal.SIGINT)
os.kill(get_process_id('my_init')[0], signal.SIGINT)
return {}
abort(403)