Skip to content

Commit

Permalink
systemd module integration
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Dec 26, 2024
1 parent 0b4580c commit d2973d2
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dist/
*_creds.json

# Environment variables file
*env*
.env

# Ignore Prettier configuration overrides for development
.prettierignore
41 changes: 35 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ RUN ARCH=$(uname -m) && \
# Install Google Cloud SDK (architecture-aware)
RUN ARCH=$(uname -m) && \
if [ "$ARCH" = "x86_64" ]; then \
curl -sSL "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-504.0.0-linux-x86_64.tar.gz" -o google-cloud-sdk.tar.gz; \
curl -sSL "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-504.0.0-linux-x86_64.tar.gz" -o google-cloud-sdk.tar.gz; \
elif [ "$ARCH" = "aarch64" ]; then \
curl -sSL "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-504.0.0-linux-arm.tar.gz" -o google-cloud-sdk.tar.gz; \
curl -sSL "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-504.0.0-linux-arm.tar.gz" -o google-cloud-sdk.tar.gz; \
fi && \
tar -xzf google-cloud-sdk.tar.gz && \
./google-cloud-sdk/install.sh -q && \
Expand Down Expand Up @@ -85,15 +85,43 @@ RUN mkdir -p $GNUPGHOME && \
# Install Bitwarden CLI (architecture-aware)
RUN ARCH=$(uname -m) && \
if [ "$ARCH" = "x86_64" ]; then \
curl -Lso /usr/local/bin/bw "https://vault.bitwarden.com/download/linux/amd64/bw"; \
curl -Lso /usr/local/bin/bw "https://vault.bitwarden.com/download/linux/amd64/bw"; \
elif [ "$ARCH" = "aarch64" ]; then \
curl -Lso /usr/local/bin/bw "https://vault.bitwarden.com/download/linux/arm64/bw"; \
curl -Lso /usr/local/bin/bw "https://vault.bitwarden.com/download/linux/arm64/bw"; \
else \
echo "Unsupported architecture: $ARCH" && exit 1; \
echo "Unsupported architecture: $ARCH" && exit 1; \
fi && \
chmod +x /usr/local/bin/bw && \
rm -rf /tmp/* /var/tmp/*

# Prepare the system for systemd usage
RUN find /etc/systemd/system \
/lib/systemd/system \
-path '*.wants/*' \
-not -name '*journald*' \
-delete; \
systemctl set-default multi-user.target; \
systemctl mask \
tmp.mount \
etc-hostname.mount \
etc-hosts.mount \
etc-resolv.conf.mount \
-- -.mount \
swap.target \
getty.target \
getty-static.service \
dev-mqueue.mount \
cgproxy.service \
systemd-remount-fs.service \
sys-kernel-config.mount \
sys-kernel-debug.mount \
sys-fs-fuse-connections.mount \
systemd-logind.service \
systemd-random-seed.service \
systemd-tmpfiles-setup-dev.service \
systemd-tmpfiles-setup.service \
systemd-update-utmp.service

# Create a new user and group with specific UID and GID, and set permissions
RUN groupadd -g ${GID} ${USER} && \
useradd -l -m -u ${UID} -g ${GID} -s /bin/bash ${USER}
Expand All @@ -110,7 +138,8 @@ RUN mkdir -p /etc/worker /home/${USER}/.cd/bin /home/${USER}/.cd/configs && \
WORKDIR /home/${USER}

# Copy built-in worker.yml to the container
COPY ./src/configs/worker.yml /etc/worker/worker.yml
COPY ./src/configs /etc/worker
COPY ./src/scripts /usr/local/scripts

# Copy the bin, etc, and lib directories
COPY ./etc/home /home/${USER}/etc
Expand Down
10 changes: 10 additions & 0 deletions etc/home/default.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=${name}
After=${after}

[Service]
ExecStart=${exec_start}
Restart=always

[Install]
WantedBy=multi-user.target
9 changes: 9 additions & 0 deletions lib/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ source_if_exists "$SCRIPT_DIR/auth.sh"
source_if_exists "$SCRIPT_DIR/secrets.sh"
# shellcheck source=./cleanup.sh
source_if_exists "$SCRIPT_DIR/cleanup.sh"
# shellcheck source=./process_manager.sh
source_if_exists "$SCRIPT_DIR/process_manager.sh"
# shellcheck source=./worker_config.sh
source_if_exists "$SCRIPT_DIR/worker_config.sh"

Expand Down Expand Up @@ -86,6 +88,13 @@ configure_environment() {
return 1
fi

# Perform process manager setup
log_info "Setting up process manager..."
if ! generate_and_activate_services; then
log_error "Failed to activate services."
return 1
fi

log_info "Environment setup completed successfully."
}

Expand Down
53 changes: 53 additions & 0 deletions lib/process_manager.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# Function to check if systemd should be enabled
should_enable_systemd() {
if [ -f "$CONFIG_FILE" ]; then
return 0
else
return 1
fi
}

# Function to parse service information from YAML configuration
parse_service_info() {
local service_yaml="$1"
name=$(echo "$service_yaml" | yq e '.name' -)
exec_start=$(echo "$service_yaml" | yq e '.exec_start' -)
after=$(echo "$service_yaml" | yq e '.after' -)
}

# Function to create a systemd service file from a template
create_service_file() {
local template_file="$SERVICE_DIR/default.service"
sed -e "s|\${name}|$name|g" \
-e "s|\${exec_start}|$exec_start|g" \
-e "s|\${after}|$after|g" \
"$template_file" > "${SERVICE_DIR}/${name}.service"
}

# Main function to generate systemd service unit files from template based on services.yml
generate_and_activate_services() {
if ! should_enable_systemd; then
echo "Systemd is not enabled. services.yml not found."
return 1
fi

echo "services.yml found. Generating and managing systemd service files..."

yq e '.services[]' "$CONFIG_FILE" | while IFS= read -r service_yaml; do
parse_service_info "$service_yaml"

if [[ -n "$name" && -n "$exec_start" && -n "$after" ]]; then
create_service_file || { echo "Failed to create service file for $name"; return 1; }
echo "Service file for $name created."
else
echo "Missing required service fields for a service in services.yml"
return 1
fi
done
}

# Variables (these should be defined or passed to the script)
CONFIG_FILE="/etc/worker/services.yml"
SERVICE_DIR="/home/${USER}/etc"
4 changes: 4 additions & 0 deletions src/configs/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:

Check warning on line 1 in src/configs/services.yml

View workflow job for this annotation

GitHub Actions / Analyze YAML Files

1:1 [document-start] missing document start "---"
- name: myservice1
exec_start: /usr/local/scripts/process_example.sh
after: network.target
9 changes: 9 additions & 0 deletions src/scripts/process_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Script to run as a systemd service in a loop

while true; do
# echo "Service is running at $(date)" >> /tmp/service_example.log
echo "Service is running at $(date)"
sleep 5
done

0 comments on commit d2973d2

Please sign in to comment.