Skip to main content

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.json and xmppdeploy.json files: the localisation_server and previous_localisation_server fields are set to the name of the destination share.
  • The pkgs database: the conf_json column in the packages table is updated, as is the pkgs_share_id column 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 pymysql module is provided by the python3-pymysql package.
  • Database access: the SQL account must have read access to pkgs.pkgs_shares and read/write access to pkgs.packages. By default, the script uses the connection information from the [database] section of the /etc/mmc/plugins/xmppmaster.ini.local file. 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; use create_pkgs_share.py if 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 its conf.json and xmppdeploy.json files must contain a valid localisation_server field.

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_shares along with the number of packages in each. The winupdates and winupdatesmajor shares 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 with o, 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

With --from-share, the script stops if the package is not in that share: this is a useful safety measure in an automated script.

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 of pkgs.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 to previous_localisation_server. new (default) sets it to the destination share; old sets it to the old share.

Package directory:

  • --packages-root: package root (/var/lib/pulse2/packages by default). The script searches for the directory in the share_path of 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 (otherwise localhost and 3306 for the host and port).
  • --db-password: SQL password. Prefer the PKGS_DB_PASSWORD environment 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).

  1. Backups: the two JSON files and the current value of conf_json are copied to the backup/ folder.
  2. Database: in the pkgs.packages table, the conf_json column is updated with the new values, and the pkgs_share_id column 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.
  3. JSON files: conf.json and xmppdeploy.json are 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.
  4. 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.
  5. 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).
  6. 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 (the name column in pkgs.pkgs_shares).
  • previous_localisation_server: the name of the default destination share, or the old share if --previous-share old is 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 destination reminds you of this at the end.
  • The package contents: the package’s installation files remain unchanged.
  • Other share columns: if pkgs.packages contains 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.
  • winupdates and winupdatesmajor shares: 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-share or --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 manual mv.
  • Folder outside any known share: if the package folder is not in the share_path of 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_id column missing: the script ignores it and modifies only conf_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 of conf.json before modification.
  • <uuid>_<YYYYMMDD_HHMMSS>_xmppdeploy.json: a copy of xmppdeploy.json before modification.
  • <uuid>_<YYYYMMDD_HHMMSS>_conf_json.sql: an UPDATE query that restores conf_json (and pkgs_share_id, if it exists) to their previous values. At the top of the file, comments provide the mv and ln -sfn commands 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:

  1. Put the folder back in place (and the symbolic link, if there was one), using the commands commented out at the beginning of the .sql file:
    mv /var/lib/pulse2/packages/sharing/clients/<uuid> /var/lib/pulse2/packages/sharing/global/<uuid>
  2. Restore the two JSON files from backup/. The cat command 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
  3. Restore the database by re-importing the .sql file:
    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_json and pkgs_share_id must 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

Module manquant : pymysql

The Python module pymysql is not installed.

apt install python3-pymysql

Connexion SQL impossible : ...

Incorrect SQL host, username, or password.

Check the [database] section of /etc/mmc/plugins/xmppmaster.ini.local, or specify --db-host, --db-user, and the PKGS_DB_PASSWORD variable.

[ATTENTION] ... introuvable ou illisible

The file xmppmaster.ini.local is missing or cannot be read by the account.

Run the script as root, or provide the --db-* options; the script will prompt for the password.

Package ... introuvable dans pkgs.packages.

The UUID does not exist in the database.

Check the UUID, or select it from the list in interactive mode.

--from-share ... inconnu, --to-share ... inconnu, Partage inconnu

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.

Le package est dans le partage « X », pas dans « Y ».

--from-share does not match the package's current share.

Correct or remove --from-share

... : JSON invalide, ... : clé « localisation_server » absente, ... : plusieurs valeurs différentes

One of the JSON or conf_json files is incomplete or contains contradictions.

Correct the affected file (the message indicates which one), then try again.

Dossier du package introuvable

The folder is not in any of the usual locations.

Specify --packages-root or --package-dir.

Plusieurs dossiers correspondent à ce package

The same UUID exists in multiple directories.

Specify the correct one using --package-dir.

Le partage de destination ... n'a pas de share_path

The share_path column is empty in pkgs.pkgs_shares.

Enter the share path in the database.

Le dossier du partage de destination n'existe pas

The share folder has not been created.

Create the share using create_pkgs_share.py, then run it again.

... existe déjà : le package ne peut pas être déplacé.

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.

... sont sur des systèmes de fichiers différents

The two shares are on separate disks or volumes: renaming is not possible.

Run the mv command specified in the message, then rerun the script: it will detect the folder that has already been moved and update the JSON files and the database.

... n'est pas modifiable par ce compte (lancer le script en root).

Insufficient permissions on the folder or files.

Run the script as root.

... a été modifié depuis sa lecture, La ligne pkgs.packages ... a été modifiée depuis sa lecture

The package was modified while the script was running.

Everything is automatically rolled back: rerun the script.

Interrompu, rien n'a été modifié.

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.