-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreshare_projects.sh
executable file
·239 lines (205 loc) · 8.2 KB
/
reshare_projects.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash
# Exit immediately if a pipeline, which may consist of a single simple command, a list,
#or a compound command returns a non-zero status: If errors are not handled by user
set -e
# Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion.
#Print everything as if it were executed, after substitution and expansion is applied: Debug|log option
#set -x
#=============================================================
# HEADER
#=============================================================
#INSTITUTION:ISCIII
#CENTRE:BU-ISCIII
#AUTHOR: Sara Monzon (smonzon@isciii.es)
# Luis Chapado (lchapado@externos.isciii.es)
VERSION=0.0.2
#CREATED: 23 April 2019
#
#ACKNOWLEDGE: longops2getops.sh: https://gist.github.com/adamhotep/895cebf290e95e613c006afbffef09d7
#
#DESCRIPTION: Reshare projects script reshares a already splitted set of users sequences from a specific run.
#
#
#================================================================
# END_OF_HEADER
#================================================================
#SHORT USAGE RULES
#LONG USAGE FUNCTION
usage() {
cat << EOF
This script reshares sequences splitted in different folders for different users, sharing it with samba shares.
usage : $0 <-f file> <-r folder> -o <output_dir> [options]
Mandatory input data:
-f | Path to reshare info file.ej. /Path/to/reshare.txt
-v | version
-h | display usage message
example: ./reshare_projects.sh -f ../sanger/reshare.txt
EOF
}
#================================================================
# OPTION_PROCESSING
#================================================================
#Make sure the script is executed with arguments
if [ $# = 0 ]; then
echo "NO ARGUMENTS SUPPLIED"
usage >&2
exit 1
fi
# Error handling
error(){
local parent_lineno="$1"
local script="$2"
local message="$3"
local code="${4:-1}"
RED='\033[0;31m'
NC='\033[0m'
if [[ -n "$message" ]] ; then
echo -e "\n---------------------------------------\n"
echo -e "${RED}ERROR${NC} in Script $script on or near line ${parent_lineno}; exiting with status ${code}"
echo -e "MESSAGE:\n"
echo -e "$message"
echo -e "\n---------------------------------------\n"
else
echo -e "\n---------------------------------------\n"
echo -e "${RED}ERROR${NC} in Script $script on or near line ${parent_lineno}; exiting with status ${code}"
echo -e "\n---------------------------------------\n"
fi
#Mail admins
echo -e "Subject:Sanger script error\nError in Script $script on or near line $parent_lineno: ${message}" | sendmail -f "bioinformatica@isciii.es" -t "bioinformatica@isciii.es"
rm -rf $PROCESSED_FILE_DIRECTORY/tmp
exit "${code}"
}
mail_user_error(){
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
RED='\033[0;31m'
NC='\033[0m'
content="ERROR on or near line ${parent_lineno}: \
MESSAGE:\n \
$message"
mkdir -p $PROCESSED_FILE_DIRECTORY/tmp
sed "s/##ERROR##/$content/g" ./template_error_sanger.htm > tmp/error_mail.htm
sendmail -t < $PROCESSED_FILE_DIRECTORY/tmp/error_mail.htm || error ${LINENO} $(basename $0) "Sending error mail error."
rm -rf $PROCESSED_FILE_DIRECTORY/tmp
exit "${code}"
}
#DECLARE FLAGS AND VARIABLES
script_dir=$(dirname $(readlink -f $0))
cwd="$(pwd)"
is_verbose=false
########## Configuration settings ########
source $script_dir/sanger_configuration
export PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin"
#SET COLORS
YELLOW='\033[0;33m'
WHITE='\033[0;37m'
CYAN='\033[0;36m'
BLUE='\033[0;34m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
#PARSE VARIABLE ARGUMENTS WITH getops
#common example with letters, for long options check longopts2getopts.sh
options=":f:r:o:Vvh"
while getopts $options opt; do
case $opt in
f )
reshare_file=$OPTARG
;;
V )
is_verbose=true
log_file="/dev/stdout"
;;
h )
usage
exit 1
;;
v )
echo $VERSION
exit 1
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
usage
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
* )
echo "Unimplemented option: -$OPTARG" >&2;
exit 1
;;
esac
done
shift $((OPTIND-1))
#================================================================
# MAIN_BODY
#================================================================
function join_by { local IFS="$1"; shift; echo "$*"; }
printf "\n\n%s"
printf "${YELLOW}------------------${NC}\n"
printf "%s"
printf "${YELLOW}Starting Resharing sanger script version:${VERSION}${NC}\n"
printf "%s"
printf "${YELLOW}------------------${NC}\n\n"
date=`date +%Y%m%d`
echo "Fetching samba includes file from filesystem file server."
## Create samba shares.
if [ ! -d $TMP_SAMBA_SHARE_DIR ]; then
mkdir -p $TMP_SAMBA_SHARE_DIR
fi
scp -q $REMOTE_USER@$REMOTE_SAMBA_SERVER:$REMOTE_SAMBA_SHARE_DIR/includes.conf $TMP_SAMBA_SHARE_DIR/ || error ${LINENO} $(basename $0) "Failed fetching of samba includes file"
while read -r line ;do
run=$(echo $line | cut -d "," -f 1 )
emails=$(echo $line | cut -d "," -f 2 | sed "s/:/,/g")
user=$(echo $line | cut -d "," -f 2 | sed "s/@externos\.isciii\.es//g" | sed "s/@isciii\.es//g")
# print an error in case the comment column contains more than 1 username and it is not sepparated by ":" but space
if [[ $users == *" "* ]] ; then
mail_user_error ${LINENO} "Unable to process the sample on the line $line.There are spaces in comment field"
error ${LINENO} $(basename $0) "Unable to process the sample on the line $line.There are spaces in comment field"
continue
fi
## Change to mtime if needed!
echo "find $SAMBA_TRANSFERED_FOLDERS -mtime +$RETENTION_TIME_CONF_FILES -mtime -$RETENTION_TIME_SHARED_FOLDERS | grep -P \".*$run.*$user.*\""
echo "number_folders=$(find $SAMBA_TRANSFERED_FOLDERS -mtime +$RETENTION_TIME_CONF_FILES -mtime -$RETENTION_TIME_SHARED_FOLDERS | grep -P \".*$run.*$user.*\" | wc -l)"
number_folders=$(find $SAMBA_TRANSFERED_FOLDERS -mtime +$RETENTION_TIME_CONF_FILES -mtime -$RETENTION_TIME_SHARED_FOLDERS | grep -P ".*$run.*$user.*" | wc -l)
if [ $number_folders -gt 0 ]; then
folders=$(find $SAMBA_TRANSFERED_FOLDERS -mtime +$RETENTION_TIME_CONF_FILES -mtime -$RETENTION_TIME_SHARED_FOLDERS | grep -P ".*$run.*$user.*")
echo $folders
for folder in $folders;
do
folder=$(basename $folder)
echo "Folder $folder is accesible for users: $user"
echo "sed \"s/##FOLDER##/$folder/g\" $SAMBA_SHARE_TEMPLATE | sed \"s/##USERS##/$user/g\""
sed "s/##FOLDER##/$folder/g" $SAMBA_SHARE_TEMPLATE | sed "s/##USERS##/$user/g" > $TMP_SAMBA_SHARE_DIR/$folder".conf"
echo "include = $REMOTE_SAMBA_SHARE_DIR/${folder}.conf" >> $TMP_SAMBA_SHARE_DIR/includes.conf
echo -e "$folder\t$date\t$users" >> $script_dir/logs/reshare_samba_folders
touch $SAMBA_TRANSFERED_FOLDERS/$folder
echo "Sending email"
sed "s/##FOLDER##/$folder/g" $TEMPLATE_EMAIL | sed "s/##USERS##/$user/g" | sed "s/##MAILS##/$emails/g" | sed "s/##RUN_NAME##/$run_name/g"> $PROCESSED_FILE_DIRECTORY/tmp/mail.tmp
## Send mail to users
sendmail -t < $PROCESSED_FILE_DIRECTORY/tmp/mail.tmp
echo "mail sended"
echo "Deleting mail temp file"
rm $PROCESSED_FILE_DIRECTORY/tmp/mail.tmp
done
else
mail_user_error ${LINENO} "Unable to process the run on the line $line. The run is older than the maximum time for storage, or the run is already been shared at the moment."
error ${LINENO} $(basename $0) "Unable to process the sample on the line $line.The run is older than the maximum time for storage, or the run is already been shared at the moment."
continue
fi
done < "$reshare_file"
echo "Copying samba shares configuration to remote filesystem server"
rsync -rlv -e "ssh -q" $TMP_SAMBA_SHARE_DIR/ $REMOTE_USER@$REMOTE_SAMBA_SERVER:$REMOTE_SAMBA_SHARE_DIR/ || error ${LINENO} $(basename $0) "Shared samba config files couldn't be copied to remote filesystem server."
echo "Deleting temporal local share folder"
rm -rf $PROCESSED_FILE_DIRECTORY/tmp/*
echo "Restarting samba service"
## samba service restart
# Ubuntu
#ssh $REMOTE_USER@$REMOTE_SAMBA_SERVER 'sudo service smbd restart'
#Centos
ssh -q $REMOTE_USER@$REMOTE_SAMBA_SERVER 'sudo service smb restart'
echo "File $reshare_file process has been completed"