Skip to content

Commit 97df5e6

Browse files
authored
Merge pull request #1 from jash90/add-scripts
Add shared library: lib/ with modular bash helpers
2 parents 725c02c + 261787e commit 97df5e6

17 files changed

Lines changed: 1595 additions & 0 deletions

lib/apache.sh

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/bin/bash
2+
# Konfiguracja Apache - noobs_lib modul
3+
[[ -n "${_NOOBS_APACHE_LOADED:-}" ]] && return 0
4+
readonly _NOOBS_APACHE_LOADED=1
5+
6+
# Uzycie: apache_create_vhost <name> <document_root> [port] [server_name]
7+
apache_create_vhost() {
8+
local vhost_name="$1"
9+
local doc_root="$2"
10+
local port="${3:-80}"
11+
local server_name="${4:-_}"
12+
13+
[[ -z "$vhost_name" ]] && { msg_error "Nie podano nazwy vhosta."; return 1; }
14+
[[ -z "$doc_root" ]] && { msg_error "Nie podano document root."; return 1; }
15+
require_root
16+
17+
local vhost_file="/etc/apache2/sites-available/${vhost_name}.conf"
18+
19+
msg_info "Tworzenie Apache VirtualHost: $vhost_name"
20+
21+
cat > "$vhost_file" <<EOF
22+
<VirtualHost *:${port}>
23+
ServerName ${server_name}
24+
DocumentRoot ${doc_root}
25+
26+
<Directory ${doc_root}>
27+
Options FollowSymLinks
28+
AllowOverride All
29+
Require all granted
30+
</Directory>
31+
32+
ErrorLog \${APACHE_LOG_DIR}/${vhost_name}_error.log
33+
CustomLog \${APACHE_LOG_DIR}/${vhost_name}_access.log combined
34+
</VirtualHost>
35+
EOF
36+
37+
msg_ok "VirtualHost utworzony: $vhost_file"
38+
}
39+
40+
# Uzycie: apache_create_alias <alias_name> <alias_url> <directory_path>
41+
apache_create_alias() {
42+
local alias_name="$1"
43+
local alias_url="$2"
44+
local dir_path="$3"
45+
46+
[[ -z "$alias_name" ]] && { msg_error "Nie podano nazwy aliasu."; return 1; }
47+
[[ -z "$alias_url" ]] && { msg_error "Nie podano URL aliasu."; return 1; }
48+
[[ -z "$dir_path" ]] && { msg_error "Nie podano sciezki katalogu."; return 1; }
49+
require_root
50+
51+
local alias_file="/etc/apache2/sites-available/${alias_name}.conf"
52+
53+
msg_info "Tworzenie Apache Alias: $alias_name"
54+
55+
cat > "$alias_file" <<EOF
56+
Alias ${alias_url} "${dir_path}/"
57+
58+
<Directory ${dir_path}/>
59+
Satisfy Any
60+
Require all granted
61+
AllowOverride All
62+
Options FollowSymLinks MultiViews
63+
64+
<IfModule mod_dav.c>
65+
Dav off
66+
</IfModule>
67+
</Directory>
68+
EOF
69+
70+
msg_ok "Alias utworzony: $alias_file"
71+
}
72+
73+
# Uzycie: webserver_enable_site <server_type> <site_name>
74+
webserver_enable_site() {
75+
local server_type="$1"
76+
local site_name="$2"
77+
78+
[[ -z "$server_type" ]] && { msg_error "Nie podano typu serwera."; return 1; }
79+
[[ -z "$site_name" ]] && { msg_error "Nie podano nazwy strony."; return 1; }
80+
81+
msg_info "Wlaczanie strony: $site_name ($server_type)"
82+
83+
case "$server_type" in
84+
apache)
85+
a2ensite "$site_name" >/dev/null 2>&1
86+
service_reload apache2
87+
;;
88+
nginx)
89+
ln -sf "/etc/nginx/sites-available/${site_name}" \
90+
"/etc/nginx/sites-enabled/${site_name}"
91+
service_reload nginx
92+
;;
93+
*)
94+
msg_error "Nieznany typ serwera: $server_type"
95+
return 1
96+
;;
97+
esac
98+
99+
msg_ok "Strona wlaczona: $site_name"
100+
}
101+
102+
# Uzycie: webserver_disable_site <server_type> <site_name>
103+
webserver_disable_site() {
104+
local server_type="$1"
105+
local site_name="$2"
106+
107+
[[ -z "$server_type" ]] && { msg_error "Nie podano typu serwera."; return 1; }
108+
[[ -z "$site_name" ]] && { msg_error "Nie podano nazwy strony."; return 1; }
109+
110+
msg_info "Wylaczanie strony: $site_name ($server_type)"
111+
112+
case "$server_type" in
113+
apache)
114+
a2dissite "$site_name" >/dev/null 2>&1
115+
service_reload apache2
116+
;;
117+
nginx)
118+
rm -f "/etc/nginx/sites-enabled/${site_name}"
119+
service_reload nginx
120+
;;
121+
*)
122+
msg_error "Nieznany typ serwera: $server_type"
123+
return 1
124+
;;
125+
esac
126+
127+
msg_ok "Strona wylaczona: $site_name"
128+
}

lib/config.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
# Operacje na plikach konfiguracyjnych - noobs_lib modul
3+
[[ -n "${_NOOBS_CONFIG_LOADED:-}" ]] && return 0
4+
readonly _NOOBS_CONFIG_LOADED=1
5+
6+
# Uzycie: config_set_value <file> <key> <value> [delimiter]
7+
config_set_value() {
8+
local file="$1"
9+
local key="$2"
10+
local value="$3"
11+
local delimiter="${4:-=}"
12+
13+
[[ -z "$file" ]] && { msg_error "Nie podano pliku konfiguracyjnego."; return 1; }
14+
[[ -z "$key" ]] && { msg_error "Nie podano klucza."; return 1; }
15+
[[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; }
16+
17+
local key_esc value_esc delim_esc
18+
key_esc=$(printf '%s' "$key" | sed 's/[]\/$*.^[|]/\\&/g')
19+
value_esc=$(printf '%s' "$value" | sed 's/[\\&|]/\\&/g')
20+
delim_esc=$(printf '%s' "$delimiter" | sed 's/[]\/$*.^[|]/\\&/g')
21+
22+
if grep -qE "^[#;]*[[:space:]]*${key_esc}[[:space:]]*${delim_esc}" "$file"; then
23+
sed -i "s|^[#;]*[[:space:]]*${key_esc}[[:space:]]*${delim_esc}.*|${key}${delimiter}${value_esc}|" "$file"
24+
msg_debug "Zmieniono: ${key}${delimiter}${value} w $file"
25+
else
26+
printf '%s%s%s\n' "$key" "$delimiter" "$value" >> "$file"
27+
msg_debug "Dodano: ${key}${delimiter}${value} do $file"
28+
fi
29+
}
30+
31+
# Uzycie: config_append_if_missing <file> <line>
32+
config_append_if_missing() {
33+
local file="$1"
34+
local line="$2"
35+
36+
[[ -z "$file" ]] && { msg_error "Nie podano pliku."; return 1; }
37+
[[ -z "$line" ]] && { msg_error "Nie podano linii."; return 1; }
38+
39+
[[ ! -f "$file" ]] && touch "$file"
40+
41+
if ! grep -qF "$line" "$file"; then
42+
echo "$line" >> "$file"
43+
msg_debug "Dodano do $file: $line"
44+
return 0
45+
else
46+
msg_debug "Linia juz istnieje w $file"
47+
return 1
48+
fi
49+
}
50+
51+
# Uzycie: config_remove_line <file> <pattern>
52+
config_remove_line() {
53+
local file="$1"
54+
local pattern="$2"
55+
56+
[[ -z "$file" ]] && { msg_error "Nie podano pliku."; return 1; }
57+
[[ -z "$pattern" ]] && { msg_error "Nie podano wzorca."; return 1; }
58+
[[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; }
59+
60+
sed -i "/${pattern}/d" "$file"
61+
msg_debug "Usunieto linie pasujace do '$pattern' z $file"
62+
}
63+
64+
# Uzycie: config_comment_line <file> <pattern> [comment_char]
65+
config_comment_line() {
66+
local file="$1"
67+
local pattern="$2"
68+
local comment="${3:-#}"
69+
70+
[[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; }
71+
72+
sed -i "s|^\(${pattern}.*\)|${comment}\1|" "$file"
73+
}
74+
75+
# Uzycie: config_uncomment_line <file> <pattern> [comment_char]
76+
config_uncomment_line() {
77+
local file="$1"
78+
local pattern="$2"
79+
local comment="${3:-#}"
80+
81+
[[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; }
82+
83+
sed -i "s|^${comment}\s*\(${pattern}.*\)|\1|" "$file"
84+
}
85+
86+
# Uzycie: secure_file <file> [mode]
87+
secure_file() {
88+
local file="$1"
89+
local mode="${2:-600}"
90+
91+
[[ -z "$file" ]] && { msg_error "Nie podano pliku."; return 1; }
92+
[[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; }
93+
94+
chown root:root "$file"
95+
chmod "$mode" "$file"
96+
msg_debug "Zabezpieczono plik: $file (mode: $mode)"
97+
}

lib/errors.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
# Obsluga bledow i pomoc - noobs_lib modul
3+
[[ -n "${_NOOBS_ERRORS_LOADED:-}" ]] && return 0
4+
readonly _NOOBS_ERRORS_LOADED=1
5+
6+
die() {
7+
msg_error "$1"
8+
exit "${2:-1}"
9+
}
10+
11+
trap_error() {
12+
trap 'msg_error "Blad w linii $LINENO. Kod wyjscia: $?"' ERR
13+
}
14+
15+
safe_exit() {
16+
local code="${1:-0}"
17+
if [[ -n "${_NOOBS_TEMP_DIR:-}" ]] && [[ -d "$_NOOBS_TEMP_DIR" ]]; then
18+
cleanup_temp "$_NOOBS_TEMP_DIR"
19+
fi
20+
exit "$code"
21+
}
22+
23+
run_or_die() {
24+
local error_msg="${2:-Blad podczas wykonywania: $1}"
25+
if ! "$@"; then
26+
die "$error_msg"
27+
fi
28+
}
29+
30+
show_help_template() {
31+
local name="$1"
32+
local description="$2"
33+
shift 2
34+
35+
echo "Uzycie: $name [opcje]"
36+
echo ""
37+
echo "$description"
38+
echo ""
39+
echo "Opcje:"
40+
for opt in "$@"; do
41+
echo " $opt"
42+
done
43+
}
44+
45+
show_version() {
46+
local name="$1"
47+
local version="$2"
48+
echo "$name wersja $version"
49+
echo "Biblioteka noobs_lib.sh wersja ${NOOBS_LIB_VERSION:-}"
50+
}
51+
52+
# Aliasy wstecznej kompatybilnosci
53+
_ask_input() { ask_input "$@"; }
54+
_service_exists() { service_exists "$@"; }
55+
status() { msg_status "$@"; }
56+
err() { die "$@"; }

0 commit comments

Comments
 (0)