forked from awslabs/iam-policy-autopilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·349 lines (281 loc) · 11 KB
/
install.sh
File metadata and controls
executable file
·349 lines (281 loc) · 11 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/sh
# IAM Policy Autopilot Installation Script
#
# This script automatically downloads and installs the IAM Policy Autopilot CLI tool
# for Unix-like systems (macOS and Linux).
#
# Supported platforms:
# - macOS (Darwin): x86_64, ARM64
# - Linux: x86_64, ARM64
#
# Usage:
# # Fresh installation (one-liner):
# curl -sSL https://github.com/awslabs/iam-policy-autopilot/raw/refs/heads/main/install.sh | sh
#
# # Update existing installation (same command):
# curl -sSL https://github.com/awslabs/iam-policy-autopilot/raw/refs/heads/main/install.sh | sh
#
# # Manual installation with sudo:
# curl -sSL https://github.com/awslabs/iam-policy-autopilot/raw/refs/heads/main/install.sh | sudo sh
#
# # Download and run locally:
# curl -sSL https://github.com/awslabs/iam-policy-autopilot/raw/refs/heads/main/install.sh -o install.sh
# chmod +x install.sh
# ./install.sh
#
# The script will automatically detect if you have an existing installation and update it
# if a newer version is available. If you're already running the latest version, it will
# notify you and exit without making changes.
# Repository configuration
GITHUB_ORG="awslabs"
GITHUB_REPO="iam-policy-autopilot"
set -e
# Global variables for cleanup
TEMP_FILE=""
TEMP_DIR=""
# Cleanup function
cleanup() {
if [ -n "$TEMP_FILE" ] && [ -f "$TEMP_FILE" ]; then
rm -f "$TEMP_FILE"
fi
if [ -n "$TEMP_DIR" ] && [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi
}
# Set up cleanup trap for temporary files
trap cleanup EXIT INT TERM
# Error handling function
error() {
echo "Error: $1" >&2
cleanup
exit 1
}
# Platform detection function
detect_platform() {
echo "Detecting platform..."
# Detect OS
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$OS" in
darwin)
OS="apple-darwin"
;;
linux)
OS="unknown-linux-gnu"
;;
*)
error "Unsupported operating system: $OS. Supported: macOS (Darwin), Linux"
;;
esac
# Detect architecture
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
# Keep as is
;;
aarch64|arm64)
ARCH="aarch64"
;;
i686|i386)
error "32-bit x86 architecture is not supported. Supported: x86_64, ARM64"
;;
*)
error "Unsupported architecture: $ARCH. Supported: x86_64, ARM64"
;;
esac
# Construct platform target triple
PLATFORM="${ARCH}-${OS}"
echo "Detected platform: $PLATFORM"
}
# GitHub API integration function
get_latest_release() {
echo "Fetching latest release information from GitHub..."
# Query GitHub API for latest release
RELEASE_JSON=$(curl -sL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$GITHUB_ORG/$GITHUB_REPO/releases/latest" 2>&1)
# Check if curl command succeeded
if [ $? -ne 0 ]; then
error "Failed to fetch release information from GitHub. Please check your internet connection and try again."
fi
# Check if response contains error
if echo "$RELEASE_JSON" | grep -q '"message".*"Not Found"'; then
error "GitHub repository or release not found. Please verify the repository exists."
fi
if echo "$RELEASE_JSON" | grep -q '"message".*"API rate limit exceeded"'; then
error "GitHub API rate limit exceeded. Please try again later."
fi
# Extract version tag
VERSION=$(echo "$RELEASE_JSON" | grep '"tag_name"' | sed -E 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/' | head -n 1)
if [ -z "$VERSION" ]; then
error "Failed to parse version from GitHub API response. The response may be malformed."
fi
echo "Latest version: $VERSION"
# Find matching binary asset based on platform
# Look for assets that contain the platform string in their name
ASSET_URL=$(echo "$RELEASE_JSON" | grep '"browser_download_url"' | grep "$PLATFORM" | sed -E 's/.*"browser_download_url"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/' | head -n 1)
if [ -z "$ASSET_URL" ]; then
error "No binary found for platform: $PLATFORM. This platform may not be supported yet. Please report this at https://github.com/$GITHUB_ORG/$GITHUB_REPO/issues"
fi
# Extract asset filename from URL
ASSET_NAME=$(basename "$ASSET_URL")
echo "Found binary: $ASSET_NAME"
echo "Download URL: $ASSET_URL"
}
# Version comparison function
check_existing_version() {
INSTALL_DIR="/usr/local/bin"
BINARY_PATH="$INSTALL_DIR/iam-policy-autopilot"
echo "Checking for existing installation..."
# Check if binary exists in installation directory
if [ -f "$BINARY_PATH" ]; then
echo "Found existing installation at $BINARY_PATH"
# Try to get installed version by executing --version command
INSTALLED_VERSION=$("$BINARY_PATH" --version 2>/dev/null | awk '{print $2}')
# If version detection failed, proceed with installation
if [ -z "$INSTALLED_VERSION" ]; then
echo "Could not determine installed version. Proceeding with installation."
return 0
fi
echo "Installed version: $INSTALLED_VERSION"
# Remove 'v' prefix from VERSION if present for comparison
LATEST_VERSION_CLEAN="${VERSION#v}"
# Compare installed version with latest release version
if [ "$INSTALLED_VERSION" = "$LATEST_VERSION_CLEAN" ]; then
echo ""
echo "iam-policy-autopilot $INSTALLED_VERSION is already installed and up-to-date"
exit 0
fi
echo "Updating iam-policy-autopilot from $INSTALLED_VERSION to $LATEST_VERSION_CLEAN"
else
echo "No existing installation found"
echo "Installing iam-policy-autopilot ${VERSION#v}"
fi
}
# Download binary function
download_binary() {
echo ""
echo "Downloading binary..."
# Create temporary file for download
TEMP_FILE=$(mktemp)
echo "Downloading from $ASSET_URL"
# Try curl first, fallback to wget
if command -v curl >/dev/null 2>&1; then
# Use curl with progress bar
if ! curl -L --progress-bar -o "$TEMP_FILE" "$ASSET_URL"; then
error "Failed to download binary using curl. Please check your internet connection and try again."
fi
elif command -v wget >/dev/null 2>&1; then
# Use wget with progress display
if ! wget -q --show-progress -O "$TEMP_FILE" "$ASSET_URL"; then
error "Failed to download binary using wget. Please check your internet connection and try again."
fi
else
error "Neither curl nor wget found. Please install curl or wget and try again."
fi
# Verify download succeeded by checking file size
if [ ! -s "$TEMP_FILE" ]; then
error "Downloaded file appears to be empty or corrupted. Please try again."
fi
echo "Download complete"
# Handle archived formats
# Check if the downloaded file is a tar.gz archive
if echo "$ASSET_NAME" | grep -q "\.tar\.gz$"; then
echo "Extracting tar.gz archive..."
# Create temporary directory for extraction
TEMP_DIR=$(mktemp -d)
# Extract archive
if ! tar -xzf "$TEMP_FILE" -C "$TEMP_DIR"; then
error "Failed to extract tar.gz archive. The file may be corrupted."
fi
# Find the binary in the extracted files
# Look for file named iam-policy-autopilot (without extension)
EXTRACTED_BINARY=$(find "$TEMP_DIR" -type f -name "iam-policy-autopilot" | head -n 1)
if [ -z "$EXTRACTED_BINARY" ]; then
error "Could not find iam-policy-autopilot binary in extracted archive."
fi
# Move extracted binary to temp file location
mv "$EXTRACTED_BINARY" "$TEMP_FILE"
# Clean up extraction directory
rm -rf "$TEMP_DIR"
echo "Extraction complete"
elif echo "$ASSET_NAME" | grep -q "\.zip$"; then
echo "Extracting zip archive..."
# Create temporary directory for extraction
TEMP_DIR=$(mktemp -d)
# Check if unzip is available
if ! command -v unzip >/dev/null 2>&1; then
error "unzip command not found. Please install unzip to extract .zip archives."
fi
# Extract archive
if ! unzip -q "$TEMP_FILE" -d "$TEMP_DIR"; then
error "Failed to extract zip archive. The file may be corrupted."
fi
# Find the binary in the extracted files
EXTRACTED_BINARY=$(find "$TEMP_DIR" -type f -name "iam-policy-autopilot" | head -n 1)
if [ -z "$EXTRACTED_BINARY" ]; then
error "Could not find iam-policy-autopilot binary in extracted archive."
fi
# Move extracted binary to temp file location
mv "$EXTRACTED_BINARY" "$TEMP_FILE"
# Clean up extraction directory
rm -rf "$TEMP_DIR"
echo "Extraction complete"
fi
# Verify the final binary file exists and is not empty
if [ ! -s "$TEMP_FILE" ]; then
error "Binary file is missing or empty after processing. Please try again."
fi
echo "Binary ready for installation"
}
# Installation function
install_binary() {
INSTALL_DIR="/usr/local/bin"
BINARY_NAME="iam-policy-autopilot"
INSTALL_PATH="$INSTALL_DIR/$BINARY_NAME"
echo ""
echo "Installing binary..."
# Check write permissions for installation directory
if [ ! -w "$INSTALL_DIR" ]; then
error "No write permission to $INSTALL_DIR. Try running with sudo: curl -sSL https://github.com/$GITHUB_ORG/$GITHUB_REPO/raw/refs/heads/main/install.sh | sudo sh"
fi
# Preserve permissions when updating existing installation
# If binary already exists, save its permissions
EXISTING_PERMS=""
if [ -f "$INSTALL_PATH" ]; then
# Get current permissions in octal format (e.g., 755)
EXISTING_PERMS=$(stat -f "%Lp" "$INSTALL_PATH" 2>/dev/null || stat -c "%a" "$INSTALL_PATH" 2>/dev/null)
echo "Preserving existing permissions: $EXISTING_PERMS"
fi
# Move downloaded binary to installation directory
if ! mv "$TEMP_FILE" "$INSTALL_PATH"; then
error "Failed to move binary to $INSTALL_PATH. Please check permissions and try again."
fi
# Set executable permissions
# If we preserved permissions from existing installation, restore them
# Otherwise, set default executable permissions (755)
if [ -n "$EXISTING_PERMS" ]; then
chmod "$EXISTING_PERMS" "$INSTALL_PATH"
else
chmod +x "$INSTALL_PATH"
fi
# Get the installed version for success message
FINAL_VERSION=$("$INSTALL_PATH" --version 2>/dev/null | awk '{print $2}' || echo "${VERSION#v}")
echo ""
echo "=========================================="
echo "Installation complete!"
echo "=========================================="
echo "Version: $FINAL_VERSION"
echo "Location: $INSTALL_PATH"
echo ""
echo "Run 'iam-policy-autopilot --help' to get started"
}
# Main execution
main() {
detect_platform
get_latest_release
check_existing_version
download_binary
install_binary
}
main