-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-configure-timezone.sh
executable file
·164 lines (142 loc) · 5.58 KB
/
09-configure-timezone.sh
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
#!/bin/sh
#-------------------------------------------------------------------------------
# Set the timezone and ntp server, by:
#
# 1. configuring the timezone; and
# 2. installing "ntp".
#
# Based on this Digital Ocean guide:
#
# - https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-20-04#conclusion
#
# N.B.
# This script needs to be run as "sudo".
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imported shared variables.
#-------------------------------------------------------------------------------
. ./linshafun/linshafun.var
#-------------------------------------------------------------------------------
# Imported project specific variables.
#-------------------------------------------------------------------------------
. ./linux-setup.var
#-------------------------------------------------------------------------------
# Imported shared functions.
#-------------------------------------------------------------------------------
. ./linshafun/comments.sh
# . ./linshafun/docker-env-variables.sh
# . ./linshafun/docker-images.sh
# . ./linshafun/docker-services.sh
# . ./linshafun/files-directories.sh
# . ./linshafun/firewall.sh
# . ./linshafun/host-env-variables.sh
# . ./linshafun/host-information.sh
# . ./linshafun/network.sh
. ./linshafun/ownership-permissions.sh
. ./linshafun/packages.sh
# . ./linshafun/services.sh
. ./linshafun/setup-config.sh
. ./linshafun/setup.sh
# . ./linshafun/ssh-keys.sh
# . ./linshafun/text.sh
. ./linshafun/user-input.sh
#-------------------------------------------------------------------------------
# Config key and service variables.
#-------------------------------------------------------------------------------
CONFIG_KEY='configuredTimezone'
SERVICE="ntp"
#-------------------------------------------------------------------------------
# Timezone related variables.
#-------------------------------------------------------------------------------
CURRENT_TIMEZONE="$(timedatectl show | grep "Timezone")"
#-------------------------------------------------------------------------------
# Lists the current settings, then asks if the user would like to change
# the timezone. If not, no settings are changed.
#-------------------------------------------------------------------------------
changeTimezone () {
listTimeDate
echoComment 'Your current timezone is:'
echoComment "$CURRENT_TIMEZONE"
promptForUserInput 'Would you like to change the timezone (y/n)?'
TIMEZONE_SET_YN="$(getUserInputYN)"
if [ "$TIMEZONE_SET_YN" = true ]; then
setNewTimezone
else
echoComment 'No changes made to timezone settings.'
fi
}
#-------------------------------------------------------------------------------
# Checks the user inputted timezone. Takes one mandatory arguement:
#
# 1. ${1:?} - the user inputted timezone
#-------------------------------------------------------------------------------
checkTimezone () {
local TIMEZONE="${1:?}"
local TIMEZONE_CHECK="$(timedatectl list-timezones | grep "$TIMEZONE")"
if [ "$TIMEZONE" = "$TIMEZONE_CHECK" ]; then
echo true
else
echo false
fi
}
#-------------------------------------------------------------------------------
# Outputs ntpq settings.
#-------------------------------------------------------------------------------
listNtpSettings () {
echoComment 'Listing ntpq settings.'
echoSeparator
ntpq -p
echoSeparator
echoComment 'It may take a moment for connections to be established.'
}
#-------------------------------------------------------------------------------
# Outputs the time and date settings via "timedatectl".
#-------------------------------------------------------------------------------
listTimeDate () {
echoComment 'Listing time and date settings.'
echoSeparator
timedatectl
echoSeparator
}
#-------------------------------------------------------------------------------
# Sets a new, user inputted, timezone, first checking the input is valid, then
# setting the new timezone.
#
# If the user inputs an invalid timezone the function is run again.
#-------------------------------------------------------------------------------
setNewTimezone () {
echoComment 'You can find a list of timezones at:'
echoComment 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'
promptForUserInput 'Which timezone would you like to switch to (Region/City)?'
NEW_TIMEZONE="$(getUserInput)"
echoComment "Checking $NEW_TIMEZONE is valid…"
local TIMEZONE_VALID="$(checkTimezone "$NEW_TIMEZONE")"
if [ "$TIMEZONE_VALID" = true ]; then
echoComment 'Timezone is valid.'
echoComment "Setting timezone to $NEW_TIMEZONE."
timedatectl set-timezone "$NEW_TIMEZONE"
echoComment "Timezone set."
listTimeDate
writeSetupConfigOption "timezone" "$NEW_TIMEZONE"
elif [ "$TIMEZONE_VALID" = false ]; then
echoComment 'Timezone is invalid. You must use a valid timezone.'
setNewTimezone
fi
}
#-------------------------------------------------------------------------------
# Runs the main functions of the script.
#-------------------------------------------------------------------------------
mainScript () {
changeTimezone
echoComment 'Ensuring set-ntp is off.'
timedatectl set-ntp no
listTimeDate
checkForPackagesAndInstall "$SERVICE"
listNtpSettings
}
#-------------------------------------------------------------------------------
# Run the script.
#-------------------------------------------------------------------------------
initialiseScript "$CONFIG_KEY"
mainScript
finaliseScript "$CONFIG_KEY"