forked from spring-guides/gs-spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2startupscript.sh
54 lines (43 loc) · 1.23 KB
/
ec2startupscript.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
#!/bin/bash
# Update package lists
sudo apt update
# Install OpenJDK 17
sudo apt install -y openjdk-17-jdk
# Verify the installation
java -version
sudo snap install aws-cli --classic
# Variables
USER=petclinicapp
GROUP=petclinicapp
S3_BUCKET=petclinicapp
S3_FILE_PATH=petclinicapp-v1.jar
LOCAL_FILE_PATH=/home/$USER/$S3_FILE_PATH
SERVICE_FILE=/etc/systemd/system/petclinicapp.service
# Create user and group
sudo groupadd $GROUP
sudo useradd -m -g $GROUP $USER
# Set permissions for the user's home directory
sudo mkdir -p /home/$USER/
sudo chown $USER:$GROUP /home/$USER/
# Download the JAR file from S3
aws s3 cp s3://$S3_BUCKET/$S3_FILE_PATH $LOCAL_FILE_PATH
sudo chown $USER:$GROUP $LOCAL_FILE_PATH
# Create systemd service
sudo bash -c "cat > $SERVICE_FILE" <<EOL
[Unit]
Description=Spring Boot Application - Petclinic
After=syslog.target [Service]
[Service]
User=$USER
ExecStart=java -jar $LOCAL_FILE_PATH
SuccessExitStatus=143
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOL
#Reload systemd and enable the service
sudo systemctl daemon-reload
sudo systemctl enable petclinicapp.service
sudo systemctl start petclinicapp.service
echo "Setup complete. The petclinicapp service has been started."