Skip to content

Instantly share code, notes, and snippets.

@danielduckworth
Last active January 22, 2024 05:22
Show Gist options
  • Save danielduckworth/90ae9ee64691f6f6e0789870871c5bc9 to your computer and use it in GitHub Desktop.
Save danielduckworth/90ae9ee64691f6f6e0789870871c5bc9 to your computer and use it in GitHub Desktop.
Conda env pack and move

Conda Pack n' Move

[WIP]

This shell script is used to transfer a Conda environment from one machine to another using rsync and SSH. It first checks if the correct number of arguments is provided, then installs conda-pack if it is not already installed. It then activates the specified Conda environment, packs it, transfers the tar file to the new directory using rsync, unpacks the environment via SSH on the remote machine, updates the .zshrc file, and reloads the shell.

Usage:

  1. Copy the file to ~/bin with cp condapack_n_move.sh ~/bin/condapack_n_move
  2. Make it executable with chmod +x ~/bin/condapack_n_move
cp condapack_n_move.sh ~/bin/condapack_n_move
chmod +x ~/bin/condapack_n_move
condapack-n-move mycondaenv path/from/home/on/remote user@host
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 4 ]; then
echo "Usage: $0 <env_name> <destination_path> <rsync://host:port/share/path> <remote_username@remote_address>"
exit 1
fi
# Assign arguments to variables for better readability
env_name=$1
destination_path=$2
rsync_server=$3 || "rsync://localhost:873/share/path"
remote=$4 # Format: username@remote_address
tar_file_name="${env_name}_env.tar.gz"
# Step 1: Install conda-pack
source activate base || conda activate base || conda deactivate
# If conda-pack is not installed, install it
if [ -z "$(conda env list | grep conda-pack)" ]; then
echo "Conda-pack is not installed. Installing conda-pack..."
conda install -c conda-forge conda-pack || pip install conda-pack
else
# conda-pack is installed
echo "Conda-pack is installed."
fi
# Step 2: Activate the environment
echo "Activating the Conda environment..."
source activate $env_name || conda activate $env_name
# Step 3: Pack the environment
echo "Packing the environment..."
condapack -n $env_name -o $tar_file_name
# Step 4: Transfer the tar file to the new directory
echo "Transferring the tar file..."
rsync -axz --zc=zstd --info=progress2 --info=name0 $tar_file_name $rsync_server/$tar_file_name
# Step 5: Unpack the environment via SSH on the remote machine
echo "Unpacking the environment on the remote machine..."
ssh $remote "mkdir -p $destination_path && \
tar -xzf $tar_file_name -C $destination_path && \
rm $tar_file_name && \
chmod -R 755 $destination_path"
# Step 6: Update the .zshrc or file
echo "Updating the .zshrc file..."
echo "export PATH=$destination_path/bin:$PATH" >> ~/.zshrc
# Set permissions
chmod -R 755 $destination_path
# Step 7: Reload the shell
echo "Reloading the shell..."
zsh
echo "Process completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment