ed7515cc3e2c4d30a7dcb2d77ea8a832b873a1b4
[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(pycurl.SSL_VERIFYPEER, 0)
44                         c.setopt(pycurl.SSL_VERIFYHOST, 0)
45
46                 res = c.perform()
47                 #print(res)
48
49                 #print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
50
51                 c.close()
52
53                 body = buffer.getvalue()
54
55         #print(body)
56
57                 return (body, None)
58
59         except Exception as inst:
60                 print inst
61         #print type(inst)     # the exception instance
62         #print inst.args      # arguments stored in .args
63         #print inst           # __str__ allows args to be printed directly
64         #x, y = inst.args
65         #print 'x =', x
66                 
67                 return (None, inst)
68
69
70 def usage():
71         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> ]'
72
73 def main(argv):
74         print 'Number of arguments:', len(sys.argv), 'arguments.'
75
76         adminHeader = 'jh0003'
77         beHost = 'localhost'
78         bePort = '8080'
79         outputfile = None 
80         scheme = 'http'
81
82         try:
83                 opts, args = getopt.getopt(argv,"i:p:f:h:s:",["ip=","port=","ofile=","scheme="])
84         except getopt.GetoptError:
85                 usage()
86                 errorAndExit(2, 'Invalid input')
87         
88         for opt, arg in opts:
89         #print opt, arg
90                 if opt == '-h':
91                         usage()
92                         sys.exit(3)
93                 elif opt in ("-i", "--ip"):
94                         beHost = arg
95                 elif opt in ("-p", "--port"):
96                         bePort = arg
97                 elif opt in ("-f", "--ofile"):
98                         outputfile = arg
99                 elif opt in ("-s", "--scheme"):
100                         scheme = arg
101
102         print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', output file =',outputfile
103
104         if ( outputfile == None ):
105                 usage()
106                 sys.exit(3)
107
108         users = getUsers(scheme, beHost, bePort, adminHeader)
109         error = users[1]
110         body = users[0]
111
112         if ( error != None ):
113                 errorAndExit(5, str(error))
114
115         #print body
116
117         io = StringIO(body)
118         usersAsJson = json.load(io)
119
120         writeFile = open(outputfile, 'w')
121
122         json.dump(usersAsJson, writeFile)
123
124         writeFile.close()
125
126         print("-------------------------------------------")
127         errorAndExit(0, None)
128
129 if __name__ == "__main__":
130         main(sys.argv[1:])