-
Notifications
You must be signed in to change notification settings - Fork 1
/
run
executable file
·100 lines (85 loc) · 2.6 KB
/
run
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
#!/bin/bash
# VARS
# ============================================
DB_USER=argos_user
DEV_DB=argos_dev
TEST_DB=argos_test
EVAL_DB=argos_eval
DEFAULT_PW=password
# FUNCS
# ============================================
function db_drop {
if [[ ! "$OSTYPE" =~ ^darwin ]]
then
# On Ubuntu, need to run these commands as the postgres user.
PREFIX='sudo -u postgres'
fi
for DB_NAME in $DEV_DB $TEST_DB $EVAL_DB
do
if [[ `$PREFIX psql -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'"` == 1 ]]
then
echo -e "Dropping database $(tput setaf 3)$DB_NAME$(tput sgr0)..."
$PREFIX dropdb $DB_NAME
else
echo -e "Database $(tput setaf 3)$DB_NAME$(tput sgr0) doesn't exist, moving on..."
fi
done
}
function db_create {
if [[ ! "$OSTYPE" =~ ^darwin ]]
then
# On Ubuntu, need to run these commands as the postgres user.
PREFIX='sudo -u postgres'
fi
# Create user if it doesn't exist.
user_exists=`$PREFIX psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'"`
if [[ $user_exists != 1 ]]
then
echo -e "Creating database user $(tput setaf 3)$DB_USER$(tput sgr0)..."
$PREFIX psql postgres -c "CREATE USER $DB_USER PASSWORD '$DEFAULT_PW' NOSUPERUSER CREATEDB NOCREATEROLE INHERIT LOGIN;"
else
echo -e "Database user $(tput setaf 3)$DB_USER$(tput sgr0) already exists, moving on..."
fi
# Create databases if they don't exist.
for DB_NAME in $DEV_DB $TEST_DB $EVAL_DB
do
if [[ `$PREFIX psql -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'"` != 1 ]]
then
echo -e "Creating database $(tput setaf 3)$DB_NAME$(tput sgr0)..."
$PREFIX createdb -U postgres -E utf8 -O $DB_USER $DB_NAME -T template0
python -c "from argos.datastore import db; db.create_all()"
else
echo -e "Database $(tput setaf 3)$DB_NAME$(tput sgr0) already exists, moving on..."
fi
done
}
# CMDS
# ============================================
if [ -z $1 ]
then
echo -e "$(tput setaf 3)Tell me what to do...$(tput sgr0)"
elif [ $1 == 'tests' ]
then
shift # to pass the rest of the args
nosetests $@ --logging-clear-handlers
elif [ $1 == 'doc' ]
then
echo -e "\n\n\n"
echo -e "$(tput setaf 3)Building documentation...$(tput sgr0)"
echo -e "\n\n\n"
source ~/env/argos/bin/activate
cd doc
make clean
make html
cd ..
elif [ $1 == 'db:create' ]
then
db_create
elif [ $1 == 'db:drop' ]
then
db_drop
elif [ $1 == 'db:reset' ]
then
db_drop
db_create
fi