-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_scripts_methods.bash
More file actions
130 lines (112 loc) · 4.2 KB
/
remote_scripts_methods.bash
File metadata and controls
130 lines (112 loc) · 4.2 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
#!/bin/bash
# Set bash execution flags:
# - Treat unset variables as an error when substituting
# - Exit immediately if a command exits with a non-zero status
# - Print each command to stdout before executing it (useful for debugging)
set -u
# set -e
# set -x
# ANSI Colors
RED='\033[0;31m' # Error
GREEN='\033[0;32m' # Success
BLUE='\033[0;34m' # Info
YELLOW='\033[0;93m' # Warning/Useful info
NC='\033[0m' # No Color
# Function to source and execute a remote script directly in the current shell environment
# source remote script -> source_rscript
# Usage:
# 1. Set environment variable:
# export RSCRIPT_BASE_URL="https://raw.githubusercontent.com/voiduin/linux-host-setup/main"
# 2. Use source_rscript script_path
# Example: source_rscript setup.sh
source_rscript() {
local script_url="${1}"
# Get base repo URL from environment variable
local base_url_rawrepo="${RSCRIPT_BASE_URL}"
if [[ -z "$base_url_rawrepo" ]]; then
echo "Error: RSCRIPT_BASE_URL is not set. Please set it before running this function." >&2
return 1
fi
# Read remote script content
local full_script_url="${base_url_rawrepo}/${script_path}"
local script_content=$(curl -Ls --fail "${full_script_url}")
local status=$?
if [[ "$status" -ne 0 ]]; then
echo -e " ${RED}Error${NC}: Failed to read the remote script" >&2
echo " From: \"${base_url_rawrepo}/${script_path}\"" >&2
echo " Curl exit status: \"${status}\"" >&2
return "${status}"
fi
# Source this script content into current shell
source <(echo -n "${script_content}")
}
# Function to read and execute a remote script with optional sudo and parameters
# run remote script -> run_rscript
# Usage:
# 1. Set environment variable:
# export RSCRIPT_BASE_URL="https://raw.githubusercontent.com/voiduin/linux-host-setup/main"
# 2. Use run_rscript script_path [--sudo] [--verbose] [params...]
# Example: run_rscript setup.sh [--sudo] [--verbose] param1 param2
run_rscript () {
local script_path="${1}"
local use_sudo=0 # Default no sudo
local verbose=0 # Default quiet
local params=() # Initialize params array
# Process optional parameters --sudo and --verbose
shift # Remove script_path from the parameters list
while (( "$#" )); do
case "$1" in
--sudo)
use_sudo=1
shift
;;
--verbose)
verbose=1
shift
;;
*) # Assume the rest are script parameters
params+=("$1")
shift
;;
esac
done
# Get base repo URL from environment variable
local base_url_rawrepo="${RSCRIPT_BASE_URL}"
if [[ -z "$base_url_rawrepo" ]]; then
echo "Error: RSCRIPT_BASE_URL is not set. Please set it before running this function." >&2
return 1
fi
if [[ "${verbose}" -eq 1 ]]; then
echo -e " ${BLUE}RUN${NC}: Try load and run script \"${script_path}\":"
echo " From base repo url: \"${base_url_rawrepo}\""
echo " With parameters: ${params[*]}"
fi
# Read remote script content
local full_script_url="${base_url_rawrepo}/${script_path}"
local script_content=$(curl -Ls --fail "${full_script_url}")
local status=$?
if [[ "$status" -ne 0 ]]; then
echo -e " ${RED}Error${NC}: Failed to read the remote script" >&2
echo " From: \"${base_url_rawrepo}/${script_path}\"" >&2
echo " Curl exit status: \"${status}\"" >&2
return "${status}"
fi
if [[ "${verbose}" -eq 1 ]]; then
echo -e " Remote script read successfully"
fi
echo " Try running readed script..."
# Execute script
if [[ "${use_sudo}" -eq 1 ]]; then
echo "${script_content}" | sudo bash -s -- "${params[@]}"
else
echo "${script_content}" | bash -s -- "${params[@]}"
fi
status=$?
if [[ "$status" -ne 0 ]]; then
echo "Error: Script execution failed. Bash exit status: $status" >&2
return "$status"
fi
# Separate from next terminal output
echo -ne "\n"
return "${status}"
}