commit install
This commit is contained in:
parent
48d498bf89
commit
1b94a10b57
@ -12,13 +12,15 @@ SERVICE_GROUP="${SERVICE_GROUP:-$(id -gn "$SERVICE_USER")}"
|
||||
|
||||
# Configure multiple programs here before publishing this script.
|
||||
# Format:
|
||||
# "service_name|download_url|start_command|stop_command|service_user|service_group"
|
||||
# "service_name|download_url|start_command|stop_command|dependency_dirs|service_user|service_group"
|
||||
#
|
||||
# Use {install_dir} in commands to reference the extracted package directory.
|
||||
# stop_command, service_user and service_group are optional.
|
||||
# Use {install_dir}, {work_dir} and {service_name} in commands and directories.
|
||||
# dependency_dirs is a comma-separated list. dependency_dirs, service_user and
|
||||
# service_group are optional.
|
||||
|
||||
INSTALL_ITEMS=(
|
||||
"iot|https://punchsky.net/down/iot-0.1.0.tar.gz|{install_dir}/bin/iot foreground|{install_dir}/bin/iot stop"
|
||||
"efka|https://punchsky.net/down/efka-0.1.0.tar.gz|{install_dir}/bin/efka foreground|{install_dir}/bin/efka stop"
|
||||
"iot|https://punchsky.cn/down/iot-0.1.0.tar.gz|{install_dir}/bin/iot foreground|{install_dir}/bin/iot stop|/var/lib/iot/mnesia,/var/lib/endpoint/database,/var/lib/endpoint/endpoint_log"
|
||||
"efka|https://punchsky.cn/down/efka-0.1.0.tar.gz|{install_dir}/bin/efka foreground|{install_dir}/bin/efka stop|/var/lib/efka/mnesia,/var/lib/efka/dets,/var/lib/efka/docker"
|
||||
)
|
||||
TEMP_FILES=()
|
||||
INSTALLED_SERVICES=()
|
||||
@ -42,15 +44,17 @@ Options:
|
||||
|
||||
Array config inside script:
|
||||
INSTALL_ITEMS=(
|
||||
"iot|https://punchsky.net/down/iot-0.1.0.tar.gz|{install_dir}/bin/iot foreground|{install_dir}/bin/iot stop"
|
||||
"efka|https://punchsky.net/down/efka-0.1.0.tar.gz|{install_dir}/bin/efka foreground|{install_dir}/bin/efka stop"
|
||||
"iot|https://punchsky.cn/down/iot-0.1.0.tar.gz|{install_dir}/bin/iot foreground|{install_dir}/bin/iot stop|{work_dir}/var/lib/iot/mnesia,{work_dir}/var/log/iot,{work_dir}/var/run/iot"
|
||||
"efka|https://punchsky.cn/down/efka-0.1.0.tar.gz|{install_dir}/bin/efka foreground|{install_dir}/bin/efka stop|{work_dir}/var/lib/efka/mnesia,{work_dir}/var/log/efka,{work_dir}/var/run/efka"
|
||||
)
|
||||
|
||||
Package format:
|
||||
"service_name|download_url|start_command|stop_command|service_user|service_group"
|
||||
"service_name|download_url|start_command|stop_command|dependency_dirs|service_user|service_group"
|
||||
|
||||
Command placeholder:
|
||||
Placeholders:
|
||||
{install_dir} will be replaced with the extracted package directory.
|
||||
{work_dir} will be replaced with the root work directory.
|
||||
{service_name} will be replaced with the service name.
|
||||
|
||||
Example:
|
||||
curl -fsSL https://punchsky.com/install | bash
|
||||
@ -222,11 +226,41 @@ validate_install_item() {
|
||||
[[ "$service_name" =~ ^[a-zA-Z0-9_.@-]+$ ]] || fail "Invalid service name: $service_name"
|
||||
}
|
||||
|
||||
replace_install_dir_placeholder() {
|
||||
local command_template="$1"
|
||||
replace_placeholders() {
|
||||
local value="$1"
|
||||
local install_dir="$2"
|
||||
local service_name="$3"
|
||||
|
||||
printf '%s\n' "${command_template//\{install_dir\}/$install_dir}"
|
||||
value="${value//\{install_dir\}/$install_dir}"
|
||||
value="${value//\{work_dir\}/$WORK_DIR}"
|
||||
value="${value//\{service_name\}/$service_name}"
|
||||
|
||||
printf '%s\n' "$value"
|
||||
}
|
||||
|
||||
create_dependency_dirs() {
|
||||
local dependency_dirs="$1"
|
||||
local install_dir="$2"
|
||||
local service_name="$3"
|
||||
local service_user="$4"
|
||||
local service_group="$5"
|
||||
local dependency_dir
|
||||
local dependency_dir_list
|
||||
local -a dependency_dir_array
|
||||
|
||||
[[ -n "$dependency_dirs" ]] || return 0
|
||||
|
||||
dependency_dir_list="$(replace_placeholders "$dependency_dirs" "$install_dir" "$service_name")"
|
||||
|
||||
IFS=',' read -ra dependency_dir_array <<< "$dependency_dir_list"
|
||||
for dependency_dir in "${dependency_dir_array[@]}"; do
|
||||
[[ -n "$dependency_dir" ]] || continue
|
||||
[[ "$dependency_dir" = /* ]] || fail "Dependency directory must be an absolute path: $dependency_dir"
|
||||
|
||||
log "Creating dependency directory: $dependency_dir"
|
||||
run_as_root install -d -m 0755 "$dependency_dir"
|
||||
run_as_root chown -R "${service_user}:${service_group}" "$dependency_dir"
|
||||
done
|
||||
}
|
||||
|
||||
install_program() {
|
||||
@ -234,8 +268,9 @@ install_program() {
|
||||
local download_url="$2"
|
||||
local start_command="$3"
|
||||
local stop_command="$4"
|
||||
local service_user="$5"
|
||||
local service_group="$6"
|
||||
local dependency_dirs="$5"
|
||||
local service_user="$6"
|
||||
local service_group="$7"
|
||||
local clean_url="$download_url"
|
||||
local archive_name
|
||||
|
||||
@ -258,11 +293,11 @@ install_program() {
|
||||
|
||||
service_user="${service_user:-$SERVICE_USER}"
|
||||
service_group="${service_group:-$SERVICE_GROUP}"
|
||||
start_command="$(replace_install_dir_placeholder "$start_command" "$install_dir")"
|
||||
stop_command="$(replace_install_dir_placeholder "$stop_command" "$install_dir")"
|
||||
start_command="$(replace_placeholders "$start_command" "$install_dir" "$service_name")"
|
||||
stop_command="$(replace_placeholders "$stop_command" "$install_dir" "$service_name")"
|
||||
|
||||
log "Downloading $download_url"
|
||||
curl -fL --retry 3 --connect-timeout 20 -o "$temp_archive" "$download_url"
|
||||
wget --tries=3 --timeout=20 -O "$temp_archive" "$download_url"
|
||||
run_as_root install -m 0644 "$temp_archive" "$archive_path"
|
||||
|
||||
log "Creating install directory: $install_dir"
|
||||
@ -274,6 +309,8 @@ install_program() {
|
||||
log "Setting owner for install directory: ${service_user}:${service_group}"
|
||||
run_as_root chown -R "${service_user}:${service_group}" "$install_dir"
|
||||
|
||||
create_dependency_dirs "$dependency_dirs" "$install_dir" "$service_name" "$service_user" "$service_group"
|
||||
|
||||
write_service_file "$service_file" "$service_name" "$service_user" "$service_group" "$install_dir" "$start_command" "$stop_command"
|
||||
INSTALLED_SERVICES+=("$service_name")
|
||||
}
|
||||
@ -284,14 +321,15 @@ install_programs() {
|
||||
local download_url
|
||||
local start_command
|
||||
local stop_command
|
||||
local dependency_dirs
|
||||
local service_user
|
||||
local service_group
|
||||
|
||||
set +u
|
||||
for item in "${INSTALL_ITEMS[@]}"; do
|
||||
set -u
|
||||
IFS='|' read -r service_name download_url start_command stop_command service_user service_group <<< "$item"
|
||||
install_program "$service_name" "$download_url" "$start_command" "$stop_command" "$service_user" "$service_group"
|
||||
IFS='|' read -r service_name download_url start_command stop_command dependency_dirs service_user service_group <<< "$item"
|
||||
install_program "$service_name" "$download_url" "$start_command" "$stop_command" "$dependency_dirs" "$service_user" "$service_group"
|
||||
set +u
|
||||
done
|
||||
set -u
|
||||
@ -324,7 +362,7 @@ main() {
|
||||
parse_args "$@"
|
||||
validate_args
|
||||
|
||||
require_command curl
|
||||
require_command wget
|
||||
require_root_runner
|
||||
require_command tar
|
||||
require_command systemctl
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user