def update_stored_mconfig(self, updated_value: str) -> GatewayConfigs: parsed = json.loads(updated_value) serialization_utils.write_to_file_atomically( self.MCONFIG_PATH, json.dumps(parsed, indent=4, sort_keys=True), ) magma_configuration_events.updated_stored_mconfig()
def _write_gps_coords_to_file(gps_lat, gps_lon): lines = '{lat}\n{lon}'.format(lat=gps_lat, lon=gps_lon) try: serialization_utils.write_to_file_atomically( CACHED_GPS_COORD_FILE_PATH, lines, ) except OSError: pass
def write_cert(cert_der, cert_file): """Write DER encoded cert to file in PEM format Args: cert_der: certificate encoded in DER format cert_file: path to certificate """ cert = x509.load_der_x509_certificate(cert_der, default_backend()) cert_pem = cert.public_bytes(serialization.Encoding.PEM) write_to_file_atomically(cert_file, cert_pem.decode("utf-8"))
def write_key(key, key_file): """Write key object to file in PEM format atomically Args: key: RSAPrivateKey or EllipticCurvePrivateKey object key_file: path to the key file """ key_pem = key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.TraditionalOpenSSL, serialization.NoEncryption()) write_to_file_atomically(key_file, key_pem.decode("utf-8"))
def process_update(self, stream_name, updates, resync): if not updates: return # For now, we only care about the last (newest) update for update in updates[:-1]: logging.info('Ignoring update %s', update.key) logging.info('Serializing stream update %s', updates[-1].key) filename = get_stream_serialize_filename(stream_name) serialization_utils.write_to_file_atomically( filename, updates[-1].value.decode(), )
def generate_template_config(service, template, out_dirname, context): """ Generate the config from the jinja template. Args: service (str): Name of the magma service. Used for looking up the config and mconfig template (str): Name of the input template, which is also used for choosing the output filename out_dirname (str): Path of the output file context (map): Context to use for Jinja (the .yml config and mconfig will be added into this context) """ # Get the template and the output filenames template_filename = _get_template_filename(template) out_filename = _get_template_out_filename(template, out_dirname) logging.info( "Generating config file: [%s] using template: [%s]" % ( out_filename, template_filename, ), ) template_context = {} # Generate the content to use from the service yml config and mconfig. try: template_context.update(load_service_config(service)) except LoadConfigError as err: logging.warning(err) template_context.update(context) try: mconfig = load_service_mconfig_as_json(service) template_context.update(mconfig) except LoadConfigError as err: logging.warning(err) # Export snowflake to template. # TODO: export a hardware-derived ID that can be used by a field tech # to easily identify a specific device. template_context.setdefault("snowflake", make_snowflake()) # Create the config file based on the template template_str = open(template_filename, 'r').read() output = Template(template_str).render(template_context) os.makedirs(out_dirname, exist_ok=True) write_to_file_atomically(out_filename, output)
def update_stored_mconfig(self, updated_value: str) -> GatewayConfigs: serialization_utils.write_to_file_atomically( self.MCONFIG_PATH, updated_value, )