def run(self, node_ip, node_conf, cluster_id=None): if not cluster_id: cluster_id = self.cluster_id try: yaml.safe_loads(node_conf) # If this throws, fail the action. except: self.logger.error('Configuration is not valid YAML.') raise url = self._get_full_url([cluster_id, 'nodeconf', node_ip]) return requests.post(url, data=node_conf).json()
def _generate_yaml(stream): if isinstance(stream, list): items = [] for item in stream: try: if isinstance(item, dict): items.append( yaml.dump(item, default_flow_style=False, explicit_start=True)) else: items.append( yaml.dump(yaml.load(item), default_flow_style=False, explicit_start=True)) except Exception: print("An error occured whilst writing the YAML object.") return yaml.dump([yaml.load(item) for item in items], default_flow_style=False, explicit_start=True) else: try: return yaml.safe_loads(stream) except Exception: return stream
def get_db_uri(self): env_data = self.get_env_state() env_name = self.get_env_name() # Prior to 1.17 or not bootstrapped if not env_data: version = self.get_version() if version > VERSION_1_15: output = subprocess.check_output([ "juju", "api-endpoints", "--format", "json", "-e", env_name ]) host, port = json.loads(output)[0].split(":", 1) env_data['bootstrap-host'] = host # Fallback to status parsing. else: output = subprocess.check_output( ["juju", "status", "-e", env_name]) host = yaml.safe_loads(output)['machines']['0']['dns-name'] env_data['bootstrap-host'] = host env_conf = yaml.load(self.get_env_conf())['environments'][env_name] env_data['admin-secret'] = env_conf['admin-secret'] # if 1.17/1.18 hack around juju (this changed without notice in 1.17.6 elif 'state-servers' in env_data: # db password only stored in old-password of agent.conf # http://pad.lv/1270434 marked won't fix/opinion.. whatever. output = subprocess.check_output([ "juju", "run", "-e", env_name, "--machine", "0", "sudo cat /var/lib/juju/agents/machine-0/agent.conf" ]) mdata = yaml.safe_load(output) env_data['admin-secret'] = mdata['oldpassword'] env_data['bootstrap-host'] = env_data['state-servers'][0].split( ":")[0] uri = "mongodb://%(bootstrap-host)s:37017/juju?w=1&ssl=true" % env_data logging.debug("Connecting to mongo @ %s" % uri) return uri, env_data['admin-secret']
def get_db_uri(self): env_data = self.get_env_state() env_name = self.get_env_name() # Prior to 1.17 or not bootstrapped if not env_data: version = self.get_version() if version > VERSION_1_15: output = subprocess.check_output( ["juju", "api-endpoints", "--format", "json", "-e", env_name]) host, port = json.loads(output)[0].split(":", 1) env_data['bootstrap-host'] = host # Fallback to status parsing. else: output = subprocess.check_output( ["juju", "status", "-e", env_name]) host = yaml.safe_loads(output)['machines']['0']['dns-name'] env_data['bootstrap-host'] = host env_conf = yaml.load(self.get_env_conf())['environments'][env_name] env_data['admin-secret'] = env_conf['admin-secret'] # if 1.17/1.18 hack around juju (this changed without notice in 1.17.6 elif 'state-servers' in env_data: # db password only stored in old-password of agent.conf # http://pad.lv/1270434 marked won't fix/opinion.. whatever. output = subprocess.check_output([ "juju", "run", "-e", env_name, "--machine", "0", "sudo cat /var/lib/juju/agents/machine-0/agent.conf"]) mdata = yaml.safe_load(output) env_data['admin-secret'] = mdata['oldpassword'] env_data['bootstrap-host'] = env_data['state-servers'][ 0].split(":")[0] uri = "mongodb://%(bootstrap-host)s:37017/juju?w=1&ssl=true" % env_data logging.debug("Connecting to mongo @ %s" % uri) return uri, env_data['admin-secret']
def _generate_yaml(stream): kwargs = { "default_flow_style": False, "explicit_start": True } if isinstance(stream, (list, set)): items = [] for item in stream: try: if isinstance(item, dict): items.append( yaml.safe_dump(item, **kwargs) ) else: items.append( yaml.safe_dump( yaml.load(item, Loader=CfnYamlLoader), **kwargs ) ) except Exception: print("An error occured whilst writing the YAML object.") return yaml.safe_dump( [yaml.load(item, Loader=CfnYamlLoader) for item in items], **kwargs ) elif isinstance(stream, dict): return yaml.dump(stream, **kwargs) else: try: return yaml.safe_loads(stream) except Exception: return stream
def post_config(): """Validate and save a new configuration when a YAML file is uploaded.""" if 'yaml' in request.files: new_config_file = request.files['yaml'] new_config_obj = yaml.safe_loads(new_config_file) try: validated_config = raw_config(new_config_obj) except ValidationError as e: return jsonify({ 'status': 'failed', 'validation_errors': e.errors() }) validated_path = os.path.join('/data', clean_config_file()) new_config_file.save(validated_path) return jsonify({ 'status': 'saved', 'config': validated_config.dict(), 'path': validated_path }) else: return jsonify({'status': 'no file'})
# config.py # OpenENDEC Configuration loader # Author: Nate Sales (@nwsnate) import yaml import constants with open("config.yml") as config_file: data = yaml.safe_loads(config_file, Loader=yaml.FullLoader) # Begin ENDEC configuration try: model = data["endec"]["model"] except KeyError: print("ERROR: ENDEC model not found.") exit() else: if model not in constants.ENDEC_MODELS: print("ERROR: ENDEC model not supported.") try: port = data["endec"]["port"] except KeyError: print("ERROR: ENDEC port not found.") exit() try: baud = int(data["endec"]["baud"]) except KeyError: baud = 9600 # Default to 9600 baud