Files
scwrypts/zsh/cloud/aws/efs/mount
T

70 lines
1.8 KiB
Bash
Raw Normal View History

2022-04-28 16:09:23 -06:00
#!/bin/zsh
2023-02-21 18:44:27 -07:00
DEPENDENCIES+=(jq)
REQUIRED_ENV+=(AWS__EFS__LOCAL_MOUNT_POINT)
use cloud/aws/cli
CHECK_ENVIRONMENT
2022-04-28 16:09:23 -06:00
#####################################################################
2023-02-21 18:44:27 -07:00
EFS_CONNECT() {
GETSUDO || exit 1
2022-04-28 16:09:23 -06:00
[ ! -d $AWS__EFS__LOCAL_MOUNT_POINT ] && {
sudo mkdir $AWS__EFS__LOCAL_MOUNT_POINT \
2023-02-21 18:44:27 -07:00
&& STATUS "created local mount point '$AWS__EFS__LOCAL_MOUNT_POINT'"
2022-04-28 16:09:23 -06:00
}
local FS_ID=$(\
2023-02-21 18:44:27 -07:00
AWS efs describe-file-systems \
2022-04-28 16:09:23 -06:00
| jq -r '.[] | .[] | .FileSystemId' \
2023-02-21 18:44:27 -07:00
| FZF 'select a filesystem to mount' \
2022-04-28 16:09:23 -06:00
)
2023-02-21 18:44:27 -07:00
[ ! $FS_ID ] && ABORT
2022-04-28 16:09:23 -06:00
local MOUNT_POINT="$AWS__EFS__LOCAL_MOUNT_POINT/$FS_ID"
2022-07-01 20:57:03 -06:00
[ -d "$MOUNT_POINT" ] && sudo rmdir "$MOUNT_POINT" >/dev/null 2>&1
2022-04-28 16:09:23 -06:00
[ -d "$MOUNT_POINT" ] && {
2023-02-21 18:44:27 -07:00
STATUS "$FS_ID is already mounted"
2022-04-28 16:09:23 -06:00
exit 0
}
2023-02-21 18:44:27 -07:00
local MOUNT_TARGETS=$(AWS efs describe-mount-targets --file-system-id $FS_ID)
2022-04-28 16:09:23 -06:00
local ZONE=$(\
echo $MOUNT_TARGETS \
| jq -r '.[] | .[] | .AvailabilityZoneName' \
2023-02-21 18:44:27 -07:00
| sort -u | FZF 'select availability zone'\
2022-04-28 16:09:23 -06:00
)
2023-02-21 18:44:27 -07:00
[ ! $ZONE ] && ABORT
2022-04-28 16:09:23 -06:00
local MOUNT_IP=$(\
echo $MOUNT_TARGETS \
| jq -r ".[] | .[] | select (.AvailabilityZoneName == \"$ZONE\") | .IpAddress" \
| head -n1 \
)
2023-02-21 18:44:27 -07:00
SUCCESS 'ready to mount!'
REMINDER 'for private file-systems, you must be connected to the appropriate VPN'
2022-04-28 16:09:23 -06:00
2023-02-21 18:44:27 -07:00
STATUS "file system id : $FS_ID"
STATUS "availability zone : $ZONE"
STATUS "file system ip : $MOUNT_IP"
STATUS "local mount point : $MOUNT_POINT"
2022-04-28 16:09:23 -06:00
2023-02-21 18:44:27 -07:00
Yn 'proceed?' || ABORT
2022-04-28 16:09:23 -06:00
sudo mkdir $MOUNT_POINT \
&& sudo mount \
-t nfs4 \
-o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport \
$MOUNT_IP:/ \
"$MOUNT_POINT" \
2023-02-21 18:44:27 -07:00
&& SUCCESS "mounted at '$MOUNT_POINT'" \
2022-04-28 16:09:23 -06:00
|| {
sudo rmdir $MOUNT_POINT >/dev/null 2>&1
2023-02-21 18:44:27 -07:00
FAIL 2 "unable to mount '$FS_ID'"
2022-04-28 16:09:23 -06:00
}
}
#####################################################################
2023-02-21 18:44:27 -07:00
EFS_CONNECT $@