本文整理汇总了Python中hcl.load方法的典型用法代码示例。如果您正苦于以下问题:Python hcl.load方法的具体用法?Python hcl.load怎么用?Python hcl.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hcl
的用法示例。
在下文中一共展示了hcl.load方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def __init__(self) -> None:
"""Parse the terraform.tfvars config file and make sure it contains every variable.
Raises:
InvalidConfigError: If any variable is defined in variables.tf but not terraform.tfvars.
"""
with open(CONFIG_FILE) as f:
self._config = hcl.load(f) # Dict[str, Union[int, str]]
with open(VARIABLES_FILE) as f:
variable_names = hcl.load(f)['variable'].keys()
for variable in variable_names:
# Verify that the variable is defined.
if variable not in self._config:
raise InvalidConfigError(
'variable "{}" is not defined in {}'.format(variable, CONFIG_FILE)
)
示例2: get_url_content
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def get_url_content(url):
"""
Return URL content
Args:
url (str): URL address to return
Returns:
str: Content of URL
Raises:
AssertionError: When couldn't load URL
"""
log.debug(f"Download '{url}' content.")
r = requests.get(url)
assert r.ok, f"Couldn't load URL: {url} content! Status: {r.status_code}."
return r.content
示例3: remove_keys_from_tf_variable_file
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def remove_keys_from_tf_variable_file(tf_file, keys):
"""
Removes the keys from the tf files and convert to json format
Args:
tf_file (str): path to tf file
keys (list): list of keys to remove
"""
# importing here to avoid dependencies
from ocs_ci.utility.templating import dump_data_to_json
with open(tf_file, 'r') as fd:
obj = hcl.load(fd)
for key in keys:
obj['variable'].pop(key)
dump_data_to_json(obj, f"{tf_file}.json")
os.rename(tf_file, f"{tf_file}.backup")
示例4: get_module_ip
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def get_module_ip(terraform_state_file, module):
"""
Gets the bootstrap node IP from terraform.tfstate file
Args:
terraform_state_file (str): Path to terraform state file
module (str): Module name in terraform.tfstate file
e.g: constants.LOAD_BALANCER_MODULE
Returns:
str: IP of bootstrap node
"""
with open(terraform_state_file) as fd:
obj = hcl.load(fd)
resources = obj['resources']
log.debug(f"Extracting module information for {module}")
log.debug(f"Resource in {terraform_state_file}: {resources}")
for resource in resources:
if resource['module'] == module:
resource_body = resource['instances'][0]['attributes']['body']
return resource_body.split("\"")[3]
示例5: test_decoder
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def test_decoder(hcl_fname, json_fname, struct):
with open(join(FIXTURE_DIR, hcl_fname), 'r') as fp:
hcl_json = hcl.load(fp)
assert json_fname is not None or struct is not None
if json_fname is not None:
with open(join(FIXTURE_DIR, json_fname), 'r') as fp:
good_json = json.load(fp)
assert hcl_json == good_json
if struct is not None:
assert hcl_json == struct
示例6: test_decoder_export_comments
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def test_decoder_export_comments(hcl_fname, sline_fname, mline_fname, aline_fname, export_comments):
with open(join(FIXTURE_DIR, hcl_fname), 'r') as fp:
hcl_json = hcl.load(fp, export_comments)
json_fname = {
"LINE": sline_fname,
"MULTILINE": mline_fname,
"ALL": aline_fname
}
with open(join(FIXTURE_DIR, json_fname[export_comments]), 'r') as fp:
good_json = json.load(fp)
assert hcl_json == good_json
示例7: _load
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def _load(path):
try:
with open(path) as settings_file:
return hcl.load(settings_file)
except IOError as error:
errors.string_exit('Error reading settings: {}'.format(error))
except Exception as error:
errors.string_exit('Error parsing settings: {}'.format(error))
示例8: load
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def load(**defaults):
if defaults.get('verbose'):
print('[ssha] finding settings file')
update(defaults)
settings_path = defaults.get('settings_path')
path = _find_settings_path(settings_path)
if defaults.get('verbose'):
print('[ssha] loading {}'.format(path))
data = _load(path)
_validate_version(data)
update(data)
示例9: find_min_required
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def find_min_required(path):
"""Inspect terraform files and find minimum version."""
found_min_required = ''
for filename in glob.glob(os.path.join(path, '*.tf')):
with open(filename, 'r') as stream:
tf_config = hcl.load(stream)
if tf_config.get('terraform', {}).get('required_version'):
found_min_required = tf_config.get('terraform',
{}).get('required_version')
break
if found_min_required:
if re.match(r'^!=.+', found_min_required):
LOGGER.error('Min required Terraform version is a negation (%s) '
'- unable to determine required version',
found_min_required)
sys.exit(1)
else:
found_min_required = re.search(r'[0-9]*\.[0-9]*(?:\.[0-9]*)?',
found_min_required).group(0)
LOGGER.debug("Detected minimum terraform version is %s",
found_min_required)
return found_min_required
LOGGER.error('Terraform version specified as min-required, but unable to '
'find a specified version requirement in this module\'s tf '
'files')
sys.exit(1)
示例10: change_mem_and_cpu
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def change_mem_and_cpu():
"""
Increase CPUs and memory for nodes
"""
worker_num_cpus = config.ENV_DATA.get('worker_num_cpus')
master_num_cpus = config.ENV_DATA.get('master_num_cpus')
worker_memory = config.ENV_DATA.get('compute_memory')
master_memory = config.ENV_DATA.get('master_memory')
if (
worker_num_cpus
or master_num_cpus
or master_memory
or worker_memory
):
with open(constants.VSPHERE_MAIN, 'r') as fd:
obj = hcl.load(fd)
if worker_num_cpus:
obj['module']['compute']['num_cpu'] = worker_num_cpus
if master_num_cpus:
obj['module']['control_plane']['num_cpu'] = master_num_cpus
if worker_memory:
obj['module']['compute']['memory'] = worker_memory
if master_memory:
obj['module']['control_plane']['memory'] = master_memory
# Dump data to json file since hcl module
# doesn't support dumping of data in HCL format
dump_data_to_json(obj, f"{constants.VSPHERE_MAIN}.json")
os.rename(constants.VSPHERE_MAIN, f"{constants.VSPHERE_MAIN}.backup")
示例11: get_infra_id
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def get_infra_id(cluster_path):
"""
Get infraID from metadata.json in given cluster_path
Args:
cluster_path: path to cluster install directory
Returns:
str: metadata.json['infraID']
"""
metadata_file = os.path.join(cluster_path, "metadata.json")
with open(metadata_file) as f:
metadata = json.load(f)
return metadata["infraID"]
示例12: generate_terraform_vars_with_out_folder
# 需要导入模块: import hcl [as 别名]
# 或者: from hcl import load [as 别名]
def generate_terraform_vars_with_out_folder():
"""
Generates the normal ( old structure ) terraform.tfvars file
"""
logger.info("Generating terraform variables without folder structure")
# upload bootstrap ignition to public access server
bootstrap_path = os.path.join(
config.ENV_DATA.get('cluster_path'),
constants.BOOTSTRAP_IGN
)
remote_path = os.path.join(
config.ENV_DATA.get('path_to_upload'),
f"{config.RUN.get('run_id')}_{constants.BOOTSTRAP_IGN}"
)
upload_file(
config.ENV_DATA.get('httpd_server'),
bootstrap_path,
remote_path,
config.ENV_DATA.get('httpd_server_user'),
config.ENV_DATA.get('httpd_server_password')
)
# generate bootstrap ignition url
path_to_bootstrap_on_remote = remote_path.replace("/var/www/html/", "")
bootstrap_ignition_url = (
f"http://{config.ENV_DATA.get('httpd_server')}/"
f"{path_to_bootstrap_on_remote}"
)
logger.info(f"bootstrap_ignition_url: {bootstrap_ignition_url}")
config.ENV_DATA['bootstrap_ignition_url'] = bootstrap_ignition_url
# load master and worker ignitions to variables
master_ignition_path = os.path.join(
config.ENV_DATA.get('cluster_path'),
constants.MASTER_IGN
)
master_ignition = read_file_as_str(f"{master_ignition_path}")
config.ENV_DATA['control_plane_ignition'] = master_ignition
worker_ignition_path = os.path.join(
config.ENV_DATA.get('cluster_path'),
constants.WORKER_IGN
)
worker_ignition = read_file_as_str(f"{worker_ignition_path}")
config.ENV_DATA['compute_ignition'] = worker_ignition
cluster_domain = (
f"{config.ENV_DATA.get('cluster_name')}."
f"{config.ENV_DATA.get('base_domain')}"
)
config.ENV_DATA['cluster_domain'] = cluster_domain
# generate terraform variables from template
create_terraform_var_file("terraform.tfvars.j2")