| 1 | #!/usr/bin/env bash |
| 2 | # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 3 | # Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 3 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | set -eu |
| 19 | |
| 20 | if [[ ${HOST_HOSTNAME} == false ]]; then |
| 21 | # if HOST_HOSTNAME is false then do nothing |
| 22 | exit 0 |
| 23 | fi |
| 24 | |
| 25 | get_ip_addr () { |
| 26 | local HOST="$1" |
| 27 | ping "$HOST" -c 1 | sed -n "s/PING $HOST (\([0-9\.]*\)).*/\1/p" |
| 28 | } |
| 29 | |
| 30 | map_ip_addr () { |
| 31 | local HOST="$1" |
| 32 | local ADDR="$2" |
| 33 | echo -e "$ADDR\t$HOST" >> /etc/hosts |
| 34 | } |
| 35 | |
| 36 | alias_hostname () { |
| 37 | # configure one hostname to point at the same IP as another |
| 38 | local FROM="$1" # source |
| 39 | local TO="$2" # target |
| 40 | map_ip_addr "$FROM" "$(get_ip_addr "$TO")" |
| 41 | |
| 42 | } |
| 43 | |
| 44 | # map the hostname of the docker host (the host that is running the container) |
| 45 | # onto whatever 'host.docker.internal' points to |
| 46 | # NOTE: |
| 47 | # * for Mac OS and Windows 'host.docker.internal' is set by docker desktop |
| 48 | # * for Linux you must set it yourself using the --add-host run arg |
| 49 | alias_hostname "$HOST_HOSTNAME" 'host.docker.internal' |