Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@
- **Install pasarguard with SQLite**:

```bash
curl -fsSLo /tmp/pg.sh https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh && sudo bash /tmp/pg.sh install
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh)" @ install
```

- **Install pasarguard with MySQL**:

```bash
curl -fsSLo /tmp/pg.sh https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh && sudo bash /tmp/pg.sh install --database mysql
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh)" @ install --database mysql
```

- **Install pasarguard with PostgreSQL**:

```bash
curl -fsSLo /tmp/pg.sh https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh && sudo bash /tmp/pg.sh install --database postgresql
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh)" @ install --database postgresql
```

- **Install pasarguard with TimescaleDB(v1+ only) and pre-release version**:

```bash
curl -fsSLo /tmp/pg.sh https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh && sudo bash /tmp/pg.sh install --database timescaledb --pre-release
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh)" @ install --database timescaledb --pre-release
```

- **Install pasarguard with MariaDB and Dev branch**:

```bash
curl -fsSLo /tmp/pg.sh https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh && sudo bash /tmp/pg.sh install --database mariadb --dev
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh)" @ install --database mariadb --dev
```

- **Install pasarguard with MariaDB and Manual version**:

```bash
curl -fsSLo /tmp/pg.sh https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh && sudo bash /tmp/pg.sh install --database mariadb --version v0.5.2
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pasarguard.sh)" @ install --database mariadb --version v0.5.2
```

## Installing Node
Expand All @@ -63,22 +63,22 @@

- **Install Node**
```bash
curl -fsSLo /tmp/pg-node.sh https://github.com/PasarGuard/scripts/raw/main/pg-node.sh && sudo bash /tmp/pg-node.sh install
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pg-node.sh)" @ install
```
- **Install Node Manual version:**
```bash
curl -fsSLo /tmp/pg-node.sh https://github.com/PasarGuard/scripts/raw/main/pg-node.sh && sudo bash /tmp/pg-node.sh install --version 0.1.0
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pg-node.sh)" @ install --version 0.1.0
```
- **Install Node pre-release version:**

```bash
curl -fsSLo /tmp/pg-node.sh https://github.com/PasarGuard/scripts/raw/main/pg-node.sh && sudo bash /tmp/pg-node.sh install --pre-release
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pg-node.sh)" @ install --pre-release
```

- **Install Node with custom name:**

```bash
curl -fsSLo /tmp/pg-node.sh https://github.com/PasarGuard/scripts/raw/main/pg-node.sh && sudo bash /tmp/pg-node.sh install --name Node2
sudo bash -c "$(curl -fsSL https://github.com/PasarGuard/scripts/raw/main/pg-node.sh)" @ install --name Node2
```

> 📌 **Tip:**
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
101 changes: 101 additions & 0 deletions lib/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash

colorized_echo() {
local color="$1"
local text="$2"
local style="${3:-0}"

case "$color" in
red)
printf "\e[${style};91m%s\e[0m\n" "$text"
;;
green)
printf "\e[${style};92m%s\e[0m\n" "$text"
;;
yellow)
printf "\e[${style};93m%s\e[0m\n" "$text"
;;
blue)
printf "\e[${style};94m%s\e[0m\n" "$text"
;;
magenta)
printf "\e[${style};95m%s\e[0m\n" "$text"
;;
cyan)
printf "\e[${style};96m%s\e[0m\n" "$text"
;;
*)
printf "%s\n" "$text"
;;
esac
}

die() {
colorized_echo red "$*"
exit 1
}

temp_root_dir() {
local root=""

if [ -n "${APP_TMP_DIR:-}" ]; then
root="$APP_TMP_DIR"
elif [ -n "${DATA_DIR:-}" ]; then
root="$DATA_DIR/tmp"
elif [ -n "${APP_NAME:-}" ]; then
root="/var/lib/$APP_NAME/tmp"
else
root="/var/lib/pasarguard-scripts/tmp"
fi

mkdir -p "$root"
printf '%s\n' "$root"
}

create_temp_dir() {
local prefix="${1:-tmpdir}"
local root=""
local candidate=""
local attempt=0

root=$(temp_root_dir)
while [ "$attempt" -lt 20 ]; do
candidate="${root}/${prefix}-$$-${RANDOM}-${attempt}"
if mkdir "$candidate" 2>/dev/null; then
printf '%s\n' "$candidate"
return 0
fi
attempt=$((attempt + 1))
done

die "Failed to create temporary directory in $root"
}

create_temp_file() {
local prefix="${1:-tmpfile}"
local suffix="${2:-}"
local root=""

root=$(temp_root_dir)
create_temp_file_in_dir "$root" "$prefix" "$suffix"
}

create_temp_file_in_dir() {
local dir="$1"
local prefix="${2:-tmpfile}"
local suffix="${3:-}"
local candidate=""
local attempt=0

mkdir -p "$dir"
while [ "$attempt" -lt 20 ]; do
candidate="${dir}/${prefix}-$$-${RANDOM}-${attempt}${suffix}"
if (set -C; : >"$candidate") 2>/dev/null; then
printf '%s\n' "$candidate"
return 0
fi
attempt=$((attempt + 1))
done

die "Failed to create temporary file in $dir"
}
35 changes: 35 additions & 0 deletions lib/docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

install_docker() {
colorized_echo blue "Installing Docker"
if ! bash -o pipefail -c 'curl -fsSL https://get.docker.com | sh'; then
die "Failed to install Docker"
fi
colorized_echo green "Docker installed successfully"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

detect_compose() {
if docker compose version >/dev/null 2>&1; then
COMPOSE='docker compose'
elif docker-compose version >/dev/null 2>&1; then
COMPOSE='docker-compose'
else
die "docker compose not found"
fi
}

compose_up() {
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" up -d --remove-orphans
}

compose_down() {
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" down
}

compose_logs() {
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" logs
}

compose_logs_follow() {
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" logs -f
}
100 changes: 100 additions & 0 deletions lib/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash

replace_or_append_env_var() {
local key="$1"
local value="$2"
local quote_value="${3:-false}"
local target_file="${4:-$ENV_FILE}"
local formatted_value="$value"
local escaped_value=""

if [ "$quote_value" = "true" ]; then
local sanitized_value="${value//\"/\\\"}"
formatted_value="\"$sanitized_value\""
fi

escaped_value=$(printf '%s' "$formatted_value" | sed -e 's/[&|\\]/\\&/g')

if grep -q "^$key=" "$target_file"; then
sed -i "s|^$key=.*|$key=$escaped_value|" "$target_file"
else
printf '%s=%s\n' "$key" "$formatted_value" >>"$target_file"
fi
}

set_or_uncomment_env_var() {
local key="$1"
local value="$2"
local quote_value="${3:-false}"
local target_file="${4:-$ENV_FILE}"
local formatted_value="$value"
local tmp_file=""
local target_dir=""

if [ "$quote_value" = "true" ]; then
local sanitized_value="${value//\"/\\\"}"
formatted_value="\"$sanitized_value\""
fi

[ -f "$target_file" ] || touch "$target_file"
target_dir=$(dirname "$target_file")
tmp_file=$(create_temp_file_in_dir "$target_dir" "env-edit" ".tmp")

awk -v env_key="$key" -v env_line="${key} = ${formatted_value}" '
BEGIN { replaced = 0 }
{
if ($0 ~ "^[[:space:]]*#?[[:space:]]*" env_key "[[:space:]]*=") {
if (replaced == 0) {
print env_line
replaced = 1
}
next
}
print
}
END {
if (replaced == 0) {
print env_line
}
}
' "$target_file" >"$tmp_file"

mv "$tmp_file" "$target_file"
}

comment_out_env_var() {
local key="$1"
local target_file="${2:-$ENV_FILE}"
local tmp_file=""
local target_dir=""

[ -f "$target_file" ] || return 0
target_dir=$(dirname "$target_file")
tmp_file=$(create_temp_file_in_dir "$target_dir" "env-comment" ".tmp")

awk -v env_key="$key" '
BEGIN { done = 0 }
{
if ($0 ~ "^[[:space:]]*#?[[:space:]]*" env_key "[[:space:]]*=") {
if (done == 0) {
line = $0
sub("^[[:space:]]*#?[[:space:]]*" env_key "[[:space:]]*=[[:space:]]*", "", line)
print "# " env_key " = " line
done = 1
}
next
}
print
}
' "$target_file" >"$tmp_file"

mv "$tmp_file" "$target_file"
}

delete_env_var() {
local key="$1"
local target_file="${2:-$ENV_FILE}"

[ -f "$target_file" ] || return 0
sed -i "/^[[:space:]]*${key}[[:space:]]*=/d" "$target_file"
}
78 changes: 78 additions & 0 deletions lib/github.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash

SHARED_LIB_INSTALL_DIR="/usr/local/lib/pasarguard-scripts/lib"

github_raw_url() {
local repo="$1"
local path="$2"

printf 'https://github.com/%s/raw/main/%s\n' "$repo" "$path"
}

github_download_file() {
local url="$1"
local target_path="$2"

curl -fsSL "$url" -o "$target_path"
}

github_install_script_from_repo() {
local repo="$1"
local script_name="$2"
local install_name="$3"
local tmp_file=""

tmp_file=$(mktemp) || return 1
trap 'rm -f "$tmp_file"' RETURN

if ! curl -fSL "$(github_raw_url "$repo" "$script_name")" -o "$tmp_file"; then
trap - RETURN
rm -f "$tmp_file"
return 1
fi

if ! chmod 755 "$tmp_file"; then
trap - RETURN
rm -f "$tmp_file"
return 1
fi

if ! install -m 755 "$tmp_file" "/usr/local/bin/$install_name"; then
trap - RETURN
rm -f "$tmp_file"
return 1
fi

trap - RETURN
rm -f "$tmp_file"
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

install_shared_libs_from_local() {
local source_dir="$1"
shift
local lib_name=""

mkdir -p "$SHARED_LIB_INSTALL_DIR"
for lib_name in "$@"; do
if [ -f "$source_dir/lib/$lib_name" ]; then
install -m 644 "$source_dir/lib/$lib_name" "$SHARED_LIB_INSTALL_DIR/$lib_name"
fi
done
}

install_shared_libs_from_repo() {
local fetch_repo="$1"
shift
local tmp_dir=""
local lib_name=""

tmp_dir=$(create_temp_dir "shared-libs")
mkdir -p "$SHARED_LIB_INSTALL_DIR"

for lib_name in "$@"; do
github_download_file "$(github_raw_url "$fetch_repo" "lib/$lib_name")" "$tmp_dir/$lib_name"
install -m 644 "$tmp_dir/$lib_name" "$SHARED_LIB_INSTALL_DIR/$lib_name"
done

rm -rf "$tmp_dir"
}
Loading