-
Notifications
You must be signed in to change notification settings - Fork 1
/
sys
executable file
·190 lines (147 loc) · 4.61 KB
/
sys
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
#!/bin/bash
## Ecosystems array (aka "ecos")
declare -A ecosystems
ecosystems["--python"]="Python"
ecosystems["--cloud"]="Cloud"
ecosystems["--containers_vms"]="Containers and VMs"
ecosystems["--db_clients"]="Database Clients"
ecosystems["--javascript_node"]="JavaScript and Node"
ecosystems["--web_tools"]="Web Tools"
ecosystems["--java"]="Java"
ecosystems["--c_c++"]="C and C++"
ecosystems["--go"]="Go"
ecosystems["--version_control"]="Version Control"
ecosystems["--php"]="PHP"
ecosystems["--ruby"]="Ruby"
ecosystems["--rust"]="Rust"
check_args() {
local valid_args=false
if [[ "$1" == "--init" ]]; then
valid_args=true
fi
# Check is present and non-empty
for arg in "$@"; do
if [ -n "$arg" ] && [ -n "${ecosystems[$arg]}" ]; then
valid_args=true
break
fi
done
if [ "$valid_args" = false ]; then
show_cli_screen
exit 1
fi
}
show_cli_screen() {
cat << EOF
Welcome. Pass in your ecosystems with options below.
Usage: ./sys --init
./sys [ecosystem options]
Options:
$(for eco_key in "${!ecosystems[@]}"; do
echo " $eco_key"
done)
EOF
}
## Determines OS: ubuntu, fedora, macos, etc
detect_system() {
if [ -e "/etc/os-release" ]; then
sys_os=$(awk -F= '/^ID=/{print $2}' /etc/os-release)
elif [ "$(uname)" == "Darwin" ]; then
sys_os="macos"
fi
}
exec_script() {
eco_key="$1"
# Escaped path
script=$(echo "$2" | sed 's/ /\\ /g')
# Extract the name from script absolute file path
name=$(echo "${script#*_}" | sed 's/_/ /g' | sed 's/\.sh$//')
echo "[$eco_key] Processing $name..."
#output=$(timeout 30s bash -c "$script" 2>&1) # Redirect stderr to stdout
output=$(timeout 30s bash -c "$script" 2>&1 | tee -a /var/log/progsys.log)
if [ $? -ne 0 ]; then
echo "[ERROR] check /var/log/progsys.log"
echo "$output"
echo "continuing..."
fi
}
# For _Init_.sh
install_core_software() {
local core_directory="$sys_root/$sys_os/#Core"
local core_lock_file="/var/run/progsys.lock"
local core_init_script=$core_directory"/_Init_.sh"
local core_key="Core"
if [ ! -e "$core_lock_file" ]; then
exec_script "$core_key" "$core_init_script"
for script in "$core_directory"/*; do
if [ $script != "$core_init_script" ]; then
exec_script "$core_key" "$script"
fi
done
sudo touch "$core_lock_file"
fi
}
sanitize_raw_filename() {
local raw_filename="$1"
# Don't process files that are commented out
if ! [[ $raw_filename =~ ^# ]]; then
sanitized=$(echo "$line" | sed 's/ /_/g')
echo "$sanitized"
fi
}
install_ecosystem_package() {
local eco_key="$1"
local package="$2"
# TODO: better ways of match file number
script=$(find "$sys_root/$sys_os/$eco_key" -maxdepth 1 -type f -name "???_${package}.sh")
exec_script "$eco_key" "$script"
}
# Main installation logic. This reads in a "requirements.txt" file, inspired by the classic Python
# packaging format. If an selected ecosystem "flag" is matched, subsequent uncommented lines (keyed
# by package name) are installed.
install_ecosystems() {
local selected_flags=("$@")
mapfile -t lines < "$sys_root/$sys_os/requirements.txt"
for selected_flag_index in "${!selected_flags[@]}"; do
selected_flag="${selected_flags[$selected_flag_index]}"
for line in "${lines[@]}"; do
if [[ $line =~ ^## ]]; then
current_ecosystem=$(echo "$line" | sed -n 's/## \(.*\) ##/\1/p')
elif [[ ! "$line" =~ ^[[:space:]]*$ && ! "$line" =~ ^# ]]; then
flag_key="${selected_flag}"
ecos_key="${ecosystems[$flag_key]}"
filename=$(sanitize_raw_filename "$line")
if [ "$current_ecosystem" == "$ecos_key" ] && [ -n "$filename" ]; then
install_ecosystem_package "$ecos_key" "$filename"
fi
fi
done
done
}
main() {
sudo -v # prompt for sudo (unix-like standard)
check_args "$@"
detect_system
export sys_root="/home/$(whoami)/system"
# called in the scripts
sys_check() {
local package=$1
if [ -e "$package" ] && [ -d "$package" ]; then
return 0
elif [ -e "$package" ]; then
return 0
else
return 1
fi
}
export -f sys_check
sys_exec() { . "$sys_root/$1"; }
export -f sys_exec
if [[ "$1" == "--init" ]]; then
install_core_software
exec bash -l # done, reset
else
install_ecosystems "$@"
fi
}
main "$@"