summaryrefslogtreecommitdiffstats
path: root/bin/delproject
blob: c35c8478edf379752304e01bdb4e650cfb95f1bd (plain) (blame)
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
#!/bin/bash

dbquery() {
	mysql -s -s -e "$*" trac
}

# main function
main() {
	echo

	# project_name may not be an empty string and must be >= 3 characters
	while [[ ${#project_name} -lt 3 ]]
	do
		eask_str "a project name (min 3 chars)"
		project_name=${REPLY}
	done
	
	svn_home=${SVN_ROOT}/${project_name}
	trac_home=${TRAC_ROOT}/${project_name}
	trac_db=trac_${project_name}

	eheading "deleting apache configuration"
	eexec sed -i -e "/^# ${project_name} /,/^\$/d" ${APACHE_PROJECT_CONF}
	eexec /etc/init.d/apache2 reload

	eheading "committing changes to our git-repository in /etc"
	pushd /etc >> /dev/null
	eexec git add ${APACHE_PROJECT_CONF/\/etc\/}
	eexec git commit -m "deleted project ${project_name}" || :
	popd >> /dev/null


	random_dir=$(echo $RANDOM |md5sum |awk '{print $1}')
	backup_file=/var/www/localhost/htdocs/backup/${random_dir}/${project_name}-$(date +%Y%b%d).tar.bz2

	eheading "creating backup in ${backup_file}"

	tmpdir=$(mktemp -d)
	eexec rsync -a ${svn_home}/ ${tmpdir}/svn/
	eexec rsync -a ${trac_home}/ ${tmpdir}/trac/
	eexec mysqldump ${trac_db} -r ${tmpdir}/trac.sql
	eexec mkdir -p $(dirname ${backup_file})
	eexec tar --exclude trac.ini -cjf ${backup_file} -C ${tmpdir} .
	eexec rm -rf ${tmpdir}

	# get all member email from the database 'mysql trac'
	members=$(mysql -s -s -e \
		"select u.email from user u join member m on m.user_id = u.id join project p on p.id = m.project_id where p.project_name = '${project_name}'" \
		trac)

	eheading "making the backup available for the project members"
	einfo "sending backup link to "
	for member in ${members}
	do
		einfo "  ${member}"
		sendmail -t <<-EOF
		From: dev@spline.de
		Subject: the project ${project_name} has been deleted
		To: ${member}

		hello ${member},

		the project ${project_name} of which you have been a member has been
		deleted. for the next week you can however download a backup of the
		project using this link:

		https://dev.spline.de/backup/${random_dir}/$(basename ${backup_file})

		yours,
		    the dev.spline.de team
		EOF
	done

	eheading "deleting project files"
	eexec rm -rf ${svn_home}
	eexec rm -rf ${trac_home}
	eexec mysqladmin --force drop ${trac_db}
	eexec mysql -e "DELETE FROM project WHERE project_name = '${project_name}'" trac
	eexec wget --no-check-certificate -q -O/dev/null https://dev.spline.de/account/genusers
	eexec rm -f /var/www/localhost/users/${project_name}

	einfo
	einfo "done! success! jipii! neeeat!"
	einfo "you cannot find your project anywhere now"
	einfo
	echo
}

# go to script dir
pushd $(dirname $0) > /dev/null

# get global functions
source "./functions.sh"

# load configuration
source "./config.sh"

# exit on errors
set -e

# start main program
main "$@"

# leave script dir
popd > /dev/null