-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
294 lines (245 loc) · 7.87 KB
/
install.sh
File metadata and controls
294 lines (245 loc) · 7.87 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
set -euo pipefail
# Ship Application Updater Installation Script
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly UPDATER_USER="ship"
readonly INSTALL_DIR="/opt/ship"
readonly CONFIG_DIR="/etc/ship"
readonly DATA_DIR="/var/lib/ship"
readonly LOG_FILE="/var/log/ship-install.log"
# Color codes for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $*" | tee -a "${LOG_FILE}"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $*" | tee -a "${LOG_FILE}"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $*" | tee -a "${LOG_FILE}"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*" | tee -a "${LOG_FILE}"
}
# Error handling
error_exit() {
log_error "Installation failed: $1"
exit 1
}
# Cleanup function
cleanup() {
if [[ -n "${TEMP_DIR:-}" && -d "${TEMP_DIR}" ]]; then
rm -rf "${TEMP_DIR}"
fi
}
trap cleanup EXIT
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
error_exit "This script must be run as root (use sudo)"
fi
}
# Check system requirements
check_requirements() {
log_info "Checking system requirements..."
local REQUIRED_COMMANDS=("python3" "git" "systemctl")
local MISSING_COMMANDS=()
for CMD in "${REQUIRED_COMMANDS[@]}"; do
if ! command -v "${CMD}" &> /dev/null; then
MISSING_COMMANDS+=("${CMD}")
fi
done
if [[ ${#MISSING_COMMANDS[@]} -ne 0 ]]; then
error_exit "Missing required commands: ${MISSING_COMMANDS[*]}"
fi
# Check Python version
local PYTHON_VERSION
PYTHON_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
if [[ $(echo "${PYTHON_VERSION} < 3.8" | bc -l) -eq 1 ]]; then
error_exit "Python 3.8 or higher required, found ${PYTHON_VERSION}"
fi
log_success "System requirements satisfied"
}
# Create ship user and directories
setup_user_and_directories() {
log_info "Setting up user and directories..."
# Create ship user if not exists
if ! id "${UPDATER_USER}" &> /dev/null; then
useradd -r -s /bin/false -d "${DATA_DIR}" -c "Ship Application Updater" "${UPDATER_USER}"
log_success "Created user: ${UPDATER_USER}"
else
log_info "User ${UPDATER_USER} already exists"
fi
# Create directories
local DIRECTORIES=(
"${INSTALL_DIR}"
"${CONFIG_DIR}"
"${DATA_DIR}"
"/var/log"
)
for DIR in "${DIRECTORIES[@]}"; do
if [[ ! -d "${DIR}" ]]; then
mkdir -p "${DIR}"
log_success "Created directory: ${DIR}"
fi
done
# Set ownership
chown -R "${UPDATER_USER}:${UPDATER_USER}" "${DATA_DIR}"
chown -R root:root "${INSTALL_DIR}" "${CONFIG_DIR}"
log_success "User and directories setup complete"
}
# Install Python dependencies
setup_python_environment() {
log_info "Setting up Python virtual environment..."
cd "${INSTALL_DIR}"
# Create virtual environment
if [[ ! -d "venv" ]]; then
python3 -m venv venv
log_success "Created Python virtual environment"
fi
# Activate and install dependencies
# shellcheck source=/dev/null
source venv/bin/activate
# Upgrade pip
pip install --upgrade pip
# Install requirements
if [[ -f "${SCRIPT_DIR}/requirements.txt" ]]; then
pip install -r "${SCRIPT_DIR}/requirements.txt"
log_success "Installed Python dependencies"
else
log_warning "requirements.txt not found, installing basic dependencies"
pip install requests paramiko tomli
fi
deactivate
}
# Install application files
install_application() {
log_info "Installing application files..."
# Copy main script
if [[ -f "${SCRIPT_DIR}/ship.py" ]]; then
cp "${SCRIPT_DIR}/ship.py" "${INSTALL_DIR}/"
chmod +x "${INSTALL_DIR}/ship.py"
log_success "Installed ship.py"
else
error_exit "ship.py not found in ${SCRIPT_DIR}"
fi
# Copy configuration template
if [[ -f "${SCRIPT_DIR}/ship.toml" ]]; then
if [[ ! -f "${CONFIG_DIR}/ship.toml" ]]; then
cp "${SCRIPT_DIR}/ship.toml" "${CONFIG_DIR}/"
chown "${UPDATER_USER}:${UPDATER_USER}" "${CONFIG_DIR}/ship.toml"
chmod 600 "${CONFIG_DIR}/ship.toml"
log_success "Installed configuration template"
else
log_warning "Configuration file already exists, skipping"
fi
fi
# Set permissions
chown -R root:root "${INSTALL_DIR}"
chmod +x "${INSTALL_DIR}/ship.py"
}
# Install systemd units
install_systemd_units() {
log_info "Installing systemd units..."
local SYSTEMD_FILES=("ship.service" "ship.timer")
for FILE in "${SYSTEMD_FILES[@]}"; do
if [[ -f "${SCRIPT_DIR}/${FILE}" ]]; then
cp "${SCRIPT_DIR}/${FILE}" "/etc/systemd/system/"
log_success "Installed ${FILE}"
else
log_warning "${FILE} not found, skipping"
fi
done
# Reload systemd
systemctl daemon-reload
log_success "Systemd units reloaded"
}
# Configure systemd service
configure_systemd() {
log_info "Configuring systemd service..."
# Enable timer
if systemctl enable ship.timer; then
log_success "Enabled ship.timer"
else
log_warning "Failed to enable timer"
fi
# Start timer
if systemctl start ship.timer; then
log_success "Started ship.timer"
else
log_warning "Failed to start timer"
fi
}
# Test installation
test_installation() {
log_info "Testing installation..."
# Test configuration loading
if sudo -u "${UPDATER_USER}" "${INSTALL_DIR}/venv/bin/python" "${INSTALL_DIR}/ship.py" --check-only 2>/dev/null; then
log_success "Configuration test passed"
else
log_warning "Configuration test failed - check configuration file"
fi
# Check systemd status
if systemctl is-active --quiet ship.timer; then
log_success "Timer is active"
else
log_warning "Timer is not active"
fi
# Show next scheduled run
local NEXT_RUN
NEXT_RUN=$(systemctl list-timers ship.timer --no-pager --no-legend | awk '{print $1, $2}' | head -1)
if [[ -n "${NEXT_RUN}" ]]; then
log_info "Next scheduled run: ${NEXT_RUN}"
fi
}
# Display post-installation information
show_post_install_info() {
log_success "Installation completed successfully!"
echo
echo "=== POST-INSTALLATION STEPS ==="
echo
echo "1. Edit configuration file:"
echo " sudo nano ${CONFIG_DIR}/ship.toml"
echo
echo "2. Test configuration:"
echo " sudo -u ${UPDATER_USER} ${INSTALL_DIR}/venv/bin/python ${INSTALL_DIR}/ship.py --check-only"
echo
echo "3. Manual test run:"
echo " sudo systemctl start ship.service"
echo
echo "4. Monitor logs:"
echo " sudo journalctl -u ship.service -f"
echo " sudo tail -f /var/log/ship.log"
echo
echo "5. Check timer status:"
echo " systemctl status ship.timer"
echo
echo "=== IMPORTANT SECURITY NOTES ==="
echo "- Review and customize the configuration file"
echo "- Set up appropriate SSH keys for Git/SFTP access"
echo "- Test all update sources before production use"
echo "- Monitor logs regularly for any issues"
echo
}
# Main installation function
main() {
log_info "Starting Ship Application Updater installation..."
check_root
check_requirements
setup_user_and_directories
setup_python_environment
install_application
install_systemd_units
configure_systemd
test_installation
show_post_install_info
}
# Run main function with error handling
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi