def _log_invocation(self): ''' log that ansible ran the module ''' # TODO: generalize a separate log function and make log_invocation use it # Sanitize possible password argument when logging. log_args = dict() passwd_keys = ['password', 'login_password'] filter_re = [ # filter out things like user:pass@foo/whatever # and http://username:pass@wherever/foo re.compile('^(?P<before>.*:)(?P<password>.*)(?P<after>\@.*)$'), ] for param in self.params: canon = self.aliases.get(param, param) arg_opts = self.argument_spec.get(canon, {}) no_log = arg_opts.get('no_log', False) if no_log: log_args[param] = 'NOT_LOGGING_PARAMETER' elif param in passwd_keys: log_args[param] = 'NOT_LOGGING_PASSWORD' else: found = False for filter in filter_re: if isinstance(self.params[param], unicode): m = filter.match(self.params[param]) else: m = filter.match(str(self.params[param])) if m: d = m.groupdict() log_args[param] = d['before'] + "********" + d['after'] found = True break if not found: log_args[param] = self.params[param] module = 'ansible-%s' % os.path.basename(__file__) msg = '' for arg in log_args: if isinstance(log_args[arg], unicode): msg = msg + arg + '=' + log_args[arg] + ' ' else: msg = msg + arg + '=' + str(log_args[arg]) + ' ' if msg: msg = 'Invoked with %s' % msg else: msg = 'Invoked' if (has_journal): journal_args = ["MESSAGE=%s %s" % (module, msg)] journal_args.append("MODULE=%s" % os.path.basename(__file__)) for arg in log_args: journal_args.append(arg.upper() + "=" + str(log_args[arg])) try: journal.sendv(*journal_args) except IOError, e: # fall back to syslog since logging to journal failed syslog.openlog(str(module), 0, syslog.LOG_USER) syslog.syslog(syslog.LOG_NOTICE, unicode(msg).encode('utf8'))
def generate_veh_stats_frames(): global VEH_STATS_COUNT VEH_STATS_COUNT += 1 seq_num = 'SEQUENCE_NUMBER=' + str(int(VEH_STATS_COUNT)) timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) journal.sendv('CHANNEL=vehicleStats', 'CHARGELEFT=100', 'MCUSTATUS=up', 'SPEED=0.0', 'THROTTLE=0.0', 'REGEN=0.0', 'RANGE=100.00', seq_num, timestamp) print('Vehicle stats frame ... COUNT = {}'.format(VEH_STATS_COUNT)) if VEH_STATS_COUNT >= 300: return else: threading.Timer(1, generate_veh_stats_frames).start()
def generate_veh_state_frames(): global VEH_STATE_COUNT VEH_STATE_COUNT += 1 seq_num = 'SEQUENCE_NUMBER=' + str(int(VEH_STATE_COUNT)) timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) journal.sendv('CHANNEL=vehicleState', 'KEYIN=on', 'LEFTINDICATOR=off', 'RIGHTINDICATOR=off', 'MOTORMODE=economy', 'HEADLIGHTSTATE=off', 'STARTSWITCH=off', 'LEFTBRAKE=off', 'RIGHTBRAKE=off', 'HORN=off', seq_num, timestamp) print('Vehicle state frame ... COUNT = {}'.format(VEH_STATE_COUNT)) if VEH_STATE_COUNT >= 300: return else: threading.Timer(1, generate_veh_state_frames).start()
def generate_lsmsensor_frames(): global LSMSENSOR_COUNT for i in range(20): LSMSENSOR_COUNT += 1 seq_num = 'SEQUENCE_NUMBER=' + str(int(LSMSENSOR_COUNT)) timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) journal.sendv('CHANNEL=lsmsensor', 'ACC_X=0.87', 'ACC_Y=0.87', 'ACC_Z=9.80', 'MAG_X=1.0', 'MAG_Y=2.0', 'MAG_Z=-3.0', 'GYR_X=-1.23', 'GYR_Y=-1.23', 'GYR_Z=1.23', seq_num, timestamp) print('Lsm Sensor burst ... COUNT = {}'.format(LSMSENSOR_COUNT)) if LSMSENSOR_COUNT >= 10000: return else: threading.Timer(1, generate_lsmsensor_frames).start()
def generate_can_frames(): global CAN_COUNT for i in range(200): #timestamp = 'TIMESTAMP=' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) can_id = 'CAN_ID=' + str(410) CAN_COUNT += 1 seq_num = 'SEQUENCE_NUMBER=' + str(int(CAN_COUNT)) journal.sendv('CHANNEL=canFrame', can_id, 'DATA=100', seq_num, timestamp) print('Can burst ... COUNT = {}'.format(CAN_COUNT)) if CAN_COUNT >= 300000: return else: threading.Timer(1, generate_can_frames).start()
def generate_peri_state_frames(): global PERI_COUNT PERI_COUNT += 1 timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) seq_num = 'SEQUENCE_NUMBER=' + str(int(PERI_COUNT)) journal.sendv('CHANNEL=peripheralState', 'NETWORKSTATUS=Up', 'NETWORKSIGNAL=50', 'ISL29023LUXVALUE=10', 'ISL29023AVGLUXVALUE=10', 'ISL29023DEVICESTATUS=on', 'GPSDEVICESTATUS=on', 'SCREENBRIGHTNESS=1', 'SCREENBRIGHTNESSCONTROL=auto', 'HEADLIGHTCONTROL=auto', 'BLUTOOTHDEVICESTATUS=on', seq_num, timestamp) print('Peripheral frame ... COUNT = {}'.format(PERI_COUNT)) if PERI_COUNT >= 300: return else: threading.Timer(1, generate_peri_state_frames).start()
def generate_gps_frames(): global GPS_COUNT GPS_COUNT += 1 seq_num = 'SEQUENCE_NUMBER=' + str(int(GPS_COUNT)) timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) journal.sendv('CHANNEL=gpsData', 'CLASS=TPV', 'TAG=dummy', 'DEVICE=ttyMX3', 'MODE=3', 'TIME=0', 'EPT=0.005', 'LAT=12.92831016', 'LON=77.637049078', 'ALT=878.655', 'EPX=17.529', 'EPY=17.529', 'EPV=0.0', 'TRACK=99.6513', 'SPEED=0.525', 'CLIMB=0.294', 'EPD=0.0', 'EPS=0.96', 'EPC=0.0', seq_num, timestamp) print('Gps frame ... COUNT = {}'.format(GPS_COUNT)) if GPS_COUNT >= 1000: return else: threading.Timer(1, generate_gps_frames).start()
def _log_invocation(self): ''' log that ansible ran the module ''' # TODO: generalize a separate log function and make log_invocation use it # Sanitize possible password argument when logging. log_args = dict() passwd_keys = ['password', 'login_password'] for param in self.params: canon = self.aliases.get(param, param) arg_opts = self.argument_spec.get(canon, {}) no_log = arg_opts.get('no_log', False) if no_log: log_args[param] = 'NOT_LOGGING_PARAMETER' elif param in passwd_keys: log_args[param] = 'NOT_LOGGING_PASSWORD' else: log_args[param] = self.params[param] module = 'ansible-%s' % os.path.basename(__file__) msg = '' for arg in log_args: if isinstance(log_args[arg], unicode): msg = msg + arg + '=' + log_args[arg] + ' ' else: msg = msg + arg + '=' + str(log_args[arg]) + ' ' if msg: msg = 'Invoked with %s' % msg else: msg = 'Invoked' if (has_journal): journal_args = ["MESSAGE=%s %s" % (module, msg)] journal_args.append("MODULE=%s" % os.path.basename(__file__)) for arg in log_args: journal_args.append(arg.upper() + "=" + str(log_args[arg])) try: journal.sendv(*journal_args) except IOError, e: # fall back to syslog since logging to journal failed syslog.openlog(str(module), 0, syslog.LOG_USER) syslog.syslog(syslog.LOG_NOTICE, msg.encode('utf8'))
def generate_sensor_frames(): global SENSOR_COUNT for i in range(20): SENSOR_COUNT += 1 seq_num = 'SEQUENCE_NUMBER=' + str(int(SENSOR_COUNT)) timestamp = 'TIMESTAMP=' + str(int(time.time() * 1000)) journal.sendv( 'CHANNEL=sensor', 'D_QUATERNION_X=7657.0', 'D_QUATERNION_Y=7657.0', 'D_QUATERNION_Z=7657.0', 'D_QUATERNION_W=7657.0', 'D_ACCEL_X=0.87', 'D_ACCEL_Y=0.87', 'D_ACCEL_Z=9.80', 'D_MAG_X=0.0', 'D_MAG_Y=0.0', 'D_MAG_Z=0.0', 'D_GYRO_X=-1.23', 'D_GYRO_Y=-1.23', 'D_GYRO_Z=1.23', 'D_EULER_H=4.15', 'D_EULER_P=4.15', 'D_EULER_R=4.15', 'D_LINEAR_ACCEL_X=4.19', 'D_LINEAR_ACCEL_Y=4.19', 'D_LINEAR_ACCEL_Z=4.19', 'D_GRAVITY_X=-4.55', 'D_GRAVITY_Y=-4.55', 'D_GRAVITY_Z=-4.55', 'ACCEL_CALIB_STAT=1', 'GYRO_CALIB_STAT=0', 'MAG_CALIB_STAT=0', 'SYS_CALIB_STAT=0', seq_num, timestamp) print('Sensor burst ... COUNT = {}'.format(SENSOR_COUNT)) if SENSOR_COUNT >= 10000: return else: threading.Timer(1, generate_sensor_frames).start()
def _log_invocation(self): ''' log that ansible ran the module ''' # TODO: generalize a separate log function and make log_invocation use it # Sanitize possible password argument when logging. log_args = dict() passwd_keys = ['password', 'login_password'] for param in self.params: canon = self.aliases.get(param, param) arg_opts = self.argument_spec.get(canon, {}) no_log = arg_opts.get('no_log', False) if no_log: log_args[param] = 'NOT_LOGGING_PARAMETER' elif param in passwd_keys: log_args[param] = 'NOT_LOGGING_PASSWORD' else: log_args[param] = self.params[param] module = 'ansible-%s' % os.path.basename(__file__) msg = '' for arg in log_args: msg = msg + arg + '=' + str(log_args[arg]) + ' ' if msg: msg = 'Invoked with %s' % msg else: msg = 'Invoked' if (has_journal): journal_args = ["MESSAGE=%s %s" % (module, msg)] journal_args.append("MODULE=%s" % os.path.basename(__file__)) for arg in log_args: journal_args.append(arg.upper() + "=" + str(log_args[arg])) try: journal.sendv(*journal_args) except IOError, e: # fall back to syslog since logging to journal failed syslog.openlog(module, 0, syslog.LOG_USER) syslog.syslog(syslog.LOG_NOTICE, msg)
else: msg = 'Invoked' # 6655 - allow for accented characters try: msg = msg.encode('utf8') except UnicodeDecodeError, e: pass if (has_journal): journal_args = ["MESSAGE=%s %s" % (module, msg)] journal_args.append("MODULE=%s" % os.path.basename(__file__)) for arg in log_args: journal_args.append(arg.upper() + "=" + str(log_args[arg])) try: journal.sendv(*journal_args) except IOError, e: # fall back to syslog since logging to journal failed syslog.openlog(str(module), 0, syslog.LOG_USER) syslog.syslog(syslog.LOG_NOTICE, msg) #1 else: syslog.openlog(str(module), 0, syslog.LOG_USER) syslog.syslog(syslog.LOG_NOTICE, msg) #2 def _set_cwd(self): try: cwd = os.getcwd() if not os.access(cwd, os.F_OK|os.R_OK): raise return cwd except:
## sudo journalctl -f {--output=json} ## from systemd import journal # simple message send # journal.send("a systemd-journal log message from python land.") # send message w/ extra fields. # journal.send( "A systemd-journal log message from python land, with extra field", EXTRAFIELD='foo field') # sendv() is also supported # journal.sendv( 'MESSAGE=systemd-journal log message using sendv() from python', 'EXTRAFIELD=foo field' ) # a very syslog like message # journal.send( 'Principal log message (from python land)', SYSLOG_FACILITY='USER', SYSLOG_IDENTIFIER='py-write-journal', SYSLOG_PID=-1, PRIORITY='NOTICE' ) ## Local Variables: ## mode: python ## End:
def build_pr(database_url: Optional[str], pr: int, ofborg_url: Optional[str] = None) -> None: log.info("Starting build", pr=pr) for dirname in glob( os.path.expanduser(f"~/.cache/nixpkgs-review/pr-{pr}*")): shutil.rmtree(dirname) if ofborg_url is not None: url = urllib.parse.urlparse(ofborg_url) raw_gist_url = ( f"https://gist.githubusercontent.com/GrahamcOfBorg{url.path}/raw/") os.environ["NIXPKGS_REVIEW_OFBORG_GIST_URL"] = raw_gist_url # This is important # We need to send a message to the journald logs with the right format # so that post-build-postgres knows what PR we're working on # https://github.com/rmcgibbo/post-build-postgres/blob/master/src/journal.rs#L18 sendv( f"MESSAGE={json.dumps({'pr': pr})}", "PRIORITY=6", "SYSLOG_IDENTIFIER=nixpkgs-review-start", ) cmd = [ # Call the unwrapped version so that we can override its PATH and hijack # nix-shell to point to our own shell script. The reason for this is because # actually running nix-shell can force a bunch more stuff to be built, because # it runs nix-shell with the nixpkgs from the PR, and so if the PR rebuilt bash # then you're going to need to build bash _EVEN IF_ it was skipped. ".nixpkgs-review-wrapped", "pr", str(pr), "--post-logs", # TODO: pass --system here "--build-args", f"--timeout {int(BUILD_TIMEOUT.total_seconds())} --max-silent-time {int(SILENT_TIMEOUT.total_seconds())}", "--run", os.environ["NIXPKGS_REVIEW_POST_BUILD_HOOK"], ] if "GITHUB_TOKEN" not in os.environ: log.error("No GITHUB_TOKEN. Proceeding without --post-logs") cmd.remove("--post-logs") start_time = time.time() sh( cmd, timeout=NIXPKGS_REVIEW_TIMEOUT.total_seconds(), env=env_with( NIXPKGS_REVIEW_START_TIME=f"{start_time}", NIXPKGS_REVIEW_PR=f"{pr}", PATH=f"{os.path.join(os.path.dirname(__file__), 'bin')}:$PATH", ), ) nixpkgs_dir = os.path.expanduser( f"~/.cache/nixpkgs-review/pr-{pr}/nixpkgs") if os.path.exists(nixpkgs_dir): shutil.rmtree(nixpkgs_dir) upload_s3(pr=pr, start_time=start_time) upload_postgres(pr=pr, start_time=start_time, database_url=database_url)