-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgive
More file actions
executable file
·175 lines (133 loc) · 4.11 KB
/
Copy pathgive
File metadata and controls
executable file
·175 lines (133 loc) · 4.11 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
RED='\033[0;31m'
BRED='\033[1;91m'
GREEN='\033[0;92m'
BGREEN='\033[1;92m'
NC='\033[0m'
# Print usage if no arguments
if [ $# = 0 ] || [ "$1" = "help" ]; then
echo "Description"
echo "\tGive is a shell script that allows give commands provided by"
echo "\tCSE run on other linux machines."
echo -n "\n\tGive will copy selected files to CSE servers with SFTP to\n"
echo "\t~/Documents/COURSECODE/, then will either run autotests if needed,"
echo -n "\tor the give command to submit.\n\n"
echo -n "\tGive requires a SSH key to be set to connect to CSE servers\n\n\n"
echo "Usage:"
echo "\tgive zId z######"
echo -n "\t\t Set zId to z######\n\n"
echo "\tgive cs#### TASKNAME [FILE.EXT]..."
echo "\t\t SFTPs files, runs autotests and give on CSE servers"
exit 0
fi
# Check if user is trying to save zId
if [ "$1" = 'zId' ]; then
if [ $# = 2 ] && echo "$2" | grep -Eq '^z[0-9]{7}$'; then
echo $2 >| "$HOME/give/zId.txt"
echo -e "${GREEN}$2 saved${NC}"
exit 0
else
if [ "${2%"${2#?}"}" != 'z' ]; then
echo "${RED}zID must start with 'z'${NC}"
elif ! echo "$2" | grep -Eq '^z[0-9]{7}$'; then
echo "${RED}zId must contain 7 digits${NC}"
else
echo "${RED}Invalid zId${NC}"
fi
exit 1
fi
fi
if [ $# -lt 3 ]; then
echo "${RED}Insufficient arguments provided${NC}"
exit 1
fi
# Check if zId.txt contains a valid zId
if [ ! -s "$HOME/give/zId.txt" ]; then
echo "Set your zId with 'give zId z000000'"
exit 1
fi
zId=`cat $HOME/give/zId.txt`
host="login$(expr substr "$zId" ${#zId} 1).cse.unsw.edu.au"
# Check if credentials are valid
ssh -o BatchMode=yes -o ConnectTimeout=5 "$zId@$host" "echo 2>&1" >/dev/null
if [ $? -eq 0 ]; then
echo "${GREEN}SSH connection successful${NC}"
else
echo "${RED}SSH connection requires public key crypto.${NC}"
exit 1
fi
remoteHome=$(ssh $zId@$host 'echo $HOME')
giveCommand="$@"
course="$1"
courseID=$(echo "$course" | sed 's/cs//')
task="$2"
shift
shift
filenames="$@"
oldIFS=$IFS
IFS=' '
filenameArray=$filenames
IFS=$oldIFS
for value in $filenameArray; do
if [ ! -f "$value" ]; then
echo "$value not found"
exit 1
fi
done
upload=$(cat <<EOF
cd /import/reed/2/$zId/Documents/$course/
EOF
)
for value in $filenameArray; do
upload=$upload"\nput $value"
done
autotest=$(cat <<EOF
cd $remoteHome/Documents/$course
$courseID autotest $task
EOF
)
# SFTP code to CSE
tmpfile=$(mktemp)
echo "$upload" > "$tmpfile"
sftp -b "$tmpfile" "$zId@$host" > /dev/null 2>&1
# Create folder if missing
if [ $? = 1 ]; then
ssh "$zId@$host" "mkdir $remoteHome/Documents/$course"
sftp -b "$tmpfile" "$zId@$host" > /dev/null 2>&1
if [ $? = 0 ]; then
echo "${GREEN}File transfer successful${NC}"
else
echo "${RED}An error occurred${NC}"
exit 1
fi
else
echo "${GREEN}File transfer successful${NC}"
fi
if [ $courseID = "1521" ] || [ $courseID = "1511" ]; then
echo "----------------------------------------"
echo " Beginning autotest "
echo -n "----------------------------------------\n\n"
ssh -t "$zId@$host" "$autotest"
if [ ! $? = 0 ]; then
echo -n "${BRED}\nTests failed. Continue to submit? (Y/n): ${NC}"
read submit
if [ ! $submit = 'y' ] && [ ! $submit = 'Y' ]; then
echo -n "${RED}\n----------------------------------------\n"
echo " ${BRED}Submission aborted${NC}"
echo -n "${RED}----------------------------------------\n${NC}"
exit 0
fi
fi
fi
echo -n "\n----------------------------------------\n"
echo " Beginning give"
echo -n "----------------------------------------\n\n"
ssh -t "$zId@$host" "cd $remoteHome/Documents/$course; echo -n "yes\nyes" | give $giveCommand"
if [ ! $? = 0 ]; then
echo "${RED}An error occurred${NC}"
exit 1
else
echo -n "${GREEN}\n----------------------------------------\n"
echo " ${BGREEN}Submission successful${GREEN}"
echo -n "----------------------------------------\n${NC}"
fi