Ejemplo n.º 1
0
    def create_script_file(self, work_dir):
        print("create_script_file ==> START")
        # Error if metadata empty
        if (not self.metadata):
            errors.print_error("create_script_file :: meta_data empty",
                               exit_type="fail")
            exit(errors.EXIT_FAILURE)

        # Metadata not empty
        print("WORKDIR  = " + work_dir)
        self.script_file_ptr = open(work_dir + "/run_me.sh", "w")
        # f = open (WORKDIR + "/run_me.sh", "a")
        self.script_file_ptr.write("#!/bin/bash\n\n")
        self.workdir = work_dir
        print("create_script_file ==> END")
Ejemplo n.º 2
0
 def write_code_unzip(self):
     print("write_code_unzip ==> START")
     # is_archive = [self.metadata["archive_name"].endswith(format) for format in archive.archive_format]
     for isource in self.metadata["source"]:
         try:
             # idx = is_archive.index(True)
             # filename = var_download_command.split("/")
             self.script_file_ptr.write("arc -overwrite unarchive " +
                                        self.workdir + "/" + isource + " " +
                                        self.workdir + "/" + "\n")
         except ValueError as e:
             errors.print_error(
                 "write_code_unzip :: " + isource +
                 " is not a recognized archive format", "fail")
         # print (e)
         # self.script_file_ptr.close()
         # exit (errors.EXIT_FAILURE)
     print("write_code_unzip ==> END")
Ejemplo n.º 3
0
 def write_pip_installs(self):
     print("write_pip_installs ==> START")
     self.script_file_ptr.write("# Additional PIP packages\n")
     # TODO : try-catch to catch missing pip_install parameter
     try:
         if self.metadata["pip_installs"]:
             print("Installing additional PIP packages")
             for ipackage in self.metadata["pip_installs"]:
                 self.script_file_ptr.write("pip3 install -e " + ipackage +
                                            "\n")
             self.script_file_ptr.write("\n")
         else:
             print("No additional PIP package to install")
             self.script_file_ptr.write(
                 "# No additional packages to install\n\n")
     except ValueError as e:
         errors.print_error("write_pip_installs", "fail")
         print(e)
         self.script_file_ptr.close()
         exit(errors.EXIT_FAILURE)
     print("write_pip_installs ==> END")
Ejemplo n.º 4
0
def github_commit(id, workdir, metadata):
    if os.environ["GIT_TOKEN"]:
        gitbase = Github(os.environ["GIT_TOKEN"])
        try:
            repo = gitbase.get_repo(metadata["source"])
            commit = repo.get_commit(sha=metadata["version"])
            cmd_to_return = "git clone " + metadata["source"] + " " + workdir  + "/" + id\
            + "\n cd " + workdir + "/" + id\
            + "\n git checkout " + metadata["version"]\
            + "\n cd " + workdir
            return cmd_to_return

        except Exception as e:
            print(e)
            errors.print_error(
                "Get commited version ... FAIL. Try to clone source code",
                "continue")
            return ("")
    else:
        errors.print_error(
            "The version number does not correspond to a commit-ID, try to clone the project",
            "continue")
        return ("")
Ejemplo n.º 5
0
    def write_code_location(self):
        print("write_code_location ==> START")
        # If file pointer is Null, exit fail
        if not self.script_file_ptr:
            errors.print_error("write_code_location:: Null file pointer",
                               exit_type="fail")

        self.script_file_ptr.write("# Download Instance Code\n")

        # Else get code location
        # If source is archive, WGET archive file
        # idx_archive = archive.is_archive(self.metadata["source"])
        for isource in self.metadata["source"]:
            try:
                # Source is an archive
                response = requests.get(isource, stream=True)
                if (response.ok):
                    self.script_file_ptr.write("wget -N --directory-prefix=" +
                                               self.workdir + " " + isource +
                                               "\n")
                    self.metadata["archive_name"] = isource.split("/")[-1]
                    print("write_code_location ==> END")
                    self.script_file_ptr.write("\n")
                    return
                else:
                    errors.print_error(
                        isource + "' Response status = " +
                        str(response.status_code), "fail")
                    self.script_file_ptr.close()
                    exit(errors.EXIT_FAILURE)
            except ValueError:
                # Source is not archive
                errors.print_error(
                    isource +
                    " is not an archive. Let's try something else ...",
                    "continue")

            # # If source is git repo
            # if (isource.startswith(main_repo["github"]["pattern"])):
            #     # If model has version number, try to get archive file of version
            #     print ("Source is GIT repository")
            #     if (self.metadata["version"]):
            #         # For all archive format, ping the file
            #         print("Try to get release archive from version number ...")
            #         for format in archive.archive_format:
            #             tar_url = self.metadata["source"] + "/archive/v" + self.metadata["version"] + format
            #             response = requests.get(tar_url, stream=True)
            #             if(response.ok):
            #                 self.script_file_ptr.write ("wget -N --directory-prefix=" + self.workdir + " " + tar_url + "\n")
            #                 self.metadata["archive_name"] = tar_url.split("/")[-1]
            #                 print("Try to get release archive from version number ... SUCCESS")
            #                 print ("write_code_location ==> END")
            #                 self.script_file_ptr.write ("\n")
            #                 return
            #
            #         print("Try to get release archive from version number ... FAIL")
            #         errors.print_error (self.metadata["source"] + " does not provide archive release, try to get GIT commit tag from version number.", "continue")
            #
            #         ## Get commit from version number
            #         ## The revision branch should be master
            #         repo.commit('master')
            #         self.script_file_ptr.write ("git clone " + self.metadata["source"] + " " + self.workdir  + "/" + self.id + "\n")
            #         print ("write_code_location ==> END")
            #         self.metadata["archive_name"] = self.metadata["source"].split("/")[-1]
            #         self.script_file_ptr.write ("\n")
            #         return
            #     else :
            #         # Git clone project
            #         errors.print_error (self.metadata["source"] + " does not have version number, try to clone GIT project.", "continue")
            #         self.script_file_ptr.write ("git clone " + self.metadata["source"] + " " + self.workdir + "/" + self.id + "\n")
            #         self.metadata["archive_name"] = self.metadata["source"].split("/")[-1]
            #         print ("write_code_location ==> END")
            #         self.script_file_ptr.write ("\n")
            #         return

        # Error :: the source does not exists or source pattern not taken into account
            errors.print_error(
                isource + " (" + self.metadata["version"] +
                ")' does not exist or service not available.", "fail")
            self.script_file_ptr.close()
            exit(errors.EXIT_FAILURE)
Ejemplo n.º 6
0
    def get_code_location(self):
        print("get_code_location ==> START")
        cmd_to_return = ""

        # If source is archive, WGET archive file
        # is_archive = [self.metadata["source"].endswith(format) for format in archive.archive_format]
        try:
            # Source is an archive
            # idx = is_archive.index(True)
            response = requests.get(self.metadata["source"], stream=True)
            if (response.ok):
                cmd_to_return = "wget -N --directory-prefix=" + self.workdir + " " + self.metadata[
                    "source"]
                self.metadata["archive_name"] = self.metadata["source"].split(
                    "/")[-1]
                print("get_code_location ==> END")
                return cmd_to_return
            else:
                errors.print_error(
                    self.metadata["source"] + "' Response status = " +
                    str(response.status_code), "fail")
                exit(errors.EXIT_FAILURE)
        except ValueError:
            # Source is not archive
            errors.print_error(
                self.metadata["source"] +
                " is not an archive. Let's try something else ...", "continue")

        # If source is git repo
        if (self.metadata["source"].startswith(
                main_repo["github"]["pattern"])):
            # If model has version number, try to get archive file of version
            print("Source is GIT repository")
            if (self.metadata["version"]):
                cmd_to_return = Github.github_release(self.id, self.workdir,
                                                      self.metadata)
                if cmd_to_return:
                    return cmd_to_return
                # For all archive format, ping the file
                print("Try to get release archive from version number ...")

                print("Get release archive from version number ... FAIL")
                errors.print_error(
                    self.metadata["source"] +
                    " does not provide archive release, try to get commit-id from version number.",
                    "continue")

                # Check Github commit ID hash
                cmd_to_return = Github.github_commit(self.id, self.workdir,
                                                     self.metadata)
                if cmd_to_return:
                    return cmd_to_return

                # cmd_to_return = "git clone " + self.metadata["source"] + " " + self.workdir  + "/" + self.id
                print("get_code_location ==> END")
                # self.metadata["archive_name"] = self.metadata["source"].split("/")[-1]
                # return cmd_to_return
                return Github.github_clone(self.id, self.workdir,
                                           self.metadata)
            else:
                # Git clone project
                errors.print_error(
                    self.metadata["source"] +
                    " does not have version number, try to clone GIT project.",
                    "continue")
                print("get_code_location ==> END")
                return Github.github_clone(self.id, self.workdir,
                                           self.metadata)

        # Error :: the source does not exists or source pattern not taken into account
        errors.print_error(
            self.metadata["source"] + " (" + self.metadata["version"] +
            ")' does not exist or service not available.", "fail")
        exit(errors.EXIT_FAILURE)