-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
165 lines (144 loc) · 5.99 KB
/
Copy pathupdate.sh
File metadata and controls
165 lines (144 loc) · 5.99 KB
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
#!/bin/bash
# .NET Website Updater for Linux
# Updates an already-deployed .NET application without changing its service configuration.
# Usage: ./update.sh <published-app-folder-or-zip> <service-name>
set -e
TEMP_EXTRACT_DIR=""
trap '[ -n "$TEMP_EXTRACT_DIR" ] && rm -rf "$TEMP_EXTRACT_DIR"' EXIT
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Help
if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]; then
echo "Usage: $(basename "$0") <published-app-folder-or-zip> <service-name>"
echo ""
echo "ARGUMENTS:"
echo " published-app-folder-or-zip Path to the .NET publish folder or a .zip of it"
echo " service-name Name of the existing systemd service to update"
echo ""
echo "NOTES:"
echo " - The service must have been deployed with deploy-dotnet first."
echo " - Port, paths and other settings are read from the existing service file."
echo " - A backup of the current deployment is kept at <deploy-path>.bak."
echo " - If the service fails to start after the update, a rollback is performed automatically."
echo ""
echo "EXAMPLES:"
echo " $(basename "$0") ~/MyApp/publish dotnet-myapp"
echo " $(basename "$0") ~/MyApp.zip dotnet-myapp"
exit 0
fi
PUBLISHED_APP_FOLDER="${1}"
SERVICE_NAME="${2}"
# Validation
if [ -z "$PUBLISHED_APP_FOLDER" ] || [ -z "$SERVICE_NAME" ]; then
echo -e "${RED}Usage: $(basename "$0") <published-app-folder-or-zip> <service-name>${NC}"
echo "Run '$(basename "$0") --help' for more information."
exit 1
fi
if [ ! -e "$PUBLISHED_APP_FOLDER" ]; then
echo -e "${RED}Error: File or folder '$PUBLISHED_APP_FOLDER' does not exist${NC}"
exit 1
fi
# Check that the service exists
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
if [ ! -f "$SERVICE_FILE" ]; then
echo -e "${RED}Error: Service '${SERVICE_NAME}' not found at ${SERVICE_FILE}${NC}"
echo "Use deploy-dotnet to perform the initial deployment first."
exit 1
fi
# Read existing configuration from the service file
DEPLOY_PATH=$(grep -oP 'WorkingDirectory=\K.*' "$SERVICE_FILE")
MAIN_DLL=$(grep -oP 'ExecStart=.*dotnet \K\S+' "$SERVICE_FILE" | xargs basename)
ASPNETCORE_URLS=$(grep -oP 'ASPNETCORE_URLS=\K[^"]+' "$SERVICE_FILE")
if [ -z "$DEPLOY_PATH" ] || [ -z "$MAIN_DLL" ]; then
echo -e "${RED}Error: Could not read configuration from ${SERVICE_FILE}${NC}"
exit 1
fi
# Check if input is a zip file
if [ -f "$PUBLISHED_APP_FOLDER" ] && [[ "$PUBLISHED_APP_FOLDER" == *.zip ]]; then
echo -e "${YELLOW}Detected zip file. Extracting...${NC}"
command -v unzip &>/dev/null || { echo -e "${RED}Error: unzip is not installed${NC}"; exit 1; }
TEMP_EXTRACT_DIR=$(mktemp -d)
unzip -q "$PUBLISHED_APP_FOLDER" -d "$TEMP_EXTRACT_DIR"
echo -e "${GREEN}✓ Extracted to temporary directory${NC}"
PUBLISHED_APP_FOLDER="$TEMP_EXTRACT_DIR"
elif [ ! -d "$PUBLISHED_APP_FOLDER" ]; then
echo -e "${RED}Error: '$PUBLISHED_APP_FOLDER' is not a directory or zip file${NC}"
exit 1
fi
# Check that the DLL exists in the new files
if [ ! -f "$PUBLISHED_APP_FOLDER/$MAIN_DLL" ]; then
echo -e "${RED}Error: Expected DLL '$MAIN_DLL' not found in the provided folder${NC}"
echo "The new build must contain the same main DLL as the existing deployment."
exit 1
fi
echo -e "${YELLOW}Updating .NET application...${NC}"
echo " Service: $SERVICE_NAME"
echo " Deploy path: $DEPLOY_PATH"
echo " Main DLL: $MAIN_DLL"
echo " URL: $ASPNETCORE_URLS"
# Step 1: Stop the service
echo -e "\n${YELLOW}Stopping service...${NC}"
if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then
sudo systemctl stop "$SERVICE_NAME"
echo -e "${GREEN}✓ Service stopped${NC}"
else
echo " (Service was not running)"
fi
# Step 2: Back up existing deployment
BACKUP_PATH="${DEPLOY_PATH}.bak"
echo -e "\n${YELLOW}Backing up existing deployment...${NC}"
sudo rm -rf "$BACKUP_PATH"
sudo cp -r "$DEPLOY_PATH" "$BACKUP_PATH"
echo -e "${GREEN}✓ Backup created at ${BACKUP_PATH}${NC}"
# Step 3: Copy new files
echo -e "\n${YELLOW}Copying new application files...${NC}"
sudo cp -r "$PUBLISHED_APP_FOLDER"/. "$DEPLOY_PATH/"
echo -e "${GREEN}✓ Files copied${NC}"
# Step 4: Restore permissions
echo -e "\n${YELLOW}Setting permissions...${NC}"
sudo chown -R "${SUDO_USER:-$(whoami)}":"${SUDO_USER:-$(whoami)}" "$DEPLOY_PATH"
echo -e "${GREEN}✓ Permissions set${NC}"
# Step 5: Restart the service
echo -e "\n${YELLOW}Starting service...${NC}"
sudo systemctl start "$SERVICE_NAME"
echo -e "${GREEN}✓ Service started${NC}"
# Step 6: Verify
sleep 2
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}✓ Update successful!${NC}"
echo -e "${GREEN}========================================${NC}"
echo "Service name: $SERVICE_NAME"
echo "URL: $ASPNETCORE_URLS"
echo ""
echo "Backup of previous version: $BACKUP_PATH"
echo "To roll back: sudo update.sh $BACKUP_PATH $SERVICE_NAME"
echo ""
echo "Useful commands:"
echo " Logs: sudo journalctl -u $SERVICE_NAME -f"
echo " Status: sudo systemctl status $SERVICE_NAME"
else
echo -e "\n${RED}========================================${NC}"
echo -e "${RED}✗ Service failed to start after update${NC}"
echo -e "${RED}========================================${NC}"
echo "Rolling back to previous version..."
sudo rm -rf "$DEPLOY_PATH"
sudo mv "$BACKUP_PATH" "$DEPLOY_PATH"
sudo systemctl start "$SERVICE_NAME"
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${GREEN}✓ Rollback successful. Previous version is running.${NC}"
else
echo -e "${RED}✗ Rollback also failed. Check logs: sudo journalctl -u $SERVICE_NAME -n 50${NC}"
fi
exit 1
fi
# Cleanup temp dir
if [ -n "$TEMP_EXTRACT_DIR" ]; then
echo -e "\n${YELLOW}Cleaning up temporary files...${NC}"
rm -rf "$TEMP_EXTRACT_DIR"
TEMP_EXTRACT_DIR=""
echo -e "${GREEN}✓ Cleanup complete${NC}"
fi