Sandbox: Synchronization Script

Overview

This script is designed to synchronize project files from a local directory to a remote server using rsync. Also, it executes a remote script after syncing. It can be useful for developers to quickly transfer data from their own computer to the sandbox to test changes.
The script can be downloaded from the GitLab repo.

Requirements

  • Python 3.x

  • SSH access to the remote server

  • rsync installed on both local and remote machines

Configuration

The script relies on a configuration file (config.json) to specify project details. Below is an example of the configuration structure:

{
    "config": {
        "sandbox_host": "test.sandbox.rockengroup.com",
        "user": "rockenadmin",
        "ssh_port": 22,
        "remote_script": "rocken_deploy"
    },
    "projects": {
        "api": {
            "repo_url": "https://api.test.sandbox.rockengroup.com",
            "local_dir": "path_to_api_source",
            "remote_dir": "/home/rockenadmin/rocken/api",
            "excludes": [""]
        }
    }
}

The config section contains global settings for synchronization. The projects section defines individual projects that can be synchronized. Each project has its own configuration.

Configuration Fields:

Some variables that require review:

  • sandbox_host: Remote sandbox DNS address. Typically, the hostname follows the pattern of including the username along with sandbox.rockengroup.com, such as ivan.sandbox.rockengroup.com or viktoriia.sandbox.rockengroup.com.

  • projects: Section of projects with their sync settings.

  • local_dir: Path to the local directory.

  • excludes: List of files or directories to exclude from syncing. Empty by default.

Some defaults (probably does not require intervention):

  • user: Username for SSH authentication. Default: root. Do not change it.

  • ssh_port: SSH port number. Default: 22. Do not change it.

  • remote_script: Remote script to run remotely after syncing. Default: rocken_deploy. Do not change it.

  • remote_dir: Destination directory on the remote server. Do not change it.

  • repo_url: Url of the project in the sandbox. Not used

Installation

  1. Copy the example configuration file before installation:

    cp config.json.example config.json
  2. Set the correct local directory path for necessary projects in the config.json file.

  3. Define the DNS address of the sandbox in the config.json file.

  4. Install the script as a system command:

    • First, ensure the script is executable:

      chmod +x rocken_sync.py
    • Then, run the script with the install argument:

      sudo python3 rocken_sync.py install

    This will set the full path to the config.json file in the CONFIG_PATH variable in the script itself, and display an appropriate message about that.

    Important: After the install step, please do not change the location of the config.json file, otherwise, it will cause an error when running the script.

Important: After the install step, please do not change the location of the config.json file, otherwise, it will cause an error when running the script.

  1. After the installation, you can call the script directly from the terminal as rocken_sync.

Usage

To run the script with a project name, use the following command:

rocken_sync <project_name>

For example, to sync the api project:

rocken_sync api

Optional argument:

--config <path_to_config>

Allows specifying a custom configuration file.

Script Logic

  • Load Configuration: Reads config.json to retrieve project details.

  • Validate Input: Ensures the specified project exists and that required fields are set.

  • Check Local Directory: Verifies that the local directory exists before proceeding.

  • Sync Files via Rsync: Transfers files using rsync with --delete to remove outdated files.

  • Execute Remote Script: Runs the deployment script via SSH to rebuild project.

  • Handle Errors: Catches and displays errors if any commands fail.

Comments

Leave a Reply