bacalhau-project / bacalhau

Compute over Data framework for public, transparent, and optionally verifiable computation
https://docs.bacalhau.org
Apache License 2.0
675 stars 88 forks source link

Install.sh should install a service file #3811

Open aronchick opened 5 months ago

aronchick commented 5 months ago

That runs Bacalhau using systemd (or equivalent)

js-ts commented 5 months ago

what do you want the service file to do ? I wrote these service files for the VMI to run bacalhau serve and restart the service when it fails

[Unit]
Description=Bacalhau Daemon
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
Environment="LOG_TYPE=json"
Environment="BACALHAU_DIR=/data"
Environment="BACALHAU_ENVIRONMENT=local"
Restart=always
RestartSec=5s
ExecStart=bacalhau serve ${args}

[Install]
WantedBy=multi-user.target
[Unit]
Description=Startup Script
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

[Service]
ExecStart=/usr/local/bin/startup-script.sh
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target
#! /bin/bash

echo "Startup script is running as user: $(whoami)"

CONFIG_FILE="/etc/config.yaml"

echo "Fetching node type..."
NODE_TYPE=$(curl -f http://metadata.google.internal/computeMetadata/v1/instance/attributes/node_type -H "Metadata-Flavor: Google")
CURL_STATUS=$?

if [ $CURL_STATUS -ne 0 ]; then
    echo "Error fetching NODE_TYPE. Exiting..."
    exit 1
fi

echo "Node Type: $NODE_TYPE"

if [ "$NODE_TYPE" = "requester" ]; then
    echo "Node type is requester."
    if [ ! -f $CONFIG_FILE ]; then
        sudo cp /etc/requester_config.yaml $CONFIG_FILE
    fi
else
    echo "Node type is not requester. Fetching requester IP..."
    REQUESTER_IP=$(curl -s -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/requester_ip")
    echo "Requester IP: $REQUESTER_IP"
    if [ ! -f $CONFIG_FILE ]; then
        sudo cp /etc/compute_config.yaml $CONFIG_FILE
        sed -i "s/\${requester_ip}/$REQUESTER_IP/g" $CONFIG_FILE
    fi
fi

if [ ! -d /data ]; then
    sudo mkdir -p /data
    sudo chmod 0700 /data
    echo "/data directory created."
else
    echo "/data directory already exists. No need to create."
fi
if [ ! -f /data/config.yaml ]; then
    echo "Moving configuration file..."
    sudo mv "$CONFIG_FILE" /data/config.yaml
else
    echo "Configuration file already exists, not moving."
fi

echo "Checking and moving service files..."
if [ ! -f /etc/systemd/system/bacalhau.service ]; then
    sudo mv /etc/bacalhau.service /etc/systemd/system/bacalhau.service
else
    echo "Service file already exists in target location, not moving."
fi

echo "Reloading systemctl daemon..."
systemctl daemon-reload

echo "Handling Docker service..."
systemctl enable docker
systemctl restart docker

echo "Enabling bacalhau service..."
systemctl enable bacalhau.service
systemctl restart bacalhau.service