-
Notifications
You must be signed in to change notification settings - Fork 2
/
.bash_docker_specific
171 lines (144 loc) · 4.86 KB
/
.bash_docker_specific
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
DOCKER_CHECK_SENTINEL_FILE=/tmp/docker_installation_in_progress
DOCKER_SOCK_PATH_DARWIN="unix://$HOME/.lima/docker.sock"
DOCKER_SOCK_PATH_WINDOWS="tcp://0.0.0.0:2375"
LIMA_MACHINE_SOURCE_PATH="${LIMA_MACHINE_PATH:-$HOME/src/setup/lima_machine.yaml}"
LIMA_MACHINE_DESTINATION_PATH="$HOME/.config/lima/${LIMA_MACHINE_NAME:-docker-light.yaml}"
export DOCKER_DEFAULT_PLATFORM="linux/$(uname -p)"
docker_machine_name() {
basename "$LIMA_MACHINE_DESTINATION_PATH" | sed 's/.yaml//' | sed 's/.yml//'
}
is_mac() {
test "$(get_os_type)" == "Darwin"
}
is_windows_wsl() {
test -f /etc/lsb_release && grep -q 'Microsoft' /etc/lsb_release
}
is_debian() {
test -e /etc/debian_release
}
is_redhat() {
test -e /etc/redhat_release
}
docker_check_mutex_lock() {
printf $$ >> "$DOCKER_CHECK_SENTINEL_FILE"
}
docker_check_mutex_unlock() {
rm -f "$DOCKER_CHECK_SENTINEL_FILE"
}
installation_is_locked() {
random_wait_time=$(bc -l <<< "scale=4 ; ${RANDOM}/32767")
sleep "$random_wait_time" && test -e "$DOCKER_CHECK_SENTINEL_FILE"
}
get_bash_process_holding_lock() {
cat "$DOCKER_CHECK_SENTINEL_FILE"
}
remove_dead_install_locks() {
bash_processes_holding_lock() {
ps -ef | grep bash | grep -q "$(get_bash_process_holding_lock)"
}
if installation_is_locked
then
if ! bash_processes_holding_lock
then
docker_check_mutex_unlock
fi
fi
}
start_docker_vm_darwin() {
test "$(get_os_type)" == "Darwin" || return 0
if ! test -f "$LIMA_MACHINE_DESTINATION_PATH"
then
if ! test -d "$(dirname "$LIMA_MACHINE_DESTINATION_PATH")"
then
mkdir -p "$(dirname "$LIMA_MACHINE_DESTINATION_PATH")"
fi
ln -s "$LIMA_MACHINE_SOURCE_PATH" "$LIMA_MACHINE_DESTINATION_PATH"
fi
if ! test -d "$HOME/.lima/$(docker_machine_name)"
then
mkdir -p "$HOME/.lima/$(docker_machine_name)"
fi
cp "${LIMA_MACHINE_DESTINATION_PATH}" "$HOME/.lima/$(docker_machine_name)/lima.yaml"
gsed -i "s#%TEMPDIR%#${TMPDIR:-$(realpath /tmp)}#g" "$HOME/.lima/$(docker_machine_name)/lima.yaml"
if ! grep -Eq "$(docker_machine_name)[ \t]+Running" < <(limactl list 2>/dev/null)
then
if ! grep -Eq "$(docker_machine_name)" < <(limactl list 2>/dev/null)
then
log_info "Docker VM [$(docker_machine_name)] not created; creating now"
limactl start "$LIMA_MACHINE_DESTINATION_PATH" --tty=false
else
log_info "Docker VM [$(docker_machine_name)] not started; starting now"
limactl start "$(docker_machine_name)"
fi
fi
}
remove_dead_install_locks
if installation_is_locked
then
log_warning "Docker installation is in progress in another window."
return 0
fi
add_user_to_docker_group_if_linux() {
(is_debian || is_redhat) || return 0
log_info "Adding your user to the 'docker' group. Password might be needed. Log out then back \
in to apply these changes."
sudo usermod -aG docker "$USER"
}
install_docker_related_bash_completions_if_darwin() {
_docker_compose() {
brew link docker-compose-completion &>/dev/null || true
mkdir -p ~/.docker/cli-plugins && ln -s $(which docker-compose) ~/.docker/cli-plugins/docker-compose
}
_docker_cli() {
if ! test -e $(brew --prefix)/etc/bash_completion.d/docker
then
sudo curl -L https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker \
-o $(brew --prefix)/etc/bash_completion.d/docker
fi
}
_limactl() {
completion_file="$(brew --prefix)/etc/bash_completion.d/limactl"
! test -e "$completion_file" && limactl completion bash > "$completion_file"
}
test "$(get_os_type)" == "Darwin" || return 0
_docker_cli && _docker_compose && _limactl
}
export LIMA_INSTANCE=$(docker_machine_name)
if is_mac
then
export DOCKER_HOST="$DOCKER_SOCK_PATH_DARWIN"
grep -Eiq 'arm' < <(uname -m) && export DOCKER_DEFAULT_PLATFORM="linux/arm64"
fi
is_mac && export DOCKER_HOST="$DOCKER_SOCK_PATH_DARWIN"
is_windows_wsl && export DOCKER_HOST="$DOCKER_SOCK_PATH_WINDOWS"
docker_check_mutex_lock
trap 'rc=$?; docker_check_mutex_unlock; exit $rc' INT EXIT
if ! which docker &>/dev/null
then
log_info "Installing Docker"
if is_mac
then
brew install docker docker-buildx
&>/dev/null which limactl || brew install lima
elif is_debian
then
install_application "apt-transport-https ca-certificates curl"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
install_application "linux-image-extra-$(uname -r) linux-image-extra-virtual"
install_application "docker-ce"
else
log_error "A Docker setup routine hasn't been written for this Linux."
return 1
fi
fi
add_user_to_docker_group_if_linux
start_docker_vm_darwin
install_docker_related_bash_completions_if_darwin
docker_check_mutex_unlock
trap '-' INT EXIT