1 ###########################################################################
2 # Copyright (C) 2012 by
3 # <savakian@sfa2.grenoble.senslab.info>
5 # Copyright: See COPYING file that comes with this distribution
7 ###########################################################################
9 from sfa.senslab.LDAPapi import *
10 import ldap.modlist as modlist
14 from sfa.util.sfalogging import logger
17 from datetime import datetime
18 from dateutil import tz
19 from time import strftime,gmtime
20 from sfa.senslab.OARrestapi import OARrestapi
23 from sfa.senslab.slabdriver import SlabDriver
24 from sfa.util.config import Config
33 if len(sys.argv) > 1 :
34 options_list = sys.argv[1:]
35 #For each valid option, execute the associated function
36 #(defined in the dictionnary supported_options)
38 valid_options_dict = {}
40 #Passing options to the script should be done like this :
41 #-10 OAR -2 SlabDriver
42 for option in options_list:
43 if option in supported_options:
44 #update the values used for the fonctions associated
47 valid_options_dict[option] = value_list
48 #empty the values list for next option
50 print valid_options_dict
53 value_list.append(option[1:])
54 print "value_list",value_list
57 return valid_options_dict
59 def TestLdap(job_id = None):
60 logger.setLevelDebug()
63 ret = ldap.conn.connect(bind=True)
65 print "TEST ldap.conn.connect(bind=True)" , ret
67 ret = ldap.conn.connect(bind=False)
69 print "TEST ldap.conn.connect(bind=False)", ret
72 ret = ldap.LdapSearch()
73 print "TEST ldap.LdapSearch ALL",ret
75 ret = ldap.LdapSearch('(uid=avakian)', [])
76 print "\r\n TEST ldap.LdapSearch ids = avakian",ret
79 password = ldap.generate_password()
80 print "\r\n TEST generate_password ",password
82 maxi = ldap.find_max_uidNumber()
83 print "\r\n TEST find_max_uidNumber " , maxi
86 data['last_name'] = "Drake"
87 data['first_name']="Tim"
88 data['givenName']= data['first_name']
89 data['mail'] = "robin@arkham.fr"
92 record['hrn'] = 'senslab2.drake'
93 record['last_name'] = "Drake"
94 record['first_name']="Tim"
95 record['mail'] = "robin@arkham.fr"
99 #login = ldap.generate_login(data)
100 #print "\r\n Robin \tgenerate_login ", ret
102 #ret = ldap.LdapAddUser(data)
103 #print "\r\n Robin \tLdapAddUser ", ret
105 #req_ldap = '(uid=' + login + ')'
106 #ret = ldap.LdapSearch(req_ldap, [])
107 #print "\r\n Robin \tldap.LdapSearch ids = %s %s"%(login,ret )
109 #password = "Thridrobin"
110 #enc = ldap.encrypt_password(password)
111 #print "\r\n Robin \tencrypt_password ", enc
113 #ret = ldap.LdapModifyUser(record, {'userPassword':enc})
114 #print "\r\n Robin \tChange password LdapModifyUser ", ret
116 #dn = 'uid=' + login + ',' + ldap.baseDN
117 #ret = ldap.LdapDelete(dn)
118 #print "\r\n Robin \tLdapDelete ", ret
121 datanight['last_name'] = "Grayson"
122 datanight['first_name']="Dick"
123 datanight['givenName']= datanight['first_name']
124 datanight['mail'] = "nightwing@arkham.fr"
128 record_night['hrn'] = 'senslab2.grayson'
129 record_night['last_name'] = datanight['last_name']
130 record_night['first_name'] = datanight['first_name']
131 record_night['mail'] = datanight['mail']
133 ret = ldap.LdapFindUser(record_night)
134 print "\r\n Nightwing \tldap.LdapFindHrn %s : %s"%(record_night,ret)
136 ret = ldap.LdapSearch('(uid=grayson)', [])
137 print "\r\n Nightwing \tldap.LdapSearch ids = %s %s"%('grayson',ret )
139 ret = ldap.LdapAddUser(datanight)
140 print "\r\n Nightwing \tLdapAddUser ", ret
142 ret = ldap.LdapResetPassword(record_night)
143 print "\r\n Nightwing \tLdapResetPassword de %s : %s "%(record_night,ret)
145 ret = ldap.LdapDeleteUser(record_night)
146 print "\r\n Nightwing \tLdapDeleteUser ", ret
150 record_avakian['hrn']= 'senslab2.avakian'
151 record_avakian['last_name'] = 'avakian'
152 record_avakian['first_name'] = 'sandrine'
153 record_avakian['mail'] = 'sandrine.avakian@inria.fr'
154 pubkey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwSUkJ+cr3xM47h8lFkIXJoJhg4wHakTaLJmgTXkzvUmQsQeFB2MjUZ6WAelMXj/EFz2+XkK+bcWNXwfbrLptJQ+XwGpPZlu9YV/kzO63ghVrAyEg0+p7Pn1TO9f1ZYg4R6JfP/3qwH1AsE+X3PNpIewsuEIKwd2wUCJDf5RXJTpl39GizcBFemrRqgs0bdqAN/vUT9YvtWn8fCYR5EfJHVXOK8P1KmnbuGZpk7ryz21pDMlgw13+8aYB+LPkxdv5zG54A5c6o9N3zOCblvRFWaNBqathS8y04cOYWPmyu+Q0Xccwi7vM3Ktm8RoJw+raQNwsmneJOm6KXKnjoOQeiQ== savakian@sfa2.grenoble.senslab.info"
157 ret = ldap.LdapModifyUser(record_avakian, {'sshPublicKey':pubkey})
158 print "\r\n Sandrine \tChange pubkey LdapModifyUser ", ret
160 password = "ReptileFight"
161 enc = ldap.encrypt_password(password)
162 print "\r\n sandrine \tencrypt_password ", enc
164 ret = ldap.LdapModifyUser(record_avakian, {'userPassword':enc})
165 print "\r\n sandrine \tChange password LdapModifyUser ", ret
169 def get_stuff(oar, uri):
173 data = json.dumps({})
175 headers['X-REMOTE_IDENT'] = 'avakian'
177 headers['content-length'] = '0' #seems that it does not work if we don't add this
180 conn = httplib.HTTPConnection(oar.oarserver['ip'],oar.oarserver['port'])
181 conn.request("GET",uri,data , headers )
182 resp = ( conn.getresponse()).read()
183 #logger.debug("OARrestapi: \t GETRequestToOARRestAPI resp %s" %( resp))
187 js = json.loads(resp)
193 def TestOAR(job_id = '1'):
195 if isinstance(job_id,list) and len(job_id) == 1:
199 jobs = oar.parser.SendRequest("GET_reserved_nodes", username = 'avakian')
200 print "\r\n OAR GET_reserved_nodes ",jobs
204 jobs = oar.parser.SendRequest("GET_jobs")
205 print "\r\n OAR GET_jobs ",jobs
208 jobs = oar.parser.SendRequest("GET_jobs_id", job_id, 'avakian')
209 print "\r\n OAR GET_jobs_id ",jobs
211 uri = '/oarapi/jobs/details.json?state=Running,Waiting,Launching&user=avakian'
212 raw_json = get_stuff(oar,uri)
213 print "\r\nOAR ", uri, raw_json, "\r\n KKK \t",raw_json.keys()
215 uri = '/oarapi/jobs/' + job_id +'.json'
216 raw_json = get_stuff(oar,uri)
217 print "\r\n OAR ",uri,raw_json, "\r\n KKK \t",raw_json.keys()
219 uri = '/oarapi/jobs/' + job_id + '/resources.json'
220 raw_json = get_stuff(oar,uri)
221 print "\r\n OAR ",uri, raw_json, "\r\n KKK \t",raw_json.keys()
223 time_format = "%Y-%m-%d %H:%M:%S"
225 server_timestamp,server_tz = oar.parser.SendRequest("GET_timezone")
227 print "\r\n OAR GetTimezone ",server_timestamp, server_tz
228 print(datetime.fromtimestamp(int(server_timestamp)).strftime('%Y-%m-%d %H:%M:%S'))
230 uri = '/oarapi/resources/full.json'
231 raw_json = get_stuff(oar,uri)
232 print "\r\n OAR ",uri, raw_json, "\r\n KKK \t",raw_json.keys()
234 uri = '/oarapi/jobs.json?user=avakian'
235 raw_json = get_stuff(oar,uri)
236 print "\r\nOAR ", uri, raw_json, "\r\n KKK \t",raw_json.keys()
239 def TestSlabDriver(job_id = '1'):
240 if isinstance(job_id,list) and len(job_id) == 1:
242 slabdriver = SlabDriver(Config())
243 nodes = slabdriver.GetReservedNodes(username='avakian')
244 print "\r\n \r\n" ,nodes
246 l = slabdriver.GetSlices(slice_filter = '29', slice_filter_type = 'record_id_user')
249 print "\r\n \r\nGetSlices" ,l
251 persons = slabdriver.GetPersons()
252 print "\r\n \r\n GetPersons" ,persons
253 #slabdriver.DeleteJobs(job_id,'senslab2.avakian_slice')
260 supported_options = {
263 'driver': TestSlabDriver,
267 opts = parse_options()
270 supported_options[opt](opts[opt])
273 if __name__ == "__main__":