42 lines
1.9 KiB
Python
42 lines
1.9 KiB
Python
import paramiko
|
|
import json
|
|
import sys
|
|
|
|
def main():
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
ssh.connect('140.44.4.41', username='root', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10)
|
|
|
|
# Check if repo already exists
|
|
stdin, stdout, stderr = ssh.exec_command('kubectl exec -n infrastructure deployment/gitea -c gitea -- ls -la /data/git/gitea-repositories/snarf')
|
|
snarf_repos = stdout.read().decode()
|
|
if 'homelab-gitops.git' in snarf_repos:
|
|
print("homelab-gitops.git already exists on Gitea!")
|
|
else:
|
|
print("Generating Gitea API token for Snarf...")
|
|
stdin, stdout, stderr = ssh.exec_command("kubectl exec -n infrastructure deployment/gitea -c gitea -- gitea admin user generate-access-token -u Snarf -t gitops-token --scopes all --raw")
|
|
token = stdout.read().decode().strip()
|
|
print(f"Token obtained (len: {len(token)})")
|
|
|
|
create_payload = json.dumps({
|
|
"name": "homelab-gitops",
|
|
"description": "Snarfnet Homelab GitOps, Terraform, and ArgoCD Declarations",
|
|
"private": False,
|
|
"auto_init": True
|
|
})
|
|
|
|
# Call Gitea API inside gitea container
|
|
curl_cmd = f"kubectl exec -i -n infrastructure deployment/gitea -c gitea -- curl -s -X POST http://localhost:3000/api/v1/user/repos -H \"Authorization: token {token}\" -H \"Content-Type: application/json\" -d @-"
|
|
stdin, stdout, stderr = ssh.exec_command(curl_cmd)
|
|
stdin.write(create_payload)
|
|
stdin.channel.shutdown_write()
|
|
resp = stdout.read().decode()
|
|
print("API Response:", resp)
|
|
|
|
stdin, stdout, stderr = ssh.exec_command('kubectl exec -n infrastructure deployment/gitea -c gitea -- ls -la /data/git/gitea-repositories/snarf')
|
|
print("Repos under snarf:\n", stdout.read().decode())
|
|
ssh.close()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|