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

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