-
Notifications
You must be signed in to change notification settings - Fork 0
/
mars_macos.sh
executable file
·140 lines (119 loc) · 4.1 KB
/
mars_macos.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
set -eo pipefail
DEV_MOUNTS=''
if [[ "$MARS_DEV" == "true" ]]; then
DEFAULT_MARS_DEV_ROOT="$(dirname $(greadlink -f $(which mars)))"
MARS_DEV_ROOT=${MARS_DEV_ROOT:-DEFAULT_MARS_DEV_ROOT}
DEV_MOUNTS="-v ${MARS_DEV_ROOT}/scripts:/opt/mars:ro \
-v ${MARS_DEV_ROOT}/ansible-roles:/opt/ansible/roles:ro \
-v ${MARS_DEV_ROOT}/ansible-plugins:/opt/ansible/plugins:ro"
fi
if [[ "$MARS_DEBUG" == "true" ]]; then
set -x
fi
fullpath() {
cd "$1" && pwd
}
getroot() {
if ! GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null); then
return
fi
if [ -z "$PROJECT_PATH"]; then
if [ -n "$GIT_ROOT" ]; then
PROJECT_PATH="$GIT_ROOT"
fi
fi
}
# NOTE: TFENV_CACHE_PATH is deliberately not the same path as tfenv's default
# installation on the host machine. On macOS mounting that path would download
# linux binaries which would render tfenv unusable outside of the container.
# While brew puts tfenv in a special place that opens up the Linux default of
# ~/.tfenv/versions but we might as well just use our own custom location.
TFENV_CACHE_PATH="$HOME/.mars/tfenv/versions"
# Cache directory for provider plugins
TF_PLUGIN_CACHE_DIR="$HOME/.mars/tf-plugin-cache"
# NOTE: ANSIBLE_INVENTORY_CACHE_MOUNT is a property of the ec2.py configuration
# (in ec2.ini) which defaults to ~/.ansible (/opt/home/.ansible in the
# container). So it is possible for an inventory configuration to fail by
# changing the property from its default value.
# NOTE2: Because Docker Desktop on macos is not able to mount unix sockets the
# inventory cache directory is persisted through a docker-native volume,
# ANSIBLE_INVENTORY_CACHE_VOL.
ANSIBLE_INVENTORY_CACHE_VOL=mars_ansible_inventory_cache
ANSIBLE_INVENTORY_CACHE_MOUNT="/opt/home/.ansible"
DOCKER_IMAGE=luthersystems/mars
END_USER=$(id -u $USER):$(id -g $USER)
DOCKER_PROJECT_PATH=/marsproject
getroot
PROJECT_PATH=$(fullpath ${PROJECT_PATH:-$(pwd)})
WORK_REL_PATH="${PWD#$PROJECT_PATH}" # Includes leading dir separator
DOCKER_WORK_DIR="$DOCKER_PROJECT_PATH$WORK_REL_PATH"
MARS_VERSION=latest
if [ -f "$PROJECT_PATH/.mars-version" ]; then
MARS_VERSION=$(cat $PROJECT_PATH/.mars-version)
elif [ -f "$GIT_ROOT/.mars-version" ]; then
MARS_VERSION=$(cat $GIT_ROOT/.mars-version)
fi
ENV_VARS=
if [ -n "${TF_LOG+x}" ]; then
# TF_LOG has been set. Forward it to the docker env.
ENV_VARS="-e TF_LOG=$TF_LOG $ENV_VARS"
fi
DOCKER_TERM_VARS=-i
if [ -t 1 -a ! -p /dev/stdin ]; then
DOCKER_TERM_VARS=-it
fi
SHELL_OPTS=
if [[ "$MARS_SHELL" == "true" ]]; then
SHELL_OPTS="--entrypoint /bin/bash"
fi
LOCAL_OPTS=
if [[ "$MARS_LOCAL" == "true" ]]; then
LOCAL_OPTS="-e ANSIBLE_TRANSPORT=local -e ANSIBLE_PIPELINING=True"
fi
NETWORK_OPTS=
if [[ -n "$MARS_NETWORK" ]]; then
NETWORK_OPTS="--network ${MARS_NETWORK}"
fi
# include GitHub credentials if available
if command -v gh >/dev/null; then
export GITHUB_TOKEN="$(gh auth token 2>/dev/null)"
fi
mkdir -p $TFENV_CACHE_PATH
mkdir -p $TF_PLUGIN_CACHE_DIR
if ! command -v docker >/dev/null; then
echo >&2 "Unable to locate docker. Please install docker first."
exit 1
fi
PINATA_OPTS=""
if command -v pinata-ssh-forward >/dev/null; then
PINATA_OPTS="$(pinata-ssh-mount)"
if [ -z "$(docker ps | grep pinata-sshd)" ]; then
echo 2>&1 "pinata-sshd not found; starting..."
pinata-ssh-forward
fi
fi
docker volume create "$ANSIBLE_INVENTORY_CACHE_VOL" >/dev/null
docker run --rm $DOCKER_TERM_VARS \
-e USER_ID=$(id -u $USER) \
-e GROUP_ID=$(id -g $USER) \
-e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY \
-e AWS_SECURITY_TOKEN -e AWS_SESSION_TOKEN \
-e TF_PLUGIN_CACHE_DIR=/opt/tf-plugin-cache-dir \
-e GITHUB_TOKEN \
$ENV_VARS \
$DEV_MOUNTS \
-v "$ANSIBLE_INVENTORY_CACHE_VOL:$ANSIBLE_INVENTORY_CACHE_MOUNT" \
-v "$TFENV_CACHE_PATH:/opt/tfenv/versions" \
-v "$TF_PLUGIN_CACHE_DIR:/opt/tf-plugin-cache-dir" \
-v "$HOME/.aws/:/opt/home/.aws" \
-v "$HOME/.azure/:/opt/home/.azure" \
-v "$PROJECT_PATH:$DOCKER_PROJECT_PATH" \
-w "$DOCKER_WORK_DIR" \
-e ANSIBLE_LOAD_CALLBACK_PLUGINS=yes \
-e ANSIBLE_STDOUT_CALLBACK=yaml \
$LOCAL_OPTS \
$NETWORK_OPTS \
$SHELL_OPTS \
$PINATA_OPTS \
"$DOCKER_IMAGE:$MARS_VERSION" "$@"