-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
Β·203 lines (168 loc) Β· 5.16 KB
/
deploy.sh
File metadata and controls
executable file
Β·203 lines (168 loc) Β· 5.16 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# Quick deployment script for Ubuntu server with Cloudflare Tunnel
# Run this on your Ubuntu server after uploading the code
set -e # Exit on error
echo "π PWRUP Slack Bot Deployment Script"
echo "======================================"
echo ""
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "β Please don't run as root. Run as your normal user."
exit 1
fi
# Get current directory
INSTALL_DIR=$(pwd)
echo "π Installing in: $INSTALL_DIR"
echo ""
# Step 1: Install system dependencies
echo "π¦ Step 1: Installing system dependencies..."
sudo apt update
sudo apt install -y python3 python3-pip python3-venv curl
# Step 2: Install Cloudflare Tunnel
echo ""
echo "π Step 2: Installing Cloudflare Tunnel..."
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared.deb
rm cloudflared.deb
echo "β
Cloudflare Tunnel installed"
# Step 3: Setup Python virtual environment
echo ""
echo "π Step 3: Setting up Python virtual environment..."
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
# Step 4: Setup environment file
echo ""
echo "βοΈ Step 4: Setting up environment configuration..."
if [ ! -f .env ]; then
cp .env.example .env
echo "β
Created .env file"
echo ""
echo "β οΈ IMPORTANT: You need to edit .env with your Slack tokens!"
echo " Run: nano .env"
echo " Add your SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET"
echo ""
read -p "Press Enter after you've edited .env..."
else
echo "β
.env file already exists"
fi
# Step 5: Test the bot
echo ""
echo "π§ͺ Step 5: Testing the bot..."
echo "Starting bot for 5 seconds to test..."
timeout 5 python main.py || true
echo "β
Bot test complete"
# Step 6: Setup systemd service for bot
echo ""
echo "π§ Step 6: Setting up systemd service for bot..."
SERVICE_FILE="/etc/systemd/system/slackbot.service"
sudo tee $SERVICE_FILE > /dev/null <<EOF
[Unit]
Description=PWRUP Slack Bot
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$INSTALL_DIR
Environment="PATH=$INSTALL_DIR/venv/bin"
EnvironmentFile=$INSTALL_DIR/.env
ExecStart=$INSTALL_DIR/venv/bin/python main.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
echo "β
Created systemd service file"
# Enable and start service
sudo systemctl daemon-reload
sudo systemctl enable slackbot
sudo systemctl start slackbot
echo "β
Service started"
echo ""
echo "π Service status:"
sudo systemctl status slackbot --no-pager
# Step 7: Setup Cloudflare Tunnel
echo ""
echo "π Step 7: Setting up Cloudflare Tunnel..."
echo ""
echo "You need to authenticate with Cloudflare."
echo "This will open a browser to log in. If you're on a headless server,"
echo "copy the URL and open it on your local machine."
echo ""
read -p "Press Enter to continue..."
cloudflared tunnel login
echo ""
echo "Enter a name for your tunnel (e.g., slackbot):"
read TUNNEL_NAME
if [ -z "$TUNNEL_NAME" ]; then
TUNNEL_NAME="slackbot"
fi
# Create tunnel
TUNNEL_ID=$(cloudflared tunnel create $TUNNEL_NAME 2>&1 | grep -oP 'Tunnel ID: \K[a-f0-9-]+' || echo "")
if [ -z "$TUNNEL_ID" ]; then
echo "β οΈ Tunnel creation may have failed. Check the output above."
echo "You can manually create a tunnel with: cloudflared tunnel create $TUNNEL_NAME"
read -p "Press Enter to continue..."
fi
# Create config file
CONFIG_DIR="$HOME/.cloudflared"
mkdir -p $CONFIG_DIR
echo ""
echo "Enter your domain (e.g., bot.example.com):"
read DOMAIN
if [ -z "$DOMAIN" ]; then
echo "β Domain is required"
exit 1
fi
# Create config
sudo tee /etc/systemd/system/cloudflared.service > /dev/null <<EOF
[Unit]
Description=Cloudflare Tunnel
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=/usr/local/bin/cloudflared tunnel --config $CONFIG_DIR/config.yml run $TUNNEL_NAME
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# Create tunnel config
tee $CONFIG_DIR/config.yml > /dev/null <<EOF
tunnel: $TUNNEL_NAME
credentials-file: $CONFIG_DIR/$TUNNEL_ID.json
ingress:
- hostname: $DOMAIN
service: http://localhost:3000
- service: http_status:404
EOF
echo "β
Cloudflare Tunnel configured"
# Enable and start tunnel
sudo systemctl daemon-reload
sudo systemctl enable cloudflared
sudo systemctl start cloudflared
echo "β
Tunnel started"
echo ""
echo "π Tunnel status:"
sudo systemctl status cloudflared --no-pager
# Final steps
echo ""
echo "======================================"
echo "β
Deployment Complete!"
echo "======================================"
echo ""
echo "π Next steps:"
echo "1. Go to https://api.slack.com/apps β Your App"
echo "2. Set Event Subscriptions URL to: https://$DOMAIN/slack/events"
echo "3. Set Slash Command URL to: https://$DOMAIN/slack/events"
echo "4. Test your bot with /ping in Slack"
echo ""
echo "π§ Useful commands:"
echo " View bot logs: sudo journalctl -u slackbot -f"
echo " View tunnel logs: sudo journalctl -u cloudflared -f"
echo " Restart bot: sudo systemctl restart slackbot"
echo " Restart tunnel: sudo systemctl restart cloudflared"
echo ""
echo "π Happy Slacking!"