-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsetup_databricks.py
More file actions
72 lines (61 loc) · 2.32 KB
/
setup_databricks.py
File metadata and controls
72 lines (61 loc) · 2.32 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
#!/usr/bin/env python
"""Configure Databricks CLI with the user's PAT from environment."""
import os
import subprocess
from pathlib import Path
from utils import ensure_https
# Set HOME if not properly set
if not os.environ.get("HOME") or os.environ["HOME"] == "/":
os.environ["HOME"] = "/app/python/source_code"
home = Path(os.environ["HOME"])
# Get credentials from environment
host = os.environ.get("DATABRICKS_HOST")
token = os.environ.get("DATABRICKS_TOKEN")
if not host or not token:
print("Databricks CLI config will be set after PAT setup")
exit(0)
host = ensure_https(host)
# Create ~/.databrickscfg with DEFAULT profile using PAT auth
databrickscfg = home / ".databrickscfg"
config_content = f"""[DEFAULT]
host = {host}
token = {token}
"""
databrickscfg.write_text(config_content)
databrickscfg.chmod(0o600) # Restrict permissions
print(f"Databricks CLI configured: {databrickscfg}")
# Verify it works
result = subprocess.run(
["databricks", "current-user", "me", "--output", "json"],
capture_output=True,
text=True,
env={
**os.environ,
# Remove OAuth vars to force PAT auth
"DATABRICKS_CLIENT_ID": "",
"DATABRICKS_CLIENT_SECRET": ""
}
)
if result.returncode == 0:
import json
try:
user = json.loads(result.stdout)
email = user.get('userName', '')
display_name = user.get('displayName', '')
print(f"Databricks CLI authenticated as: {email}")
# Configure git with user's email and name
if email:
subprocess.run(["git", "config", "--global", "user.email", email], check=False)
print(f"Git configured with email: {email}")
if display_name:
subprocess.run(["git", "config", "--global", "user.name", display_name], check=False)
print(f"Git configured with name: {display_name}")
elif email:
# Fall back to email prefix as name if no display name
name_from_email = email.split('@')[0].replace('.', ' ').title()
subprocess.run(["git", "config", "--global", "user.name", name_from_email], check=False)
print(f"Git configured with name: {name_from_email}")
except json.JSONDecodeError:
print("Databricks CLI configured (couldn't parse user)")
else:
print(f"Warning: CLI config may have issues: {result.stderr}")