-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-service.sh
More file actions
executable file
·143 lines (123 loc) · 4.33 KB
/
install-service.sh
File metadata and controls
executable file
·143 lines (123 loc) · 4.33 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
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
SERVICE_NAME="tipfax"
SERVICE_USER="tipfax"
SERVICE_GROUP="tipfax"
INSTALL_DIR="/opt/tipfax"
BINARY_NAME="tipfax-server"
echo -e "${GREEN}TipFax Service Installation Script${NC}"
echo "=================================="
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}This script must be run as root (use sudo)${NC}"
exit 1
fi
# Check if SE_JWT_TOKEN is provided
if [[ -z "$SE_JWT_TOKEN" ]]; then
echo -e "${YELLOW}Warning: SE_JWT_TOKEN environment variable not set${NC}"
echo "You'll need to set it manually in the service file later"
fi
echo "Step 1: Creating service user and group..."
if ! id "$SERVICE_USER" &>/dev/null; then
useradd --system --no-create-home --shell /bin/false "$SERVICE_USER"
echo -e "${GREEN}Created user: $SERVICE_USER${NC}"
else
echo "User $SERVICE_USER already exists"
fi
echo "Step 2: Creating installation directory..."
mkdir -p "$INSTALL_DIR"
chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
chmod 755 "$INSTALL_DIR"
echo "Step 3: Installing the application binary..."
if [[ ! -f "go.mod" ]]; then
echo -e "${RED}Error: go.mod not found. Please run this script from the project root directory.${NC}"
exit 1
fi
# Check if binary already exists (pre-built)
if [[ -f "$BINARY_NAME" ]]; then
echo -e "${GREEN}Found pre-built binary: $BINARY_NAME${NC}"
cp "$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME"
else
echo "Building the application..."
# Check if Go is available
if ! command -v go &> /dev/null; then
echo -e "${YELLOW}Go not found in PATH. Trying common Go installation paths...${NC}"
# Common Go installation paths
GO_PATHS=(
"/usr/local/go/bin/go"
"/opt/go/bin/go"
"$HOME/go/bin/go"
"/snap/bin/go"
"$HOME/.nix-profile/bin/go"
)
GO_CMD=""
for path in "${GO_PATHS[@]}"; do
if [[ -x "$path" ]]; then
GO_CMD="$path"
echo -e "${GREEN}Found Go at: $GO_CMD${NC}"
break
fi
done
if [[ -z "$GO_CMD" ]]; then
echo -e "${RED}Error: Go compiler not found and no pre-built binary available.${NC}"
echo "Please either:"
echo "1. Build the binary first: go build -o $BINARY_NAME ./cmd/server"
echo "2. Install Go and ensure it's in your PATH"
exit 1
fi
else
GO_CMD="go"
fi
$GO_CMD build -o "$INSTALL_DIR/$BINARY_NAME" ./cmd/server
fi
chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR/$BINARY_NAME"
chmod 755 "$INSTALL_DIR/$BINARY_NAME"
echo -e "${GREEN}Built and installed binary to $INSTALL_DIR/$BINARY_NAME${NC}"
echo "Step 4: Installing systemd service..."
if [[ ! -f "tipfax.service" ]]; then
echo -e "${RED}Error: tipfax.service file not found in current directory${NC}"
exit 1
fi
# Update SE_JWT_TOKEN in service file if provided
if [[ -n "$SE_JWT_TOKEN" ]]; then
sed -i "s/Environment=SE_JWT_TOKEN=/Environment=SE_JWT_TOKEN=$SE_JWT_TOKEN/" tipfax.service
echo -e "${GREEN}Updated SE_JWT_TOKEN in service file${NC}"
fi
cp tipfax.service /etc/systemd/system/
chmod 644 /etc/systemd/system/tipfax.service
echo -e "${GREEN}Installed service file to /etc/systemd/system/tipfax.service${NC}"
echo "Step 5: Adding user to lp group for printer access..."
usermod -a -G lp "$SERVICE_USER"
echo "Step 6: Reloading systemd and enabling service..."
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
echo -e "${GREEN}Service enabled${NC}"
echo ""
echo -e "${GREEN}Installation completed successfully!${NC}"
echo ""
echo "Next steps:"
echo "1. If you didn't set SE_JWT_TOKEN, edit the service file:"
echo " sudo systemctl edit tipfax --full"
echo " And update the Environment=SE_JWT_TOKEN= line"
echo ""
echo "2. Start the service:"
echo " sudo systemctl start tipfax"
echo ""
echo "3. Check service status:"
echo " sudo systemctl status tipfax"
echo ""
echo "4. View logs:"
echo " sudo journalctl -u tipfax -f"
echo ""
echo "5. To uninstall:"
echo " sudo systemctl stop tipfax"
echo " sudo systemctl disable tipfax"
echo " sudo rm /etc/systemd/system/tipfax.service"
echo " sudo rm -rf $INSTALL_DIR"
echo " sudo userdel $SERVICE_USER"