-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_cod_free_gcloud.yaml
More file actions
105 lines (83 loc) · 3.34 KB
/
active_cod_free_gcloud.yaml
File metadata and controls
105 lines (83 loc) · 3.34 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
#!/bin/bash
# GCP On-Demand Connector
PROJECT_ID="your-project-id"
# Check if gcloud is authenticated
if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" > /dev/null 2>&1; then
echo "No active session found. Connecting on-demand..."
# --no-launch-browser is useful for remote dev environments
gcloud auth login --no-launch-browser
else
echo "Active session detected: $(gcloud auth list --filter=status:ACTIVE --format="value(account)")"
fi
# Set project context
gcloud config set project $PROJECT_ID
# Verify connection with a free-tier resource check (e.g., Cloud Storage)
echo "Verifying connection to project: $PROJECT_ID"
gcloud storage ls
import os
from google.auth import default
from google.cloud import storage
def connect_gcp_on_demand():
try:
# Automatically finds credentials in environment or via gcloud ADC
credentials, project = default()
print(f"Connected to Project: {project}")
# Test connection using the Free Tier (Cloud Storage 5GB limit)
client = storage.Client(credentials=credentials, project=project)
buckets = list(client.list_buckets(max_results=1))
print("Connection Active.")
return client
except Exception as e:
print(f"Connection failed: {e}")
print("Run 'gcloud auth application-default login' to fix.")
if __name__ == "__main__":
connect_gcp_on_demand()
import os
import subprocess
from google.auth import default
from google.cloud import storage
class GCPConnector:
def __init__(self, project_id=None):
self.project_id = project_id
self._credentials = None
self._client = None
def _ensure_active_session(self):
"""Checks for active ADC or triggers gcloud login."""
try:
self._credentials, self.project_id = default()
print(f"Session Active: {self.project_id}")
except Exception:
print("No active credentials found. Initializing gcloud handshake...")
# Automatically triggers the browser/console login flow
subprocess.run(["gcloud", "auth", "application-default", "login"], check=True)
self._credentials, self.project_id = default()
@property
def storage_client(self):
"""On-demand access to Cloud Storage (Free Tier: 5GB)."""
if self._client is None:
self._ensure_active_session()
self._client = storage.Client(credentials=self._credentials, project=self.project_id)
return self._client
# Usage for gilbertalgordo/dev environment
if __name__ == "__main__":
connector = GCPConnector(project_id="your-gcp-project-id")
# This triggers the 'Active Connect' only when the client is accessed
print(f"Buckets found: {list(connector.storage_client.list_buckets(max_results=5))}")
#!/bin/bash
# GCP Active Connect On-Demand Utility
# Function to refresh tokens silently
refresh_gcp_session() {
echo "[$(date)] Refreshing GCP Active Session..."
gcloud auth print-access-token > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Connection: STABLE"
else
echo "Connection: EXPIRED. Reconnecting..."
gcloud auth login --brief
fi
}
# Advanced: Set up a crontab-style background loop for 'Always-On'
while true; do
refresh_gcp_session
sleep 3000 # Refresh every 50 minutes to prevent token expiry
done