Skip to content

Commit

Permalink
Improve systemd services recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanleonov committed Oct 3, 2019
1 parent 53d8b92 commit b78cd14
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
4 changes: 2 additions & 2 deletions AppController/djinn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3431,8 +3431,8 @@ def start_search2_role
def stop_search2_role
# Stop Solr
Djinn.log_debug('Stopping SOLR on this node.')
Djinn.log_run('systemctl stop solr')
Djinn.log_run('systemctl disable solr')
Djinn.log_run('systemctl stop appscale-solr')
Djinn.log_run('systemctl disable appscale-solr')
Djinn.log_debug('Done stopping SOLR.')
end

Expand Down
74 changes: 38 additions & 36 deletions Hermes/appscale/hermes/resources/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ class Process(object):
sample_time_diff = attr.ib(default=None)


MONIT_PROCESS_PATTERN = re.compile(
r"^Process \'(?P<name>[^']+)\' *\n"
r"(^ .*\n)*?"
r"^ pid +(?P<pid>\d+)\n",
re.MULTILINE
)
PROCESS_ATTRS = (
'pid', 'ppid', 'name', 'cwd', 'exe', 'cmdline', 'status', 'username',
'cpu_times', 'cpu_percent', 'memory_info', 'io_counters', 'num_threads',
Expand Down Expand Up @@ -193,42 +187,50 @@ async def get_known_processes():
"""
known_processes = {}

dependencies_cmd = ('cat /lib/systemd/system/appscale-*.target '
'| grep -E "^After=.*\.service$" | cut -d "=" -f 2')
try:
# Detect processes supervised by monit
output, error = await helper.subprocess('monit status', timeout=5)
# Detect appscale dependency services
output, error = await helper.subprocess(dependencies_cmd, timeout=5)
services = output.strip().split('\n')
except SubprocessError as err:
logger.warning('Failed to run `monit status` ({})'.format(err))
output = ''
for match in MONIT_PROCESS_PATTERN.finditer(output):
monit_name = match.group('name')
pid = int(match.group('pid'))
service = find_service_by_monit_name(monit_name)
application_id = service.get_application_id_by_monit_name(monit_name)
tags = [APPSCALE_PROCESS_TAG, service.name, monit_name]
if application_id:
tags.append('app___{}'.format(application_id))
known_processes[pid] = tags

# Detect processes supervised by AppScale ServiceManager
for server in ServiceManager.get_state():
known_processes[server.process.pid] = [APPSCALE_PROCESS_TAG, server.type]
logger.warning('Failed to detect appscale dependency services '
'by running `{}` ({})'.format(dependencies_cmd, err))
services = []


services_cmd = ('systemctl --no-legend list-units "appscale-*.service" '
'| cut -d " " -f 1')
try:
# Detect processes supervised by systemd
output, error = await helper.subprocess(
'systemctl status solr.service '
'| grep \'Main PID\' | awk \'{ print $3 }\'',
timeout=5
)
# Detect appscale own services
output, error = await helper.subprocess(services_cmd, timeout=5)
services += output.strip().split('\n')
except SubprocessError as err:
logger.warning('Failed to run `systemctl status solr.service` ({})'
logger.warning('Failed to detect appscale own services '
'by running `{}` ({})'.format(services_cmd, err))


for service in services:
try:
status_cmd = (
'systemctl status {} | grep "Main PID" | awk "{{ print $3 }}"'
.format(service)
)
output, error = await helper.subprocess(status_cmd, timeout=5)
if output.isdigit() and output != '0':
pid = int(output)
process_tags = [APPSCALE_PROCESS_TAG]
# Sample service names are:
# appscale-instance-run@testapp_default_v1_1570022208920-20000.service
# appscale-memcached.service
before_at, after_at = service[len('appscale-'):-len('.service')].split('@')
process_tags.append(before_at)
for part in after_at.split('_'):
process_tags.append('_{}'.format(part))
known_processes[pid] = process_tags
except SubprocessError as err:
logger.warning('Failed to run `systemctl status solr.service` ({})'
.format(err))
output = ''
if output.isdigit() and output != '0':
solr_pid = int(output)
known_processes[solr_pid] = [
APPSCALE_PROCESS_TAG, 'solr'
]
return known_processes


Expand Down
18 changes: 9 additions & 9 deletions SearchService2/solr-management/ensure_solr_running.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ export PRIVATE_IP
envsubst '$SOLR_HEAP $ZK_HOST $PRIVATE_IP' \
< "${SOLR_MANAGEMENT_DIR}/solr.in.sh" > "/tmp/solr.in.sh"
envsubst '$MEMORY_LOW $MEMORY_HIGH $MEMORY_MAX'\
< "${SOLR_MANAGEMENT_DIR}/solr.service" > "/tmp/solr.service"
< "${SOLR_MANAGEMENT_DIR}/appscale-solr.service" > "/tmp/appscale-solr.service"
if cmp -s "/tmp/solr.in.sh" "/etc/default/solr.in.sh" \
&& cmp -s "/tmp/solr.service" "/etc/systemd/system/solr.service"
&& cmp -s "/tmp/appscale-solr.service" "/etc/systemd/system/appscale-solr.service"
then
echo "/etc/default/solr.in.sh has no changes."
echo "/etc/systemd/system/solr.service has no changes."
echo "/etc/systemd/system/appscale-solr.service has no changes."
echo "Making sure Solr is running."
sudo systemctl enable solr
sudo systemctl start solr
sudo systemctl enable appscale-solr
sudo systemctl start appscale-solr
else
echo "Copying new solr.in.sh to /etc/default/solr.in.sh"
sudo cp "/tmp/solr.in.sh" "/etc/default/solr.in.sh"
echo "Copying new solr.service to /etc/systemd/system/solr.service"
sudo cp "/tmp/solr.service" "/etc/systemd/system/solr.service"
echo "Copying new solr.service to /etc/systemd/system/appscale-solr.service"
sudo cp "/tmp/appscale-solr.service" "/etc/systemd/system/appscale-solr.service"
echo "Making sure Solr is restarted."
sudo systemctl daemon-reload
sudo systemctl enable solr
sudo systemctl restart solr
sudo systemctl enable appscale-solr
sudo systemctl restart appscale-solr
fi

echo "Making sure appscale-specific config set is uploaded to zookeeper."
Expand Down
2 changes: 1 addition & 1 deletion SearchService2/solr-management/solr.service
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# /etc/systemd/system/solr.service
# /etc/systemd/system/appscale-solr.service
[Unit]
Description=Apache SOLR
After=syslog.target network.target remote-fs.target nss-lookup.target
Expand Down

0 comments on commit b78cd14

Please sign in to comment.