source: trunk/Monitoring/smon/repo_io.py

Last change on this file was 965, checked in by jripsl, 11 years ago
  • fix database connectivity test script.
File size: 9.2 KB
Line 
1# -*- coding: ISO-8859-1 -*-
2
3##################################
4#  @program        smon
5#  @description    simulation monitor
6#  @copyright      Copyright “(c)2009 Centre National de la Recherche Scientifique CNRS.
7#                             All Rights Reserved”
8#  @svn_file       $Id: repo_io.py 2599 2013-03-24 19:01:23Z jripsl $
9#  @version        $Rev: 2599 $
10#  @lastrevision   $Date: 2013-03-24 20:01:23 +0100 (Sun, 24 Mar 2013) $
11#  @license        CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE)
12##################################
13
14"""
15This module contains repository I/O code
16"""
17
18import sys
19import datetime
20
21
22
23
24
25# --- module static initialization --- #
26
27CSTE_MODE_LOCAL_REPO="local_repo"
28CSTE_MODE_REMOTE_REPO="remote_repo"
29CSTE_MODE_REMOTE_REPO_STUB="remote_repo_stub"
30
31
32g__remote_database_host="ib-pp-db-dev.ipslnet"
33g__remote_database_port="5432"
34g__remote_database_name="pdgr_1_0b5"
35
36
37
38
39# set mode
40mode=CSTE_MODE_REMOTE_REPO
41#mode=CSTE_MODE_LOCAL_REPO
42
43# mode specific init. (e.g. repository driver)
44if mode==CSTE_MODE_REMOTE_REPO_STUB:
45        raise Exception()
46
47elif mode==CSTE_MODE_REMOTE_REPO:
48
49        # line below is to include Prodiguer database I/O library in the search path
50        sys.path.append("/opt/supervisor/prodiguer_lib/src")
51
52        import types
53
54        # import Prodiguer database I/O library
55        import prodiguer_shared.mq.hooks as repo
56        import prodiguer_shared.repo.session as repo_session
57        import prodiguer_shared.models as models
58
59
60        # Test constants.
61        _SIM_ACTIVITY = 'IPSL'
62        _SIM_COMPUTE_NODE = 'TGCC'
63        _SIM_COMPUTE_NODE_LOGIN = 'p86denv'
64        _SIM_COMPUTE_NODE_MACHINE = 'TGCC - Curie'
65        _SIM_EXECUTION_START_DATE = datetime.datetime.now()
66        _SIM_EXECUTION_STATE = models.EXECUTION_STATE_RUNNING
67        _SIM_EXPERIMENT = '1pctCO2'
68        _SIM_MODEL_ENGINE = 'IPSL-CM5A-LR'
69        _SIM_SPACE = models.SIMULATION_SPACE_TEST
70        _MSG_TYPE = "0000"
71        _MSG_CONTENT1 = "12345690"
72        _MSG_CONTENT2 = "12345690"
73
74elif mode==CSTE_MODE_LOCAL_REPO:
75
76        import local_repo as repo
77
78
79else:
80        raise Exception("ERR001 - incorrect mode")
81
82
83
84
85
86
87
88# -- methods -- #
89
90def init():
91        if mode==CSTE_MODE_LOCAL_REPO:
92                repo.connect()
93        elif mode==CSTE_MODE_REMOTE_REPO:
94                _CONNECTION = "postgresql://postgres:Silence107!@%s:%s/%s"%(g__remote_database_host,g__remote_database_port,g__remote_database_name)
95                repo_session.start(_CONNECTION)
96
97        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
98                pass
99        else:
100                raise Exception("ERR004 - incorrect mode")
101
102def free():
103        if mode==CSTE_MODE_LOCAL_REPO:
104                repo.free()
105        elif mode==CSTE_MODE_REMOTE_REPO:
106                repo_session.end()
107        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
108                pass
109        else:
110                raise Exception("ERR009 - incorrect mode")
111
112def populate_tables_with_sample():
113        """
114        used only by "repo-state" pgm
115        """
116       
117        repo.populate_tables_with_sample()
118
119def retrieve_simulations():
120        """
121        used by get_running_simulations
122        """
123        simulations=None
124
125        # debug
126        #print "bla"
127
128
129        if mode==CSTE_MODE_LOCAL_REPO:
130                simulations=repo.retrieve_simulations()
131        elif mode==CSTE_MODE_REMOTE_REPO:
132
133                # prepare
134                simulations=[]
135
136                # execute
137                sims=repo.retrieve_simulations()
138
139                # process return values
140
141                for s in sims:
142
143                        status=get_repo_execution_state(s.ExecutionState_ID)
144
145                        # HACK
146                        if status=="queued":
147                                status="waiting"
148
149                       
150
151                        simulations.append(types.Simulation(id=s.ID,name=s.Name,status=status.lower()))
152
153
154        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
155                pass
156        else:
157                raise Exception("ERR115 - incorrect mode")
158
159
160        # debug
161        """
162        if len(simulations)<1:
163                raise Exception("ERR915 - debug")
164        else:
165                print "%d"%len(simulations)
166        """
167
168
169        return simulations
170
171def test():
172        """
173        not used
174        """
175
176        repo.create_message("test2", 2, "bla2")
177        commit()
178
179        repo.update_simulation_status('1pctCO22', 'ERROR')
180        commit()
181
182        repo.create_message("test3", 3, "bla3")
183        rollback()
184
185def cleanup():
186        if mode==CSTE_MODE_LOCAL_REPO:
187                repo.cleanup()
188        elif mode==CSTE_MODE_REMOTE_REPO:
189
190
191                simulations_to_delete=["BIGBRO.clim.TEST.LMDZOR.p86denv.TGCC.CURIE",
192                                                                "BIGBRO.clim.TEST.LMDZOR.p86denv.TGCC.CURIE",
193                                                                "v5cf.amipMR1.amip.PROD.LMDZOR.p86denv.TGCC.CURIE",
194                                                                "v5cf.amipMR2.amip.PROD.LMDZOR.p86denv.TGCC.CURIE",
195                                                                "v5cf.amipMR3.amip.PROD.LMDZOR.p86denv.TGCC.CURIE"]
196
197
198                for s in simulations_to_delete:
199                        repo.delete_messages(s)
200                        repo.delete_simulation(s)
201
202                        commit()
203
204
205
206        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
207                pass
208        else:
209                raise Exception("ERR007 - incorrect mode")
210
211def commit():
212        if mode==CSTE_MODE_LOCAL_REPO:
213                repo.commit()
214        elif mode==CSTE_MODE_REMOTE_REPO:
215                repo_session.commit()
216        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
217                pass
218        else:
219                raise Exception("ERR002 - incorrect mode")
220
221def rollback():
222        if mode==CSTE_MODE_LOCAL_REPO:
223                repo.rollback()
224        elif mode==CSTE_MODE_REMOTE_REPO:
225                repo_session.commit()
226        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
227                pass
228        else:
229                raise Exception("ERR003 - incorrect mode")
230
231def get_repo_execution_state(state_id):
232    for key, value in models.EXECUTION_STATE_ID_SET.items():
233        if value == state_id:
234            return key
235    return None
236
237def retrieve_simulation(name):
238        simulation=None
239
240        if mode==CSTE_MODE_LOCAL_REPO:
241
242                try:
243                        simulation=repo.retrieve_simulation(name)
244                except:
245                        raise
246
247        elif mode==CSTE_MODE_REMOTE_REPO:
248
249                # prepare args
250                # ..
251
252                # execute
253                s=repo.retrieve_simulation(name)
254
255                if s is None:
256                        #raise Exception("RG543534")
257                        return None
258
259
260
261                # process return values
262
263                status=get_repo_execution_state(s.ExecutionState_ID)
264
265                # HACK
266                if status=="queued":
267                        status="waiting"
268
269                simulation=types.Simulation(id=s.ID,name=s.Name,status=status) 
270
271
272
273        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
274                pass
275        else:
276                raise Exception("ERR014 - incorrect mode")
277
278        return simulation
279
280def delete_simulation(name):
281        if mode==CSTE_MODE_LOCAL_REPO:
282                repo.delete_simulation(name)
283        elif mode==CSTE_MODE_REMOTE_REPO:
284
285                # prepare args
286                # ..
287
288                # execute
289                repo.delete_simulation(name)
290
291                # process return values
292                # ..
293
294        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
295                pass
296        else:
297                raise Exception("ERR015 - incorrect mode")
298
299def create_simulation(simulation):
300        if mode==CSTE_MODE_LOCAL_REPO:
301                repo.create_simulation(simulation)
302        elif mode==CSTE_MODE_REMOTE_REPO:
303
304                # prepare args
305                model_engine=None
306                space=None
307                exp=None
308
309                if "BIGBRO" in simulation.name:
310
311                        model_engine="IPSL-CM5A-LR"
312                        space="TEST"
313                        exp="sstClim"
314
315                elif "v5cf.amipMR" in simulation.name:
316
317                        model_engine="IPSL-CM5A-MR"
318                        space="PROD"
319                        exp="amip"
320
321                else:
322
323                        model_engine=_SIM_MODEL_ENGINE
324                        space=_SIM_SPACE
325                        exp=_SIM_EXPERIMENT
326
327
328                # execute
329                repo.create_simulation(_SIM_ACTIVITY,
330                                                                _SIM_COMPUTE_NODE,
331                                                                _SIM_COMPUTE_NODE_LOGIN,
332                                                                _SIM_COMPUTE_NODE_MACHINE,
333                                                                _SIM_EXECUTION_START_DATE,
334                                                                _SIM_EXECUTION_STATE,
335                                                                exp,
336                                                                model_engine,
337                                                                simulation.name,
338                                                                space,
339                                                                parent_simulation_name=None)
340
341
342                # process return values
343                # ..
344
345        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
346                pass
347        else:
348                raise Exception("ERR016 - incorrect mode")
349
350def update_simulation_status(simulation):
351        if mode==CSTE_MODE_LOCAL_REPO:
352
353                try:
354                        repo.update_simulation_status(simulation)
355                except:
356                        raise
357
358        elif mode==CSTE_MODE_REMOTE_REPO:
359
360                # prepare args
361                # ..
362
363
364                # HACK
365                prodiguer_status=simulation.status
366                if simulation.status == "waiting":
367                        prodiguer_status="queued"
368
369
370                # execute
371                repo.update_simulation_status(simulation.name, prodiguer_status.upper())
372
373                # process return values
374                # ..
375
376                commit()
377
378        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
379                pass
380        else:
381                raise Exception("ERR017 - incorrect mode")
382
383def retrieve_messages(simulation):
384        message=None
385
386        if mode==CSTE_MODE_LOCAL_REPO:
387                message=repo.retrieve_messages(simulation)
388        elif mode==CSTE_MODE_REMOTE_REPO:
389
390                # prepare args
391                # ..
392
393                # execute
394                repo.retrieve_messages(name)
395
396                # process return values
397                # ..
398
399        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
400                pass
401        else:
402                raise Exception("ERR018 - incorrect mode")
403
404        return message
405
406def delete_messages(simulation):
407        if mode==CSTE_MODE_LOCAL_REPO:
408                repo.delete_messages(name)
409        elif mode==CSTE_MODE_REMOTE_REPO:
410
411                # prepare args
412                # ..
413
414                # execute
415                repo.delete_messages(name)
416
417                # process return values
418                # ..
419
420        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
421                pass
422        else:
423                raise Exception("ERR019 - incorrect mode")
424
425def create_message(message,simulation):
426        if mode==CSTE_MODE_LOCAL_REPO:
427                repo.create_message(message,simulation)
428        elif mode==CSTE_MODE_REMOTE_REPO:
429
430                # prepare args
431                # ..
432
433                # debug
434                #print "%s %s %s"%(simulation.name,message.code,"message.body")
435
436                # execute
437                try:
438                        repo.create_message(simulation.name,message.code,"message.body")
439                except ValueError as i:
440                        print "bla (%s)"%str(i)
441
442                commit()
443
444
445                # process return values
446                # ..
447
448        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
449                pass
450        else:
451                raise Exception("ERR020 - incorrect mode")
452
453        commit()
454
455
456def retrieve_last_message(simulation):
457        message=None
458
459        if mode==CSTE_MODE_LOCAL_REPO:
460                message=repo.retrieve_last_message(simulation)
461        elif mode==CSTE_MODE_REMOTE_REPO:
462
463                # execute
464                message=repo.retrieve_last_message(simulation.name)
465
466                if message is None:
467                        raise Exception("ERR221 - null value")
468
469                # process return values
470                di={}
471                di["crea_date"]=message.CreateDate
472                message=types.Message(di)
473
474        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
475                pass
476        else:
477                raise Exception("ERR021 - incorrect mode")
478
479        return message
480
481
482# --- higher level methods --- #
483
484def get_running_simulations():
485        running_simulation=[]
486
487        for s in retrieve_simulations():
488
489                if s.status=="running":
490                        running_simulation.append(s)
491                       
492        return running_simulation
493
494
Note: See TracBrowser for help on using the repository browser.