|
130 | 130 |
|
131 | 131 | echo "📁 Install directory: $INSTALL_DIR" |
132 | 132 |
|
133 | | -# Download and install |
| 133 | +# Download and extract |
134 | 134 | TEMP_DIR=$(mktemp -d) |
135 | 135 | cd "$TEMP_DIR" |
136 | 136 |
|
137 | 137 | echo "⬇️ Downloading Google Cloud CLI..." |
138 | 138 | 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 |
139 | 139 |
|
140 | | -echo "📦 Installing Google Cloud CLI..." |
| 140 | +echo "📦 Extracting Google Cloud CLI..." |
141 | 141 | tar -xzf google-cloud-cli.tar.gz |
142 | 142 |
|
143 | | -echo "Installing Google Cloud CLI to $INSTALL_DIR..." |
| 143 | +echo "🔧 Installing Google Cloud CLI to $INSTALL_DIR..." |
144 | 144 |
|
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 |
150 | 174 |
|
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 |
153 | 179 |
|
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 |
157 | 200 | fi |
158 | 201 |
|
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 | | - |
171 | 202 | # Create symlinks |
172 | 203 | echo "🔗 Creating symlinks..." |
173 | 204 | if [ ! -L "$BIN_DIR/gcloud" ]; then |
@@ -207,13 +238,16 @@ if [ -f "$INSTALL_DIR/completion.bash.inc" ]; then |
207 | 238 | fi |
208 | 239 | fi |
209 | 240 |
|
210 | | -# Set up PATH if not already set |
| 241 | +# Set up PATH and environment variables |
211 | 242 | if [ "$(id -u)" -eq 0 ]; then |
212 | 243 | # For root, create a profile.d script |
213 | 244 | PROFILE_SCRIPT="/etc/profile.d/google-cloud-sdk.sh" |
214 | 245 | cat > "$PROFILE_SCRIPT" << EOF |
215 | 246 | #!/bin/sh |
216 | 247 | 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" |
217 | 251 | EOF |
218 | 252 | chmod +x "$PROFILE_SCRIPT" |
219 | 253 | else |
|
222 | 256 | echo "" >> "$BASHRC_FILE" |
223 | 257 | echo "# Google Cloud SDK PATH" >> "$BASHRC_FILE" |
224 | 258 | 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" |
225 | 262 | fi |
226 | 263 | fi |
227 | 264 |
|
|
238 | 275 | echo "❌ ERROR: 'gcloud' command not found in PATH" |
239 | 276 | echo "Trying to add to PATH manually..." |
240 | 277 | 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" |
241 | 281 | if command -v gcloud >/dev/null 2>&1; then |
242 | 282 | echo "✅ Google Cloud CLI found after PATH adjustment." |
243 | 283 | gcloud version |
|
249 | 289 |
|
250 | 290 | # Initialize gcloud (non-interactive) |
251 | 291 | 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" |
252 | 295 | "$INSTALL_DIR/bin/gcloud" init --skip-diagnostics --quiet || { |
253 | 296 | echo "⚠️ Warning: Could not initialize gcloud automatically. You may need to run 'gcloud init' manually." |
254 | 297 | } |
|
0 commit comments