Skip to content

Commit a11e7e2

Browse files
Merge pull request #117 from HPC-SimTools/new_portal
Get new portal working
2 parents 3f51372 + ad27910 commit a11e7e2

File tree

4 files changed

+83
-48
lines changed

4 files changed

+83
-48
lines changed

ipsframework/configurationManager.py

+13-21
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,11 @@ def initialize(self, data_mgr, resource_mgr, task_mgr):
217217
else:
218218
use_accurate_nodes = True
219219

220-
user_def_tprocs = int(self.platform_conf.get('TOTAL_PROCS', 0))
221-
user_def_nodes = int(self.platform_conf.get('NODES', 0))
222-
user_def_ppn = int(self.platform_conf.get('PROCS_PER_NODE', 0))
223-
user_def_cpn = int(self.platform_conf.get('CORES_PER_NODE', 0))
224-
user_def_spn = int(self.platform_conf.get('SOCKETS_PER_NODE', 0))
225-
226-
self.platform_conf['TOTAL_PROCS'] = user_def_tprocs
227-
self.platform_conf['NODES'] = user_def_nodes
228-
self.platform_conf['PROCS_PER_NODE'] = user_def_ppn
229-
self.platform_conf['CORES_PER_NODE'] = user_def_cpn
230-
self.platform_conf['SOCKETS_PER_NODE'] = user_def_spn
220+
self.platform_conf['TOTAL_PROCS'] = int(self.platform_conf.get('TOTAL_PROCS', 0))
221+
self.platform_conf['NODES'] = int(self.platform_conf.get('NODES', 0))
222+
self.platform_conf['PROCS_PER_NODE'] = int(self.platform_conf.get('PROCS_PER_NODE', 0))
223+
self.platform_conf['CORES_PER_NODE'] = int(self.platform_conf.get('CORES_PER_NODE', 0))
224+
self.platform_conf['SOCKETS_PER_NODE'] = int(self.platform_conf.get('SOCKETS_PER_NODE', 0))
231225
self.platform_conf['USE_ACCURATE_NODES'] = use_accurate_nodes
232226
self.platform_conf['MPIRUN_VERSION'] = mpirun_version
233227

@@ -253,6 +247,10 @@ def initialize(self, data_mgr, resource_mgr, task_mgr):
253247
if key not in conf_keys:
254248
conf[key] = self.platform_conf[key]
255249

250+
# Override platform value for PORTAL_URL if in simulation
251+
if 'PORTAL_URL' in conf_keys:
252+
self.platform_conf['PORTAL_URL'] = conf['PORTAL_URL']
253+
256254
except (IOError, SyntaxError):
257255
self.fwk.exception('Error opening config file %s: ', conf_file)
258256
raise
@@ -382,20 +380,14 @@ def _initialize_fwk_components(self):
382380
portal_conf['USER'] = self.sim_map[self.fwk_sim_name].sim_conf['USER']
383381
except KeyError:
384382
portal_conf['USER'] = self.platform_conf['USER']
385-
havePortal = True
386383
if self.fwk.log_level == logging.DEBUG:
387384
portal_conf['LOG_LEVEL'] = 'DEBUG'
388385

389-
try:
390-
portal_conf['PORTAL_URL'] = self.get_platform_parameter('PORTAL_URL', silent=True)
391-
portal_conf['RUNID_URL'] = self.get_platform_parameter('RUNID_URL', silent=True)
392-
except KeyError:
393-
havePortal = False
386+
portal_conf['PORTAL_URL'] = self.get_platform_parameter('PORTAL_URL', silent=True)
394387

395-
if havePortal:
396-
component_id = self._create_component(portal_conf,
397-
self.sim_map[self.fwk_sim_name])
398-
self.fwk_components.append(component_id)
388+
component_id = self._create_component(portal_conf,
389+
self.sim_map[self.fwk_sim_name])
390+
self.fwk_components.append(component_id)
399391

400392
def _initialize_sim(self, sim_data):
401393
"""

ipsframework/portalBridge.py

+11-26
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
import datetime
77
import sys
8-
import urllib.request
9-
import urllib.parse
10-
import urllib.error
118
import os
129
from subprocess import Popen, PIPE
1310
import time
@@ -17,6 +14,7 @@
1714
import glob
1815
import itertools
1916
import json
17+
import shutil
2018
from ipsframework import ipsutil, Component
2119
from ipsframework.convert_log_function import convert_logdata_to_html
2220

@@ -88,7 +86,6 @@ def __init__(self, services, config):
8886
self.startTime = self.curTime
8987
self.services = services
9088
self.sim_map = {}
91-
self.runid_url = None
9289
self.portal_url = None
9390
self.done = False
9491
self.first_event = True
@@ -109,14 +106,10 @@ def init(self, timestamp=0.0, **keywords):
109106
Try to connect to the portal, subscribe to *_IPS_MONITOR* events and
110107
register callback :py:meth:`.process_event`.
111108
"""
112-
# try:
113-
# self.portal_url = self.PORTAL_URL
114-
# except AttributeError:
115-
# pass
116-
# try:
117-
# self.runid_url = self.RUNID_URL
118-
# except AttributeError:
119-
# pass
109+
try:
110+
self.portal_url = self.PORTAL_URL
111+
except AttributeError:
112+
pass
120113
self.host = self.services.get_config_param('HOST')
121114
self.services.subscribe('_IPS_MONITOR', "process_event")
122115
try:
@@ -268,16 +261,17 @@ def send_event(self, sim_data, event_data):
268261
self.services.exception("Error writing html file into USER_W3_DIR directory")
269262
self.write_to_htmldir = False
270263
if self.portal_url:
271-
webmsg = urllib.parse.urlencode(event_data).encode("utf-8")
264+
webmsg = json.dumps(event_data)
272265
try:
273266
if self.first_event: # First time, launch sendPost.py daemon
274-
cmd = os.path.join(sys.path[0], 'sendPost.py')
275-
self.childProcess = Popen(cmd, shell=True, bufsize=128,
267+
cmd = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sendPost.py')
268+
python_exec = shutil.which('python')
269+
self.childProcess = Popen([python_exec, cmd], bufsize=128,
276270
stdin=PIPE, stdout=PIPE,
277271
stderr=PIPE, close_fds=True)
278272
self.first_event = False
279-
self.childProcess.stdin.write('%s %s\n' %
280-
(self.portal_url, webmsg))
273+
self.childProcess.stdin.write(('%s %s\n' %
274+
(self.portal_url, webmsg)).encode())
281275
self.childProcess.stdin.flush()
282276
except Exception as e:
283277
self.services.exception('Error transmitting event number %6d to %s : %s',
@@ -481,15 +475,6 @@ def init_simulation(self, sim_name, sim_root):
481475
d = datetime.datetime.now()
482476
date_str = "%s.%03d" % (d.strftime("%Y-%m-%dT%H:%M:%S"), int(d.microsecond / 1000))
483477
sim_data.portal_runid = "_".join([self.host, "USER", date_str])
484-
if self.runid_url is not None:
485-
self.services.debug('PORTAL_RUNID_URL = %s', str(self.runid_url))
486-
try:
487-
f = urllib.request.urlopen(self.runid_url, None, 10)
488-
sim_data.portal_runid = f.read().strip()
489-
except (urllib.error.URLError) as e:
490-
self.services.error('Error obtaining runID from service at %s : %s' %
491-
(self.runid_url, str(e)))
492-
self.services.error('Using a datetime instead')
493478
try:
494479
self.services.set_config_param('PORTAL_RUNID', sim_data.portal_runid,
495480
target_sim_name=sim_name)

ipsframework/sendPost.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
# -------------------------------------------------------------------------------
3+
# Copyright 2006-2020 UT-Battelle, LLC. See LICENSE for more information.
4+
# -------------------------------------------------------------------------------
5+
6+
import sys
7+
from urllib import request, error
8+
import socket
9+
import time
10+
import traceback
11+
12+
headers = {'Content-Type': 'application/json'}
13+
14+
15+
def sendEncodedMessage(url, msg):
16+
if not isinstance(msg, bytes):
17+
msg = msg.encode('utf-8')
18+
19+
num_trials = 2
20+
trial = 0
21+
delay = [0.4, 0.8, 1.2]
22+
23+
while trial < num_trials:
24+
try:
25+
req = request.Request(url, data=msg, headers=headers, method='POST')
26+
resp = request.urlopen(req)
27+
except error.URLError:
28+
trial += 1
29+
if trial > num_trials:
30+
open('PORTAL.err', 'a').write('%s\n' % (msg))
31+
else:
32+
time.sleep(delay[trial-1])
33+
else:
34+
break
35+
try:
36+
resp.close()
37+
except Exception:
38+
pass
39+
40+
41+
if __name__ == "__main__":
42+
""" Loop over input from stdin, expecting lines of the format:
43+
URL ENCODED_WEB_MSG
44+
"""
45+
timeout = 3
46+
socket.setdefaulttimeout(timeout)
47+
error_f = open("sendpost.err", 'w')
48+
line = ' '
49+
while True:
50+
try:
51+
line = sys.stdin.readline().rstrip('\n')
52+
if line == '':
53+
break
54+
tokens = line.split(' ', 1)
55+
sendEncodedMessage(tokens[0], tokens[1])
56+
except Exception:
57+
traceback.print_exc(file=error_f)
58+
sys.exit(0)

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
max-line-length = 160
3-
max-complexity = 38
3+
max-complexity = 39
44
exclude =
55
ipsframework/configobj.py,
66
ipsframework/six.py,

0 commit comments

Comments
 (0)