-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-from-package.sh
executable file
·189 lines (148 loc) · 5.41 KB
/
install-from-package.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
#!/bin/bash
LOGFILE="/var/log/ckan/install.log"
# Load environment variables
if [ -f .env ]; then
source .env
else
echo "Error: .env file not found, please set this up first as per the README"
exit 1
fi
set -e
check_env_vars() {
required_vars=("CKAN_DB_PASSWORD" "CKAN_SITE_URL" "CKAN_SYSADMIN_NAME")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "Error: $var is not set in .env file"
exit 1
fi
done
}
is_installed() {
dpkg -l | grep -q "$1"
}
install_dependencies() {
echo "Updating package list and installing dependencies..."
sudo apt update
sudo apt install -y libpq5 redis-server nginx supervisor postgresql wget
}
install_ckan() {
echo "Checking if CKAN package is already downloaded..."
if [ ! -f python-ckan_2.11-jammy_amd64.deb ]; then
echo "Downloading CKAN package..."
wget https://packaging.ckan.org/python-ckan_2.11-jammy_amd64.deb
else
echo "CKAN package already exists. Skipping download."
fi
if dpkg -l | grep -q python-ckan; then
echo "CKAN is already installed. Uninstalling it first."
sudo dpkg -r python-ckan
fi
echo "Installing CKAN package..."
sudo dpkg -i python-ckan_2.11-jammy_amd64.deb
}
setup_postgresql() {
echo "Setting up PostgreSQL database..."
echo "Dropping CKAN related databases..."
sudo -u postgres psql -c "DROP DATABASE IF EXISTS datastore_default;"
sudo -u postgres psql -c "DROP ROLE IF EXISTS datastore_default;"
sudo -u postgres psql -c "DROP DATABASE IF EXISTS ckan_default;"
echo "Dropping CKAN related role..."
sudo -u postgres psql -c "DROP ROLE IF EXISTS ckan_default;"
CKAN_DB_PASSWORD=$CKAN_DB_PASSWORD
sudo -u postgres psql -c "CREATE ROLE ckan_default WITH LOGIN PASSWORD '$CKAN_DB_PASSWORD';"
sudo -u postgres createdb -O ckan_default ckan_default -E utf8
}
setup_ckan_directories() {
echo "Setting up CKAN directories and permissions..."
sudo mkdir -p /var/lib/ckan/default
sudo chown www-data /var/lib/ckan/default
sudo chmod u+rwx /var/lib/ckan/default
}
update_ckan_config() {
echo "Updating CKAN configuration file..."
CKAN_INI="/etc/ckan/default/ckan.ini"
sudo sed -i "s|sqlalchemy.url = postgresql://ckan_default:pass@localhost/ckan_default|sqlalchemy.url = postgresql://ckan_default:${CKAN_DB_PASSWORD}@localhost/ckan_default|" $CKAN_INI
sudo sed -i "s|ckan.site_url = .*|ckan.site_url = ${CKAN_SITE_URL}|" $CKAN_INI
sudo sed -i 's|ckan.auth.route_after_login = dashboard.datasets|ckan.auth.route_after_login = home.index|' "$CKAN_INI"
echo "Configuration file updated."
}
initialise_solr() {
echo "Initialising Solr..."
# Check if a container named 'ckan-solr' is already running
if [ "$(sudo docker ps -q -f name=ckan-solr)" ]; then
echo "Solr is already running."
else
# Check if a container named 'ckan-solr' exists but is stopped
if [ "$(sudo docker ps -a -q -f name=ckan-solr)" ]; then
echo "Removing stopped Solr container..."
sudo docker rm ckan-solr
fi
sudo docker run --name ckan-solr -p 8983:8983 --restart unless-stopped -d ckan/ckan-solr:2.10-solr9
# Wait until the container is running
while [ -z "$(sudo docker ps -q -f name=ckan-solr)" ]; do
echo "Waiting for Solr container to start..."
sleep 2
done
echo "Solr container has started."
fi
}
initialize_ckan_db() {
echo "Initializing CKAN database..."
sudo ckan db init
}
# Fix SQL errors (optional step) this can be removed for other versions of Ubuntu
fix_sql_errors() {
echo "Fixing SQL errors..."
sudo -u postgres psql ckan_default -c "ALTER TABLE activity ADD COLUMN permission_labels TEXT[];"
}
restart_services() {
echo "Restarting Supervisor and Nginx services..."
sudo supervisorctl reload
sudo service nginx restart
}
setup_sysadmin() {
SYSADMIN_NAME=$CKAN_SYSADMIN_NAME
sudo ckan sysadmin add $SYSADMIN_NAME
}
# Add after set -e
LOGFILE="/var/log/ckan/install.log"
cleanup() {
local exit_code=$?
echo "Cleanup triggered with exit code: $exit_code"
if [ $exit_code -ne 0 ]; then
echo "[$(date)] Installation failed with exit code $exit_code" | sudo tee -a "$LOGFILE"
# Stop running services
sudo service nginx stop 2>/dev/null
sudo supervisorctl stop all 2>/dev/null
# Remove partial installations
if dpkg -l | grep -q python-ckan; then
sudo dpkg -r python-ckan
fi
# Clean up Solr container
if [ "$(sudo docker ps -q -f name=ckan-solr)" ]; then
sudo docker stop ckan-solr
sudo docker rm ckan-solr
fi
echo "Cleanup completed. Check $LOGFILE for details"
else
echo "[$(date)] Installation completed successfully" | sudo tee -a "$LOGFILE"
fi
}
trap cleanup EXIT
main() {
check_env_vars
sudo mkdir -p /var/log/ckan
install_dependencies || exit 1
install_ckan || exit 1
setup_postgresql || exit 1
setup_ckan_directories || exit 1
update_ckan_config || exit 1
initialise_solr || exit 1
sleep 10
initialize_ckan_db || exit 1
fix_sql_errors || exit 1
setup_sysadmin || exit 1
restart_services || exit 1
echo "CKAN installation and setup complete!"
}
main "$@"