Skip to content

Commit ca8d9f0

Browse files
committed
add example
Signed-off-by: kerthcet <kerthcet@gmail.com>
1 parent 8801688 commit ca8d9f0

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ print(f"Output: {result.stdout}")
114114
# ... repeat for n+ machines
115115
```
116116

117+
## Examples
118+
119+
See the [examples/](./examples) directory for common use cases.
120+
117121
## Development
118122

119123
See [DEVELOP.md](./docs/DEVELOP.md) for the complete developer guide including build commands, testing, and troubleshooting.

examples/install_git.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env python3
2+
"""Example: Installing git on daemons
3+
4+
This example shows how to check if git is available and install it if needed.
5+
6+
Usage:
7+
python examples/install_git.py
8+
"""
9+
10+
from sandd import Server
11+
import sys
12+
import time
13+
14+
15+
def check_git_available(server, daemon_id):
16+
"""Check if git is available on a daemon"""
17+
result = server.execute_command(daemon_id, "which git", timeout=5)
18+
return result.success
19+
20+
21+
def install_git(server, daemon_id):
22+
"""Install git on a daemon using the system package manager"""
23+
print(f"Installing git on {daemon_id}...")
24+
25+
# Detect platform
26+
platform_result = server.execute_command(daemon_id, "uname -s", timeout=5)
27+
if not platform_result.success:
28+
print("❌ Could not detect platform")
29+
return False
30+
31+
platform = platform_result.stdout.strip().lower()
32+
33+
# Determine install command
34+
if "linux" in platform:
35+
# Check if it's Debian/Ubuntu or RHEL/CentOS
36+
distro_result = server.execute_command(
37+
daemon_id,
38+
"cat /etc/os-release 2>/dev/null || echo 'unknown'",
39+
timeout=5
40+
)
41+
distro = distro_result.stdout.lower()
42+
43+
if "ubuntu" in distro or "debian" in distro:
44+
cmd = "sudo apt-get update && sudo apt-get install -y git"
45+
elif "centos" in distro or "rhel" in distro or "fedora" in distro:
46+
cmd = "sudo yum install -y git"
47+
else:
48+
# Default to apt for unknown Linux
49+
cmd = "sudo apt-get update && sudo apt-get install -y git"
50+
51+
elif "darwin" in platform:
52+
cmd = "brew install git"
53+
else:
54+
print(f"❌ Unsupported platform: {platform}")
55+
return False
56+
57+
# Execute installation
58+
result = server.execute_command(daemon_id, cmd, timeout=300)
59+
60+
if result.success:
61+
print("✓ git installed successfully")
62+
return True
63+
else:
64+
print(f"❌ Failed to install git: {result.stderr}")
65+
return False
66+
67+
68+
def main():
69+
print("Git Installation Example")
70+
print("=" * 50)
71+
72+
# Connect to server
73+
server = Server("127.0.0.1", 8765)
74+
print(f"✓ Server started on {server.address}\n")
75+
76+
# Wait for at least one daemon
77+
print("Waiting for daemons to connect...")
78+
daemons = server.list_daemons()
79+
while not daemons:
80+
time.sleep(1)
81+
daemons = server.list_daemons()
82+
83+
daemon_id = daemons[0]
84+
print(f"✓ Found daemon: {daemon_id}\n")
85+
86+
# Check if git is available
87+
print("Checking if git is available...")
88+
if check_git_available(server, daemon_id):
89+
print("✓ git is already installed")
90+
91+
# Get git version
92+
result = server.execute_command(daemon_id, "git --version", timeout=5)
93+
print(f" Version: {result.stdout.strip()}")
94+
else:
95+
print("✗ git is not installed")
96+
print()
97+
98+
# Install git
99+
if install_git(server, daemon_id):
100+
# Verify installation
101+
result = server.execute_command(daemon_id, "git --version", timeout=5)
102+
if result.success:
103+
print(f" Version: {result.stdout.strip()}")
104+
else:
105+
print("Failed to install git")
106+
sys.exit(1)
107+
108+
print()
109+
110+
# Test git functionality
111+
print("Testing git functionality...")
112+
print("-" * 50)
113+
114+
# Create a test repo
115+
test_commands = [
116+
("mkdir -p /tmp/test-repo && cd /tmp/test-repo", "Create test directory"),
117+
("git init", "Initialize git repo"),
118+
("git config user.name 'Test User'", "Configure user"),
119+
("git config user.email 'test@example.com'", "Configure email"),
120+
("echo 'Hello from SandD' > README.md", "Create file"),
121+
("git add README.md", "Stage file"),
122+
("git commit -m 'Initial commit'", "Create commit"),
123+
("git log --oneline", "Show commit"),
124+
]
125+
126+
for cmd, description in test_commands:
127+
result = server.execute_command(daemon_id, cmd, timeout=10)
128+
if result.success:
129+
print(f"✓ {description}")
130+
if "git log" in cmd:
131+
print(f" {result.stdout.strip()}")
132+
else:
133+
print(f"✗ {description}: {result.stderr}")
134+
135+
print()
136+
print("=" * 50)
137+
print("Example complete!")
138+
139+
140+
if __name__ == "__main__":
141+
try:
142+
main()
143+
except KeyboardInterrupt:
144+
print("\n\nInterrupted by user")
145+
sys.exit(0)
146+
except Exception as e:
147+
print(f"\n❌ Error: {e}")
148+
sys.exit(1)

0 commit comments

Comments
 (0)