-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_dependencies.sh
executable file
·204 lines (166 loc) · 6.8 KB
/
install_dependencies.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
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
#!/bin/zsh
# Detect the operating system
OS=$(uname -s)
# Install Xcode Command Line Tools on macOS
install_xcode_clt() {
echo "Checking for Xcode Command Line Tools..."
if ! xcode-select --print-path &> /dev/null; then
echo "Xcode Command Line Tools not found. Installing..."
# This command installs the tools and requires user interaction
xcode-select --install
# Wait until the installation is complete
until xcode-select --print-path &> /dev/null; do
sleep 5
done
# Check if the OS is macOS before running softwareupdate
if [[ "$(uname)" == "Darwin" ]]; then
softwareupdate -i -a --verbose
fi
echo "Xcode Command Line Tools installed successfully."
else
echo "Xcode Command Line Tools are already installed."
fi
}
# Install dependencies on macOS
install_macos() {
echo "Detected macOS. Installing dependencies..."
# Install Xcode Command Line Tools
install_xcode_clt
# Check if Homebrew is installed
if ! command -v brew &> /dev/null; then
echo "Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo "Homebrew installed successfully."
# Add Homebrew to PATH for current and future shell sessions
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/$USER/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
echo "Homebrew added to PATH."
else
echo "Homebrew is already installed."
fi
# Install Git, Python3, and Ansible
brew install git python3 ansible pipx
echo "Git, Python3, Pipx and Ansible installed successfully."
# Add pre-commit using pipx to avoid conflicts
pipx install pre-commit
pipx ensurepath
# Add pipx binary directory to PATH in .zshrc if not already present
if ! grep -q 'export PATH="$HOME/.local/bin:$PATH"' "$HOME/.zshrc"; then
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.zshrc"
fi
# Source the .zshrc file to ensure PATH is updated
if [ -f "/home/$(whoami)/.zshrc" ]; then
source "/home/$(whoami)/.zshrc"
fi
# Verify if pre-commit is in PATH
if ! command -v pre-commit &> /dev/null; then
echo "pre-commit is not in the PATH after installation. Please check your shell configuration."
exit 1
fi
echo "pre-commit installed successfully."
# Add ansible-dev-tools which includes ansible-lint for pre-commit linting using pipx to avoid conflicts
pipx install ansible-dev-tools
pipx ensurepath
# Add ansible-dev-tools binary directory to PATH in .zshrc if not already present
if ! grep -q 'export PATH="$HOME/.local/pipx/venvs/ansible-dev-tools/bin:$PATH"' "$HOME/.zshrc"; then
echo 'export PATH="$HOME/.local/pipx/venvs/ansible-dev-tools/bin:$PATH"' >> "$HOME/.zshrc"
fi
# Source the .zshrc file to ensure PATH is updated
if [ -f "/Users/$(whoami)/.zshrc" ]; then
source "/Users/$(whoami)/.zshrc"
fi
# Verify if ansible-lint is in PATH
if ! command -v ansible-lint &> /dev/null; then
echo "ansible-lint is not in the PATH after installation. Please check your shell configuration."
exit 1
fi
echo "Verified ansible-dev-tools installed successfully."
# Ensure Git is installed
if ! command -v git &> /dev/null; then
echo "Git is not installed. Please install Git and try again."
exit 1
fi
# Ensure the script is run in a Git repository
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
echo "This script must be run within a Git repository."
exit 1
fi
# Install the pre-commit hooks
cd ../ && pre-commit install --config ../.pre-commit-config.yaml
if [ $? -eq 0 ]; then
echo "Verified pre-commit hooks installed successfully."
else
echo "Failed to install pre-commit hooks. Check the log at /Users/$(whoami)/.cache/pre-commit/pre-commit.log"
exit 1
fi
}
# Install dependencies on Ubuntu
install_ubuntu() {
echo "Detected Ubuntu. Installing dependencies..."
# Update the package list
sudo apt update
# Install Git, Python3, and Ansible
sudo apt install -y ca-certificates git python3 python3-pip software-properties-common pipx
echo "Git, Python3, Pipx and additional dependencies installed successfully."
# Add Ansible PPA and install Ansible
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install -y ansible
echo "Ansible installed successfully."
# Add pre-commit using pipx to avoid conflicts
pipx install pre-commit
pipx ensurepath
# Add pipx binary directory to PATH in .zshrc if not already present
if ! grep -q 'export PATH="$HOME/.local/bin:$PATH"' "$HOME/.zshrc"; then
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.zshrc"
fi
# Source the .zshrc file to ensure PATH is updated
if [ -f "/home/$(whoami)/.zshrc" ]; then
source "/home/$(whoami)/.zshrc"
fi
# Verify if pre-commit is in PATH
if ! command -v pre-commit &> /dev/null; then
echo "pre-commit is not in the PATH after installation. Please check your shell configuration."
exit 1
fi
echo "pre-commit installed successfully."
# Add ansible-dev-tools which includes ansible-lint for pre-commit linting using pipx to avoid conflicts
pipx install ansible-dev-tools
pipx ensurepath
# Add ansible-dev-tools binary directory to PATH in .zshrc if not already present
if ! grep -q 'export PATH="$HOME/.local/pipx/venvs/ansible-dev-tools/bin:$PATH"' "$HOME/.zshrc"; then
echo 'export PATH="$HOME/.local/pipx/venvs/ansible-dev-tools/bin:$PATH"' >> "$HOME/.zshrc"
fi
# Source the .zshrc file to ensure PATH is updated
if [ -f "/Users/$(whoami)/.zshrc" ]; then
source "/Users/$(whoami)/.zshrc"
fi
# Verify if ansible-lint is in PATH
if ! command -v ansible-lint &> /dev/null; then
echo "ansible-lint is not in the PATH after installation. Please check your shell configuration."
exit 1
fi
echo "Verified ansible-dev-tools installed successfully."
# Install the pre-commit hooks
pre-commit install --config ../.pre-commit-config.yaml
echo "Verified pre-commit hooks installed successfully."
}
# Install dependencies based on detected OS
case "$OS" in
Darwin)
install_macos
;;
Linux)
# Check if it's Ubuntu
if [ -f /etc/lsb-release ] && grep -q "Ubuntu" /etc/lsb-release; then
install_ubuntu
else
echo "Unsupported Linux distribution. Exiting..."
exit 1
fi
;;
*)
echo "Unsupported OS. Exiting..."
exit 1
;;
esac
echo "Ansible and dependencies installed successfully!"