-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp_workflow.sh
executable file
·80 lines (67 loc) · 2.55 KB
/
exp_workflow.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
# Copyright 2024 tu-studio
# This file is licensed under the Apache License, Version 2.0.
# See the LICENSE file in the root of this project for details.
# Description: This script runs an experiment with DVC within a temporary directory copy and pushes the results to the DVC and Git remote.
# Set environment variables defined in global.env
set -o allexport
source global.env
set +o allexport
# Define DEFAULT_DIR in the host environment
export DEFAULT_DIR="$PWD"
TUSTU_TMP_DIR=tmp
# Setup a global git configuration if beeing inside a docker container
# Docker containers create a /.dockerenv file in the root directory
if [ -f /.dockerenv ]; then
if [ -f local.env ]; then
source local.env;
fi
if [ -z "$TUSTU_GIT_USERNAME" ] || [ -z "$TUSTU_GIT_EMAIL" ]; then
echo "[ERROR] Please create a local.env with the vars:";
echo "TUSTU_GIT_USERNAME=MY NAME";
echo "[email protected]";
exit 1;
fi
git config --global user.name "$TUSTU_GIT_USERNAME"
git config --global user.email "$TUSTU_GIT_EMAIL"
git config --global safe.directory "$PWD"
fi
# Create a new sub-directory in the temporary directory for the experiment
echo "Creating temporary sub-directory..." &&
# Generate a unique ID with the current timestamp, process ID, and hostname for the sub-directory
UNIQUE_ID=$(date +%s)-$$-$HOSTNAME &&
TUSTU_EXP_TMP_DIR="$TUSTU_TMP_DIR/$UNIQUE_ID" &&
mkdir -p $TUSTU_EXP_TMP_DIR &&
# Copy the necessary files to the temporary directory
echo "Copying files..." &&
{
# Add all git-tracked files
git ls-files;
if [ -f ".dvc/config.local" ]; then
echo ".dvc/config.local";
fi;
echo ".git";
} | while read file; do
# --chown flag is needed for docker to avoid permission issues
rsync -aR --chown $(id -u):$(id -g) "$file" $TUSTU_EXP_TMP_DIR;
done &&
# Change the working directory to the temporary sub-directory
cd $TUSTU_EXP_TMP_DIR &&
# Set the DVC cache directory to the shared cache located in the host directory
echo "Setting DVC cache directory..." &&
dvc cache dir $DEFAULT_DIR/.dvc/cache &&
# Pull the data from the DVC remote repository
if [ -f "data/raw.dvc" ]; then
echo "Pulling data with DVC..."
dvc pull data/raw;
fi &&
# Run the experiment with passed parameters. Runs with the default parameters if none are passed.
echo "Running experiment..." &&
dvc exp run $EXP_PARAMS &&
# Push the results to the DVC remote repository
echo "Pushing experiment..." &&
dvc exp push origin &&
# Clean up the temporary sub-directory
echo "Cleaning up..." &&
cd .. &&
rm -rf $UNIQUE_ID