Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 69 additions & 26 deletions src/gcloud-cli/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,44 +130,75 @@ fi

echo "📁 Install directory: $INSTALL_DIR"

# Download and install
# Download and extract
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"

echo "⬇️ Downloading Google Cloud CLI..."
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

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

echo "Installing Google Cloud CLI to $INSTALL_DIR..."
echo "🔧 Installing Google Cloud CLI to $INSTALL_DIR..."

# Create installation directory
mkdir -p "$INSTALL_DIR"

# Copy the SDK
cp -r google-cloud-sdk/* "$INSTALL_DIR/"
# Handle installation based on user permissions
if [ "$(id -u)" -eq 0 ]; then
echo "🔧 Running as root - using temporary user for installation..."

# Create a temporary user for installation
TEMP_USER="gcloud_installer_$$"
useradd -m -s /bin/bash "$TEMP_USER" || {
echo "⚠️ Could not create temporary user, trying with existing user..."
TEMP_USER="nobody"
}

# Set up the SDK in the temporary user's home
TEMP_INSTALL_DIR="/home/$TEMP_USER/google-cloud-sdk"
cp -r google-cloud-sdk "$TEMP_INSTALL_DIR"
chown -R "$TEMP_USER:$TEMP_USER" "$TEMP_INSTALL_DIR"

# Run the installer as the temporary user
echo "🔧 Running official installer script as $TEMP_USER..."
su "$TEMP_USER" -c "cd '$TEMP_INSTALL_DIR' && ./install.sh --quiet --usage-reporting=false --path-update=false --command-completion=false --skip-diagnostics" || {
echo "⚠️ Installer failed, trying alternative approach..."
}

# Move the installed SDK to the final location
if [ -d "$TEMP_INSTALL_DIR" ]; then
mkdir -p "$(dirname "$INSTALL_DIR")"
rm -rf "$INSTALL_DIR" 2>/dev/null || true
mv "$TEMP_INSTALL_DIR" "$INSTALL_DIR"
chown -R root:root "$INSTALL_DIR"
fi

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

# Remove any file at $INSTALL_DIR/properties before creating the directory
if [ -f "$INSTALL_DIR/properties" ]; then
rm -f "$INSTALL_DIR/properties"
else
echo "🔧 Running as non-root user - using standard installation..."

# Set up the SDK in the current user's home
TEMP_INSTALL_DIR="$HOME/google-cloud-sdk"
cp -r google-cloud-sdk "$TEMP_INSTALL_DIR"

# Run the installer script
echo "🔧 Running official installer script..."
cd "$TEMP_INSTALL_DIR"
./install.sh --quiet --usage-reporting=false --path-update=false --command-completion=false --skip-diagnostics || {
echo "⚠️ Installer failed, trying alternative approach..."
}

# Move to final location
if [ -d "$TEMP_INSTALL_DIR" ]; then
mkdir -p "$(dirname "$INSTALL_DIR")"
rm -rf "$INSTALL_DIR" 2>/dev/null || true
mv "$TEMP_INSTALL_DIR" "$INSTALL_DIR"
fi
fi

# Create the properties directory
PROPERTIES_DIR="$INSTALL_DIR/properties"
mkdir -p "$PROPERTIES_DIR"

# Create a basic properties file
cat > "$PROPERTIES_DIR/core.json" << 'EOF'
{
"disable_usage_reporting": true,
"disable_updater": true
}
EOF

# Create symlinks
echo "🔗 Creating symlinks..."
if [ ! -L "$BIN_DIR/gcloud" ]; then
Expand Down Expand Up @@ -207,13 +238,16 @@ if [ -f "$INSTALL_DIR/completion.bash.inc" ]; then
fi
fi

# Set up PATH if not already set
# Set up PATH and environment variables
if [ "$(id -u)" -eq 0 ]; then
# For root, create a profile.d script
PROFILE_SCRIPT="/etc/profile.d/google-cloud-sdk.sh"
cat > "$PROFILE_SCRIPT" << EOF
#!/bin/sh
export PATH="$INSTALL_DIR/bin:\$PATH"
export CLOUDSDK_ROOT="$INSTALL_DIR"
export CLOUDSDK_ROOT_DIR="$INSTALL_DIR"
export CLOUDSDK_PYTHON="$INSTALL_DIR/platform/bundledpythonunix/bin/python3"
EOF
chmod +x "$PROFILE_SCRIPT"
else
Expand All @@ -222,6 +256,9 @@ else
echo "" >> "$BASHRC_FILE"
echo "# Google Cloud SDK PATH" >> "$BASHRC_FILE"
echo "export PATH=\"$INSTALL_DIR/bin:\$PATH\"" >> "$BASHRC_FILE"
echo "export CLOUDSDK_ROOT=\"$INSTALL_DIR\"" >> "$BASHRC_FILE"
echo "export CLOUDSDK_ROOT_DIR=\"$INSTALL_DIR\"" >> "$BASHRC_FILE"
echo "export CLOUDSDK_PYTHON=\"$INSTALL_DIR/platform/bundledpythonunix/bin/python3\"" >> "$BASHRC_FILE"
fi
fi

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

# Initialize gcloud (non-interactive)
echo "🔧 Initializing Google Cloud CLI..."
export CLOUDSDK_ROOT="$INSTALL_DIR"
export CLOUDSDK_ROOT_DIR="$INSTALL_DIR"
export CLOUDSDK_PYTHON="$INSTALL_DIR/platform/bundledpythonunix/bin/python3"
"$INSTALL_DIR/bin/gcloud" init --skip-diagnostics --quiet || {
echo "⚠️ Warning: Could not initialize gcloud automatically. You may need to run 'gcloud init' manually."
}
Expand Down
4 changes: 4 additions & 0 deletions test/gcloud-cli/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ check "bq version" bash -c "bq version"
check "gcloud config works" bash -c "gcloud config list --format=text"
check "gcloud auth works" bash -c "gcloud auth list --format=text"

# Test SDK root configuration (this was the main issue)
check "gcloud sdk_root is configured" bash -c "gcloud info --format='value(installation.sdk_root)' | grep -q ."
check "gcloud components list works" bash -c "gcloud components list --format=text"

# Check if completion files are installed
if test -f /etc/bash_completion.d/gcloud; then
check "bash completion installed system-wide" echo "Bash completion found at /etc/bash_completion.d/gcloud"
Expand Down