if TemplateParsing.options.ose_registry is not None and TemplateParsing.options.copy_build_config.lower() == "no" \
       and TemplateParsing.options.ose_token is not None and TemplateParsing.options.docker_username is not None:
        ose_resources_to_export = ['imagestream', 'deploymentconfig', 'service', 'route']
        resource_dictionary['image_deployment'] = TemplateParsing.options.ose_registry
        promote_image = True
    for resource in ose_resources_to_export:
        resource_with_apps.append("%s/%s" % (resource, TemplateParsing.options.app_name))
    export_command = "/usr/bin/oc export %s --as-template=%s" % (" ".join(resource_with_apps), template_name)
    # If the optional url flag was passed into the script, search the text for a route spec
    # At the time of writing this is denoted by "host: <url>" in the spec section of a route
    if TemplateParsing.options.url is not None or TemplateParsing.options.env_variables is not None:
        resource_dictionary['source_project'] = TemplateParsing.options.source_project_name
        resource_dictionary['destination_project'] = TemplateParsing.options.destination_project_name
        if TemplateParsing.options.url is not None:
            resource_dictionary["url"] = TemplateParsing.options.url
        if TemplateParsing.options.env_variables is not None:
            resource_dictionary["environment_vars"] = TemplateParsing.options.env_variables
        TemplateParsing.substitute_values_in_template(export_command, template_output, resource_dictionary)
    else:
        TemplateParsing.export_as_template(export_command, template_output)
else:
    print("%s was not found in project %s" % (TemplateParsing.options.app_name,
                                              TemplateParsing.options.source_project_name))
    sys.exit(2)

TemplateParsing.create_objects(TemplateParsing.options.destination_project_name, template_output)

if promote_image:
    TemplateParsing.docker_promote_image(TemplateParsing.options.ose_registry, TemplateParsing.options.docker_username,
                                     TemplateParsing.options.ose_token, TemplateParsing.options.source_project_name,
                                     TemplateParsing.options.destination_project_name, TemplateParsing.options.app_name)
        resource_dictionary[
            'source_project'] = TemplateParsing.options.source_project_name
        resource_dictionary[
            'destination_project'] = TemplateParsing.options.destination_project_name
        if TemplateParsing.options.url is not None:
            resource_dictionary["url"] = TemplateParsing.options.url
        if TemplateParsing.options.env_variables is not None:
            resource_dictionary[
                "environment_vars"] = TemplateParsing.options.env_variables
        TemplateParsing.substitute_values_in_template(export_command,
                                                      template_output,
                                                      resource_dictionary)
    else:
        TemplateParsing.export_as_template(export_command, template_output)
else:
    print("%s was not found in project %s" %
          (TemplateParsing.options.app_name,
           TemplateParsing.options.source_project_name))
    sys.exit(2)

TemplateParsing.create_objects(
    TemplateParsing.options.destination_project_name, template_output)

if promote_image:
    TemplateParsing.docker_promote_image(
        TemplateParsing.options.ose_registry,
        TemplateParsing.options.docker_username,
        TemplateParsing.options.ose_token,
        TemplateParsing.options.source_project_name,
        TemplateParsing.options.destination_project_name,
        TemplateParsing.options.app_name)
# Check for a previous template
if os.path.exists(template_output):
    os.rename(template_output, (template_output + "_" + script_run_date))

export_command = "/usr/bin/oc export %s --as-template=%s" % (" ".join(resources_to_import), template_name)

if TemplateParsing.options.url is not None:
    resource_dictionary['source_project'] = TemplateParsing.options.source_project_name
    resource_dictionary['destination_project'] = TemplateParsing.options.destination_project_name
    resource_dictionary["url"] = TemplateParsing.options.url
    TemplateParsing.substitute_values_in_template(export_command, template_output, resource_dictionary)
else:
    print("No URL options are specified, this means that you would have projects sharing the same route.\n"
          "This is probably a bad idea. Exiting...")
    sys.exit()

if TemplateParsing.options.credentials_file:
    TemplateParsing.create_objects(TemplateParsing.options.destination_project_name, template_output,
                                   TemplateParsing.options.credentials_file)
else:
    TemplateParsing.create_objects(TemplateParsing.options.destination_project_name, template_output)

if promote_image:
    # Because a project could have multiple applications, loop over each and then start docker pull/push
    for application in valid_apps:
        TemplateParsing.docker_promote_image(TemplateParsing.options.ose_registry, TemplateParsing.options.docker_username,
                                        TemplateParsing.options.ose_token, TemplateParsing.options.source_project_name,
                                        TemplateParsing.options.destination_project_name,
                                        application)
        'source_project'] = TemplateParsing.options.source_project_name
    resource_dictionary[
        'destination_project'] = TemplateParsing.options.destination_project_name
    resource_dictionary["url"] = TemplateParsing.options.url
    TemplateParsing.substitute_values_in_template(export_command,
                                                  template_output,
                                                  resource_dictionary)
else:
    print(
        "No URL options are specified, this means that you would have projects sharing the same route.\n"
        "This is probably a bad idea. Exiting...")
    sys.exit()

if TemplateParsing.options.credentials_file:
    TemplateParsing.create_objects(
        TemplateParsing.options.destination_project_name, template_output,
        TemplateParsing.options.credentials_file)
else:
    TemplateParsing.create_objects(
        TemplateParsing.options.destination_project_name, template_output)

if promote_image:
    # Because a project could have multiple applications, loop over each and then start docker pull/push
    for application in valid_apps:
        TemplateParsing.docker_promote_image(
            TemplateParsing.options.ose_registry,
            TemplateParsing.options.docker_username,
            TemplateParsing.options.ose_token,
            TemplateParsing.options.source_project_name,
            TemplateParsing.options.destination_project_name, application)