Move a package from one share to another
Applies to: Medulla – Packages
Version: All versions
Environment: On-Premise / Private SaaS / Shared SaaS
Category: Usage
Objective
The move_package_share.py script moves a package from one share to another in a single operation, without having to recreate it. It synchronizes the three locations where Medulla stores a package’s share information:
- The
conf.jsonandxmppdeploy.jsonfiles: thelocalisation_serverandprevious_localisation_serverfields are set to the name of the destination share. - The
pkgsdatabase: theconf_jsoncolumn in thepackagestable is updated, as is thepkgs_share_idcolumn if it exists. - The package directory: it changes from
/var/lib/pulse2/packages/sharing/<source share>/<uuid>to/var/lib/pulse2/packages/sharing/<destination share>/<uuid>, and any symbolic link is updated to point to the new location.
The script can be run either interactively (it prompts you for input) or from the command line. It creates a backup before making any changes and operates on an “all-or-nothing” basis: if any step fails, everything is restored to its original state. The --dry-run mode lets you see what would happen without actually making any changes.
Prerequisites
The script runs on the main Medulla server, where the pkgs database and the /var/lib/pulse2/packages directory are located. Before you begin, check the following:
- Root account: the script modifies package files and moves the package directory. Run it as
root; otherwise, it will stop before making any changes (n'est pas modifiable par ce compte). - Python 3 and pymysql: the
pymysqlmodule is provided by thepython3-pymysqlpackage. - Database access: the SQL account must have read access to
pkgs.pkgs_sharesand read/write access topkgs.packages. By default, the script uses the connection information from the[database]section of the/etc/mmc/plugins/xmppmaster.ini.localfile. If this file is unreadable, the script will report an error and prompt for the password. - Existing destination share: the share must be declared in Medulla, and its folder
/var/lib/pulse2/packages/sharing/<share name>must exist. The script does not create it; usecreate_pkgs_share.pyif the share does not yet exist. - Syncthing share folder on the relay server: the destination share folder must also exist on the relay server. The script does not modify the Syncthing configuration.
- Same file system: the folders for both shares must be on the same file system, since the move is performed by simply renaming them (see the Troubleshooting section if this is not the case).
- Package recognized by the database: the package must exist in
pkgs.packages, and itsconf.jsonandxmppdeploy.jsonfiles must contain a validlocalisation_serverfield.
Installing the script
Installation involves installing a single dependency and copying the file to the main server.
1. Install the dependency
apt install python3-pymysql
2. Copy the script
Place move_package_share.py in the folder of your choice (for example, /root/scripts/) and make it executable:
mkdir -p /root/scripts
cp move_package_share.py /root/scripts/
chmod +x /root/scripts/move_package_share.py
The script creates a backup/ folder next to it to store its backups, whose files are readable only by root.
3. Check that it works
/root/scripts/move_package_share.py --help
The help menu displays all available options. If the pymysql module is missing, the script will display the message Module manquant : pymysql (apt install python3-pymysql).
Usage
The script moves only one package per execution. Run it as root and always start with a simulation.
Simulate the move
/root/scripts/move_package_share.py --dry-run
In simulation mode, the script reads the database and files but does not write anything: no database, no JSON files, no folder, and no backup. It displays a summary and a list of the actions it would perform. All of the options below also work with --dry-run.
Interactive mode
/root/scripts/move_package_share.py
The script asks questions in this order:
- Available shares: it displays the list of shares from
pkgs.pkgs_sharesalong with the number of packages in each. Thewinupdatesandwinupdatesmajorshares are not offered. - Current package share: enter a name or an ID. Leave blank to view packages from all shares.
- Package to move: enter the list number, the package’s UUID, or a text to filter the list by UUID or name. The list shows a maximum of 100 packages; refine the search with a text if there are more.
- Destination share: enter a name or an ID.
- Confirmation: the script displays a summary, then asks,
Confirmer le déplacement ? [o/N](Confirm move?). If you do not respond witho, nothing will be changed.
Example summary (7-Zip package moved from the global share to a clients share):
--- Récapitulatif ---
Package : 7-Zip 26.05 (a0303e44-4f3e-576c-9f74-9afe06969720)
Dossier : /var/lib/pulse2/packages/sharing/global/a0303e44-4f3e-576c-9f74-9afe06969720
Destination : clients (id 3, relai relay01)
Emplacement localisation_server previous_localisation_server Action
----------------------- ------------------- ---------------------------- -------
conf.json global -> clients global -> clients modifié
xmppdeploy.json global -> clients global -> clients modifié
pkgs.packages.conf_json global -> clients global -> clients modifié
pkgs.packages.pkgs_share_id : 1 -> 3
Dossier du package : mv /var/lib/pulse2/packages/sharing/global/a0303e44-4f3e-576c-9f74-9afe06969720 /var/lib/pulse2/packages/sharing/clients/a0303e44-4f3e-576c-9f74-9afe06969720
The share names, IDs, and relay in this example are fictitious.
Non-interactive mode
If you know the package’s UUID and the destination share, specify them on the command line. With --yes, the script asks no questions:
/root/scripts/move_package_share.py \
--package a0303e44-4f3e-576c-9f74-9afe06969720 \
--from-share global --to-share clients --yes
Script options
General:
--dry-run: simulates the actions; nothing is written or moved.-y,--yes: does not prompt for final confirmation.--package UUID: package ID (folder name and value ofpkgs.packages.uuid).--from-share NOM|ID: current share of the package. In interactive mode, it filters the list; with--package, it serves as a check.--to-share NOM|ID: destination share.--previous-share {new,old}: value written toprevious_localisation_server.new(default) sets it to the destination share;oldsets it to the old share.
Package directory:
--packages-root: package root (/var/lib/pulse2/packagesby default). The script searches for the directory in theshare_pathof each share, then in<racine>/sharing/<share>/<uuid>, and finally in<racine>/<uuid>.--package-dir DOSSIER: package directory; specify this if it is not found automatically or if multiple directories match.
Database connection:
--db-host,--db-port,--db-user: SQL host, port, and user. By default, these values are taken from/etc/mmc/plugins/xmppmaster.ini.local(otherwiselocalhostand3306for the host and port).--db-password: SQL password. Prefer thePKGS_DB_PASSWORDenvironment variable, which prevents the password from appearing in the command history.
For the password, the order of priority is: --db-password option, PKGS_DB_PASSWORD variable, ini file (only for the user read from that file), then keyboard input.
What the script modifies
After your confirmation, the script performs the following operations in sequence. Either all of them are performed, or none are (see the Backups, cancellation, and undo section).
- Backups: the two JSON files and the current value of
conf_jsonare copied to thebackup/folder. - Database: in the
pkgs.packagestable, theconf_jsoncolumn is updated with the new values, and thepkgs_share_idcolumn is updated with the ID of the destination share, if it exists. The row is locked and then compared to the value that was read: if someone has modified it in the meantime, the script stops without writing anything. - JSON files:
conf.jsonandxmppdeploy.jsonare rewritten with the new values. Only the values of the two fields change; the rest of the file (formatting, field order) remains unchanged, and the owner and permissions are preserved. - Package directory: it is moved by renaming
/var/lib/pulse2/packages/sharing/<source>/<uuid>to/var/lib/pulse2/packages/sharing/<destination>/<uuid>. No copy is made. - Symbolic link: if
/var/lib/pulse2/packages/<uuid>is a link to the moved folder, it is repointed to the new location (absolute or relative path, as before). - Commit: the SQL transaction is committed (
COMMIT) at the very end.
The values written to the JSON files and to conf_json are as follows:
localisation_server: the name of the destination share (thenamecolumn inpkgs.pkgs_shares).previous_localisation_server: the name of the default destination share, or the old share if--previous-share oldis used.
What the script does not modify:
- Syncthing and the relay: the script does not alter their configuration. Syncthing sees the folder leave the source share and arrive in the destination share, and the message
[INFO] Syncthing voit le dossier quitter le partage source et arriver dans le partage de destinationreminds you of this at the end. - The package contents: the package’s installation files remain unchanged.
- Other share columns: if
pkgs.packagescontains other columns whose names include “share,” the script displays their values ([INFO] ... colonne non modifiée par ce script) without changing them.
Special cases
The script detects several situations that deviate from the simple case and adapts its actions accordingly. These are always listed in the summary before confirmation.
- Folder accessible via a symbolic link: if
/var/lib/pulse2/packages/<uuid>is a symbolic link, the script works on the actual folder behind the link, moves it, and then points the link to the new location. The summary indicates this:Lien symbolique : ... (sera repointé vers ...). In case of an error, the link is restored to its previous state. winupdatesandwinupdatesmajorshares: these shares are not displayed in the list, are not accepted when the script requests them, and their packages do not appear in the general list. However, they remain usable if you explicitly specify them with--from-shareor--to-share.- Package already in the destination share: if the database, JSON files, and folder are already correct, the script displays
Rien à faire : le package est déjà dans le partage « ... »and stops without making any changes. - Folder already moved manually: if the folder is already in the destination share folder, the script does not move it and only updates the JSON files and the database (d
éjà dans le dossier du partage de destination). This is the method to use after a manualmv. - Folder outside any known share: if the package folder is not in the
share_pathof any share, the script displays a[ATTENTION]message, does not move it, and only updates the JSON files and the database. - Conflicting information: if the JSON file, the database, and the folder location do not point to the same share, the script displays a
[ATTENTION]and aligns all three with the destination share. pkgs_share_idcolumn missing: the script ignores it and modifies onlyconf_json.- Multiple folders for the same package: the script stops and prompts you to specify the correct one using
--package-dir.
Backups, cancellation, and undo
Before writing anything, the script creates three backups in the backup/ folder located next to it. These are readable only by root. In --dry-run mode, no backups are created.
<uuid>_<YYYYMMDD_HHMMSS>_conf.json: a copy ofconf.jsonbefore modification.<uuid>_<YYYYMMDD_HHMMSS>_xmppdeploy.json: a copy ofxmppdeploy.jsonbefore modification.<uuid>_<YYYYMMDD_HHMMSS>_conf_json.sql: anUPDATEquery that restoresconf_json(andpkgs_share_id, if it exists) to their previous values. At the top of the file, comments provide themvandln -sfncommands to restore the folder and the symbolic link to their original locations.
Automatic rollback
If a step fails (a row was modified in the database in the meantime, a file was modified since it was read, a move operation failed, etc.), the script rolls back everything it has already done: the SQL transaction is rolled back (rollback), the symbolic link is restored to its previous state, the folder is moved back to its original location, and the JSON files are restored. It concludes with:
Transaction annulée (rollback). Les sauvegardes restent disponibles dans le dossier backup/.
If any of these rollbacks is itself impossible, the script displays a [ATTENTION] message with the exact command to run manually. If you interrupt the script (Ctrl+C) before confirmation, nothing is changed.
Revert after a successful move
The simplest method is to rerun the script in reverse, swapping the two shares:
/root/scripts/move_package_share.py \
--package a0303e44-4f3e-576c-9f74-9afe06969720 \
--from-share clients --to-share global
To restore the exact state from before the move using the backups, proceed in this order:
- Put the folder back in place (and the symbolic link, if there was one), using the commands commented out at the beginning of the
.sqlfile:mv /var/lib/pulse2/packages/sharing/clients/<uuid> /var/lib/pulse2/packages/sharing/global/<uuid> - Restore the two JSON files from
backup/. Thecatcommand preserves the owner and permissions of the existing files:cd /root/scripts/backup cat <uuid>_<date>_conf.json > /var/lib/pulse2/packages/sharing/global/<uuid>/conf.json cat <uuid>_<date>_xmppdeploy.json > /var/lib/pulse2/packages/sharing/global/<uuid>/xmppdeploy.json - Restore the database by re-importing the
.sqlfile:mysql -u <user> -p < <uuid>_<date>_conf_json.sql
After execution
The script ends with [OK] lines that summarize what was done: one line per modified location, one for the moved folder, and one for each symbolic link that was repointed.
[OK] Package 7-Zip 26.05 (a0303e44-4f3e-576c-9f74-9afe06969720) déplacé vers le partage « clients ».
[OK] conf.json : modifié
[OK] xmppdeploy.json : modifié
[OK] pkgs.packages.conf_json : modifié
[OK] Dossier déplacé : .../sharing/global/a0303e44-... -> .../sharing/clients/a0303e44-...
[INFO] Syncthing voit le dossier quitter le partage source et arriver dans le partage de destination (sa configuration n'a pas été touchée).
To verify the result yourself:
- The folder: it should be located in the destination share folder.
ls -ld /var/lib/pulse2/packages/sharing/clients/<uuid> - The JSON files: both fields must point to the destination share.
grep -H localisation_server /var/lib/pulse2/packages/sharing/clients/<uuid>/conf.json /var/lib/pulse2/packages/sharing/clients/<uuid>/xmppdeploy.json - The database:
conf_jsonandpkgs_share_idmust match the destination share.SELECT uuid, pkgs_share_id, JSON_UNQUOTE(JSON_EXTRACT(conf_json, '$.localisation_server')) AS partage FROM pkgs.packages WHERE uuid = '<uuid>'; - Synchronization: let Syncthing finish, then verify that the package is present in the destination share folder on the relay server.
Troubleshooting
When the script stops before confirmation, it almost always ends with the message Aucune modification n'a été faite.: you can fix the issue and safely run it again.
|
Message |
Cause |
Solution |
|---|---|---|
|
|
The Python module |
|
|
|
Incorrect SQL host, username, or password. |
Check the |
|
|
The file |
Run the script as |
|
|
The UUID does not exist in the database. |
Check the UUID, or select it from the list in interactive mode. |
|
|
The share name or ID does not exist, or it is a hidden share requested in interactive mode. |
Use a name or ID from the list displayed by the script. |
|
|
|
Correct or remove |
|
|
One of the JSON or |
Correct the affected file (the message indicates which one), then try again. |
|
|
The folder is not in any of the usual locations. |
Specify |
|
|
The same UUID exists in multiple directories. |
Specify the correct one using |
|
|
The |
Enter the share path in the database. |
|
|
The share folder has not been created. |
Create the share using |
|
|
A folder with the same UUID already exists in the destination share. |
Check this folder and manually remove it if it is a leftover, then run it again. |
|
|
The two shares are on separate disks or volumes: renaming is not possible. |
Run the |
|
|
Insufficient permissions on the folder or files. |
Run the script as |
|
|
The package was modified while the script was running. |
Everything is automatically rolled back: rerun the script. |
|
|
Interrupted by Ctrl+C or input ended before confirmation. |
Run the script again. |
To create a new share before moving packages to it, see the documentation for the create_pkgs_share.py script.