Ticket #122: scripts-remove

File scripts-remove, 6.1 KB (added by elefthei, 11 years ago)

fixed version of scripts-remove that deletes rails dbs as documented on config/databases.yml

Line 
1#!/bin/bash
2
3if [ "$scriptsdev" != "" -a "$scriptsdev" != "dev" ]; then
4    echo "ERROR:"
5    echo "The \$scriptsdev variable is set to an invalid value."
6    echo "(The variable should not be set.)"
7    echo "Please contact scripts@mit.edu."
8fi
9
10sshrun() {
11    athrun scripts scripts-ssh "$lname" "/mit/scripts/$@" 2>/dev/null
12}
13
14checksqlpass() {
15    errors=`sshrun "sql/bin$scriptsdev/test-password"`
16    if [ "$errors" != "" ]; then
17        if [ "$1" -eq 1 ]; then
18          rm -f "$lroot/.sql/my.cnf"
19        fi
20        echo
21        echo "ERROR:"
22        printf "$2"
23        exit 1
24    fi
25}
26attach scripts sql 2>/dev/null
27
28echo "Welcome to the scripts.mit.edu uninstaller. This program will"
29echo "help you cleanly remove software that you have auto-installed."
30echo
31echo "Are you removing an installation from:"
32echo "1. Your personal Athena account"
33echo "2. A locker that you control (a club, a course, etc.)"
34echo "If you do not understand this question, you should answer 1."
35printf "Please enter either 1 or 2: "
36read whofor
37if [ "$whofor" = 1 ]; then
38    lname=$USER
39    lroot=$HOME
40elif [ "$whofor" = 2 ]; then
41    echo
42    echo "Please enter the name of the selected locker below."
43    echo "(For the locker /mit/sipb, you would enter sipb.)"
44    read lname
45    lroot="/mit/$lname"
46else
47    echo
48    echo "ERROR:"
49    echo "You must select either 1 or 2."
50    exit 1
51fi
52attach "$lname" 2>/dev/null
53
54echo
55echo "When you installed the software, you chose a URL"
56echo "that starts with http://$lname.scripts.mit.edu/"
57echo "(for software installed after March 2009) or"
58echo "http://scripts.mit.edu/~$lname/ (for software"
59echo "installed before then)."
60echo "Please enter the new-style full URL where this"
61echo "software was installed.  (This should correspond"
62echo "to a directory in /mit/$lname/web_scripts/.)"
63printf "%s" "URL: http://$lname.scripts.mit.edu/"
64read addrend
65addrend=`perl -0e 'print $ARGV[0] =~ /^([\w\/-]*[\w-])\/*$/' -- "$addrend"`
66
67if [ "$addrend" = "" ]; then
68    echo
69    echo "ERROR:"
70    echo "You must enter one or more characters after mit.edu/"
71    echo "The completed address must only contain a-z, 0-9, and /."
72    exit 1
73fi
74
75if [ ! -d "$lroot/web_scripts/$addrend" ]; then
76    echo
77    echo "ERROR:"
78    echo "The directory $lroot/web_scripts/$addrend"
79    echo "does not exist. Please make sure that this is the"
80    echo "correct installation directory, and try again, or"
81    echo "contact scripts@mit.edu for assistance."
82    exit 1
83fi
84
85sqlinfo=`sshrun "sql/bin$scriptsdev/get-password"`
86if [ "$sqlinfo" = "" ]; then
87    echo
88    echo "You have a MySQL account but you do not have a .my.cnf file."
89    echo "If you do not remember your MySQL account password, you can change it"
90    echo "at http://sql.mit.edu using MIT certificates."
91    printf "Please type your MySQL password and press [enter]: "
92    stty -echo
93    read sqlpass
94    stty echo
95    echo
96    sqlhost="sql.mit.edu"
97    sqluser=$lname
98    . "/mit/scripts/sql/bin$scriptsdev/save-password"
99    checksqlpass 1 'The MySQL password that you typed appears to be incorrect.\n'
100    echo
101    echo "OK.  Continuing with the uninstaller..."
102else
103    checksqlpass 0 'The MySQL login information in your .my.cnf file\nappears to be incorrect.\n'
104fi
105
106sqldb=`sshrun "sql/bin$scriptsdev/get-next-database" "$addrend"`
107if [ "$sqldb" != "${addrend}1" -a "$sqldb" != "$addrend" ]; then
108    echo
109    echo "ERROR:"
110    echo "The auto-uninstaller was unable to find the appropriate"
111    echo "database to drop. Please examine the installation to"
112    echo "find the database it uses, drop the database from"
113    echo "http://sql.mit.edu/, and manually remove the $addrend"
114    echo "directory (or re-run the auto-installer). Contact"
115    echo "scripts@mit.edu if you need assistance."
116    exit 1
117fi
118
119if [ -f "$lroot/web_scripts/$addrend/config/database.yml" ]; then
120    #Rails installation, need to take care of special databases
121    for sqldb in `grep -oP "(?<=database: ).+" $lroot/web_scripts/$addrend/config/database.yml`; do
122        dropped=`sshrun "sql/bin$scriptsdev/drop-database" "$sqldb"`
123        if [ "$dropped" ]; then
124            echo "The database $sqldb"
125            echo "was successfully removed."
126            attach sql 2>/dev/null
127            if [ -f "/mit/sql/backup/$lname/$sqldb.sql.gz" ]; then
128                echo "A one-day-old backup of your SQL database is"
129                echo "available in /mit/sql/backup/$lname".
130            fi
131        else
132            echo "ERROR:"
133            echo "The database $sqldb"
134            echo "could not be automatically removed. You can"
135            echo "try removing it from http://sql.mit.edu/,"
136            echo "or you can contact sql@mit.edu for assistance."
137            exit 1
138        fi
139    done
140fi
141
142echo "Removing files. Please wait..."
143echo "(This may take several seconds for large software.)"
144if rm -rf "$lroot/web_scripts/$addrend"; then
145    echo "The directory $lroot/web_scripts/$addrend"
146    echo "was successfully removed."
147    if [ -d "$lroot/OldFiles/web_scripts/$addrend" ]; then
148        echo "A one-day-old backup of the installation is"
149        echo "available from $lroot/OldFiles/web_scripts/$addrend".
150    fi
151else
152    echo "ERROR:"
153    echo "The directory $lroot/web_scripts/$addrend"
154    echo "could not be removed. Please ensure that you have"
155    echo "access to this directory and try again, or"
156    echo "contact scripts@mit.edu for assistance."
157    exit 1
158fi
159
160echo
161if [ "$sqldb" = "${addrend}1" ]; then
162    sqldb="$lname+$addrend"
163    dropped=`sshrun "sql/bin$scriptsdev/drop-database" "$sqldb"`
164    if [ "$dropped" ]; then
165        echo "The database $sqldb"
166        echo "was successfully removed."
167        attach sql 2>/dev/null
168        if [ -f "/mit/sql/backup/$lname/$sqldb.sql.gz" ]; then
169            echo "A one-day-old backup of your SQL database is"
170            echo "available in /mit/sql/backup/$lname".
171        fi
172    else
173        echo "ERROR:"
174        echo "The database $lname+$addrend"
175        echo "could not be automatically removed. You can"
176        echo "try removing it from http://sql.mit.edu/,"
177        echo "or you can contact sql@mit.edu for assistance."
178        exit 1
179    fi
180fi
181echo
182echo "The installation in http://$lname.scripts.mit.edu/$addrend"
183echo "has been successfully uninstalled."