-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·219 lines (179 loc) · 7.16 KB
/
setup.sh
File metadata and controls
executable file
·219 lines (179 loc) · 7.16 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
set -e
# Dry-run mode configuration.
dry_run=false
[[ "$1" == "--dry-run" ]] && dry_run=true
# Function to handle dry-run execution.
run_cmd() {
if ${dry_run}; then
echo "[DRY-RUN] $*" | tee -a dry-run.log
else
"$@"
fi
}
# Override key system commands for dry-run mode.
if ${dry_run}; then
brew() { run_cmd brew "$@"; }
git() { run_cmd git "$@"; }
code() { run_cmd code "$@"; }
PrivilegesCLI() { run_cmd PrivilegesCLI "$@"; }
rm() { run_cmd rm "$@"; }
source() { run_cmd source "$@"; }
fi
################
# Setup script to configure MacOS tool chain for DevOps Engineers at Opencast Software.
################
version=$(grep '"version"' package.json | awk -F: '{print $2}' | sed 's/[",]//g' | tr -d '[:space:]') || { echo "Error reading version"; exit 1; }
echo "Running Script Version ${version}"
timestamp() {
date +"%T" # current time
}
# Generic error handler
throw_error() {
tput setaf 1
error_time=$(timestamp)
# If dry-run mode is enabled, just display the message and exit gracefully & continue.
[[ $dry_run == true ]] && echo -e "[DRY-RUN] No action taken, but error detected." && tput sgr0 && exit 0
declare -A error_messages
error_messages=(
["source packages/brew_packages.sh"]="Failed to load package lists. Ensure 'brew_packages.sh' exists and is accessible."
["source packages/vscode_extensions.sh"]="Failed to load VS Code extensions. Ensure 'vscode_extensions.sh' exists and is accessible."
["brew bundle install"]="Brew package installation failed. Check for missing dependencies or network issues."
["rm -f Brewfile.lock.json"]="Failed to clean up Brewfile.lock.json. Check permissions."
["gh auth login --git-protocol ssh"]="GitHub authentication failed. Verify your credentials and SSH setup."
["PrivilegesCLI --add"]="Failed to elevate access using PrivilegesCLI. Try running manually."
["PrivilegesCLI --remove"]="Failed to revoke privileges using PrivilegesCLI."
["source ~/.zprofile"]="Failed to source ~/.zprofile. Check if the file exists."
["brew update"]="Failed to update Homebrew. Ensure network connectivity."
)
# Handling VS Code extension failures separately
if [[ "${BASH_COMMAND}" == code\ --install-extension\ * ]]; then
extension_name=$(echo "${BASH_COMMAND}" | awk '{print $NF}')
echo -e "ERROR | ${error_time} | Failed to install VS Code extension: \e[1;31m${extension_name}\e[0m."
else
# Print the error message from the lookup table, or a default message if missing
echo -e "ERROR | ${error_time} | ${error_messages[${BASH_COMMAND}]:-Unexpected failure while executing: ${BASH_COMMAND}}"
fi
tput sgr0
exit 1
}
trap 'throw_error' ERR
echo "Homebrew Package Manager Setup:"
# adding privileges using Privileges App's CLI to install brew
PrivilegesCLI --add
if ! command -v brew &>/dev/null; then
echo "Homebrew not found, installing in user directory..."
# Run Homebrew installation
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
# shellcheck source=$HOME/.zprofile
source ~/.zprofile
# Update Homebrew
brew update
else
echo "Homebrew is already installed, skipping installation."
# Update Homebrew
brew update
fi
# Load the package lists from the external file
source packages/brew_packages.sh
# Generate Brewfile dynamically
{
for tap in "${taps[@]}"; do
echo "tap \"$tap\""
done
for formula in "${formulas[@]}"; do
echo "brew \"${formula}\""
done
for cask in "${casks[@]}"; do
echo "cask \"${cask}\""
done
} > Brewfile
# Install brew packages
brew bundle check || brew bundle install
# Cleanup Brewfile.lock.json
if [[ -f Brewfile.lock.json ]]; then
rm -f Brewfile.lock.json
fi
# Revoke privileges after brew installations
PrivilegesCLI --remove
# Install podman
podmanmachine=$(podman machine ls|grep podman-machine-default|awk -F " " '{print $1}'|tr -d "*")
if [[ "${podmanmachine}" == "podman-machine-default" ]]; then
podman info
else
podman machine init --cpus 2 --memory 4096 --disk-size 20 && podman machine start && podman info
fi
echo "Visual Studio Code Setup:"
# Load VS Code extensions from the external file
echo "Ignore error of extensions"
set +e
source packages/vscode_extensions.sh
# Install VS Code extensions dynamically
tput setaf 4 && echo "Configuring Visual Studio Code Extensions" && tput sgr0
for extension in "${vscode_extensions[@]}"; do
code --install-extension "${extension}"
done
echo "Ignore overridden"
set -e
tput setaf 2 && read -rp "Do you want to configure git now? yes[y]/no[n] " configure_git && tput sgr0
case "$(echo "$configure_git" | tr '[:upper:]' '[:lower:]')" in
y|yes)
my_username="$(whoami)" || throw_error "Failed to fetch system username."
formatted_username=$(echo "${my_username}" | sed -r 's/[.]/ /g' | awk '{for (i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2); } 1')
git config --replace-all --global user.name "${formatted_username}"
git config --replace-all --global user.email "${my_username}@opencastsoftware.com"
git_config_output=$(git config --list)
printf "Git configured with credentials:\n%s\n" "${git_config_output}"
echo "Choose your preferred Git editor:"
select editor in "VS Code" "Vim" "Nano" "Skip"; do
case $editor in
"VS Code")
git config --global core.editor "code -w"
echo "VS Code set as default Git editor."
break
;;
"Vim")
git config --global core.editor "vim"
echo "Vim set as default Git editor."
break
;;
"Nano")
git config --global core.editor "nano"
echo "Nano set as default Git editor."
break
;;
"Skip")
echo "Skipping editor setup."
break
;;
*)
echo "Invalid option, please try again."
;;
esac
done
git config --global init.defaultBranch main
tput setaf 4 && read -rp "Please ensure that you have a GitHub account associated with your Opencast credentials. Would you like to test the connection now? yes[y]/no[n] " git_login && tput sgr0
case "$(echo "${git_login}" | tr '[:upper:]' '[:lower:]')" in
y|yes)
gh config set git_protocol ssh
gh auth login --git-protocol ssh
echo "Git credentials configured! Please check GitHub in your browser, copy and paste the above confirmation code."
echo -e "\n\nWhen we are all happy, please follow the instructions here $(tput setaf 4)https://opencastsoftware.atlassian.net/wiki/spaces/DPEC/pages/4633624586/Miscellanous#Local-administrator-rights$(tput sgr0) to setup GPG signing on your $(tput setaf 4)git commits$(tput sgr0)."
;;
n|no)
echo "Skipping GitHub connection test."
;;
*)
echo "Invalid choice, skipping GitHub setup."
;;
esac
;;
n|no)
echo "Skipping Git configuration."
;;
*)
echo "Invalid choice, skipping Git setup."
;;
esac