File size: 2,802 Bytes
19844ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Portable RunPod control helper. Credentials come only from the environment."""
import argparse,json,os
from pathlib import Path
import requests

ap=argparse.ArgumentParser()
sub=ap.add_subparsers(dest='action',required=True)
create=sub.add_parser('create');create.add_argument('--public-key',type=Path,required=True);create.add_argument('--disk',type=int,default=450);create.add_argument('--ram',type=int,default=128);create.add_argument('--vcpus',type=int,default=16);create.add_argument('--max-hourly',type=float,default=2.0)
for action in ['show','terminate']:
    p=sub.add_parser(action);p.add_argument('--id',required=True)
args=ap.parse_args();key=os.environ.get('RUNPOD_API_KEY')
if not key:raise SystemExit('Set RUNPOD_API_KEY in your environment.')
headers={'Authorization':'Bearer '+key};base='https://rest.runpod.io/v1/pods'
if args.action=='create':
    pub=args.public_key.read_text().strip()
    if not pub.startswith(('ssh-ed25519 ','ssh-rsa ')) or '\n' in pub or "'" in pub:
        raise SystemExit('Provide a single-line SSH public key, never a private key.')
    start="mkdir -p /root/.ssh /run/sshd; chmod 700 /root/.ssh; echo '"+pub+"' > /root/.ssh/authorized_keys; chmod 600 /root/.ssh/authorized_keys; ssh-keygen -A; /usr/sbin/sshd -D -e"
    payload={'name':'kolibri-gguf-tutorial','imageName':'runpod/pytorch:2.8.0-py3.11-cuda12.8.1-cudnn-devel-ubuntu22.04','gpuTypeIds':['NVIDIA A100-SXM4-80GB'],'gpuCount':1,'containerDiskInGb':args.disk,'volumeInGb':0,'minRAMPerGPU':args.ram,'minVCPUPerGPU':args.vcpus,'ports':['22/tcp'],'dockerEntrypoint':['/bin/bash','-lc'],'dockerStartCmd':[start]}
    response=requests.post(base,headers=headers,json=payload,timeout=60);response.raise_for_status();pod=response.json()
    if float(pod['costPerHr'])>args.max_hourly:
        deletion=requests.delete(base+'/'+pod['id'],headers=headers,timeout=60);deletion.raise_for_status()
        raise SystemExit('Actual hourly price exceeded your chosen ceiling; pod terminated.')
    print(json.dumps({k:pod.get(k) for k in ['id','name','costPerHr','memoryInGb','vcpuCount','containerDiskInGb','publicIp','portMappings']},indent=2))
elif args.action=='show':
    response=requests.get(base+'/'+args.id,headers=headers,timeout=60);response.raise_for_status();pod=response.json()
    print(json.dumps({k:pod.get(k) for k in ['id','name','desiredStatus','costPerHr','memoryInGb','vcpuCount','publicIp','portMappings']},indent=2))
else:
    response=requests.delete(base+'/'+args.id,headers=headers,timeout=60);response.raise_for_status()
    check=requests.get(base,headers=headers,timeout=60);check.raise_for_status()
    if any(p['id']==args.id for p in check.json()):raise RuntimeError('Pod still appears in list; check RunPod console.')
    print('Terminated and absence verified:',args.id)