|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Function to display usage |
| 6 | +usage() { |
| 7 | + echo "Usage: $0 <url_or_local_file>" |
| 8 | + echo "Example: $0 https://github.com/nearai/private-ml-sdk/releases/download/v0.5.3.1/dstack-nvidia-0.5.3.1.tar.gz" |
| 9 | + echo "Example: $0 /path/to/local/file.tar.gz" |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +# Check if argument is provided |
| 14 | +if [ $# -ne 1 ]; then |
| 15 | + usage |
| 16 | +fi |
| 17 | + |
| 18 | +INPUT="$1" |
| 19 | +TEMP_DIR=$(mktemp -d) |
| 20 | +EXTRACT_DIR="$TEMP_DIR/extracted" |
| 21 | + |
| 22 | +# Cleanup function |
| 23 | +cleanup() { |
| 24 | + echo "Cleaning up temporary directory: $TEMP_DIR" |
| 25 | + rm -rf "$TEMP_DIR" |
| 26 | +} |
| 27 | +trap cleanup EXIT |
| 28 | + |
| 29 | +echo "Working directory: $TEMP_DIR" |
| 30 | + |
| 31 | +# Download or copy the file |
| 32 | +if [[ "$INPUT" =~ ^https?:// ]]; then |
| 33 | + echo "Downloading from URL: $INPUT" |
| 34 | + ARCHIVE_FILE="$TEMP_DIR/archive.tar.gz" |
| 35 | + if command -v curl >/dev/null 2>&1; then |
| 36 | + curl -L -o "$ARCHIVE_FILE" "$INPUT" |
| 37 | + elif command -v wget >/dev/null 2>&1; then |
| 38 | + wget -O "$ARCHIVE_FILE" "$INPUT" |
| 39 | + else |
| 40 | + echo "Error: Neither curl nor wget is available for downloading" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | +else |
| 44 | + echo "Using local file: $INPUT" |
| 45 | + if [ ! -f "$INPUT" ]; then |
| 46 | + echo "Error: Local file does not exist: $INPUT" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + ARCHIVE_FILE="$INPUT" |
| 50 | +fi |
| 51 | + |
| 52 | +# Create extraction directory |
| 53 | +mkdir -p "$EXTRACT_DIR" |
| 54 | + |
| 55 | +# Extract the archive |
| 56 | +echo "Extracting archive to: $EXTRACT_DIR" |
| 57 | +tar -xzf "$ARCHIVE_FILE" -C "$EXTRACT_DIR" |
| 58 | + |
| 59 | +# Find and read the digest |
| 60 | +DIGEST_FILE=$(find "$EXTRACT_DIR" -name "digest.txt" -type f | head -1) |
| 61 | +if [ -z "$DIGEST_FILE" ]; then |
| 62 | + echo "Error: digest.txt file not found in the extracted archive" |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +DIGEST=$(cat "$DIGEST_FILE" | tr -d '\n\r' | sed 's/[^a-zA-Z0-9]//g') |
| 67 | +if [ -z "$DIGEST" ]; then |
| 68 | + echo "Error: Could not read digest from $DIGEST_FILE" |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +echo "Found digest: $DIGEST" |
| 73 | + |
| 74 | +# Remove rootfs file(s) |
| 75 | +echo "Removing rootfs files..." |
| 76 | +find "$EXTRACT_DIR" -name "rootfs*" -type f -delete |
| 77 | +REMOVED_COUNT=$(find "$EXTRACT_DIR" -name "rootfs*" -type f 2>/dev/null | wc -l) |
| 78 | +if [ $REMOVED_COUNT -eq 0 ]; then |
| 79 | + echo "Rootfs files removed successfully" |
| 80 | +else |
| 81 | + echo "Warning: Some rootfs files may still exist" |
| 82 | +fi |
| 83 | + |
| 84 | +# Create flattened structure in a new directory |
| 85 | +FLATTEN_DIR="$TEMP_DIR/flattened" |
| 86 | +mkdir -p "$FLATTEN_DIR" |
| 87 | + |
| 88 | +echo "Flattening directory structure..." |
| 89 | +# Find all files (not directories) and copy them to the flattened directory |
| 90 | +find "$EXTRACT_DIR" -type f -exec cp {} "$FLATTEN_DIR/" \; |
| 91 | + |
| 92 | +# Count files for verification |
| 93 | +FILE_COUNT=$(find "$FLATTEN_DIR" -type f | wc -l) |
| 94 | +echo "Flattened $FILE_COUNT files" |
| 95 | + |
| 96 | +# Create the final archive with the digest-based name |
| 97 | +OUTPUT_FILE="mr_${DIGEST}.tar.gz" |
| 98 | +echo "Creating final archive: $OUTPUT_FILE" |
| 99 | + |
| 100 | +# Change to the flattened directory and create archive without directory structure |
| 101 | +cd "$FLATTEN_DIR" |
| 102 | +tar -czf "../$OUTPUT_FILE" * |
| 103 | +cd - >/dev/null |
| 104 | + |
| 105 | +# Move the final file to the current working directory |
| 106 | +mv "$TEMP_DIR/$OUTPUT_FILE" "./$OUTPUT_FILE" |
| 107 | + |
| 108 | +echo "Successfully created: $OUTPUT_FILE" |
| 109 | +echo "Archive contains $FILE_COUNT files with flattened structure" |
0 commit comments