-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotnet-deploy.sh
More file actions
167 lines (145 loc) · 6.07 KB
/
Copy pathdotnet-deploy.sh
File metadata and controls
167 lines (145 loc) · 6.07 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
166
167
#!/bin/bash
# .NET Website Auto-Publisher for Linux
# Usage: ./dotnet-deploy.sh <published-app-folder-or-zip> <ip> <port> <main-dll> <service-name> [deployment-path]
set -e
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> <ip> <port> <main-dll> <service-name> [deployment-path]"
echo ""
echo "ARGUMENTS:"
echo " published-app-folder-or-zip Path to the .NET publish folder or a .zip of it"
echo " ip IP address to listen on (e.g. 0.0.0.0 for all interfaces)"
echo " port Port number to listen on (e.g. 5000)"
echo " main-dll Name of the main DLL (e.g. MyApp.dll)"
echo " service-name Name for the systemd service (e.g. dotnet-myapp)"
echo " deployment-path (Optional) Where to deploy the app (default: /var/www/<service-name>)"
echo ""
echo "EXAMPLES:"
echo " $(basename "$0") ~/MyApp/publish 0.0.0.0 5000 MyApp.dll dotnet-myapp"
echo " $(basename "$0") ~/MyApp.zip 0.0.0.0 5000 MyApp.dll dotnet-myapp /opt/myapp"
exit 0
fi
# Configuration
PUBLISHED_APP_FOLDER="${1}"
LISTEN_IP="${2}"
LISTEN_PORT="${3}"
MAIN_DLL="${4}"
SERVICE_NAME="${5}"
DEPLOY_PATH="${6:-/var/www/$SERVICE_NAME}"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
# Validation
if [ -z "$PUBLISHED_APP_FOLDER" ] || [ -z "$LISTEN_IP" ] || [ -z "$LISTEN_PORT" ] || [ -z "$MAIN_DLL" ] || [ -z "$SERVICE_NAME" ]; then
echo -e "${RED}Usage: $(basename "$0") <published-app-folder-or-zip> <ip> <port> <main-dll> <service-name> [deployment-path]${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 if input is a zip file
TEMP_EXTRACT_DIR=""
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 if the specified DLL exists
if [ ! -f "$PUBLISHED_APP_FOLDER/$MAIN_DLL" ]; then
echo -e "${RED}Error: DLL file '$MAIN_DLL' not found in '$PUBLISHED_APP_FOLDER'${NC}"
exit 1
fi
# Check required tools
command -v dotnet &>/dev/null || { echo -e "${RED}Error: dotnet is not installed${NC}"; exit 1; }
echo -e "${YELLOW}Deploying .NET application...${NC}"
echo " Published folder: $PUBLISHED_APP_FOLDER"
echo " Main DLL: $MAIN_DLL"
echo " Deploy path: $DEPLOY_PATH"
echo " Listen IP: $LISTEN_IP"
echo " Listen port: $LISTEN_PORT"
# Step 1: Stop existing service if running
echo -e "\n${YELLOW}Stopping existing 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 not running or not found)"
fi
# Step 2: Create deployment directory
echo -e "\n${YELLOW}Creating deployment directory...${NC}"
sudo mkdir -p "$DEPLOY_PATH"
echo -e "${GREEN}✓ Directory created${NC}"
# Step 3: Copy files
echo -e "\n${YELLOW}Copying application files...${NC}"
sudo cp -r "$PUBLISHED_APP_FOLDER"/* "$DEPLOY_PATH/"
echo -e "${GREEN}✓ Files copied${NC}"
# Step 4: Set 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: Create or update systemd service
echo -e "\n${YELLOW}Creating systemd service...${NC}"
SERVICE_CONTENT="[Unit]
Description=.NET Web Application
After=network.target
[Service]
Type=simple
User=$(whoami)
WorkingDirectory=$DEPLOY_PATH
ExecStart=/usr/bin/dotnet $DEPLOY_PATH/$MAIN_DLL
Restart=on-failure
RestartSec=10
KillSignal=SIGINT
Environment=\"ASPNETCORE_URLS=http://$LISTEN_IP:$LISTEN_PORT\"
Environment=\"ASPNETCORE_ENVIRONMENT=Production\"
[Install]
WantedBy=multi-user.target"
echo "$SERVICE_CONTENT" | sudo tee "$SERVICE_FILE" > /dev/null
echo -e "${GREEN}✓ Service file created${NC}"
# Step 6: Reload systemd and enable service
echo -e "\n${YELLOW}Enabling and starting service...${NC}"
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE_NAME"
sudo systemctl start "$SERVICE_NAME"
echo -e "${GREEN}✓ Service enabled and started${NC}"
# Step 7: Verify service is running
sleep 2
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}✓ Deployment successful!${NC}"
echo -e "${GREEN}========================================${NC}"
echo "Service name: $SERVICE_NAME"
echo "Access the app at: http://$LISTEN_IP:$LISTEN_PORT"
echo ""
echo "Useful commands:"
echo " Status: sudo systemctl status $SERVICE_NAME"
echo " Logs: sudo journalctl -u $SERVICE_NAME -f"
echo " Stop: sudo systemctl stop $SERVICE_NAME"
echo " Start: sudo systemctl start $SERVICE_NAME"
echo " Restart: sudo systemctl restart $SERVICE_NAME"
else
echo -e "\n${RED}========================================${NC}"
echo -e "${RED}✗ Service failed to start${NC}"
echo -e "${RED}========================================${NC}"
echo "Check logs with: sudo journalctl -u $SERVICE_NAME -n 50"
exit 1
fi
# Cleanup temporary directory if it was created
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