-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathset_hosts.sh
More file actions
executable file
·64 lines (50 loc) · 2.2 KB
/
Copy pathset_hosts.sh
File metadata and controls
executable file
·64 lines (50 loc) · 2.2 KB
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
#!/bin/bash
script_path=$( cd "$(dirname $0)" || exit; pwd -P )
project_path=$( cd "$script_path" && cd ..; pwd -P )
set -a; source "$project_path/.env"; set +a
# If this is being run inside WSL, then we need to modify the /etc/hosts file on Windows
if [[ -f "/mnt/c/Windows/System32/drivers/etc/hosts" ]]; then
hosts_file="/mnt/c/Windows/System32/drivers/etc/hosts"
else
hosts_file="/etc/hosts"
fi
if ! sudo touch "$hosts_file" &> /dev/null; then
echo -e "\x1B[33m$hosts_file is not writable!\x1B[0m"
exit
fi
# Create a backup of the existing hosts file in case something goes wrong
date=$(date -Idate)
backup_path="$hosts_file.pre-$date.backup"
if ! sudo test -r "$backup_path" -a -w "$backup_path"; then
backup_path="$HOME/hosts.pre-$date.backup"
fi
sudo rm -f "$backup_path"
sudo cp "$hosts_file" "$backup_path"
echo -e "\x1B[2mBacked up \x1B[4m$hosts_file\x1B[0m\x1B[2m to \x1B[4m$backup_path\x1B[0m"
# Remove existing docker-dev hosts entries
sudo sh -c "sed '/totara-docker-dev/d' $hosts_file > /tmp/hosts"
sudo sh -c "sed -E '/totara[0-9]{2}/d' /tmp/hosts > $hosts_file"
sudo rm "/tmp/hosts"
# Shouldn't need to change this
host_ip="127.0.0.1"
# Get all the possible php hosts from the docker compose yml file
php_versions=($(cat "$project_path/compose/php.yml" | sed -E -n 's/.*\php-([0-9]).([0-9])[^:]*:/\1\2/p' | uniq | sort))
# Get the sub sites that we should also add host entries for
sites=($(find "$LOCAL_SRC" -mindepth 2 -maxdepth 2 -name "version.php" -type f -exec dirname {} \; | sort | xargs -n 1 basename))
hosts=""
for php_version in "${php_versions[@]}"; do
hosts+="\n${host_ip} totara${php_version} totara${php_version}.behat totara${php_version}.debug"
done
# Sub site hosts
for site in "${sites[@]}"; do
for php_version in "${php_versions[@]}"; do
hosts+="\n${host_ip} ${site}.totara${php_version} ${site}.totara${php_version}.behat ${site}.totara${php_version}.debug"
done
done
hosts="\n# totara-docker-dev start$hosts\n# totara-docker-dev end\n"
# Add the hosts
sudo -- sh -c -e "echo '$hosts' >> $hosts_file"
echo -e "Successfully updated \x1B[4m$hosts_file\x1B[0m with docker-dev hosts"
if [ -n "$sites" ]; then
echo "Hosts have been added for the following sites: ${sites[@]}"
fi