-
Notifications
You must be signed in to change notification settings - Fork 526
/
curltool.py
198 lines (178 loc) · 6.8 KB
/
curltool.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
196
197
198
#!/usr/bin/env python
# coding=utf-8
import config
import requests
import json
errcodelist = {
'0': 'Exec success',
'-1': 'Internal error',
'-2': 'Server init fail',
'-3': 'Server start fail',
'-4': 'Service is stop',
'-5': 'BadRequest: Invalid request',
'-6': 'Task already exist',
'-7': 'Invalid user',
'-8': 'File not exist',
'-9': 'File status invalid',
'-10': 'Chunk size not aligned',
'-11': 'FileName not match',
'-12': 'Cannot delete unfinished',
'-13': 'Cannot create when has error',
'-14': 'Cannot cancel finished',
'-15': 'Invalid snapshot',
'-16': 'Cannot delete when using',
'-17': 'Cannot clean task unfinished',
'-18': 'Snapshot count reach the limit',
'-19': 'File exist',
'-20': 'Task is full',
'-21': 'Not support',
}
def encodeParam(params):
url = ''
for (key, value) in params.items():
url += key
url += '='
url += value
url += '&'
url = url[:-1]
return url
def query(params):
serverhost = config.get('server.address')
encoded_params = encodeParam(params)
url = "http://%s/SnapshotCloneService?%s" % (serverhost, encoded_params)
ret = requests.get(url)
return ret.json()
def query_snapshot_progress(user, filename, uuid):
params = {'Action':'GetFileSnapshotInfo', 'Version':'0.0.6', 'User':user, 'File':filename, 'UUID':uuid}
jsonobj = query(params)
if jsonobj['Code'] != '0' or jsonobj['TotalCount'] != 1:
return -1
snapInfo = jsonobj['Snapshots'][0]
return snapInfo['Progress']
def delete_or_cancel_snapshot(method, uuid, user, file):
params = None
if user and uuid:
params = {'Action':method, 'Version':'0.0.6', 'User':user, 'UUID':uuid, 'File':file}
if params is None:
return -1
jsonobj = query(params)
if jsonobj['Code'] != '0':
print('%s uuid=%s, user=%s failed, ecode=%s, etext=%s' % (method, uuid, user, jsonobj['Code'], errcodelist[jsonobj['Code']]))
return -1
print('%s uuid=%s, user=%s success' % (method, uuid, user))
return 0
def clean_clone_or_recover(taskid, user):
params = None
if taskid or user:
params = {'Action':'CleanCloneTask', 'Version':'0.0.6', 'User':user, 'UUID':taskid}
if params is None:
return -1
jsonobj = query(params)
if jsonobj['Code'] != '0':
print('clean taskid=%s, user=%s failed, ecode=%s, etext=%s' % (taskid, user, jsonobj['Code'], errcodelist[jsonobj['Code']]))
return -1
print('clean taskid=%s, user=%s success' % (taskid, user))
return 0
def create_snapshot(user, filename, snapshotname):
if user is None or filename is None or snapshotname is None:
print('user, filename, snapshotname need')
return
params = {'Action':'CreateSnapshot', 'Version':'0.0.6', 'User':user, 'File':filename, 'Name':snapshotname}
jsonobj = query(params)
if jsonobj['Code'] != '0':
print("create snapshot fail, ecode=%s, etext=%s" % (jsonobj['Code'], errcodelist[jsonobj['Code']]))
return
print("create snapshot success, UUID=%s" % jsonobj['UUID'])
def clone_or_recover(type, user, src, dest, lazy):
if user is None or src is None or dest is None or lazy is None:
print('user, src, dest, lazy need')
return
params = ''
if type == "clone":
params = {'Action':'Clone', 'Version':'0.0.6', 'User':user, 'Source':src, 'Destination':dest, 'Lazy':lazy}
else:
params = {'Action':'Recover', 'Version':'0.0.6', 'User':user, 'Source':src, 'Destination':dest, 'Lazy':lazy}
jsonobj = query(params)
if jsonobj['Code'] != '0':
print("%s fail, ecode=%s, etest=%s" % (type, jsonobj['Code'], errcodelist[jsonobj['Code']]))
return
print("%s success. UUID=%s" % (type, jsonobj['UUID']))
def flatten(user, taskid):
params = None
if user is None or taskid is None:
print('user, taskid need')
return
params = {'Action':'Flatten', 'Version':'0.0.6', 'User':user, 'UUID':taskid}
jsonobj = query(params)
if jsonobj['Code'] != '0':
print("flatten fail, ecode=%s, etest=%s" % (jsonobj['Code'], errcodelist[jsonobj['Code']]))
return
print("flatten success. UUID=%s" % (taskid))
def get_clone_list(user = None, src = None, dest = None, uuid = None, clonetype = None, status = None, limit = None, offset = None):
params = {'Action':'GetCloneTaskList', 'Version':'0.0.6'}
if user is not None:
params['User'] = user
if uuid is not None:
params['UUID'] = uuid
if src is not None:
params['Source'] = src
if dest is not None:
params['Destination'] = dest
if limit is not None:
params['Limit'] = str(limit)
if offset is not None:
params['Offset'] = str(offset)
if status is not None:
params['Status'] = str(status)
if clonetype is not None:
params['Type'] = str(clonetype)
jsonobj = query(params)
if jsonobj['Code'] != '0':
print("get clone list fail, ecode=%s, etest=%s" % (jsonobj['Code'], errcodelist[jsonobj['Code']]))
return
return jsonobj['TotalCount'], jsonobj['TaskInfos']
def get_snapshot_list(user = None, file = None, uuid = None, status = None, limit = None, offset = None):
params = {'Action':'GetFileSnapshotList', 'Version':'0.0.6'}
if user is not None:
params['User'] = user
if uuid is not None:
params['UUID'] = uuid
if file is not None:
params['File'] = file
if limit is not None:
params['Limit'] = str(limit)
if offset is not None:
params['Offset'] = str(offset)
if status is not None:
params['Status'] = str(status)
jsonobj = query(params)
if jsonobj['Code'] != '0':
print("get snap list fail, ecode=%s, etest=%s" % (jsonobj['Code'], errcodelist[jsonobj['Code']]))
return
return jsonobj['TotalCount'], jsonobj['Snapshots']
def get_snapshot_list_all(user = None, file = None, uuid = None, status = None):
limit = 100
offset = 0
receiveCount = 0
receiveRecords = []
while True:
totalCount, records = get_snapshot_list(user, file, uuid, status, limit, offset)
if (records is None) or len(records) == 0:
break
receiveCount += totalCount
receiveRecords.extend(records)
offset += limit
return receiveCount, receiveRecords
def get_clone_list_all(user = None, src = None, dest = None, uuid = None, clonetype = None, status = None):
limit = 100
offset = 0
receiveCount = 0
receiveRecords = []
while True:
totalCount, records = get_clone_list(user, src, dest, uuid, clonetype, status, limit, offset)
if (records is None) or len(records) == 0:
break
receiveCount += totalCount
receiveRecords.extend(records)
offset += limit
return receiveCount, receiveRecords