-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (83 loc) · 2.56 KB
/
main.py
File metadata and controls
91 lines (83 loc) · 2.56 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
# -*- coding: utf-8 -*-
import os
import logging
import random
from ucloud.client import Client
from ucloud.helpers import wait, utils
logger = logging.getLogger("ucloud")
logger.setLevel(logging.DEBUG)
def main():
client = Client(
{
"region": "cn-bj2",
"project_id": os.getenv("UCLOUD_PROJECT_ID"),
"public_key": os.getenv("UCLOUD_PUBLIC_KEY"),
"private_key": os.getenv("UCLOUD_PRIVATE_KEY"),
}
)
logger.info("finding image, random choice one")
images = (
client.uhost()
.describe_image({"ImageType": "Base", "OsType": "Linux"})
.get("ImageSet", [])
)
assert len(images) > 0
logger.info("creating uhost instance ...")
image = random.choice(images)
resp = client.uhost().create_uhost_instance(
{
"Name": "sdk-python-example",
"Zone": image["Zone"],
"ImageId": image["ImageId"],
"LoginMode": "Password",
"Password": utils.gen_password(20),
"CPU": 1,
"Memory": 1024,
"Disks": [
{
"Size": image["ImageSize"],
"Type": "LOCAL_NORMAL",
"IsBoot": "True",
}
],
}
)
uhost_id = utils.first(resp["UHostIds"])
logger.info("uhost instance is created")
def refresh_state():
uhost = utils.first(
client.uhost()
.describe_uhost_instance({"UHostIds": [uhost_id]})
.get("UHostSet", [])
)
if uhost.get("State") in ["Running", "Stopped"]:
return uhost["State"].lower()
return "pending"
logger.info("wait uhost state is running ...")
try:
wait.wait_for_state(
pending=["pending"],
target=["running"],
timeout=300,
refresh=refresh_state,
)
except wait.WaitTimeoutException as e:
logger.error(e)
logger.info("uhost instance is running")
logger.info("stopping uhost for clean up resources ...")
client.uhost().stop_uhost_instance({"UHostId": uhost_id})
try:
wait.wait_for_state(
pending=["pending"],
target=["stopped"],
timeout=180,
refresh=refresh_state,
)
except wait.WaitTimeoutException as e:
logger.error(e)
else:
logger.info("uhost instance is stopped")
logger.info("remove uhost instance from cloud")
client.uhost().terminate_uhost_instance({"UHostId": uhost_id})
if __name__ == "__main__":
main()