-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalhost-setup.sh
More file actions
executable file
·85 lines (71 loc) · 2.54 KB
/
localhost-setup.sh
File metadata and controls
executable file
·85 lines (71 loc) · 2.54 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# run:
# ./localhost-setup.sh add_host localhost portal.cssat.org
# ./localhost-setup.sh add_host localhost viz.portal.cssat.org
# ./localhost-setup.sh remove_host localhost portal.cssat.org
# ./localhost-setup.sh remove_host localhost viz.portal.cssat.org
# ./localhost-setup.sh add_dns_daemon_key
# ./localhost-setup.sh remove_dns_daemon_key
# PATH TO YOUR HOSTS FILE
ETC_HOSTS=/etc/hosts
DAEMON_FILE=${HOME%%}/.docker/daemon.json
function remove_host() {
# IP to add/remove.
IP=$1
# Hostname to add/remove.
HOSTNAME=$2
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
if [ -n "$(ggrep $HOSTS_LINE $ETC_HOSTS)" ]
then
echo "$HOSTS_LINE Found in your $ETC_HOSTS, Removing now...";
sudo sed -i".bak" "/$HOSTS_LINE/d" $ETC_HOSTS
else
echo "$HOSTS_LINE was not found in your $ETC_HOSTS";
fi
}
function add_host() {
IP=$1
HOSTNAME=$2
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
line_content=$( printf "%s\t%s\n" "$IP" "$HOSTNAME" )
if [ -n "$(ggrep -P $HOSTS_LINE $ETC_HOSTS)" ]
then
echo "$line_content already exists : $(grep $HOSTNAME $ETC_HOSTS)"
else
echo "Adding $line_content to your $ETC_HOSTS";
sudo -- sh -c -e "echo '$line_content' >> /etc/hosts";
if [ -n "$(ggrep -P $HOSTNAME $ETC_HOSTS)" ]
then
echo "$line_content was added succesfully";
else
echo "Failed to Add $line_content, Try again!";
fi
fi
}
function remove_dns_daemon_key() {
if [ $(jq 'has("dns")' $DAEMON_FILE) == true]
then
echo "dns key found in your $DAEMON_FILE, Removing now...";
jq 'del(.dns)' $DAEMON_FILE
else
echo "dns key was not found in your $DAEMON_FILE";
fi
}
function add_dns_daemon_key() {
if [[ $(jq 'has("dns")' $DAEMON_FILE) == true ]]
then
echo "dns key already exists with the following values: "
echo "$(jq '.dns[]' $DAEMON_FILE)"
echo "Please edit file manually or through Docker Desktop to avoid system corruption."
else
echo "Adding dns: [172.20.0.3, 8.8.8.8, 8.8.4.4] to your $DAEMON_FILE";
printf '%s' "$(jq '. += {"dns": ["172.20.0.03", "8.8.8.8", "8.8.4.4"]}' $DAEMON_FILE)" > $DAEMON_FILE;
if [[ $(jq 'has("dns")' $DAEMON_FILE) == true ]]
then
echo "dns key was added succesfully";
else
echo "Failed to Add dns key, Try again!";
fi
fi
}
$@