install 8.62 KB
Newer Older
Spctrl's avatar
Spctrl committed
1
#!/bin/bash
2

Spctrl's avatar
Spctrl committed
3
#=================================================
Spctrl's avatar
Spctrl committed
4
# GENERIC START
Spctrl's avatar
Spctrl committed
5 6 7 8 9
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source /usr/share/yunohost/helpers
10
source _common.sh
Spctrl's avatar
Spctrl committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24

#=================================================
# MANAGE SCRIPT FAILURE
#=================================================

ynh_abort_if_errors

#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================

domain=$YNH_APP_ARG_DOMAIN
path_url="/"
server_name=$YNH_APP_ARG_INSTANCE_NAME
25
instance_user=$YNH_APP_ARG_INSTANCE_USER
26
ynh_print_OFF
27 28
instance_user_password=$YNH_APP_ARG_INSTANCE_USER_PASSWORD
instance_user_password_verify=$YNH_APP_ARG_INSTANCE_USER_PASSWORD_VERIFY
29
ynh_print_ON
30 31 32
instance_user_mail=$YNH_APP_ARG_INSTANCE_USER_MAIL
email=$instance_user_mail
is_public=$YNH_APP_ARG_IS_PUBLIC
33
allow_scopes=$YNH_APP_ARG_ALLOW_SCOPES
Spctrl's avatar
Spctrl committed
34
app=$YNH_APP_INSTANCE_NAME
35
db_name=$app
Spctrl's avatar
Spctrl committed
36

37
#==================================================
Spctrl's avatar
Spctrl committed
38
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
39
#==================================================
Spctrl's avatar
Spctrl committed
40

41
ynh_print_OFF
42
if [ $instance_user_password != $instance_user_password_verify ]
43
 then ynh_die "Passwords didn't match"
44
fi
45
ynh_print_ON
46

Spctrl's avatar
Spctrl committed
47 48 49 50 51 52 53 54 55 56 57
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"

# Normalize the url path syntax
path_url=$(ynh_normalize_url_path $path_url)

# Check web path availability
ynh_webpath_available $domain $path_url
# Register (book) web path
ynh_webpath_register $app $domain $path_url

58 59 60
# Find a free port
port=$(ynh_find_port 4000)

Spctrl's avatar
Spctrl committed
61 62 63 64 65 66
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================

ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app path $path_url
67
ynh_app_setting_set $app port $port
Spctrl's avatar
Spctrl committed
68
ynh_app_setting_set $app is_public $is_public
69
ynh_app_setting_set $app allow_scopes $allow_scopes
Spctrl's avatar
Spctrl committed
70
ynh_app_setting_set $app final_path $final_path
Spctrl's avatar
Spctrl committed
71
ynh_app_setting_set $app db_name $db_name
Spctrl's avatar
Spctrl committed
72 73 74 75 76 77 78

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# INSTALL DEPENDENCIES
#=================================================

79 80
ynh_print_info "Installing dependencies"

81 82 83
# In case https capability for apt insn't installed yet
apt-get install --yes apt-transport-https

Spctrl's avatar
Spctrl committed
84 85 86 87
if ! [ -e /etc/apt/sources.list.d/erlang-solutions.list ]
then
 mkdir --parents /etc/apt/sources.list.d/
 touch /etc/apt/sources.list.d/erlang-solutions.list
88
 echo "deb https://packages.erlang-solutions.com/debian stretch contrib" > /etc/apt/sources.list.d/erlang-solutions.list
Spctrl's avatar
Spctrl committed
89 90 91 92 93
 wget https://packages.erlang-solutions.com/debian/erlang_solutions.asc
 apt-key add erlang_solutions.asc
 rm erlang_solutions.asc
fi
apt-get update
94
ynh_install_app_dependencies git build-essential postgresql postgresql-contrib elixir erlang-dev erlang-parsetools erlang-xmerl erlang-tools
Spctrl's avatar
Spctrl committed
95 96 97 98

#=================================================
# CREATE DEDICATED USER
#=================================================
99

Spctrl's avatar
Spctrl committed
100
mkdir --parents $final_path
Spctrl's avatar
Spctrl committed
101 102 103
useradd $app --home-dir=$final_path

#=================================================
Spctrl's avatar
Spctrl committed
104
# DOWNLOAD AND UNPACK SOURCE
Spctrl's avatar
Spctrl committed
105
#=================================================
Spctrl's avatar
Spctrl committed
106

107
ynh_print_info "Downloading source"
108

109 110 111 112
git clone --branch 0.9.9-beta-2 https://git.pleroma.social/pleroma/pleroma/ $final_path/pleroma
# Pleroma will use tags on the master-branch. To only clone in one branch, we could do
# git clone --single-branch --branch v0.9.9 https://git.pleroma.social/pleroma/pleroma/
# And then checkout the tag
113 114 115

ynh_print_info "Building the Pleroma instance"

Spctrl's avatar
Spctrl committed
116 117
chown -R $app: $final_path

118
su - $app -c "cd $final_path/pleroma;mix local.hex --force;mix deps.get;mix local.rebar --force;mix pleroma.instance gen --domain "$domain" --instance-name "$server_name" --admin-email "$email" --dbhost localhost --dbname "$db_name" --dbuser "$app" --dbpass "$(ynh_string_random 32)";cp config/generated_config.exs config/prod.secret.exs;"
119

Spctrl's avatar
Spctrl committed
120 121 122 123
# Check that there are no unreplaced variables in the generated config files
cat $final_path/pleroma/config/generated_config.exs | grep "<%= .* %>" && ynh_die "$final_path/pleroma/config/generated_config.exs has unset variables"
cat $final_path/pleroma/config/setup_db.psql | grep "<%= .* %>" && ynh_die "$final_path/pleroma/config/setup_db.psql has unset variables"

Spctrl's avatar
Spctrl committed
124
#=================================================
125
# CREATE AND SETUP POSTGRESQL DATABASE
Spctrl's avatar
Spctrl committed
126
#=================================================
127

128 129
ynh_print_info "Setting up the database"

130
# Fail installation if database already exists
Spctrl's avatar
Spctrl committed
131
! su - postgres -c "psql -lqt" | cut -d \| -f 1 | grep -ws $db_name || ynh_die 'The database $db_name already exists'
Spctrl's avatar
Spctrl committed
132

Spctrl's avatar
Spctrl committed
133 134 135 136
chmod +x $final_path/pleroma/config/setup_db.psql
su - postgres -c "psql -f $final_path/pleroma/config/setup_db.psql"

su - $app -c "cd $final_path/pleroma;mix local.rebar --force;mix local.hex --force;MIX_ENV=prod mix ecto.migrate;"
Spctrl's avatar
Spctrl committed
137 138 139 140 141

#=================================================
# CUSTOM PLEROMA CONFIGURATION
#=================================================

142 143
ynh_print_info "Configuring the instance"

144 145 146 147
settingsfile="$final_path/pleroma/config/prod.secret.exs"
pleroma_ynh_add_setting "config :pleroma, Pleroma.Web.Endpoint" "http" "[port: $port]" $settingsfile
pleroma_ynh_add_setting "config :pleroma, :fe" "scope_options_enabled" "$(pleroma_ynh_int_to_boolstring $allow_scopes)" $settingsfile
pleroma_ynh_add_setting "config :pleroma, :instance" "registrations_open" "$(pleroma_ynh_int_to_boolstring $is_public)" $settingsfile
Spctrl's avatar
Spctrl committed
148 149

#=================================================
150
# ADD USER TO INSTANCE
Spctrl's avatar
Spctrl committed
151 152
#=================================================

153 154
ynh_print_info "Adding user to the instance"

155
# We have to set the the pasword for the postgresql DB-user first, otherwise the task fails.
Spctrl's avatar
Spctrl committed
156
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
157 158

ynh_print_OFF
159
su - $app -c "cd $final_path/pleroma; MIX_ENV=prod mix local.hex --force;MIX_ENV=prod mix pleroma.user new $instance_user $instance_user_mail --name $instance_user --bio 'admin and moderator of this instance' --password $instance_user_password --moderator --admin --assume-yes > /dev/null;"
160
ynh_print_ON
Spctrl's avatar
Spctrl committed
161 162 163 164

#=================================================
# NGINX CONFIGURATION
#=================================================
Spctrl's avatar
Spctrl committed
165

166 167
ynh_print_info "Final configurations"

Spctrl's avatar
Spctrl committed
168
cp -rf "../conf/nginx.conf" "/etc/nginx/conf.d/$domain.d/$app.conf"
169 170
ynh_replace_string "{APP}" "$app" "/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_replace_string "{PORT}" "$port" "/etc/nginx/conf.d/$domain.d/$app.conf"
Spctrl's avatar
Spctrl committed
171
cp -rf "../conf/pleroma_cache.conf" "/etc/nginx/conf.d/$app-cache.conf"
172
ynh_replace_string "{APP}" "$app" "/etc/nginx/conf.d/$app-cache.conf"
Spctrl's avatar
Spctrl committed
173 174
ynh_store_file_checksum "/etc/nginx/conf.d/$domain.d/$app.conf"

175 176 177
# TODO: Pleroma sets the headers so ynh doesn't need to do it for us
# TODO: Make this more granular (also, this seems to fail the installation)
#  ynh_replace_string "add_header" "# add_header" "/etc/nginx/conf.d/$domain.conf"
178

179 180 181
# TODO: Does the prod.secret.ext needs _enabled: true_ for media proxy to work? => I think yes
# Do we keep it at 10g? => Should be a choice
# Do we want it on by default or should it be a choice? => Should be a choice + "Home-server, not recomended (more bandwith usage). Trusted cloud infra, use it (more privacy for the users)."
182

183 184
systemctl reload nginx

Spctrl's avatar
Spctrl committed
185 186 187
#=================================================
# SETUP SYSTEMD
#=================================================
188

Spctrl's avatar
Spctrl committed
189 190
systemd_location=/usr/lib/systemd/system
mkdir --parents $systemd_location
Spctrl's avatar
Spctrl committed
191
cp ../conf/pleroma.service $systemd_location/$app.service
Spctrl's avatar
Spctrl committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
ynh_replace_string "{USER}" "$app" "$systemd_location/$app.service"
ynh_replace_string "{FINAL_PATH}" "$final_path" "$systemd_location/$app.service"
systemctl daemon-reload
systemctl enable --now $app.service

#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================

yunohost service add $app
yunohost service add postgresql

#=================================================
# SETUP SSOWAT
#=================================================

# Skip ynh login for accessing the application
ynh_app_setting_set $app skipped_uris "/"

#=================================================
Spctrl's avatar
Spctrl committed
212
# FINALIZATION
Spctrl's avatar
Spctrl committed
213
#=================================================
Spctrl's avatar
Spctrl committed
214

215
ynh_print_info "Sending love to Lain"
Spctrl's avatar
Spctrl committed
216 217 218

# Restart 'cause I always have an internal server error the first time.
sleep 10
219
systemctl reload nginx
Spctrl's avatar
Spctrl committed
220
systemctl restart $app
Spctrl's avatar
Spctrl committed
221

222
ynh_print_info "If you're not afraid of the terminal, check out https://git.pleroma.social/pleroma/pleroma/wikis/home to see what more you can do with your awesome instance!"