-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46dbe85
commit 24f309d
Showing
3 changed files
with
252 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,115 @@ | ||
#!/bin/bash | ||
USAGE="Usage: ./build.sh [PREFIX] [VERSION] [TAG] [PLUGIN_NAME] [ARCH]" | ||
set -e | ||
|
||
if [[ "$1" == "-h" ]]; then | ||
echo "$USAGE" | ||
exit 0 | ||
fi | ||
# Colors for output | ||
RED='\033[0;31m' | ||
GREEN='\033[0;32m' | ||
YELLOW='\033[1;33m' | ||
NC='\033[0m' | ||
|
||
# Default values | ||
VERSION=${2:-latest} | ||
ARCH=${5:-linux/amd64} | ||
PLUGIN_NAME=${4:-swarm-csi-swaweedfs} | ||
PLUGIN_TAG=${3:-v1.2.0} | ||
PLUGIN_NAME=${4:-swarm-csi-seaweedfs} | ||
PLUGIN_TAG=${3:-v1.2.7} | ||
PREFIX=${1:-gradlon} | ||
|
||
docker build --platform ${ARCH} --build-arg BASE_IMAGE=chrislusf/seaweedfs-csi-driver:${VERSION} --build-arg ARCH=$ARCH -t seawadd-csi_tmp_img . | ||
mkdir -p ./plugin/rootfs | ||
cp config.json ./plugin/ | ||
docker container create --name seawadd-csi_tmp seawadd-csi_tmp_img | ||
docker container export seawadd-csi_tmp | tar -x -C ./plugin/rootfs | ||
docker container rm -vf seawadd-csi_tmp | ||
docker image rm seawadd-csi_tmp_img | ||
|
||
docker plugin disable gradlon/swarm-csi-swaweedfs:v1.2.0 | ||
docker plugin rm ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} 2> /dev/null || true | ||
docker plugin create ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} ./plugin | ||
docker plugin push ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} | ||
rm -rf ./plugin/ | ||
# Help function | ||
show_help() { | ||
echo "Build script for SeaweedFS CSI Docker Plugin" | ||
echo | ||
echo "Usage: $0 [PREFIX] [VERSION] [TAG] [PLUGIN_NAME] [ARCH]" | ||
echo | ||
echo "Parameters:" | ||
echo " PREFIX Docker registry prefix (default: gradlon)" | ||
echo " VERSION SeaweedFS CSI driver version (default: latest)" | ||
echo " TAG Plugin tag version (default: v1.2.0)" | ||
echo " PLUGIN_NAME Plugin name (default: swarm-csi-seaweedfs)" | ||
echo " ARCH Target architecture (default: linux/amd64)" | ||
echo | ||
echo "Example:" | ||
echo " $0 myregistry latest v1.2.0 swarm-csi-seaweedfs linux/amd64" | ||
} | ||
|
||
# Error handling function | ||
handle_error() { | ||
echo -e "${RED}Error: $1${NC}" | ||
exit 1 | ||
} | ||
|
||
# Cleanup function | ||
cleanup() { | ||
echo -e "${YELLOW}Cleaning up temporary resources...${NC}" | ||
docker container rm -f seaweed-csi_tmp 2>/dev/null || true | ||
docker image rm -f seaweed-csi_tmp_img 2>/dev/null || true | ||
rm -rf ./plugin 2>/dev/null || true | ||
} | ||
|
||
# Parse command line arguments | ||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | ||
show_help | ||
exit 0 | ||
fi | ||
|
||
# Trap for cleanup | ||
trap cleanup EXIT | ||
|
||
# Log build configuration | ||
echo -e "${GREEN}Building SeaweedFS CSI Plugin with:${NC}" | ||
echo -e "PREFIX: ${YELLOW}$PREFIX${NC}" | ||
echo -e "VERSION: ${YELLOW}$VERSION${NC}" | ||
echo -e "TAG: ${YELLOW}$PLUGIN_TAG${NC}" | ||
echo -e "PLUGIN_NAME: ${YELLOW}$PLUGIN_NAME${NC}" | ||
echo -e "ARCH: ${YELLOW}$ARCH${NC}" | ||
|
||
# Check required files | ||
for file in "config.json" "Dockerfile" "entrypoint.sh"; do | ||
if [ ! -f "$file" ]; then | ||
handle_error "Required file $file not found" | ||
fi | ||
done | ||
|
||
# Create plugin directory | ||
echo -e "${GREEN}Creating plugin directory structure...${NC}" | ||
mkdir -p ./plugin/rootfs || handle_error "Failed to create plugin directory" | ||
|
||
# Copy config file | ||
echo -e "${GREEN}Copying config.json...${NC}" | ||
cp config.json ./plugin/ || handle_error "Failed to copy config.json" | ||
|
||
# Build the image | ||
echo -e "${GREEN}Building Docker image...${NC}" | ||
docker build \ | ||
--platform ${ARCH} \ | ||
--build-arg BASE_IMAGE=chrislusf/seaweedfs-csi-driver:${VERSION} \ | ||
--build-arg ARCH=$ARCH \ | ||
-t seaweed-csi_tmp_img . || handle_error "Docker build failed" | ||
|
||
# Create and export container | ||
echo -e "${GREEN}Creating temporary container and extracting rootfs...${NC}" | ||
docker container create --name seaweed-csi_tmp seaweed-csi_tmp_img || handle_error "Failed to create temporary container" | ||
docker container export seaweed-csi_tmp | tar -x -C ./plugin/rootfs || handle_error "Failed to export container filesystem" | ||
|
||
# Remove existing plugin if it exists | ||
echo -e "${GREEN}Removing existing plugin if present...${NC}" | ||
docker plugin disable ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} 2>/dev/null || true | ||
docker plugin rm ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} 2>/dev/null || true | ||
|
||
# Create and push plugin | ||
echo -e "${GREEN}Creating plugin...${NC}" | ||
docker plugin create ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} ./plugin || handle_error "Failed to create plugin" | ||
|
||
echo -e "${GREEN}Pushing plugin to registry...${NC}" | ||
docker plugin push ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} || handle_error "Failed to push plugin" | ||
|
||
echo -e "${GREEN}Build completed successfully!${NC}" | ||
echo -e "${GREEN}Plugin: ${YELLOW}${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG}${NC}" | ||
|
||
# Usage instructions | ||
echo -e "\n${GREEN}To use the plugin:${NC}" | ||
echo -e "1. Install the plugin:" | ||
echo -e "${YELLOW}docker plugin install --grant-all-permissions ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG}${NC}" | ||
echo -e "\n2. Configure the plugin:" | ||
echo -e "${YELLOW}docker plugin set ${PLUGIN_NAME}:${PLUGIN_TAG} FILER=<IP>:<PORT>${NC}" | ||
echo -e "\n3. Enable the plugin:" | ||
echo -e "${YELLOW}docker plugin enable ${PLUGIN_NAME}:${PLUGIN_TAG}${NC}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,106 @@ | ||
#!/bin/sh | ||
|
||
MAX_RETRIES=5 | ||
RETRY_INTERVAL=5 | ||
|
||
check_filer_connection() { | ||
local filer=$1 | ||
curl -s -f "http://${filer}/dir/status" > /dev/null | ||
return $? | ||
} | ||
|
||
wait_for_filer() { | ||
local filer=$1 | ||
local retries=0 | ||
|
||
echo "Checking connection to filer: ${filer}" | ||
while [ $retries -lt $MAX_RETRIES ]; do | ||
if check_filer_connection "${filer}"; then | ||
echo "Successfully connected to filer ${filer}" | ||
return 0 | ||
fi | ||
retries=$((retries + 1)) | ||
echo "Failed to connect to filer ${filer}, attempt ${retries}/${MAX_RETRIES}" | ||
sleep $RETRY_INTERVAL | ||
done | ||
|
||
return 1 | ||
} | ||
|
||
cleanup_orphaned_mounts() { | ||
echo "Checking for orphaned mounts..." | ||
# Nettoyer les points de montage orphelins dans /data/published | ||
for mount in $(find /data/published -maxdepth 1 -type d); do | ||
if ! mountpoint -q "$mount"; then | ||
echo "Cleaning up orphaned mount: $mount" | ||
rm -rf "$mount" | ||
fi | ||
done | ||
} | ||
|
||
if [ -z "$FILER" ]; then | ||
echo "FILER is not set!" | ||
exit 1 | ||
echo "FILER is not set!" | ||
exit 1 | ||
fi | ||
|
||
# Validate and split filer endpoints | ||
IFS=',' read -r -a FILER_ENDPOINTS <<< "$FILER" | ||
CONNECTED=false | ||
|
||
for endpoint in "${FILER_ENDPOINTS[@]}"; do | ||
if wait_for_filer "$endpoint"; then | ||
CONNECTED=true | ||
ACTIVE_FILER=$endpoint | ||
break | ||
fi | ||
done | ||
|
||
if [ "$CONNECTED" = false ]; then | ||
echo "Failed to connect to any filer endpoint after ${MAX_RETRIES} attempts" | ||
exit 1 | ||
fi | ||
|
||
NODE_ID=$(cat /node_hostname) | ||
C_WRITER=${C_WRITER:-32} | ||
CMD="/seaweedfs-csi-driver --filer=$FILER --nodeid=${NODE_ID} --endpoint=unix://run/docker/plugins/seaweed.sock --concurrentWriters=${C_WRITER} --dataCenter=${DATACENTER} --dataLocality=none --logtostderr --map.uid=${UID_MAP} --map.gid=${GID_MAP} --cacheCapacityMB=${CACHE_SIZE} --cacheDir=/tmp/seaweedFS/docker-csi" | ||
|
||
# Ensure cache directory exists with proper permissions | ||
CACHE_DIR=${CACHE_DIR:-/tmp/seaweedFS/docker-csi} | ||
mkdir -p "$CACHE_DIR" | ||
chmod 755 "$CACHE_DIR" | ||
|
||
# Cleanup any orphaned mounts before starting | ||
cleanup_orphaned_mounts | ||
|
||
# Set up periodic health check | ||
( | ||
while true; do | ||
if ! check_filer_connection "$ACTIVE_FILER"; then | ||
echo "Lost connection to primary filer, attempting failover..." | ||
for endpoint in "${FILER_ENDPOINTS[@]}"; do | ||
if [ "$endpoint" != "$ACTIVE_FILER" ] && wait_for_filer "$endpoint"; then | ||
echo "Failing over to $endpoint" | ||
ACTIVE_FILER=$endpoint | ||
break | ||
fi | ||
done | ||
fi | ||
sleep 30 | ||
done | ||
) & | ||
|
||
# Start the CSI driver with improved options | ||
CMD="/seaweedfs-csi-driver \ | ||
--filer=$FILER \ | ||
--nodeid=${NODE_ID} \ | ||
--endpoint=unix://run/docker/plugins/seaweed.sock \ | ||
--concurrentWriters=${C_WRITER} \ | ||
--dataCenter=${DATACENTER} \ | ||
--dataLocality=none \ | ||
--logtostderr \ | ||
--map.uid=${UID_MAP} \ | ||
--map.gid=${GID_MAP} \ | ||
--cacheCapacityMB=${CACHE_SIZE:-256} \ | ||
--cacheDir=${CACHE_DIR} \ | ||
--v=2" | ||
|
||
exec $CMD |