-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
174 lines (145 loc) · 4.48 KB
/
install.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
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
#!/usr/bin/env bash
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BINARY_NAME="observer"
BINARY_PATH="/usr/local/bin/$BINARY_NAME"
CONFIG_DIR="/etc/observer"
CONFIG_FILE="$CONFIG_DIR/config.toml"
SERVICE_FILE="/etc/systemd/system/observer.service"
info() { echo -e "${BLUE}INFO:${NC} $1"; }
warn() { echo -e "${YELLOW}WARN:${NC} $1"; }
error() { echo -e "${RED}ERROR:${NC} $1"; exit 1; }
success() { echo -e "${GREEN}SUCCESS:${NC} $1"; }
check_root() {
if [ "$EUID" -ne 0 ]; then
error "Please run as root (sudo ./install.sh)"
fi
}
check_system() {
if [ "$(uname)" != "Linux" ]; then
error "This script only works on Linux systems"
fi
if ! command -v systemctl >/dev/null 2>&1; then
error "Systemd is required but not found"
fi
for cmd in dirname readlink; do
if ! command -v $cmd >/dev/null 2>&1; then
error "Required command '$cmd' not found"
fi
done
}
check_cpu() {
if [ ! -d "/sys/devices/system/cpu" ]; then
error "CPU management interface not found"
fi
CPU1_ONLINE="/sys/devices/system/cpu/cpu1/online"
if [ ! -f "$CPU1_ONLINE" ]; then
warn "CPU core control might not be supported on this system"
fi
}
find_binary() {
if [ -f "$SCRIPT_DIR/$BINARY_NAME" ]; then
echo "$SCRIPT_DIR/$BINARY_NAME"
elif [ -f "$SCRIPT_DIR/$BINARY_NAME-linux-amd64" ]; then
echo "$SCRIPT_DIR/$BINARY_NAME-linux-amd64"
elif [ -f "$SCRIPT_DIR/target/release/$BINARY_NAME" ]; then
echo "$SCRIPT_DIR/target/release/$BINARY_NAME"
else
error "Could not find observer binary. Please make sure you're running this script from the correct directory"
fi
}
backup_config() {
if [ -f "$CONFIG_FILE" ]; then
BACKUP_FILE="$CONFIG_FILE.backup.$(date +%Y%m%d_%H%M%S)"
info "Backing up existing config to $BACKUP_FILE"
cp "$CONFIG_FILE" "$BACKUP_FILE"
fi
}
install_files() {
info "Installing observer..."
mkdir -p "$CONFIG_DIR"
BINARY=$(find_binary)
info "Installing binary to $BINARY_PATH"
cp "$BINARY" "$BINARY_PATH" || error "Failed to install binary"
chmod +x "$BINARY_PATH"
if [ -f "$SCRIPT_DIR/config.toml" ]; then
CONFIG_SOURCE="$SCRIPT_DIR/config.toml"
else
error "Configuration file not found"
fi
if [ ! -f "$CONFIG_FILE" ]; then
info "Installing default configuration"
cp "$CONFIG_SOURCE" "$CONFIG_FILE" || error "Failed to install config"
else
info "Keeping existing configuration"
info "New default config available at $CONFIG_FILE.new"
cp "$CONFIG_SOURCE" "$CONFIG_FILE.new"
fi
if [ -f "$SCRIPT_DIR/observer.service" ]; then
info "Installing systemd service"
cp "$SCRIPT_DIR/observer.service" "$SERVICE_FILE" || error "Failed to install service"
else
error "Service file not found"
fi
}
set_permissions() {
info "Setting permissions..."
chown -R root:root "$CONFIG_DIR"
chmod 755 "$CONFIG_DIR"
chmod 644 "$CONFIG_FILE"
chmod 644 "$SERVICE_FILE"
}
setup_service() {
info "Configuring service..."
systemctl daemon-reload || error "Failed to reload systemd"
if ! systemctl enable observer; then
error "Failed to enable observer service"
fi
if ! systemctl start observer; then
error "Failed to start observer service"
fi
if ! systemctl is-active --quiet observer; then
error "Service failed to start. Check 'journalctl -u observer' for details"
fi
}
print_success() {
echo
success "Observer has been successfully installed!"
echo
echo "Configuration file: $CONFIG_FILE"
echo "Service status: $(systemctl is-active observer)"
echo
echo "Useful commands:"
echo " Check status: systemctl status observer"
echo " View logs: journalctl -u observer -f"
echo " Edit config: sudo nano $CONFIG_FILE"
echo
}
cleanup() {
if [ $? -ne 0 ]; then
echo
warn "Installation failed! Cleaning up..."
[ -f "$BINARY_PATH" ] && rm -f "$BINARY_PATH"
[ -f "$SERVICE_FILE" ] && rm -f "$SERVICE_FILE"
systemctl daemon-reload
fi
}
main() {
trap cleanup EXIT
echo "Starting Observer installation..."
echo
check_root
check_system
check_cpu
backup_config
install_files
set_permissions
setup_service
print_success
}
main