-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnUP.sh
156 lines (141 loc) · 4.76 KB
/
snUP.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Default values
LOGFILE="./pushtohpc.log"
WORKSPACE_DIR="./"
DESTINATION="[email protected]:lam"
DEFAULT_SCRIPT="test.jl"
IS_DRY_RUN=0
IS_SKIP_JULIA=0
IS_SKIP_RSYNC=0
SCRIPT_TO_RUN="$DEFAULT_SCRIPT"
# Function to print usage
print_usage() {
echo "Usage: $0 [-h] [-n] [-j] [-r] [-s script] [-d directory] [-t target]"
echo " -h Display help"
echo " -n Dry run: display what would be done"
echo " -j Skip running Julia script"
echo " -r Skip performing rsync"
echo " -s script Julia script to run (default: $DEFAULT_SCRIPT)"
echo " -d directory Directory to rsync (default: $WORKSPACE_DIR)"
echo " -t target Target for rsync (default: $DESTINATION)"
return 1
}
# Function to log messages
log_message() {
local message="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> $LOGFILE
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message"
}
# Function to handle errors
handle_error() {
local exit_code="$1"
local action="$2"
if [ $exit_code -ne 0 ]; then
log_message "Error during $action. Exiting."
exit 1
fi
log_message "$action completed successfully."
}
# Function to run Julia script
run_julia_script() {
local script="$1"
if [ $IS_DRY_RUN -eq 1 ]; then
log_message "Dry run: Would run Julia script: $script"
else
log_message "Running Julia script: $script"
julia "$script"
handle_error $? "running Julia script"
fi
}
# Function to perform rsync
perform_rsync_operation() {
local dir="$1"
local dest="$2"
if [ $IS_DRY_RUN -eq 1 ]; then
log_message "Dry run: Would perform rsync operation from $dir to $dest"
rsync -avz --info=progress2 --include 'm*/' --include '*.dat' --include 'Proc*.bin' --include '*.vts' --include '*.sh' --exclude '*' --exclude '.*' --update --dry-run "$dir" "$dest"
else
log_message "Starting rsync..."
# rsync -avz --info=progress2 --include 'm*/' --include '*.dat' --include 'Proc*.bin' --include '*.vts' --include '*.sh' --exclude '*' --exclude '.*' "$dir" "$dest"
rsync -avz --info=progress2 --include 'm*/' --include '*.dat' --include 'Proc*.bin' --include '*.vts' --include '*.sh' --exclude '*' --exclude '.*' --update "$dir" "$dest"
handle_error $? "rsync operation"
fi
}
# Function to ssh and run script
ssh_and_run_script() {
if [ $IS_DRY_RUN -eq 1 ]; then
log_message "Dry run: Would SSH into server and run script"
else
log_message "SSH into the server and running script..."
ssh -t [email protected] "cd lam && sbatch lamsa.sh"
handle_error $? "SSH or running script"
fi
}
# Function to parse command line options
parse_command_line_options() {
local _is_dry_run=$IS_DRY_RUN
local _is_skip_julia=$IS_SKIP_JULIA
local _is_skip_rsync=$IS_SKIP_RSYNC
local _script_to_run=$SCRIPT_TO_RUN
local _workspace_dir=$WORKSPACE_DIR
local _destination=$DESTINATION
while getopts "hnjrs:d:t:" opt; do
case ${opt} in
h)
print_usage
return 1
;;
n)
_is_dry_run=1
;;
j)
_is_skip_julia=1
;;
r)
_is_skip_rsync=1
;;
s)
_script_to_run="$OPTARG"
;;
d)
_workspace_dir="$OPTARG"
;;
t)
_destination="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
print_usage
return 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
print_usage
return 1
;;
esac
done
shift $((OPTIND -1))
echo "$_is_dry_run $_is_skip_julia $_is_skip_rsync $_script_to_run $_workspace_dir $_destination"
}
# Main script execution
# Main script execution
main() {
PARSE_OUTPUT=$(parse_command_line_options "$@")
if [ $? -eq 1 ]; then
return 1
fi
read IS_DRY_RUN IS_SKIP_JULIA IS_SKIP_RSYNC SCRIPT_TO_RUN WORKSPACE_DIR DESTINATION <<< "$PARSE_OUTPUT"
if [ $IS_SKIP_JULIA -eq 0 ]; then
run_julia_script "$SCRIPT_TO_RUN"
# ""
awk '/nstep_out/ {print; print " nstep_rdb = 500"; next}1' output.dat > temp && mv temp output.dat
awk '/min_cohes/ {print; print " DII_ref = 1e-18"; next}1' output.dat > temp && mv temp output.dat
fi
if [ $IS_SKIP_RSYNC -eq 0 ]; then
ssh -t [email protected] "cd lam && cleanL.sh"
perform_rsync_operation "$WORKSPACE_DIR" "$DESTINATION"
fi
ssh_and_run_script
}
main "$@"