/
host_utils.py
195 lines (159 loc) · 5.23 KB
/
host_utils.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@version: 1.0.0
@author: zheng guang
@contact: zg.zhu@daocloud.io
@time: 16/3/7 上午11:10
"""
import base64
import json
import time
from sdk import UcloudApiClient
from config import *
def getImage(ImageType="Base"):
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "DescribeImage",
"Region": region,
"Zone":zone,
"ImageType": ImageType,
"OsType": 'Linux',
"Offset": 0,
"Limit": 1000,
"ProjectId":project_id
}
response = ApiClient.get("/", Parameters)
if 0 != response['RetCode']:
print response
exit(1)
print response
def create(tag=None, name=None, password='dangerous', cpu='1', memory=2, diskSpace=50, imageId="uimage-j4fbrn",
uhostType='Normal',StorageType='LocalDisk'):
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "CreateUHostInstance",
"Region": region,
"Zone":zone,
"ImageId": imageId,
"LoginMode": "Password",
"Password": base64.b64encode(password),
"Tag": tag,
"CPU": cpu,
"Memory": int(memory) * 1024,
"DiskSpace": int(diskSpace),
"Name": name,
"StorageType":StorageType,
"UHostType": uhostType # SATA_SSD ,BigData
}
response = ApiClient.get("/", Parameters)
if 0 != response['RetCode']:
print response
exit(1)
for i in range(120):
time.sleep(1)
host = get(name)
if host and 'IPSet' in host and 'IP' in host['IPSet'][0]: # and 'Running' in host['State']
print host['IPSet'][0]['IP']
return host
raise TypeError("can not get host info")
def get_all():
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "DescribeUHostInstance",
"Region": region,
"Limit": '100000'
}
response = ApiClient.get("/", Parameters);
print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
def get(host_name='', ip=''):
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "DescribeUHostInstance",
"Region": region,
"Limit": '100000'
}
hosts = ApiClient.get("/", Parameters)
for host in hosts['UHostSet'] :
if 'Name' in host and 'IPSet' in host and len(host['IPSet'])>0 and 'IP' in host['IPSet'][0]:
if host_name == host['Name'] or ip == host['IPSet'][0]['IP']:
return host
else:
print "get host Describe err"
def stop(ip, check=False):
host = get(ip=ip)
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "StopUHostInstance",
"Region": region,
"UHostId": host['UHostId']
}
response = ApiClient.get("/", Parameters);
if check:
checkStatus(ip, 'Stopped')
print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
def poweroff(ip, check=False):
host = get(ip=ip)
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "PoweroffUHostInstance",
"Region": region,
"UHostId": host['UHostId']
}
response = ApiClient.get("/", Parameters);
if check:
checkStatus(ip, 'Stopped')
print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
"""
status :Running ,Stopping
"""
def checkStatus(ip, status='Running'):
host = get(ip=ip)
for i in range(120):
host = get(ip=ip)
if host and 'State' in host:
print host['State']
if status in host['State']:
return
time.sleep(1)
exit(1)
def reboot(ip, check=False):
host = get(ip=ip)
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "RebootUHostInstance",
"Region": region,
"UHostId": host['UHostId']
}
response = ApiClient.get("/", Parameters);
if check:
checkStatus(ip)
print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
def ReinstallUHostInstance(ip, password='dangerous', imageId='uimage-j4fbrn'):
host = get(ip=ip)
poweroff(ip, check=True)
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "ReinstallUHostInstance",
"Region": region,
"UHostId": host['UHostId'],
"Password": base64.b64encode(password),
"ImageId": imageId
}
response = ApiClient.get("/", Parameters);
print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
def delete(ip):
host = get(ip=ip)
stop(ip, check=True)
ApiClient = UcloudApiClient(base_url, public_key, private_key)
Parameters = {
"Action": "TerminateUHostInstance",
"Region": region,
"UHostId": host['UHostId']
}
response = ApiClient.get("/", Parameters);
print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
if __name__ == '__main__':
#getImage()# centos 14.04 uimage-kg0w4u ubuntu 14.04 uimage-pbalgu
# create('test','test','dangerous',2,4,200)
pass