-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudview
executable file
·125 lines (114 loc) · 2.91 KB
/
cloudview
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
#!/bin/bash
# Allow use of DOCKER=podman
DOCKER="${DOCKER:-podman}"
IMAGE="${IMAGE:-ghcr.io/ricardobranco777/cloudview:latest}"
ARGS=("$@")
CHECK_CERTIFICATES=(
/etc/ssl/certs/ca-certificates.crt
/etc/pki/tls/certs/ca-bundle.crt
/etc/ssl/ca-bundle.pem
/etc/ssl/cert.pem
)
CHECK_VARIABLES=(
REQUESTS_CA_BUNDLE
# EC2
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
# Azure
AZURE_CLIENT_ID
AZURE_CLIENT_SECRET
AZURE_TENANT_ID
AZURE_SUBSCRIPTION_ID
# Azure (Terraform)
ARM_CLIENT_ID
ARM_CLIENT_SECRET
ARM_TENANT_ID
ARM_SUBSCRIPTION_ID
# GCE
GOOGLE_APPLICATION_CREDENTIALS
)
# Openstack
mapfile -t openstack_variables < <(env | grep ^OS_ | awk -F= '{ print $1 }')
CHECK_VARIABLES+=("${openstack_variables[@]}")
name="cloudview$$"
container_options=(
--security-opt label=disable
--rm # Remove container after running
-it # Allow user to interrupt execution
--net=host # Use host's DNS
--name "$name"
-e LIBCLOUD_DEBUG="$LIBCLOUD_DEBUG"
)
check_certificates() {
if [ -z "$REQUESTS_CA_BUNDLE" ] ; then
for file in "${CHECK_CERTIFICATES[@]}" ; do
if [ -f "$file" ] ; then
export REQUESTS_CA_BUNDLE="$file"
fi
done
fi
}
check_certificates
# Get clouds.yaml
get_config () {
index=-1
for ((i=0; i<${#ARGS[@]}; i++)) ; do
if [[ ${ARGS[i]} =~ ^-c|--config$ ]] ; then
index="$i"
break
fi
done
if [[ $index -ge 0 ]] ; then
clouds_yaml="${ARGS[$((index+1))]}"
if [ -z "$clouds_yaml" ] ; then
echo "ERROR: the ${ARGS[$index]} option needs an argument" >&2
exit 1
elif [ ! -f "$clouds_yaml" ] ; then
echo "ERROR: No such file: $clouds_yaml" >&2
exit 1
fi
else
clouds_yaml="${clouds_yaml:-$HOME/clouds.yaml}"
if [ -f "$clouds_yaml" ] ; then
ARGS+=(--config "$clouds_yaml")
fi
fi
}
get_config
# Mount as volumes all values in clouds.yaml that are pathnames
volumes=()
if [ -f "$clouds_yaml" ] ; then
volumes+=(-v "$clouds_yaml:$clouds_yaml:ro,Z")
if [[ $(stat -c '%u' "$clouds_yaml") -ne $EUID ]] ; then
volumes+=(-v "$clouds_yaml:$clouds_yaml:ro")
else
volumes+=(-v "$clouds_yaml:$clouds_yaml:ro,Z")
fi
mapfile -t values < <(sed -re 's/#.*//' -e 's/"(.*)"/\1/' -e "s/'(.*)'/\1/" < "$clouds_yaml" | awk '$NF ~ /^\// { print $NF }')
for value in "${values[@]}" ; do
if [ -f "$value" ] ; then
if [[ $(stat -c '%u' "$value") -ne $EUID ]] ; then
volumes+=(-v "$value:$value:ro")
else
volumes+=(-v "$value:$value:ro,Z")
fi
fi
done
fi
# Add variables
variables=()
for var in "${CHECK_VARIABLES[@]}" ; do
if [[ -v $var ]] ; then
variables+=(-e "$var")
fi
# Mount as volume if variable is a file
if [[ -f ${!var} ]] ; then
if [[ $(stat -c '%u' "${!var}") -ne $EUID ]] ; then
volumes+=(-v "${!var}:${!var}:ro")
else
volumes+=(-v "${!var}:${!var}:ro,Z")
fi
fi
done
#echo "$DOCKER" run "${container_options[@]}" "${variables[@]}" "${volumes[@]}" "$IMAGE" "${ARGS[@]}"
exec "$DOCKER" run "${container_options[@]}" "${variables[@]}" "${volumes[@]}" "$IMAGE" "${ARGS[@]}"