-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-man-client.py
executable file
·211 lines (172 loc) · 7.8 KB
/
app-man-client.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
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
import argparse
import urllib2
import json
import os
class AppManClient:
def __init__( self, app_man_url ):
self.app_man_url = app_man_url
def list_apps( self ):
"""
list all application
Returns:
array of applications
"""
f = urllib2.urlopen( '%s/list/app' % self.app_man_url )
return json.load( f )
def start_app( self, name ):
"""
start application
"""
req = urllib2.Request( '%s/start/app/%s' % (self.app_man_url, name) )
req.get_method = lambda: 'POST'
f = urllib2.urlopen( req )
return f.read()
def stop_app( self, name ):
"""
stop application
"""
req = urllib2.Request( '%s/stop/app/%s' % (self.app_man_url, name) )
req.get_method = lambda: 'POST'
f = urllib2.urlopen( req )
return f.read()
def restart_app( self, name ):
"""
restart an application
Args:
name - the application name
"""
req = urllib2.Request( '%s/restart/app/%s' % (self.app_man_url, name) )
req.get_method = lambda: 'POST'
f = urllib2.urlopen( req )
return f.read()
def list_files( self, name ):
"""
list application related files
Args:
name - the application name
Returns:
the array of configuration files
"""
f = urllib2.urlopen( '%s/list/conf/%s' % ( self.app_man_url, name ) )
return json.load( f )
def upload_file( self, app_name, srcfile, destfile ):
"""
upload a file to the application
Args:
app_name - the application name
srfile - the local file to be loaded
destfile - the remote file should be uploaded
"""
if not os.path.exists( srcfile ):
return "local file %s does not exist" % srcfile
if not os.path.isfile( srcfile ):
return "local file %s is not a regular file" % srcfile
offset = 0
with open( srcfile ) as fp:
while True:
data = fp.read( 1024 * 1024)
data_len = len( data )
if data_len <= 0: return "succeed to save file"
req = urllib2.Request( '%s/upload/conf/%s/%s' % (self.app_man_url, app_name, destfile) )
req.add_header( 'Content-Type','application/octet-stream')
req.add_header( 'Content-length', data_len )
req.add_header( 'Content-Range', 'bytes %d-%d/*' % ( offset, offset + data_len - 1 ) )
req.add_data( data )
offset = offset + data_len
req.get_method = lambda: 'POST'
urllib2.urlopen( req )
return "fail to open local file %s" % srcfile
def delete_file( self, app_name, filename ):
"""
delete a file of application
Args:
app_name - the application name
filename - the remote file of application should be deleted
"""
req = urllib2.Request( '%s/delete/conf/%s/%s' % ( self.app_man_url, app_name, filename ) )
req.get_method = lambda: 'PUT'
f = urllib2.urlopen( req )
return f.read()
def download_file( self, app_name, filename ):
"""
download a file of application
Args:
app_name - the application name
filename - the remote file of application
"""
f = urllib2.urlopen( '%s/download/conf/%s/%s' % ( self.app_man_url, app_name, filename ) )
return f.read()
def list_apps( args ):
client = AppManClient( args.url )
print client.list_apps()
def start_app( args ):
client = AppManClient( args.url )
print client.start_app( args.name )
def stop_app( args ):
client = AppManClient( args.url )
print client.stop_app( args.name )
def restart_app( args ):
client = AppManClient( args.url )
print client.restart_app( args.name )
def list_files( args ):
client = AppManClient( args.url )
files = client.list_files( args.name )
print "\n".join( files )
def upload_file( args ):
client = AppManClient( args.url )
print client.upload_file( args.name, args.localfile, args.destfile )
def download_file( args ):
client = AppManClient( args.url )
content = client.download_file( args.name, args.filename )
filename = args.destfile if args.destfile else os.path.basename( args.filename )
with open(filename, "wb" ) as fp:
fp.write( content )
def delete_file( args ):
client = AppManClient( args.url )
print client.delete_file( args.name, args.filename )
def parse_args():
parser = argparse.ArgumentParser( description = "the client of restful application manager" )
subparsers = parser.add_subparsers( help = "supported commands" )
list_app_parser = subparsers.add_parser( "list-app", help = "list all the applications" )
list_app_parser.add_argument( "url", help = "the url of application manager" )
list_app_parser.set_defaults( func = list_apps )
start_app_parser = subparsers.add_parser( "start-app", help = "start the application" )
start_app_parser.add_argument( "url", help = "the url of application manager" )
start_app_parser.add_argument( "name", help = "the application name" )
start_app_parser.set_defaults( func = start_app )
stop_app_parser = subparsers.add_parser( "stop-app", help = "stop the application" )
stop_app_parser.add_argument( "url", help = "the url of application manager" )
stop_app_parser.add_argument( "name", help = "the application name" )
stop_app_parser.set_defaults( func = stop_app )
restart_app_parser = subparsers.add_parser( "restart-app", help = "restart the application" )
restart_app_parser.add_argument( "url", help = "the url of application manager" )
restart_app_parser.add_argument( "name", help = "the application name" )
restart_app_parser.set_defaults( func = restart_app )
list_files_parser = subparsers.add_parser( "list-file", help = "list the application configuration files" )
list_files_parser.add_argument( "url", help = "the url of application manager" )
list_files_parser.add_argument( "name", help = "the application name" )
list_files_parser.set_defaults( func = list_files )
upload_file_parser = subparsers.add_parser( "upload-file", help = "upload the application configuration file" )
upload_file_parser.add_argument( "url", help = "the url of application manager" )
upload_file_parser.add_argument( "name", help = "the application name" )
upload_file_parser.add_argument( "localfile", help = "the local file to be loaded" )
upload_file_parser.add_argument( "destfile", help = "the remote destination file" )
upload_file_parser.set_defaults( func = upload_file )
download_file_parser = subparsers.add_parser( "download-file", help = "download the application configuration file" )
download_file_parser.add_argument( "url", help = "the url of application manager" )
download_file_parser.add_argument( "name", help = "the application name" )
download_file_parser.add_argument( "filename", help = "the name of remote file" )
download_file_parser.add_argument( "--destfile", help = "the local destination file" )
download_file_parser.set_defaults( func = download_file )
delete_file_parser = subparsers.add_parser( "delete-file", help = "delete an application configuration file" )
delete_file_parser.add_argument( "url", help = "the url of application manager" )
delete_file_parser.add_argument( "name", help = "the application name" )
delete_file_parser.add_argument( "filename", help = "the name of remote file" )
delete_file_parser.set_defaults( func = delete_file )
args = parser.parse_args()
args.func( args )
def main():
parse_args()
if __name__ == "__main__":
main()