-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_replica.py
More file actions
91 lines (79 loc) · 3.06 KB
/
deploy_replica.py
File metadata and controls
91 lines (79 loc) · 3.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""Deploy a CVM replica sharing the same app_id as an existing CVM."""
import os, json, sys
import requests
from pathlib import Path
def load_api_key():
creds = json.loads((Path.home() / ".phala-cloud" / "credentials.json").read_text())
profile = creds["profiles"][creds["current_profile"]]
return profile["token"], profile["api_prefix"]
API_KEY, CLOUD_API = (os.environ.get("PHALA_CLOUD_API_KEY"), os.environ.get("PHALA_CLOUD_API_PREFIX")) if os.environ.get("PHALA_CLOUD_API_KEY") else load_api_key()
HEADERS = {"X-API-Key": API_KEY, "Content-Type": "application/json"}
def get_cvm(identifier):
resp = requests.get(f"{CLOUD_API}/cvms/{identifier}", headers=HEADERS)
resp.raise_for_status()
return resp.json()
def provision(name, compose_content, node_id, kms_id):
payload = {
"name": name,
"image": "dstack-0.5.4",
"vcpu": 1, "memory": 2048, "disk_size": 20,
"teepod_id": node_id,
"kms_id": kms_id,
"compose_file": {
"docker_compose_file": compose_content,
"allowed_envs": [],
"features": ["kms"],
"kms_enabled": True,
"manifest_version": 2,
"name": name,
"public_logs": True,
"public_sysinfo": True,
"tproxy_enabled": False,
},
"env_keys": [],
"listed": True,
"instance_type": "tdx.small",
}
resp = requests.post(f"{CLOUD_API}/cvms/provision", headers=HEADERS, json=payload)
resp.raise_for_status()
return resp.json()
def create_with_app_id(app_id, compose_hash, app_auth_address, deployer_address):
payload = {
"app_id": app_id,
"compose_hash": compose_hash,
"encrypted_env": "",
"app_auth_contract_address": app_auth_address,
"deployer_address": deployer_address,
}
resp = requests.post(f"{CLOUD_API}/cvms", headers=HEADERS, json=payload)
resp.raise_for_status()
return resp.json()
def main():
source_name = sys.argv[1] if len(sys.argv) > 1 else "shared-key-demo-1"
replica_name = sys.argv[2] if len(sys.argv) > 2 else "shared-key-demo-2"
print(f"Fetching source CVM: {source_name}")
source = get_cvm(source_name)
app_id = source["app_id"]
node_id = source["teepod"]["id"]
kms_slug = source["kms_info"]["slug"]
app_auth = source["contract_address"]
deployer = source["deployer_address"]
print(f" app_id: {app_id}")
print(f" app_auth: {app_auth}")
print(f" deployer: {deployer}")
compose = Path("docker-compose-demo.yaml").read_text()
print(f"\nProvisioning replica: {replica_name}")
prov = provision(replica_name, compose, node_id, kms_slug)
print(f" compose_hash: {prov.get('compose_hash')}")
print(f"\nCreating CVM with existing app_id...")
result = create_with_app_id(
app_id=app_id,
compose_hash=prov["compose_hash"],
app_auth_address=app_auth,
deployer_address=deployer,
)
print(f"\nReplica created!")
print(json.dumps(result, indent=2))
if __name__ == "__main__":
main()