Skip to content

Commit

Permalink
feat: add basic scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Nov 7, 2024
1 parent bac35d3 commit ffbafcf
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
28 changes: 28 additions & 0 deletions bin/docker_build.sh
Original file line number Diff line number Diff line change
@@ -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}"
38 changes: 38 additions & 0 deletions bin/git_pull.sh
Original file line number Diff line number Diff line change
@@ -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"
67 changes: 67 additions & 0 deletions bin/s3_download.sh
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions bin/s3_test_upload.sh
Original file line number Diff line number Diff line change
@@ -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"
66 changes: 66 additions & 0 deletions bin/s3_upload.sh
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions lib/validate.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ffbafcf

Please sign in to comment.