diff --git a/Dockerfile b/Dockerfile index a3642fb..924979a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,5 +14,12 @@ RUN apt-get update && \ apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin && \ # - Install Docker # + Install Nixpacks - curl -sSL https://nixpacks.com/install.sh | bash + curl -sSL https://nixpacks.com/install.sh | bash && \ # - Install Nixpacks + echo 'All packages installed' + +WORKDIR /ptah/bin +COPY bin/ ./ +RUN chmod +x ./* + +ENV PATH="/ptah/bin:${PATH}" diff --git a/bin/docker_build.sh b/bin/docker_build.sh new file mode 100644 index 0000000..1f4e5e1 --- /dev/null +++ b/bin/docker_build.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +set -e + +# Source the shared library +. "$(dirname "$0")/../lib/validate.sh" + +check_var TARGET_DIR +check_var DOCKERFILE_PATH +check_var IMAGE_NAME +check_var REGISTRY_URL + +# Create a temporary build directory +BUILD_DIR=$(mktemp -d) +trap 'rm -rf "$BUILD_DIR"' EXIT + +# Copy the target directory contents to the build directory +cp -r "$TARGET_DIR"/* "$BUILD_DIR" + +# Change to the build directory +cd "$BUILD_DIR" +docker build -f "$DOCKERFILE_PATH" -t "$IMAGE_NAME" . + +# Tag the image with the registry URL +docker tag "$IMAGE_NAME" "${REGISTRY_URL}/${IMAGE_NAME}" + +# Push the image to the registry +docker push "${REGISTRY_URL}/${IMAGE_NAME}" diff --git a/bin/git_pull.sh b/bin/git_pull.sh new file mode 100644 index 0000000..f7254d2 --- /dev/null +++ b/bin/git_pull.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +set -e + +# Source the shared library +. "$(dirname "$0")/../lib/validate.sh" + +check_var GIT_REPO_URL +check_var GIT_REF +check_var TARGET_DIR + +# Create a lock file in /tmp (using TARGET_DIR in name to make it unique) +LOCK_FILE="/tmp/$(echo "${TARGET_DIR}" | sed 's/\//_/g').lock" +touch "$LOCK_FILE" + +# Attempt to acquire lock, fail if cannot obtain it +echo "Acquiring lock..." +( + if ! flock -n 9; then + echo "Error: Another process is currently updating the repository" + exit 1 + fi + + echo "Syncing with remote repository" + + if [ -d "$TARGET_DIR/.git" ]; then + echo "Repository already exists, updating origin and pulling latest changes" + cd "$TARGET_DIR" + # Update the remote origin URL if it has changed + git remote set-url origin "$GIT_REPO_URL" + git fetch origin + git checkout "$GIT_REF" + git pull origin "$GIT_REF" + else + echo "Cloning repository" + git clone --depth 1 "$GIT_REPO_URL" -b "$GIT_REF" "$TARGET_DIR" + fi +) 9>"$LOCK_FILE" diff --git a/bin/s3_download.sh b/bin/s3_download.sh new file mode 100644 index 0000000..f8183e9 --- /dev/null +++ b/bin/s3_download.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +set -e + +# Source the shared library +. "$(dirname "$0")/../lib/validate.sh" + +echo "Starting s3 download script validation" + +check_var SRC_FILE_PATH +check_var DEST_FILE_PATH +check_var S3_ACCESS_KEY +check_var S3_SECRET_KEY +check_var S3_ENDPOINT +check_var S3_REGION +check_var S3_BUCKET +check_var PATH_PREFIX + +DEST_FILE_PATH="/$DEST_FILE_PATH" + +echo "Removing $DEST_FILE_PATH/*" +rm -rf "$DEST_FILE_PATH/*" 2> /dev/null || true + +echo "Downloading from s3://$S3_BUCKET/$PATH_PREFIX/$SRC_FILE_PATH" + +case "$SRC_FILE_PATH" in + *".tar.gz") + ARCHIVE_FORMAT="tar.gz" + ;; + *".zip") + ARCHIVE_FORMAT="zip" + ;; + *) + echo "Unsupported archive format: $SRC_FILE_PATH" + exit 1 + ;; +esac + +ARCHIVE_PATH="/tmp/archive.$ARCHIVE_FORMAT" + +s3cmd --access_key "$S3_ACCESS_KEY" \ + --secret_key "$S3_SECRET_KEY" \ + --host "$S3_ENDPOINT" \ + --host-bucket "$S3_ENDPOINT" \ + --region "$S3_REGION" \ + get "s3://$S3_BUCKET/$PATH_PREFIX/$SRC_FILE_PATH" "$ARCHIVE_PATH" + +echo "Extracting $SRC_FILE_PATH to $DEST_FILE_PATH" + +case "$ARCHIVE_FORMAT" in + "tar.gz") + tar -xzvf "$ARCHIVE_PATH" -C "$DEST_FILE_PATH" + ;; + "zip") + apk add zip + unzip "$ARCHIVE_PATH" + ;; + *) + echo "Unsupported archive format: $ARCHIVE_FORMAT" + exit 1 + ;; +esac + +echo "Removing $ARCHIVE_PATH" +rm -f "$ARCHIVE_PATH" + +echo "Done" diff --git a/bin/s3_test_upload.sh b/bin/s3_test_upload.sh new file mode 100755 index 0000000..c268a37 --- /dev/null +++ b/bin/s3_test_upload.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +set -e + +# Source the shared library +. "$(dirname "$0")/../lib/validate.sh" + +echo "Starting s3 test upload validation" + +# Create temporary test file +TEST_FILE="/tmp/check-access.txt" + +# Generate content for the test file +{ + echo "https://ptah.sh" + echo "Test upload date: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" + echo "S3 Endpoint: $S3_ENDPOINT" + echo "S3 Region: $S3_REGION" + echo "S3 Bucket: $S3_BUCKET" + echo "Path Prefix: $PATH_PREFIX" +} > "$TEST_FILE" + +echo "Created test file with the following content:" +cat "$TEST_FILE" + +# Set up environment variables for s3_upload.sh +export ARCHIVE_FORMAT="" +export SRC_FILE_PATH="$TEST_FILE" +export DEST_FILE_PATH="check-access.txt" + +# Call the main upload script +"$(dirname "$0")/s3_upload.sh" + +echo "Test upload completed successfully" diff --git a/bin/s3_upload.sh b/bin/s3_upload.sh new file mode 100644 index 0000000..0c4d394 --- /dev/null +++ b/bin/s3_upload.sh @@ -0,0 +1,66 @@ +#!/bin/sh + +set -e + +# Source the shared library +. "$(dirname "$0")/../lib/validate.sh" + +echo "Starting s3 upload script validation" + +check_var SRC_FILE_PATH +check_var DEST_FILE_PATH +check_var S3_ACCESS_KEY +check_var S3_SECRET_KEY +check_var S3_ENDPOINT +check_var S3_REGION +check_var S3_BUCKET +check_var PATH_PREFIX + +SRC_FILE_PATH="/$SRC_FILE_PATH" + +if [ -n "$ARCHIVE_FORMAT" ]; then + if [ -d "$SRC_FILE_PATH" ]; then + cd "$SRC_FILE_PATH" + else + cd "$(dirname "$SRC_FILE_PATH")" + fi + + echo "Archiving $SRC_FILE_PATH" + + ARCHIVED_FILE="/tmp/archive.$ARCHIVE_FORMAT" + + case "$ARCHIVE_FORMAT" in + "tar.gz") + tar -czvf "$ARCHIVED_FILE" "." + ;; + "zip") + apk add zip + zip -r "$ARCHIVED_FILE" "." + ;; + *) + echo "Unsupported archive format: $ARCHIVE_FORMAT" + exit 1 + ;; + esac + + UPLOAD_FILE="$ARCHIVED_FILE" +else + UPLOAD_FILE="$SRC_FILE_PATH" +fi + +echo "Uploading $UPLOAD_FILE to s3://$S3_BUCKET/$PATH_PREFIX/$DEST_FILE_PATH" + +s3cmd --guess-mime-type \ + --access_key "$S3_ACCESS_KEY" \ + --secret_key "$S3_SECRET_KEY" \ + --host "$S3_ENDPOINT" \ + --host-bucket "$S3_ENDPOINT" \ + --region "$S3_REGION" \ + put "$UPLOAD_FILE" "s3://$S3_BUCKET/$PATH_PREFIX/$DEST_FILE_PATH" + +if [ -n "$ARCHIVE_FORMAT" ]; then + echo "Removing $ARCHIVED_FILE" + rm -f "$ARCHIVED_FILE" +fi + +echo "Done" diff --git a/lib/validate.sh b/lib/validate.sh new file mode 100644 index 0000000..a74a86a --- /dev/null +++ b/lib/validate.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Function to check if a variable is set and not empty +check_var() { + eval value=\$$1 + if [ -z "$value" ]; then + echo "Error: $1 is not set or is empty" + exit 1 + fi +} \ No newline at end of file