-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl12n-shell
executable file
·92 lines (79 loc) · 3.47 KB
/
l12n-shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
# This scripts runs bash on the CLI container.
# - if no argument is provided, opens an interactive shell in the container
# - arguments are passed to bash, e.g run `./l12n -c 'ls'` to run that command
# on the container
# Environment:
# - L12N_BUILD: set the path of the lambdatization repo to build the image
# - L12N_TRACE: more verbose error messages when something unexpected happens
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath ${BASH_SOURCE[0]})")" &> /dev/null && pwd )
DOCKER_DIR=$SCRIPT_DIR/docker/cli
# Forward the AWS config files from the host to the container
AWS_SHARED_CREDENTIALS_FILE="${AWS_SHARED_CREDENTIALS_FILE:=$HOME/.aws/credentials}"
AWS_CONFIG_FILE="${AWS_CONFIG_FILE:=$HOME/.aws/config}"
export HOST_CREDENTIALS_FILE="${HOST_CREDENTIALS_FILE:=$AWS_SHARED_CREDENTIALS_FILE}"
export HOST_AWS_CONFIG_FILE="${HOST_AWS_CONFIG_FILE:=$AWS_CONFIG_FILE}"
# REPO_DIR is the root directory of the repository
export REPO_DIR="${REPO_DIR:=/host$SCRIPT_DIR}"
# CALLING_DIR is the current working directory when calling this script
export CALLING_DIR="${CALLING_DIR:=/host$(pwd)}"
# The CLI runs `docker` commands from the container, so it needs to have access
# to the host Docker socket.
export HOST_DOCKER_SOCKET="${HOST_DOCKER_SOCKET:=/var/run/docker.sock}"
# The calling user id and group id (as well as the Docker group id) are
# propagated into the container so that bindfs can properly map read/write
# permissions on bind mounted files.
export HOST_UID="${HOST_UID:=$(id -u)}"
export HOST_GID="${HOST_GID:=$(id -g)}"
export HOST_DOCKER_GID="${HOST_DOCKER_GID:=$(stat -c '%g' ${HOST_DOCKER_SOCKET})}"
build () {
docker buildx build \
-t cloudfuse-io/lambdatization:cli \
--target cli \
-f $DOCKER_DIR/Dockerfile \
$SCRIPT_DIR \
1>&2 \
|| exit $? # call exit to fail CLI if build fails
}
docker image inspect cloudfuse-io/lambdatization:cli > /dev/null
if [ $? -ne 0 ] && [[ -z $L12N_BUILD ]]
then
echo "Run this script with the environment variable L12N_BUILD=1 to build the CLI image"
exit 1
fi
if [[ ! -z $L12N_BUILD ]] && [[ ! -f $DOCKER_DIR/Dockerfile ]]
then
echo "The script does not seem to be located in a valid L12N repository directory"
exit 1
fi
# only build if explicitely required
[[ -z $L12N_BUILD ]] || build
# Run image with both AWS credentials file and env credentials if available
# First argument passed to docker as flags
# Second argument passed to the container as command arguments
run () {
docker run \
$(echo "$1") \
--mount type=bind,source=$HOST_DOCKER_SOCKET,target=/var/run/docker.sock \
--mount type=bind,source=/,target=/mnt/host \
--mount source=l12n-cli-persistent-configs,target=/mnt/persistent-configs \
--env-file <(cat .env 2>/dev/null && true) \
-e AWS_PROFILE \
$([[ -f "$AWS_SHARED_CREDENTIALS_FILE" ]] && echo "-e AWS_SHARED_CREDENTIALS_FILE=/host$HOST_CREDENTIALS_FILE") \
$([[ -f "$AWS_CONFIG_FILE" ]] && echo "-e AWS_CONFIG_FILE=/host$HOST_AWS_CONFIG_FILE") \
-e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY \
--env-file <(env | grep L12N_) \
--env-file <(env | grep HOST_) \
--env-file <(env | grep TF_) \
-e REPO_DIR \
-e CALLING_DIR \
--hostname=lambdatization \
--rm \
--privileged \
cloudfuse-io/lambdatization:cli "${@:2}"
}
# if no argument is provided and no data is piped in, add the tty flag
tty_flag=$([[ $# -eq 0 ]] && [ -t 0 ] && echo '-t')
# run bash in the container with the provided arguments
run "-i $tty_flag" "$@"