|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright (C) 2024 Nethesis S.r.l. |
| 5 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | +# |
| 7 | + |
| 8 | +# first release where buildt with a static file NextFpmPort set to 9000 |
| 9 | +# during upgrade the value of virtualhost ID was overwritten with the default value 9000 |
| 10 | +# this script will attempt to discover how many configurations we have |
| 11 | +# and set the NextFpmPort file accordingly |
| 12 | +# Added with the release 1.0.20 |
| 13 | +# Todo: NextFpmPort should be named LastFpmPort (when the vhost is created, the last ID is written there) |
| 14 | + |
| 15 | +# actions: |
| 16 | +# find all existing ID of vhosts |
| 17 | +# keep the max value or default to 10 000 if no vhosts are found or exception (probably a module never used, but be prudent) |
| 18 | +# read the NextFpmPort file |
| 19 | +# if current_id is None or last_id > current_id write the last_id to the file |
| 20 | + |
| 21 | + |
| 22 | +import os |
| 23 | +import sys |
| 24 | + |
| 25 | +# Define the directory and the target file |
| 26 | +directory = "databases/vhosts/" |
| 27 | +output_file = "databases/NextFpmPort" |
| 28 | + |
| 29 | +# no NextFpmPort, no need to continue |
| 30 | +if not os.path.exists(output_file): |
| 31 | + sys.exit(0) |
| 32 | + |
| 33 | +# Find all .ini files |
| 34 | +ini_files = [f for f in os.listdir(directory) if f.endswith('.ini')] |
| 35 | + |
| 36 | +# Extract IDs from filenames |
| 37 | +ids = [] |
| 38 | +try: |
| 39 | + for file in ini_files: |
| 40 | + # Remove the .ini suffix and convert the remaining part to an integer |
| 41 | + id_str = file.removesuffix(".ini") |
| 42 | + ids.append(int(id_str)) |
| 43 | + |
| 44 | + # Determine the last ID |
| 45 | + if ids: |
| 46 | + last_id = max(ids) |
| 47 | + else: |
| 48 | + last_id = 0 # Default to 0 if no IDs exist, no vhosts |
| 49 | +except: |
| 50 | + last_id = 10000 # Default to 10 000 if ecxeption, few chances to get 1000 active vhosts |
| 51 | + |
| 52 | +# Read the current value from the NextFpmPort file if it exists |
| 53 | +current_id = None |
| 54 | +with open(output_file, "r") as f: |
| 55 | + try: |
| 56 | + current_id = int(f.read().strip()) |
| 57 | + except ValueError: |
| 58 | + current_id = None |
| 59 | + |
| 60 | +# Write the last ID only if it's greater than the current value |
| 61 | +if current_id is None or last_id > current_id: |
| 62 | + with open(output_file, "w") as f: |
| 63 | + f.write(str(last_id)) |
| 64 | + print(f"{os.environ['MODULE_ID']}: Last virtualhost ID {last_id} written to {output_file}", file=sys.stderr) |
| 65 | +else: |
| 66 | + 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 commit comments