Skip to content

Commit b957533

Browse files
authored
Merge pull request #22 from jajera/21-fix-cloud-sdk-broken
fix: handle sdk error
2 parents 260c682 + a116af8 commit b957533

2 files changed

Lines changed: 73 additions & 26 deletions

File tree

src/gcloud-cli/install.sh

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,44 +130,75 @@ fi
130130

131131
echo "📁 Install directory: $INSTALL_DIR"
132132

133-
# Download and install
133+
# Download and extract
134134
TEMP_DIR=$(mktemp -d)
135135
cd "$TEMP_DIR"
136136

137137
echo "⬇️ Downloading Google Cloud CLI..."
138138
curl --proto '=https' --tlsv1.2 -sSf "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-linux-${ARCH_NAME}.tar.gz" -o google-cloud-cli.tar.gz
139139

140-
echo "📦 Installing Google Cloud CLI..."
140+
echo "📦 Extracting Google Cloud CLI..."
141141
tar -xzf google-cloud-cli.tar.gz
142142

143-
echo "Installing Google Cloud CLI to $INSTALL_DIR..."
143+
echo "🔧 Installing Google Cloud CLI to $INSTALL_DIR..."
144144

145-
# Create installation directory
146-
mkdir -p "$INSTALL_DIR"
147-
148-
# Copy the SDK
149-
cp -r google-cloud-sdk/* "$INSTALL_DIR/"
145+
# Handle installation based on user permissions
146+
if [ "$(id -u)" -eq 0 ]; then
147+
echo "🔧 Running as root - using temporary user for installation..."
148+
149+
# Create a temporary user for installation
150+
TEMP_USER="gcloud_installer_$$"
151+
useradd -m -s /bin/bash "$TEMP_USER" || {
152+
echo "⚠️ Could not create temporary user, trying with existing user..."
153+
TEMP_USER="nobody"
154+
}
155+
156+
# Set up the SDK in the temporary user's home
157+
TEMP_INSTALL_DIR="/home/$TEMP_USER/google-cloud-sdk"
158+
cp -r google-cloud-sdk "$TEMP_INSTALL_DIR"
159+
chown -R "$TEMP_USER:$TEMP_USER" "$TEMP_INSTALL_DIR"
160+
161+
# Run the installer as the temporary user
162+
echo "🔧 Running official installer script as $TEMP_USER..."
163+
su "$TEMP_USER" -c "cd '$TEMP_INSTALL_DIR' && ./install.sh --quiet --usage-reporting=false --path-update=false --command-completion=false --skip-diagnostics" || {
164+
echo "⚠️ Installer failed, trying alternative approach..."
165+
}
166+
167+
# Move the installed SDK to the final location
168+
if [ -d "$TEMP_INSTALL_DIR" ]; then
169+
mkdir -p "$(dirname "$INSTALL_DIR")"
170+
rm -rf "$INSTALL_DIR" 2>/dev/null || true
171+
mv "$TEMP_INSTALL_DIR" "$INSTALL_DIR"
172+
chown -R root:root "$INSTALL_DIR"
173+
fi
150174

151-
# Set up the installation manually instead of using the problematic installer script
152-
echo "🔧 Setting up Google Cloud CLI manually..."
175+
# Clean up temporary user
176+
if [ "$TEMP_USER" != "nobody" ]; then
177+
userdel -r "$TEMP_USER" 2>/dev/null || true
178+
fi
153179

154-
# Remove any file at $INSTALL_DIR/properties before creating the directory
155-
if [ -f "$INSTALL_DIR/properties" ]; then
156-
rm -f "$INSTALL_DIR/properties"
180+
else
181+
echo "🔧 Running as non-root user - using standard installation..."
182+
183+
# Set up the SDK in the current user's home
184+
TEMP_INSTALL_DIR="$HOME/google-cloud-sdk"
185+
cp -r google-cloud-sdk "$TEMP_INSTALL_DIR"
186+
187+
# Run the installer script
188+
echo "🔧 Running official installer script..."
189+
cd "$TEMP_INSTALL_DIR"
190+
./install.sh --quiet --usage-reporting=false --path-update=false --command-completion=false --skip-diagnostics || {
191+
echo "⚠️ Installer failed, trying alternative approach..."
192+
}
193+
194+
# Move to final location
195+
if [ -d "$TEMP_INSTALL_DIR" ]; then
196+
mkdir -p "$(dirname "$INSTALL_DIR")"
197+
rm -rf "$INSTALL_DIR" 2>/dev/null || true
198+
mv "$TEMP_INSTALL_DIR" "$INSTALL_DIR"
199+
fi
157200
fi
158201

159-
# Create the properties directory
160-
PROPERTIES_DIR="$INSTALL_DIR/properties"
161-
mkdir -p "$PROPERTIES_DIR"
162-
163-
# Create a basic properties file
164-
cat > "$PROPERTIES_DIR/core.json" << 'EOF'
165-
{
166-
"disable_usage_reporting": true,
167-
"disable_updater": true
168-
}
169-
EOF
170-
171202
# Create symlinks
172203
echo "🔗 Creating symlinks..."
173204
if [ ! -L "$BIN_DIR/gcloud" ]; then
@@ -207,13 +238,16 @@ if [ -f "$INSTALL_DIR/completion.bash.inc" ]; then
207238
fi
208239
fi
209240

210-
# Set up PATH if not already set
241+
# Set up PATH and environment variables
211242
if [ "$(id -u)" -eq 0 ]; then
212243
# For root, create a profile.d script
213244
PROFILE_SCRIPT="/etc/profile.d/google-cloud-sdk.sh"
214245
cat > "$PROFILE_SCRIPT" << EOF
215246
#!/bin/sh
216247
export PATH="$INSTALL_DIR/bin:\$PATH"
248+
export CLOUDSDK_ROOT="$INSTALL_DIR"
249+
export CLOUDSDK_ROOT_DIR="$INSTALL_DIR"
250+
export CLOUDSDK_PYTHON="$INSTALL_DIR/platform/bundledpythonunix/bin/python3"
217251
EOF
218252
chmod +x "$PROFILE_SCRIPT"
219253
else
@@ -222,6 +256,9 @@ else
222256
echo "" >> "$BASHRC_FILE"
223257
echo "# Google Cloud SDK PATH" >> "$BASHRC_FILE"
224258
echo "export PATH=\"$INSTALL_DIR/bin:\$PATH\"" >> "$BASHRC_FILE"
259+
echo "export CLOUDSDK_ROOT=\"$INSTALL_DIR\"" >> "$BASHRC_FILE"
260+
echo "export CLOUDSDK_ROOT_DIR=\"$INSTALL_DIR\"" >> "$BASHRC_FILE"
261+
echo "export CLOUDSDK_PYTHON=\"$INSTALL_DIR/platform/bundledpythonunix/bin/python3\"" >> "$BASHRC_FILE"
225262
fi
226263
fi
227264

@@ -238,6 +275,9 @@ else
238275
echo "❌ ERROR: 'gcloud' command not found in PATH"
239276
echo "Trying to add to PATH manually..."
240277
export PATH="$INSTALL_DIR/bin:$PATH"
278+
export CLOUDSDK_ROOT="$INSTALL_DIR"
279+
export CLOUDSDK_ROOT_DIR="$INSTALL_DIR"
280+
export CLOUDSDK_PYTHON="$INSTALL_DIR/platform/bundledpythonunix/bin/python3"
241281
if command -v gcloud >/dev/null 2>&1; then
242282
echo "✅ Google Cloud CLI found after PATH adjustment."
243283
gcloud version
@@ -249,6 +289,9 @@ fi
249289

250290
# Initialize gcloud (non-interactive)
251291
echo "🔧 Initializing Google Cloud CLI..."
292+
export CLOUDSDK_ROOT="$INSTALL_DIR"
293+
export CLOUDSDK_ROOT_DIR="$INSTALL_DIR"
294+
export CLOUDSDK_PYTHON="$INSTALL_DIR/platform/bundledpythonunix/bin/python3"
252295
"$INSTALL_DIR/bin/gcloud" init --skip-diagnostics --quiet || {
253296
echo "⚠️ Warning: Could not initialize gcloud automatically. You may need to run 'gcloud init' manually."
254297
}

test/gcloud-cli/test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ check "bq version" bash -c "bq version"
1919
check "gcloud config works" bash -c "gcloud config list --format=text"
2020
check "gcloud auth works" bash -c "gcloud auth list --format=text"
2121

22+
# Test SDK root configuration (this was the main issue)
23+
check "gcloud sdk_root is configured" bash -c "gcloud info --format='value(installation.sdk_root)' | grep -q ."
24+
check "gcloud components list works" bash -c "gcloud components list --format=text"
25+
2226
# Check if completion files are installed
2327
if test -f /etc/bash_completion.d/gcloud; then
2428
check "bash completion installed system-wide" echo "Bash completion found at /etc/bash_completion.d/gcloud"

0 commit comments

Comments
 (0)