-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_db.sh
executable file
·195 lines (156 loc) · 4.23 KB
/
load_db.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
#!/bin/bash
prod=true
if [ $# -gt 0 -a "$1" == -d ]; then
prod=false
shift
fi
if $prod; then
DJROOT=${DJROOT:-/var/django}
DJVIRT=${DJVIRT:-$DJROOT/virtualenv/django}
export DJANGO_SETTINGS_MODULE=datavis.settings.production
else
DJVIRT=${DJVIRT:-$HOME/virtualenvs/django}
fi
[ $VIRTUAL_ENV ] || source $DJVIRT/bin/activate
# Make sure key is set.
checkkey() {
if [ -z "$EOL_DATAVIS_SECRET_KEY" ]; then
. key.sh
fi
if [ -z "$EOL_DATAVIS_SECRET_KEY" ]; then
echo "Could not set secret key."
exit 1
fi
}
# This does not usually need to be done, so keep it as a separate operation
# from the actions which do not require sudo. This is only needed on
# production, and if the permissions do get messed up, it may need to run
# both before and after database modifications.
fixperms() {
if [ `id -u` -ne 0 ]; then
echo "Run this command as root with sudo."
exit 1
fi
echo "fixing permissions..."
set -x
# make sure git checkout can only be modified by datavis group
gitd=/var/django/ncharts
find $gitd -type d -exec chmod g+s {} \;
find $gitd -type d -exec setfacl -d -m u::rwX,g::rwX,o::rX {} \;
chmod -R g+w $gitd
chown -R datavis.datavis $gitd
# allow datavis group to write and see logs
logd=/var/log/django
chown -R datavis.datavis $logd
chmod -R g+w $logd
# Set group ID on execution (s) / setgid
libd=/var/lib/django
find $libd -type d -exec chmod g+s {} \;
# Set to inherit group write permissions via setfacl
find $libd -type d -exec setfacl -d -m u::rwX,g::rwX,o::rX {} \;
chmod -R g+w $libd
chown -R datavis.datavis $libd
set +x
}
loaddata() {
echo "loading fixtures from json files..."
for f in timezones.json projects.json platforms.json variables.json; do
echo $f
python3 manage.py loaddata $f || exit 1
done
for f in ncharts/fixtures/datasets_*.json; do
ff=${f##*/}
echo $ff
python3 manage.py loaddata $ff || exit 1
done
}
cleandata() {
echo "running full_clean on Datasets"
python3 manage.py shell << EOD
from ncharts.models import Dataset, FileDataset
from django.core.exceptions import ValidationError
print("{0} datasets".format(len(FileDataset.objects.all())))
for d in FileDataset.objects.all():
print("d.name=",d.name)
try:
d.full_clean()
except ValidationError as e:
print(e)
exit(1)
exit(0)
EOD
}
printkeys() {
# Print an ordered list of primary keys from the files on the
# command-line, usually datasets_*
if [ $# -eq 0 ]; then
echo "printkeys requires json files to extract 'pk' fields from."
exit 1
fi
fgrep -h '"pk"' "$@" | cut -d: -f 2 | sort -u -n
}
usage() {
cat <<EOF
Usage: $0 [-d] [update|full|fixperms|loaddata|cleandata]
-d Run for development rather than production.
update Load fixture updates and run full_clean on datasets.
full Use sudo to fix permissions and update database.
fixperms Just fix the permissions.
loaddata Just load fixture data.
cleandata Run the full_clean on the datasets.
printkeys file1 [file2 ...]
Extract and sort all the 'pk' primary keys, eg:
$0 printkeys datasets_*
Default operation is 'update'.
EOF
}
# The permissions should not need to be fixed, everything is forced to the
# datavis group, so sudo should no longer be needed, and the default
# operation can just be update.
[ -z "$1" ] && set update
case "$1" in
full)
if $prod ; then
# fixperms
checkkey
fi
loaddata
cleandata
# fixperms
;;
update)
# Just update the fixtures, do not fix perms.
if $prod ; then
checkkey
fi
loaddata
cleandata
;;
fixperms)
fixperms
;;
loaddata)
if $prod ; then
checkkey
fi
loaddata
;;
cleandata)
if $prod ; then
checkkey
fi
cleandata
;;
printkeys)
shift
printkeys "$@"
;;
-h)
usage
;;
*)
echo "Unrecognied argument: $1"
usage
exit 1
;;
esac