Last active 1703076495

Implement multitenant moonglade pure

build.sh Raw
1# Runs on each web machine.
2#!/bin/bash
3sudo apt install jq dotnet7 -y
4
5echo "Cleaning up..."
6rm -rf /home/anduin/build
7mkdir -p /home/anduin/build
8
9echo "Downloading source code..."
10wget https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/archive/master/moongladepure-master.tar.gz -O /home/anduin/build/moongladepure-master.tar.gz
11
12echo "Extracting source code..."
13tar -zxf /home/anduin/build/moongladepure-master.tar.gz -C /home/anduin/build
14
15echo "Building source code..."
16dotnet publish -c Release -o /home/anduin/build/bin -r linux-x64 --no-self-contained /home/anduin/build/moongladepure-master/src/Moonglade.Web/MoongladePure.Web.csproj
17
18echo 'Installing fonts...'
19sudo mkdir /usr/share/fonts
20sudo wget https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/OpenSans-Regular.ttf -O /usr/share/fonts/OpenSans-Regular.ttf
21sudo chown www-data:www-data /usr/share/fonts/OpenSans-Regular.ttf
22
23curl https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/tenants --output /tmp/tenants.txt
24while read -r tenant port
25do
26 echo "Building tenant $tenant..."
27 echo "His port is $port..."
28
29 echo 'Stopping service...'
30 sudo systemctl stop $tenant
31
32 echo 'Creating tenant folder...'
33 sudo mkdir -p /opt/apps/$tenant > /dev/null
34
35 echo 'Copying files...'
36 sudo cp /home/anduin/build/bin/* /opt/apps/$tenant/ -r
37
38 echo 'Setting permissions...'
39 sudo chown -R www-data:www-data /opt/apps/$tenant
40
41 echo 'Creating appsettings.json...'
42 sudo -u www-data cp /opt/apps/$tenant/appsettings.json /opt/apps/$tenant/appsettings.Production.json
43
44 echo 'Setting appsettings.json...'
45 sudo -u www-data jq --arg tenant "$tenant" '.ConnectionStrings.MoongladeDatabase = "Server=moongladedb;Port=3306;Database=" + $tenant + ";uid=moongladepure;Password=DATABASE_PASSWORD@1234;"' /opt/apps/$tenant/appsettings.Production.json | sudo -u www-data tee /tmp/tmp.json > /dev/null
46 sudo -u www-data mv /tmp/tmp.json /opt/apps/$tenant/appsettings.Production.json
47 sudo -u www-data jq '.ImageStorage.FileSystemPath = "/mnt/Store/'$tenant'"' /opt/apps/$tenant/appsettings.Production.json | sudo -u www-data tee /tmp/tmp.json > /dev/null
48 sudo -u www-data mv /tmp/tmp.json /opt/apps/$tenant/appsettings.Production.json
49 sudo -u www-data jq '.OpenAI.Token = "OPENAI-KEY"' /opt/apps/$tenant/appsettings.Production.json | sudo -u www-data tee /tmp/tmp.json > /dev/null
50 sudo -u www-data mv /tmp/tmp.json /opt/apps/$tenant/appsettings.Production.json
51
52 echo 'Creating service...'
53 echo "[Unit]
54Description=$tenant Service
55After=network.target
56Wants=network.target
57
58[Service]
59Type=simple
60User=www-data
61ExecStart=/usr/bin/dotnet /opt/apps/$tenant/MoongladePure.Web.dll --urls=http://0.0.0.0:$port/
62WorkingDirectory=/opt/apps/$tenant
63Restart=always
64RestartSec=10
65SyslogIdentifier=$tenant
66KillSignal=SIGINT
67Environment=\"ASPNETCORE_ENVIRONMENT=Production\"
68Environment=\"DOTNET_PRINT_TELEMETRY_MESSAGE=false\"
69Environment=\"DOTNET_CLI_TELEMETRY_OPTOUT=1\"
70Environment=\"ASPNETCORE_FORWARDEDHEADERS_ENABLED=true\"
71
72[Install]
73WantedBy=multi-user.target" | sudo tee /etc/systemd/system/$tenant.service > /dev/null
74
75 sudo systemctl daemon-reload
76 sudo systemctl enable $tenant
77 sudo systemctl start $tenant
78
79done < /tmp/tenants.txt
80
81# Runs on each DB machine:
82curl https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/tenants --output /tmp/tenants.txt
83while read -r tenant port
84do
85
86 echo "Creating database $tenant"
87 sudo mysql -u root --password=mynewpassword -e "CREATE DATABASE $tenant;" > /dev/null 2>&1
88
89 echo "Granting permissions on $tenant to user moongladepure"
90 sudo mysql -u root --password=mynewpassword -e "GRANT ALL PRIVILEGES ON $tenant.* TO 'moongladepure'@'%';"
91
92 sudo mkdir /mnt/Store/$tenant
93 sudo chmod 666 /mnt/Store/$tenant
94 sudo chown -R nobody:nogroup /mnt/Store/$tenant
95
96done < /tmp/tenants.txt