def update_sonic_environment(bootloader, binary_image_version):
    'Prepare sonic environment variable using incoming image template file. If incoming image template does not exist\n       use current image template file.\n    '
    SONIC_ENV_TEMPLATE_FILE = os.path.join('usr', 'share', 'sonic', 'templates', 'sonic-environment.j2')
    SONIC_VERSION_YML_FILE = os.path.join('etc', 'sonic', 'sonic_version.yml')
    sonic_version = re.sub(IMAGE_PREFIX, '', binary_image_version)
    new_image_dir = bootloader.get_image_path(binary_image_version)
    new_image_mount = os.path.join('/', 'tmp', 'image-{0}-fs'.format(sonic_version))
    env_dir = os.path.join(new_image_dir, 'sonic-config')
    env_file = os.path.join(env_dir, 'sonic-environment')
    with bootloader.get_rootfs_path(new_image_dir) as new_image_squashfs_path:
        try:
            mount_squash_fs(new_image_squashfs_path, new_image_mount)
            next_sonic_env_template_file = os.path.join(new_image_mount, SONIC_ENV_TEMPLATE_FILE)
            next_sonic_version_yml_file = os.path.join(new_image_mount, SONIC_VERSION_YML_FILE)
            sonic_env = run_command_or_raise(['sonic-cfggen', '-d', '-y', next_sonic_version_yml_file, '-t', next_sonic_env_template_file])
            os.mkdir(env_dir, 493)
            with open(env_file, 'w+') as ef:
                print(sonic_env, file=ef)
            os.chmod(env_file, 420)
        except SonicRuntimeException as ex:
            echo_and_log('Warning: SONiC environment variables are not supported for this image: {0}'.format(str(ex)), LOG_ERR, fg='red')
            if os.path.exists(env_file):
                os.remove(env_file)
                os.rmdir(env_dir)
        finally:
            umount(new_image_mount)
Prepare sonic environment variable using incoming image template file. If incoming image template does not exist
use current image template file.
def update_sonic_environment(bootloader, binary_image_version):
    'Prepare sonic environment variable using incoming image template file. If incoming image template does not exist\n       use current image template file.\n    '
    SONIC_ENV_TEMPLATE_FILE = os.path.join('usr', 'share', 'sonic', 'templates', 'sonic-environment.j2')
    SONIC_VERSION_YML_FILE = os.path.join('etc', 'sonic', 'sonic_version.yml')
    sonic_version = re.sub(IMAGE_PREFIX, , binary_image_version)
    new_image_dir = bootloader.get_image_path(binary_image_version)
    new_image_mount = os.path.join('/', 'tmp', 'image-{0}-fs'.format(sonic_version))
    env_dir = os.path.join(new_image_dir, 'sonic-config')
    env_file = os.path.join(env_dir, 'sonic-environment')
    with bootloader.get_rootfs_path(new_image_dir) as new_image_squashfs_path:
        try:
            mount_squash_fs(new_image_squashfs_path, new_image_mount)
            next_sonic_env_template_file = os.path.join(new_image_mount, SONIC_ENV_TEMPLATE_FILE)
            next_sonic_version_yml_file = os.path.join(new_image_mount, SONIC_VERSION_YML_FILE)
            sonic_env = run_command_or_raise(['sonic-cfggen', '-d', '-y', next_sonic_version_yml_file, '-t', next_sonic_env_template_file])
            os.mkdir(env_dir, 493)
            with open(env_file, 'w+') as ef:
                print(sonic_env, file=ef)
            os.chmod(env_file, 420)
        except SonicRuntimeException as ex:
            echo_and_log('Warning: SONiC environment variables are not supported for this image: {0}'.format(str(ex)), LOG_ERR, fg='red')
            if os.path.exists(env_file):
                os.remove(env_file)
                os.rmdir(env_dir)
        finally:
            umount(new_image_mount)