source: trunk/Monitoring/smon/repo_io.py @ 1434

Last change on this file since 1434 was 965, checked in by jripsl, 11 years ago
  • fix database connectivity test script.
File size: 9.2 KB
RevLine 
[859]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
[879]19import datetime
[859]20
21
22
[865]23
24
[871]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
[879]31
[965]32g__remote_database_host="ib-pp-db-dev.ipslnet"
33g__remote_database_port="5432"
34g__remote_database_name="pdgr_1_0b5"
[879]35
36
37
38
[871]39# set mode
[965]40mode=CSTE_MODE_REMOTE_REPO
41#mode=CSTE_MODE_LOCAL_REPO
[871]42
[937]43# mode specific init. (e.g. repository driver)
[871]44if mode==CSTE_MODE_REMOTE_REPO_STUB:
[879]45        raise Exception()
46
[871]47elif mode==CSTE_MODE_REMOTE_REPO:
[879]48
[965]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
[879]54        # import Prodiguer database I/O library
55        import prodiguer_shared.mq.hooks as repo
[937]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
[871]74elif mode==CSTE_MODE_LOCAL_REPO:
[937]75
[871]76        import local_repo as repo
[937]77
78
[871]79else:
80        raise Exception("ERR001 - incorrect mode")
81
82
83
84
85
86
[937]87
[865]88# -- methods -- #
[859]89
90def init():
[865]91        if mode==CSTE_MODE_LOCAL_REPO:
[866]92                repo.connect()
[865]93        elif mode==CSTE_MODE_REMOTE_REPO:
[965]94                _CONNECTION = "postgresql://postgres:Silence107!@%s:%s/%s"%(g__remote_database_host,g__remote_database_port,g__remote_database_name)
[879]95                repo_session.start(_CONNECTION)
96
[865]97        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
[859]98                pass
99        else:
100                raise Exception("ERR004 - incorrect mode")
101
[865]102def free():
103        if mode==CSTE_MODE_LOCAL_REPO:
[871]104                repo.free()
[865]105        elif mode==CSTE_MODE_REMOTE_REPO:
[879]106                repo_session.end()
[865]107        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
108                pass
109        else:
110                raise Exception("ERR009 - incorrect mode")
111
[871]112def populate_tables_with_sample():
[877]113        """
114        used only by "repo-state" pgm
115        """
116       
[871]117        repo.populate_tables_with_sample()
118
119def retrieve_simulations():
[877]120        """
121        used by get_running_simulations
122        """
[879]123        simulations=None
[871]124
[879]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
[859]171def test():
[875]172        """
173        not used
174        """
175
[865]176        repo.create_message("test2", 2, "bla2")
[859]177        commit()
178
[865]179        repo.update_simulation_status('1pctCO22', 'ERROR')
[859]180        commit()
181
[865]182        repo.create_message("test3", 3, "bla3")
[859]183        rollback()
184
[875]185def cleanup():
186        if mode==CSTE_MODE_LOCAL_REPO:
187                repo.cleanup()
188        elif mode==CSTE_MODE_REMOTE_REPO:
[879]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
[875]206        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
207                pass
208        else:
209                raise Exception("ERR007 - incorrect mode")
210
[859]211def commit():
[865]212        if mode==CSTE_MODE_LOCAL_REPO:
[866]213                repo.commit()
[865]214        elif mode==CSTE_MODE_REMOTE_REPO:
[879]215                repo_session.commit()
[865]216        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
[859]217                pass
218        else:
219                raise Exception("ERR002 - incorrect mode")
220
221def rollback():
[865]222        if mode==CSTE_MODE_LOCAL_REPO:
[866]223                repo.rollback()
[865]224        elif mode==CSTE_MODE_REMOTE_REPO:
[879]225                repo_session.commit()
[865]226        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
[859]227                pass
228        else:
229                raise Exception("ERR003 - incorrect mode")
230
[879]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
[859]237def retrieve_simulation(name):
[865]238        simulation=None
[859]239
[865]240        if mode==CSTE_MODE_LOCAL_REPO:
[879]241
242                try:
243                        simulation=repo.retrieve_simulation(name)
244                except:
[965]245                        raise
[879]246
[865]247        elif mode==CSTE_MODE_REMOTE_REPO:
248
249                # prepare args
250                # ..
251
252                # execute
253                s=repo.retrieve_simulation(name)
254
[879]255                if s is None:
[935]256                        #raise Exception("RG543534")
257                        return None
[879]258
259
260
[865]261                # process return values
262
[879]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
[865]273        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
274                pass
275        else:
276                raise Exception("ERR014 - incorrect mode")
277
[866]278        return simulation
[865]279
[859]280def delete_simulation(name):
[865]281        if mode==CSTE_MODE_LOCAL_REPO:
282                repo.delete_simulation(name)
283        elif mode==CSTE_MODE_REMOTE_REPO:
[859]284
[865]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
[859]299def create_simulation(simulation):
[865]300        if mode==CSTE_MODE_LOCAL_REPO:
301                repo.create_simulation(simulation)
302        elif mode==CSTE_MODE_REMOTE_REPO:
[859]303
[865]304                # prepare args
[879]305                model_engine=None
306                space=None
307                exp=None
[859]308
[879]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
[865]328                # execute
[879]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)
[859]340
[879]341
[865]342                # process return values
343                # ..
[859]344
[865]345        elif mode==CSTE_MODE_REMOTE_REPO_STUB:
346                pass
347        else:
348                raise Exception("ERR016 - incorrect mode")
[859]349
[865]350def update_simulation_status(simulation):
351        if mode==CSTE_MODE_LOCAL_REPO:
[879]352
353                try:
354                        repo.update_simulation_status(simulation)
355                except:
[965]356                        raise
[879]357
[865]358        elif mode==CSTE_MODE_REMOTE_REPO:
[859]359
[865]360                # prepare args
361                # ..
[859]362
[879]363
364                # HACK
365                prodiguer_status=simulation.status
366                if simulation.status == "waiting":
367                        prodiguer_status="queued"
368
369
[865]370                # execute
[879]371                repo.update_simulation_status(simulation.name, prodiguer_status.upper())
[865]372
373                # process return values
374                # ..
375
[879]376                commit()
377
[865]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
[877]425def create_message(message,simulation):
[865]426        if mode==CSTE_MODE_LOCAL_REPO:
[877]427                repo.create_message(message,simulation)
[865]428        elif mode==CSTE_MODE_REMOTE_REPO:
429
430                # prepare args
431                # ..
432
[935]433                # debug
434                #print "%s %s %s"%(simulation.name,message.code,"message.body")
[879]435
[865]436                # execute
[879]437                try:
438                        repo.create_message(simulation.name,message.code,"message.body")
439                except ValueError as i:
440                        print "bla (%s)"%str(i)
[865]441
[879]442                commit()
443
444
[865]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
[879]453        commit()
454
455
[865]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
[879]464                message=repo.retrieve_last_message(simulation.name)
[865]465
[879]466                if message is None:
467                        raise Exception("ERR221 - null value")
468
[865]469                # process return values
[879]470                di={}
471                di["crea_date"]=message.CreateDate
472                message=types.Message(di)
[865]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
[859]482# --- higher level methods --- #
483
484def get_running_simulations():
485        running_simulation=[]
486
[876]487        for s in retrieve_simulations():
[879]488
[859]489                if s.status=="running":
490                        running_simulation.append(s)
491                       
492        return running_simulation
[866]493
494
Note: See TracBrowser for help on using the repository browser.