Beispiel #1
0
def processWorkflow(args):
    try:
        okit_json = readJsonFile(args["json"])
        if args['format'].lower() == 'terraform':
            generator = OCITerraformGenerator(args['templateroot'],
                                              args["outputdir"], okit_json,
                                              False)
        elif args['format'].lower() == 'ansible':
            generator = OCIAnsibleGenerator(args['templateroot'],
                                            args["outputdir"], okit_json,
                                            False)
        elif args['format'].lower() == 'resource-manager':
            generator = OCIResourceManagerGenerator(args['templateroot'],
                                                    args["outputdir"],
                                                    okit_json, False)
        else:
            logger.warn('Not a Valid Output Format : {0!s:s}'.format(
                args['format']))
            logger.warn('Valid Options :')
            logger.warn('   ansible          : Generate Ansible Playbook')
            logger.warn('   terraform        : Generate Terraform Files')
            logger.warn(
                '   resource-manager : Generate Resource Manager Terraform Files'
            )
            raise Exception()
        generator.generate()
        generator.writeFiles()
    except Exception:
        pass
    return
def generate(language):
    logger.info('Language : {0:s} - {1:s}'.format(str(language), str(request.method)))
    logger.debug('JSON     : {0:s}'.format(str(request.json)))
    if request.method == 'POST':
        use_vars = request.json.get("use_variables", True)
        try:
            destination_dir = tempfile.mkdtemp();
            if language == 'terraform':
                generator = OCITerraformGenerator(template_root, destination_dir, request.json, use_vars=use_vars)
            elif language == 'ansible':
                generator = OCIAnsibleGenerator(template_root, destination_dir, request.json, use_vars=use_vars)
            elif language == 'terraform11':
                generator = OCITerraform11Generator(template_root, destination_dir, request.json)
            elif language == 'resource-manager':
                generator = OCIResourceManagerGenerator(template_root, destination_dir, request.json)
            generator.generate()
            generator.writeFiles()
            zipname = generator.createZipArchive(os.path.join(destination_dir, language), "/tmp/okit-{0:s}".format(str(language)))
            logger.info('Zipfile : {0:s}'.format(str(zipname)))
            shutil.rmtree(destination_dir)
            filename = os.path.split(zipname)
            logger.info('Split Zipfile : {0:s}'.format(str(filename)))
            return zipname
        except Exception as e:
            logger.exception(e)
            return str(e), 500
    else:
        return send_from_directory('/tmp', "okit-{0:s}.zip".format(str(language)), mimetype='application/zip', as_attachment=True)
Beispiel #3
0
def generate(language, destination):
    logger.info('Language : {0:s} - {1:s}'.format(str(language), str(request.method)))
    logger.info('Destination : {0:s} - {1:s}'.format(str(destination), str(request.method)))
    logger.debug('JSON     : {0:s}'.format(str(request.json)))
    if request.method == 'POST':
        use_vars = request.json.get("use_variables", True)
        try:
            if destination == 'git':
                git_url, git_branch = request.json['git_repository'].split('*')
                parsed_git_url = giturlparse.parse(git_url)
                generate_git_dir = os.path.abspath(os.path.join(bp.static_folder, 'git'))
                logger.info(generate_git_dir)
                if not os.path.exists(generate_git_dir):
                    os.makedirs(generate_git_dir, exist_ok=True)
                git_repo_dir = os.path.abspath(os.path.join(generate_git_dir, parsed_git_url.name))
                if os.path.exists(git_repo_dir):
                    repo = Repo(git_repo_dir)
                    repo.remotes.origin.pull()
                else:
                    repo = Repo.clone_from(git_url, git_repo_dir, branch=git_branch, no_single_branch=True)
                    repo.remotes.origin.pull()
                destination_dir = os.path.abspath(os.path.join(git_repo_dir, request.json['git_repository_filename']))
            else:
                destination_dir = tempfile.mkdtemp();
            if language == 'terraform':
                generator = OCITerraformGenerator(template_root, destination_dir, request.json, use_vars=use_vars)
            elif language == 'ansible':
                generator = OCIAnsibleGenerator(template_root, destination_dir, request.json, use_vars=use_vars)
            elif language == 'terraform11':
                generator = OCITerraform11Generator(template_root, destination_dir, request.json)
            elif language == 'resource-manager':
                generator = OCIResourceManagerGenerator(template_root, destination_dir, request.json)
            elif language == 'markdown':
                generator = OkitMarkdownGenerator(template_root, destination_dir, request.json)
            generator.generate()
            generator.writeFiles()
            if destination == 'git':
                git_commit_msg = request.json['git_repository_commitmsg']
                repo.index.add(destination_dir)
                repo.index.commit("commit changes from okit:" + git_commit_msg)
                repo.remotes.origin.push(git_branch)
                return language.capitalize()+" files successfully uploaded to GIT Repository"
            else:
                zipname = generator.createZipArchive(os.path.join(destination_dir, language), "/tmp/okit-{0:s}".format(str(language)))
                logger.info('Zipfile : {0:s}'.format(str(zipname)))
                shutil.rmtree(destination_dir)
                filename = os.path.split(zipname)
                logger.info('Split Zipfile : {0:s}'.format(str(filename)))
                return zipname
        except Exception as e:
            logger.exception(e)
            return str(e), 500
    else:
        return send_from_directory('/tmp', "okit-{0:s}.zip".format(str(language)), mimetype='application/zip', as_attachment=True)
def generate(language):
    logger.info('Language : {0:s} - {1:s}'.format(str(language), str(request.method)))
    logger.debug('JSON     : {0:s}'.format(str(request.json)))
    if request.method == 'POST':
        use_vars = request.json.get("use_variables", True)
        try:
            destination_dir = tempfile.mkdtemp();
            gitpush_flag = False
            if language == 'terraformtogit':
                gitpush_flag = True
                language = 'terraform'
            if language == 'ansibletogit':
                gitpush_flag = True
                language = 'ansible'
            if language == 'terraform':
                generator = OCITerraformGenerator(template_root, destination_dir, request.json, use_vars=use_vars)
            elif language == 'ansible':
                generator = OCIAnsibleGenerator(template_root, destination_dir, request.json, use_vars=use_vars)
            elif language == 'terraform11':
                generator = OCITerraform11Generator(template_root, destination_dir, request.json)
            elif language == 'resource-manager':
                generator = OCIResourceManagerGenerator(template_root, destination_dir, request.json)
            generator.generate()
            generator.writeFiles()
            zipname = generator.createZipArchive(os.path.join(destination_dir, language), "/tmp/okit-{0:s}".format(str(language)))
            logger.info('Zipfile : {0:s}'.format(str(zipname)))

            if gitpush_flag:
                get_selection_details = request.json['git_repository']
                git_url, git_branch = get_selection_details.split('*')
                git_file_name = request.json['git_repository_filename']
                git_commit_msg = request.json['git_repository_commitmsg']
                destination = '/okit/okitweb/static/okit/templates/tmpgit'

                repo = Repo.clone_from(git_url, destination, branch=git_branch, no_single_branch=True)
                repo.remotes.origin.pull()

                copyfrom = destination_dir+'/'+language
                copyto = destination +'/' + git_file_name

                if os.path.exists(copyto):
                    return "Name already exists"
                else:
                    os.makedirs(copyto)

                for item in os.listdir(copyfrom):
                    s = os.path.join(copyfrom, item)
                    d = os.path.join(copyto, item)
                    shutil.copy2(s, d)

                repo.index.add(copyto)
                repo.index.commit("commit changes from okit:" + git_commit_msg)
                #origin = repo.remote('origin')
                #origin.push(git_branch)
                repo.remotes.origin.push(git_branch)

                os.system("rm -rf " + destination)
                shutil.rmtree(destination_dir)

                return language.capitalize()+" files successfully uploaded to GIT Repository"
            shutil.rmtree(destination_dir)
            filename = os.path.split(zipname)
            logger.info('Split Zipfile : {0:s}'.format(str(filename)))
            return zipname
        except Exception as e:
            logger.exception(e)
            return str(e), 500
    else:
        return send_from_directory('/tmp', "okit-{0:s}.zip".format(str(language)), mimetype='application/zip', as_attachment=True)