|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -# Temporarily disable exit on error to run a robust cleanup process. |
4 |
| -set +e |
| 3 | +echo "--- Checking VS Code repository state ---" |
5 | 4 |
|
6 |
| -echo "--- Running VS Code Pre-installation Cleanup ---" |
| 5 | +# First, check if the system's apt configuration is healthy by running an update. |
| 6 | +# We specifically look for the known conflict error. |
| 7 | +if sudo apt-get update -y 2>&1 | grep -q "Conflicting values set for option Signed-By"; then |
| 8 | + # --- CONFLICT DETECTED --- # |
| 9 | + echo "Conflict detected. Forcing a cleanup and recreation of the VS Code repository." |
| 10 | + |
| 11 | + # Temporarily disable exit on error to ensure the full cleanup runs. |
| 12 | + set +e |
7 | 13 |
|
8 |
| -# Find and remove ANY apt source file mentioning the conflicting repository. |
9 |
| -CONFLICTING_FILES=$(grep -lr "packages.microsoft.com/repos/code" /etc/apt/sources.list.d/ /etc/apt/sources.list 2>/dev/null) |
10 |
| -if [ -n "$CONFLICTING_FILES" ]; then |
11 |
| - echo "Found conflicting repository files. Removing them..." |
12 |
| - echo "$CONFLICTING_FILES" | sudo xargs -r rm -f |
13 |
| - echo "Removed: $CONFLICTING_FILES" |
14 |
| -fi |
15 |
| - |
16 |
| -# Remove both potential GPG keys if they exist. |
17 |
| -if [ -f "/usr/share/keyrings/microsoft.gpg" ]; then |
18 |
| - echo "Removing old GPG key..." |
19 |
| - sudo rm -f /usr/share/keyrings/microsoft.gpg |
20 |
| -fi |
21 |
| -if [ -f "/etc/apt/keyrings/packages.microsoft.gpg" ]; then |
22 |
| - echo "Removing new GPG key..." |
23 |
| - sudo rm -f /etc/apt/keyrings/packages.microsoft.gpg |
24 |
| -fi |
| 14 | + # Find and remove ANY apt source file mentioning the conflicting repository. |
| 15 | + CONFLICTING_FILES=$(grep -lr "packages.microsoft.com/repos/code" /etc/apt/sources.list.d/ /etc/apt/sources.list 2>/dev/null) |
| 16 | + if [ -n "$CONFLICTING_FILES" ]; then |
| 17 | + echo "Removing conflicting repository files: $CONFLICTING_FILES" |
| 18 | + echo "$CONFLICTING_FILES" | sudo xargs -r rm -f |
| 19 | + fi |
25 | 20 |
|
26 |
| -# Refresh apt lists and check if the conflict is resolved. |
27 |
| -echo "Refreshing package lists to verify the fix..." |
28 |
| -sudo apt-get update -y |
29 |
| -APT_EXIT_CODE=$? |
| 21 | + # Remove both potential GPG keys if they exist. |
| 22 | + if [ -f "/usr/share/keyrings/microsoft.gpg" ]; then sudo rm -f /usr/share/keyrings/microsoft.gpg; fi |
| 23 | + if [ -f "/etc/apt/keyrings/packages.microsoft.gpg" ]; then sudo rm -f /etc/apt/keyrings/packages.microsoft.gpg; fi |
| 24 | + |
| 25 | + # Re-enable exit on error. |
| 26 | + set -e |
30 | 27 |
|
31 |
| -# Re-enable exit on error for the rest of the script. |
32 |
| -set -e |
| 28 | + # Now, add the repository back cleanly. |
| 29 | + echo "Adding the official VS Code repository..." |
| 30 | + cd /tmp |
| 31 | + wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg |
| 32 | + sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg |
| 33 | + echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list > /dev/null |
| 34 | + rm -f packages.microsoft.gpg |
| 35 | + cd - |
33 | 36 |
|
34 |
| -if [ $APT_EXIT_CODE -ne 0 ]; then |
35 |
| - echo "--------------------------------------------------------------------" |
36 |
| - echo "FATAL: APT conflict could not be resolved automatically." |
37 |
| - echo "The installation of VS Code cannot proceed." |
38 |
| - echo "--------------------------------------------------------------------" |
39 |
| - exit 1 |
| 37 | +else |
| 38 | + # --- NO CONFLICT DETECTED --- # |
| 39 | + echo "No conflicts found. Checking if repository needs to be added." |
| 40 | + # Check if the repository is already configured. If not, add it. |
| 41 | + if ! grep -qr "packages.microsoft.com/repos/code" /etc/apt/sources.list.d/ /etc/apt/sources.list; then |
| 42 | + echo "VS Code repository not found. Adding it..." |
| 43 | + cd /tmp |
| 44 | + wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg |
| 45 | + sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg |
| 46 | + echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list > /dev/null |
| 47 | + rm -f packages.microsoft.gpg |
| 48 | + cd - |
| 49 | + else |
| 50 | + echo "VS Code repository already configured and is valid." |
| 51 | + fi |
40 | 52 | fi
|
41 | 53 |
|
42 |
| -echo "--- Cleanup Successful. Proceeding with VS Code installation. ---" |
43 |
| - |
44 |
| -# Now, proceed with a clean installation of the VS Code repository. |
45 |
| -cd /tmp |
46 |
| -wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg |
47 |
| -sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg |
48 |
| -echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list > /dev/null |
49 |
| -rm -f packages.microsoft.gpg |
50 |
| -cd - |
| 54 | +# --- FINAL INSTALLATION --- # |
| 55 | +# At this point, the configuration is guaranteed to be correct. |
51 | 56 |
|
52 |
| -# Update package list again and install VS Code |
| 57 | +echo "Updating package list and installing VS Code..." |
53 | 58 | sudo apt-get update -y
|
54 | 59 | sudo apt-get install -y code
|
55 | 60 |
|
|
0 commit comments