Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Commit

Permalink
rendered_files refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tsorya committed Jul 28, 2020
1 parent 729c0d1 commit 1d4e74a
Showing 1 changed file with 65 additions and 50 deletions.
115 changes: 65 additions & 50 deletions render_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import logging
import subprocess
from contextlib import contextmanager
import sys
import os
import shutil
Expand Down Expand Up @@ -38,21 +39,25 @@ def upload_to_aws(s3_client, local_file, bucket, s3_file):


def update_bmh_files(ignition_file, cluster_id, inventory_endpoint):
if inventory_endpoint:
hosts_list = utils.get_inventory_hosts(inventory_endpoint, cluster_id)
else:
hosts_list = test_utils.get_test_list_hosts(cluster_id)

with open(ignition_file, "r") as file_obj:
data = json.load(file_obj)
storage_files = data['storage']['files']
# since we don't remove file for now, we don't need to iterate through copy
for file_data in storage_files:
if bmh_utils.is_bmh_cr_file(file_data['path']):
bmh_utils.update_bmh_cr_file(file_data, hosts_list)

with open(ignition_file, "w") as file_obj:
json.dump(data, file_obj)
try:
if inventory_endpoint:
hosts_list = utils.get_inventory_hosts(inventory_endpoint, cluster_id)
else:
logging.info("Using test data to get hosts list")
hosts_list = test_utils.get_test_list_hosts(cluster_id)

with open(ignition_file, "r") as file_obj:
data = json.load(file_obj)
storage_files = data['storage']['files']
# since we don't remove file for now, we don't need to iterate through copy
for file_data in storage_files:
if bmh_utils.is_bmh_cr_file(file_data['path']):
bmh_utils.update_bmh_cr_file(file_data, hosts_list)

with open(ignition_file, "w") as file_obj:
json.dump(data, file_obj)
except Exception as ex:
raise Exception('Failed to update BMH CRs in bootstrap ignition, exception: {}'.format(ex))


def upload_to_s3(s3_endpoint_url, bucket, aws_access_key_id, aws_secret_access_key, install_dir, cluster_id):
Expand Down Expand Up @@ -81,11 +86,43 @@ def debug_print_upload_to_s3(install_dir):
print("Uploading file %s as object %s" % (file_path, s3_file_name))


def install_config_manipulation(before_installer, config_dir):
if before_installer:
shutil.copyfile(os.path.join(config_dir, INSTALL_CONFIG), os.path.join(config_dir,INSTALL_CONFIG_BACKUP))
else:
shutil.move(os.path.join(config_dir,INSTALL_CONFIG_BACKUP), os.path.join(config_dir, INSTALL_CONFIG))
@contextmanager
def backup_restore_install_config(config_dir):
logging.info("Saving %s cause it will be deleted by installer", INSTALL_CONFIG)
shutil.copyfile(os.path.join(config_dir, INSTALL_CONFIG), os.path.join(config_dir, INSTALL_CONFIG_BACKUP))
yield
logging.info("Restoring %s", INSTALL_CONFIG)
shutil.move(os.path.join(config_dir, INSTALL_CONFIG_BACKUP), os.path.join(config_dir, INSTALL_CONFIG))


def generate_installation_files(work_dir, config_dir):
# command = "OPENSHIFT_INSTALL_INVOKER=\"assisted-installer\" %s/openshift-baremetal-install create ignition-configs --dir %s" \
# % (work_dir, config_dir)
with backup_restore_install_config(config_dir=config_dir):
command = "OPENSHIFT_INSTALL_INVOKER=\"assisted-installer\" %s/openshift-install create " \
"ignition-configs --dir %s" % (work_dir, config_dir)
try:
logging.info("Generating installation files")
subprocess.check_output(command, shell=True, stderr=sys.stdout)
except Exception as ex:
raise Exception('Failed to generate files, exception: {}'.format(ex))


def prepare_install_config(config_dir, install_config):
install_config_path = os.path.join(config_dir, INSTALL_CONFIG)
if not install_config and not os.path.exists(install_config_path):
raise Exception("install config was not provided")

if not os.path.exists(install_config_path):
logging.info("writing install config to file")
with open(os.path.join(config_dir, INSTALL_CONFIG), 'w+') as file_obj:
file_obj.write(install_config)


def create_config_dir(work_dir):
config_dir = os.path.join(work_dir, "installer_dir")
subprocess.check_output(["mkdir", "-p", config_dir])
return config_dir


def main():
Expand All @@ -106,49 +143,27 @@ def main():
if not work_dir:
raise Exception("working directory was not defined")

config_dir = os.path.join(work_dir, "installer_dir")
if install_config:
subprocess.check_output(["mkdir", "-p", config_dir])
with open(os.path.join(config_dir, INSTALL_CONFIG), 'w+') as file_obj:
file_obj.write(install_config)
if not os.path.isdir(config_dir):
raise Exception('installer directory is not mounted')

if not os.path.isfile(os.path.join(config_dir, INSTALL_CONFIG)):
raise Exception("install config file not located in installer dir")

install_config_manipulation(config_dir=config_dir, before_installer=True)
config_dir = create_config_dir(work_dir=work_dir)
prepare_install_config(config_dir=config_dir, install_config=install_config)
generate_installation_files(work_dir=work_dir, config_dir=config_dir)

# [TODO] - add extracting openshift-baremetal-install from release image and using it instead of locally compile openshift-intall
# try:
# command = "%s/oc adm release extract --command=openshift-baremetal-install --to=%s \
# quay.io/openshift-release-dev/ocp-release-nightly@sha256:ba2e09a06c7fca19e162286055c6922135049e6b91f71e2a646738b2d7ab9983" \
# % (work_dir, work_dir)
# command = "%s/oc adm release extract --command=openshift-baremetal-install --to=%s \
# quay.io/openshift-release-dev/ocp-release-nightly@sha256:ba2e09a06c7fca19e162286055c6922135049e6b91f71e2a646738b2d7ab9983" \
# % (work_dir, work_dir)
# subprocess.check_output(command, shell=True, stderr=sys.stdout)
# except Exception as ex:
# raise Exception('Failed to extract installer, exception: {}'.format(ex))

# command = "OPENSHIFT_INSTALL_INVOKER=\"assisted-installer\" %s/openshift-baremetal-install create ignition-configs --dir %s" \
# % (work_dir, config_dir)

command = "OPENSHIFT_INSTALL_INVOKER=\"assisted-installer\" %s/openshift-install create ignition-configs --dir %s" % (work_dir, config_dir)
try:
subprocess.check_output(command, shell=True, stderr=sys.stdout)
except Exception as ex:
raise Exception('Failed to generate files, exception: {}'.format(ex))

install_config_manipulation(config_dir=config_dir, before_installer=True)

try:
update_bmh_files("%s/bootstrap.ign" % config_dir, cluster_id, inventory_endpoint)
except Exception as ex:
raise Exception('Failed to update BMH CRs in bootstrap ignition, exception: {}'.format(ex))
update_bmh_files("%s/bootstrap.ign" % config_dir, cluster_id, inventory_endpoint)

if s3_endpoint_url:
upload_to_s3(s3_endpoint_url, bucket, aws_access_key_id, aws_secret_access_key, config_dir, cluster_id)
else:
# for debug purposes
debug_print_upload_to_s3(config_dir)


if __name__ == "__main__":
main()

0 comments on commit 1d4e74a

Please sign in to comment.