Skip to content

Commit

Permalink
Merge pull request #86 from NethServer/fixPort_reload
Browse files Browse the repository at this point in the history
Reload PHP-FPM services after configuration changes and update NextFpmPort script NethServer/dev#7200
  • Loading branch information
stephdl authored Dec 9, 2024
2 parents 685197e + dedd219 commit 12e15ed
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions imageroot/actions/create-vhost/30SystemdServices
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if PhpVersion:
subprocess.run(["download-php-fpm",str(PhpVersion)])
# start the container if stopped
subprocess.run(["systemctl", "--user", "enable", "--now", "phpfpm@"+str(PhpVersion)+".service"])
# we need to reload the php fpm service to let him know about the new configuration
subprocess.run(["systemctl", "--user", "reload", "phpfpm@"+str(PhpVersion)+".service"])
else:
subprocess.run(["systemctl", "--user", "reload", "nginx.service"])

1 change: 1 addition & 0 deletions imageroot/actions/update-vhost/30SystemdServices
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if PhpVersion:
subprocess.run(["download-php-fpm",str(PhpVersion)])
# start the container if stopped
subprocess.run(["systemctl", "--user", "enable", "--now", "phpfpm@"+str(PhpVersion)+".service"])
# php version might have changed, reload all php-fpm services
subprocess.run(["systemctl", "--user", "reload", "phpfpm@*.service"])
else:
subprocess.run(["systemctl", "--user", "reload", "nginx.service"])
Expand Down
66 changes: 66 additions & 0 deletions imageroot/update-module.d/10fix_NextFpmPort
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

# first release where buildt with a static file NextFpmPort set to 9000
# during upgrade the value of virtualhost ID was overwritten with the default value 9000
# this script will attempt to discover how many configurations we have
# and set the NextFpmPort file accordingly
# Added with the release 1.0.20
# Todo: NextFpmPort should be named LastFpmPort (when the vhost is created, the last ID is written there)

# actions:
# find all existing ID of vhosts
# keep the max value or default to 10 000 if no vhosts are found or exception (probably a module never used, but be prudent)
# read the NextFpmPort file
# if current_id is None or last_id > current_id write the last_id to the file


import os
import sys

# Define the directory and the target file
directory = "databases/vhosts/"
output_file = "databases/NextFpmPort"

# no NextFpmPort, no need to continue
if not os.path.exists(output_file):
sys.exit(0)

# Find all .ini files
ini_files = [f for f in os.listdir(directory) if f.endswith('.ini')]

# Extract IDs from filenames
ids = []
try:
for file in ini_files:
# Remove the .ini suffix and convert the remaining part to an integer
id_str = file.removesuffix(".ini")
ids.append(int(id_str))

# Determine the last ID
if ids:
last_id = max(ids)
else:
last_id = 0 # Default to 0 if no IDs exist, no vhosts
except:
last_id = 10000 # Default to 10 000 if ecxeption, few chances to get 1000 active vhosts

# Read the current value from the NextFpmPort file if it exists
current_id = None
with open(output_file, "r") as f:
try:
current_id = int(f.read().strip())
except ValueError:
current_id = None

# Write the last ID only if it's greater than the current value
if current_id is None or last_id > current_id:
with open(output_file, "w") as f:
f.write(str(last_id))
print(f"{os.environ['MODULE_ID']}: Last virtualhost ID {last_id} written to {output_file}", file=sys.stderr)
else:
print(f"{os.environ['MODULE_ID']}: Last virtualhost ID {last_id} not written, the current ID {current_id} in {output_file} is correct", file=sys.stderr)

0 comments on commit 12e15ed

Please sign in to comment.