build.sh
· 3.8 KiB · Bash
Raw
# Runs on each web machine.
#!/bin/bash
sudo apt install jq dotnet7 -y
echo "Cleaning up..."
rm -rf /home/anduin/build
mkdir -p /home/anduin/build
echo "Downloading source code..."
wget https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/archive/master/moongladepure-master.tar.gz -O /home/anduin/build/moongladepure-master.tar.gz
echo "Extracting source code..."
tar -zxf /home/anduin/build/moongladepure-master.tar.gz -C /home/anduin/build
echo "Building source code..."
dotnet 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
echo 'Installing fonts...'
sudo mkdir /usr/share/fonts
sudo wget https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/OpenSans-Regular.ttf -O /usr/share/fonts/OpenSans-Regular.ttf
sudo chown www-data:www-data /usr/share/fonts/OpenSans-Regular.ttf
curl https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/tenants --output /tmp/tenants.txt
while read -r tenant port
do
echo "Building tenant $tenant..."
echo "His port is $port..."
echo 'Stopping service...'
sudo systemctl stop $tenant
echo 'Creating tenant folder...'
sudo mkdir -p /opt/apps/$tenant > /dev/null
echo 'Copying files...'
sudo cp /home/anduin/build/bin/* /opt/apps/$tenant/ -r
echo 'Setting permissions...'
sudo chown -R www-data:www-data /opt/apps/$tenant
echo 'Creating appsettings.json...'
sudo -u www-data cp /opt/apps/$tenant/appsettings.json /opt/apps/$tenant/appsettings.Production.json
echo 'Setting appsettings.json...'
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
sudo -u www-data mv /tmp/tmp.json /opt/apps/$tenant/appsettings.Production.json
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
sudo -u www-data mv /tmp/tmp.json /opt/apps/$tenant/appsettings.Production.json
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
sudo -u www-data mv /tmp/tmp.json /opt/apps/$tenant/appsettings.Production.json
echo 'Creating service...'
echo "[Unit]
Description=$tenant Service
After=network.target
Wants=network.target
[Service]
Type=simple
User=www-data
ExecStart=/usr/bin/dotnet /opt/apps/$tenant/MoongladePure.Web.dll --urls=http://0.0.0.0:$port/
WorkingDirectory=/opt/apps/$tenant
Restart=always
RestartSec=10
SyslogIdentifier=$tenant
KillSignal=SIGINT
Environment=\"ASPNETCORE_ENVIRONMENT=Production\"
Environment=\"DOTNET_PRINT_TELEMETRY_MESSAGE=false\"
Environment=\"DOTNET_CLI_TELEMETRY_OPTOUT=1\"
Environment=\"ASPNETCORE_FORWARDEDHEADERS_ENABLED=true\"
[Install]
WantedBy=multi-user.target" | sudo tee /etc/systemd/system/$tenant.service > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable $tenant
sudo systemctl start $tenant
done < /tmp/tenants.txt
# Runs on each DB machine:
curl https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/tenants --output /tmp/tenants.txt
while read -r tenant port
do
echo "Creating database $tenant"
sudo mysql -u root --password=mynewpassword -e "CREATE DATABASE $tenant;" > /dev/null 2>&1
echo "Granting permissions on $tenant to user moongladepure"
sudo mysql -u root --password=mynewpassword -e "GRANT ALL PRIVILEGES ON $tenant.* TO 'moongladepure'@'%';"
sudo mkdir /mnt/Store/$tenant
sudo chmod 666 /mnt/Store/$tenant
sudo chown -R nobody:nogroup /mnt/Store/$tenant
done < /tmp/tenants.txt
1 | # Runs on each web machine. |
2 | #!/bin/bash |
3 | sudo apt install jq dotnet7 -y |
4 | |
5 | echo "Cleaning up..." |
6 | rm -rf /home/anduin/build |
7 | mkdir -p /home/anduin/build |
8 | |
9 | echo "Downloading source code..." |
10 | wget https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/archive/master/moongladepure-master.tar.gz -O /home/anduin/build/moongladepure-master.tar.gz |
11 | |
12 | echo "Extracting source code..." |
13 | tar -zxf /home/anduin/build/moongladepure-master.tar.gz -C /home/anduin/build |
14 | |
15 | echo "Building source code..." |
16 | dotnet 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 | |
18 | echo 'Installing fonts...' |
19 | sudo mkdir /usr/share/fonts |
20 | sudo wget https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/OpenSans-Regular.ttf -O /usr/share/fonts/OpenSans-Regular.ttf |
21 | sudo chown www-data:www-data /usr/share/fonts/OpenSans-Regular.ttf |
22 | |
23 | curl https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/tenants --output /tmp/tenants.txt |
24 | while read -r tenant port |
25 | do |
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] |
54 | Description=$tenant Service |
55 | After=network.target |
56 | Wants=network.target |
57 | |
58 | [Service] |
59 | Type=simple |
60 | User=www-data |
61 | ExecStart=/usr/bin/dotnet /opt/apps/$tenant/MoongladePure.Web.dll --urls=http://0.0.0.0:$port/ |
62 | WorkingDirectory=/opt/apps/$tenant |
63 | Restart=always |
64 | RestartSec=10 |
65 | SyslogIdentifier=$tenant |
66 | KillSignal=SIGINT |
67 | Environment=\"ASPNETCORE_ENVIRONMENT=Production\" |
68 | Environment=\"DOTNET_PRINT_TELEMETRY_MESSAGE=false\" |
69 | Environment=\"DOTNET_CLI_TELEMETRY_OPTOUT=1\" |
70 | Environment=\"ASPNETCORE_FORWARDEDHEADERS_ENABLED=true\" |
71 | |
72 | [Install] |
73 | WantedBy=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 | |
79 | done < /tmp/tenants.txt |
80 | |
81 | # Runs on each DB machine: |
82 | curl https://gitlab.aiursoft.cn/aiursoft/moongladepure/-/raw/master/assets/tenants --output /tmp/tenants.txt |
83 | while read -r tenant port |
84 | do |
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 | |
96 | done < /tmp/tenants.txt |