Sync Integ to Master
[sdc.git] / asdctool / src / main / resources / scripts / python / user / exportUsers.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5
6
7 ####################################################################################################################################################################################
8 #                                                                                                                                                                                  #
9 # Export all active users to file - for 1602+                                                                                                                                      #
10 #                                                                                                                                                                                  #
11 # activation :                                                                                                                                                                     #
12 #       python exportUsers.py  [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ]   #
13 #                                                                                                                                                                                  #
14 # shortest activation (be host = localhost, be port = 8080):                                                                                                                       #
15 #        python exportUsers.py [-f <output file> | --ofile=<output file> ]                                                                                                         #
16 #                                                                                                                                                                                  #
17 ####################################################################################################################################################################################
18
19 ALL_USERS_SUFFIX = '/sdc2/rest/v1/user/users'
20
21 def errorAndExit(errorCode, errorDesc):
22         if ( errorCode > 0 ):
23                 print("status=" + str(errorCode) + ". " + errorDesc)
24         else:
25                 print("status=" + str(errorCode))
26         sys.exit(errorCode)
27
28 def getUsers(scheme, beHost, bePort, adminUser):
29
30         try:
31                 buffer = StringIO()
32                 c = pycurl.Curl()
33
34                 url = scheme + '://' + beHost + ':' + bePort + ALL_USERS_SUFFIX 
35                 print(url)
36                 c.setopt(c.URL, url)
37                 c.setopt(c.WRITEFUNCTION, buffer.write)
38                 #c.setopt(c.WRITEFUNCTION, lambda x: None)
39                 adminHeader = 'USER_ID: ' + adminUser
40                 c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', adminHeader])
41
42                 if scheme == 'https':
43                         c.setopt(c.SSL_VERIFYPEER, 0)
44
45                 res = c.perform()
46                 #print(res)
47
48                 #print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
49
50                 c.close()
51
52                 body = buffer.getvalue()
53
54         #print(body)
55
56                 return (body, None)
57
58         except Exception as inst:
59                 print inst
60         #print type(inst)     # the exception instance
61         #print inst.args      # arguments stored in .args
62         #print inst           # __str__ allows args to be printed directly
63         #x, y = inst.args
64         #print 'x =', x
65                 
66                 return (None, inst)
67
68
69 def usage():
70         print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <output file> | --ofile=<output file> ]'
71
72 def main(argv):
73         print 'Number of arguments:', len(sys.argv), 'arguments.'
74
75         adminHeader = 'jh0003'
76         beHost = 'localhost'
77         bePort = '8080'
78         outputfile = None 
79         scheme = 'http'
80
81         try:
82                 opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ofile=","scheme="])
83         except getopt.GetoptError:
84                 usage()
85                 errorAndExit(2, 'Invalid input')
86         
87         for opt, arg in opts:
88         #print opt, arg
89                 if opt == '-h':
90                         usage()
91                         sys.exit(3)
92                 elif opt in ("-i", "--ip"):
93                         beHost = arg
94                 elif opt in ("-p", "--port"):
95                         bePort = arg
96                 elif opt in ("-f", "--ofile"):
97                         outputfile = arg
98                 elif opt in ("-s", "--scheme"):
99                         scheme = arg
100
101         print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', output file =',outputfile
102
103         if ( outputfile == None ):
104                 usage()
105                 sys.exit(3)
106
107         users = getUsers(scheme, beHost, bePort, adminHeader)
108         error = users[1]
109         body = users[0]
110
111         if ( error != None ):
112                 errorAndExit(5, str(error))
113
114         #print body
115
116         io = StringIO(body)
117         usersAsJson = json.load(io)
118
119         writeFile = open(outputfile, 'w')
120
121         json.dump(usersAsJson, writeFile)
122
123         writeFile.close()
124
125         print("-------------------------------------------")
126         errorAndExit(0, None)
127
128 if __name__ == "__main__":
129         main(sys.argv[1:])