-
Notifications
You must be signed in to change notification settings - Fork 13
/
hue_create_db.sh
executable file
·140 lines (118 loc) · 3.54 KB
/
hue_create_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
#!/bin/bash
set -x
PARCEL_DIR=/opt/cloudera/parcels/CDH
LOG_FILE=/var/log/hue/`basename "$0" | awk -F\. '{print $1}'`.log
DATABASE=$1
PASSWORD=$2
TYPE=$3
if [[ -z ${DBUSER} ]]
then
DBUSER="cloudera"
fi
if [[ -z ${DBPASSWORD} ]]
then
DBPASSWORD="cloudera"
fi
if [[ -z ${TYPE} ]]
then
TYPE="mysql"
fi
if [[ -z ${DATABASE} ]]
then
echo "Usage: hue_create_db.sh <database_name> <password> <dbtype-mysql-postgres>"
exit 1
fi
export HUE_DATABASE_PASSWORD=${PASSWORD}
export HUE_IGNORE_PASSWORD_SCRIPT_ERRORS=1
HUE_CONF_DIR=/tmp/hue_create_db/${DATABASE}
mkdir -p ${HUE_CONF_DIR}
if [ ! -d "/usr/lib/hadoop" ]
then
CDH_HOME=$PARCEL_DIR
else
CDH_HOME=/usr
fi
if [ -d "${CDH_HOME}/lib/hue/build/env/bin" ]
then
COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue"
else
COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue"
fi
DATABASE_DUMP=${HUE_CONF_DIR}/hue_database_dump.json
ORACLE_HOME=/opt/cloudera/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2/
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}
export CDH_HOME HUE_CONF_DIR ORACLE_HOME LD_LIBRARY_PATH COMMAND PASSWORD DATABASE
${COMMAND} dumpdata --indent 2 > ${HUE_CONF_DIR}/hue_database_dump.json
if [[ ${TYPE} =~ .*mysql.* ]]
then
cat > ${HUE_CONF_DIR}/hue.ini << EOF
[desktop]
[[database]]
#engine=sqlite3
#name=/var/lib/hue/desktop.db
engine=mysql
host=`hostname`
port=3306
user=${DATABASE}
password=${PASSWORD}
name=${DATABASE}
EOF
cat > ${HUE_CONF_DIR}/create.sql << EOF
drop database if exists ${DATABASE};
create database ${DATABASE};
grant all on *.* to '${DATABASE}'@'%' identified by '${PASSWORD}';
EOF
mysql -uroot -p${PASSWORD} < ${HUE_CONF_DIR}/create.sql
elif [[ ${TYPE} =~ .*postgres.* ]]
then
yum -y install postgresql-server
chkconfig postgresql on
if [[ ! -f /var/lib/pgsql/data/postgresql.conf ]]
then
service postgresql initdb
fi
CHECK_HBA=$(grep ${DATABASE} /var/lib/pgsql/data/pg_hba.conf)
if [[ -z ${CHECK_HBA} ]]
then
echo "host ${DATABASE} ${DATABASE} 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
fi
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '0.0.0.0'/g" /var/lib/pgsql/data/postgresql.conf
service postgresql restart
sudo -u postgres /bin/bash -c psql -U postgres << EOF
create database ${DATABASE};
\c ${DATABASE};
create user ${DATABASE} with password '${PASSWORD}';
grant all privileges on database ${DATABASE} to ${DATABASE};
\q
EOF
cat > ${HUE_CONF_DIR}/hue.ini << EOF
[desktop]
[[database]]
#engine=sqlite3
#name=/var/lib/hue/desktop.db
engine=postgresql_psycopg2
host=`hostname`
port=5432
user=${DATABASE}
password=${PASSWORD}
name=${DATABASE}
EOF
fi
${COMMAND} syncdb --noinput
${COMMAND} migrate --merge
if [[ ${TYPE} =~ .*mysql.* ]]
then
CONSTRAINT_ID=$(mysql -uroot -p${PASSWORD} ${DATABASE} -e "show create table auth_permission" | grep content_type_id_refs_id | awk -Fid_ '{print $3}' | awk -F\` '{print $1}')
cat > ${HUE_CONF_DIR}/prepare.sql << EOF
ALTER TABLE auth_permission DROP FOREIGN KEY content_type_id_refs_id_${CONSTRAINT_ID};
delete from django_content_type;
EOF
mysql -uroot -p${PASSWORD} ${DATABASE} < ${HUE_CONF_DIR}/prepare.sql
elif [[ ${TYPE} =~ .*postgres.* ]]
then
CONSTRAINT_ID=$(PGPASSWORD=${PASSWORD} psql -h `hostname` -U ${DATABASE} -d ${DATABASE} -c '\d auth_permission;' | grep content_type_id_refs_id | awk -Fid_ '{print $3}' | awk -F\" '{print $1}')
#PGPASSWORD=${PASSWORD} psql -h `hostname` -U ${DATABASE} -d ${DATABASE} << EOF
#ALTER TABLE auth_permission DROP CONSTRAINT content_type_id_refs_id_${CONSTRAINT_ID};
#TRUNCATE django_content_type CASCADE;
#EOF
fi