Skip to content

Commit

Permalink
fix: update supervisord conf and reload, log everything
Browse files Browse the repository at this point in the history
  • Loading branch information
gavindsouza committed Jun 10, 2020
1 parent e26a0a9 commit c43da5b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions bench/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def reload_nginx():
@click.option("--user", help="optional user argument")
@click.option("--yes", help="Yes to regeneration of supervisor config", is_flag=True, default=False)
def setup_supervisor(user=None, yes=False):
bench.config.supervisor.update_supervisord_config(user=user, yes=yes)
bench.config.supervisor.generate_supervisor_config(bench_path=".", user=user, yes=yes)


Expand Down
3 changes: 2 additions & 1 deletion bench/config/production_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# imports - module imports
from bench.config.common_site_config import get_config
from bench.config.nginx import make_nginx_conf
from bench.config.supervisor import generate_supervisor_config
from bench.config.supervisor import generate_supervisor_config, update_supervisord_config
from bench.config.systemd import generate_systemd_config
from bench.utils import CommandFailedError, exec_cmd, find_executable, fix_prod_setup_perms, get_bench_name, get_cmd_output

Expand All @@ -30,6 +30,7 @@ def setup_production(user, bench_path='.', yes=False):
if get_config(bench_path).get('restart_systemd_on_update'):
generate_systemd_config(bench_path=bench_path, user=user, yes=yes)
else:
update_supervisord_config(user=user, yes=yes)
generate_supervisor_config(bench_path=bench_path, user=user, yes=yes)
make_nginx_conf(bench_path=bench_path, yes=yes)
fix_prod_setup_perms(bench_path, frappe_user=user)
Expand Down
39 changes: 29 additions & 10 deletions bench/config/supervisor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# imports - standard imports
import getpass
import logging
import os

# imports - module imports
Expand All @@ -13,13 +14,14 @@
from six.moves import configparser


logger = logging.getLogger(bench.PROJECT_NAME)


def generate_supervisor_config(bench_path, user=None, yes=False):
"""Generate supervisor config for respective bench path"""
if not user:
user = getpass.getuser()

update_supervisord_conf(user=user)

template = bench.config.env.get_template('supervisor.conf')
config = get_config(bench_path=bench_path)
bench_dir = os.path.abspath(bench_path)
Expand Down Expand Up @@ -64,12 +66,17 @@ def get_supervisord_conf():
return possibility


def update_supervisord_conf(user):
"""From bench v5.0, we're moving to supervisor running as user"""
def update_supervisord_config(user=None, yes=False):
"""From bench v5.x, we're moving to supervisor running as user"""
from bench.config.production_setup import service

supervisord_conf = get_supervisord_conf()
section = "unix_http_server"
updated_values = {
"chmod": "0760",
"chown": "{user}:{user}".format(user=user)
}
supervisord_conf_updated = False

if not supervisord_conf:
return
Expand All @@ -79,12 +86,24 @@ def update_supervisord_conf(user):

if section not in config.sections():
config.add_section(section)
supervisord_conf_updated = True

config.set(section, "chmod", "0760")
config.set(section, "chown", "{user}:{user}".format(user=user))
for key, value in updated_values.items():
current_value = config[section].get(key, "")
if current_value.strip() != value:
config.set(section, key, value)
supervisord_conf_updated = True
logger.log("Updated supervisord config: '{0}' changed from '{1}' to '{2}'".format(key, current_value, value))

if not supervisord_conf_updated:
return

with open(supervisord_conf, "w") as f:
config.write(f)
try:
with open(supervisord_conf, "w") as f:
config.write(f)
logger.log("Updated supervisord config at '{0}'".format(supervisord_conf))
except Exception as e:
logger.log("Updating supervisord config failed due to '{0}'".format(e))

# restart supervisor to take new changes into effect
service('supervisor', 'restart')
# Reread supervisor configuration, reload supervisord and supervisorctl, restart services that were started
service('supervisor', 'reload')

0 comments on commit c43da5b

Please sign in to comment.