dynamic-infra-tooling/infra_cli/samba.py

43 lines
1.6 KiB
Python
Raw Normal View History

from .ssh import SSHClient
class SambaManager:
def __init__(self, config):
# Samba is on la-samba-01 (handled via Jump host or direct IP)
# In this infra, we often use pct exec on the host vmh-11
node = config.get_node('la-vmh-11')
if not node:
raise ValueError("Node 'la-vmh-11' not found in config")
self.host = node['host']
self.password = node.get('pass')
self.user = config.get('proxmox.user', 'root')
self.ssh_key = config.get('proxmox.ssh_key_path')
self.client = SSHClient(self.host, self.user, self.ssh_key, self.password)
# Container ID for la-samba-01
self.lxc_id = "1113"
def exec_samba(self, cmd):
# Executes samba-tool inside the LXC
return self.client.run(f"pct exec {self.lxc_id} -- samba-tool {cmd}")
def list_users(self):
res = self.exec_samba("user list")
return res.stdout
def add_user(self, username, password):
res = self.exec_samba(f"user create {username} {password}")
if res.returncode != 0:
raise RuntimeError(f"Failed to create user {username}: {res.stderr}")
return res.stdout
def add_to_group(self, group, username):
res = self.exec_samba(f"group addmembers {group} {username}")
if res.returncode != 0:
raise RuntimeError(f"Failed to add {username} to {group}: {res.stderr}")
return res.stdout
def list_group_members(self, group):
res = self.exec_samba(f"group listmembers {group}")
return res.stdout