Exemplo n.º 1
0
 def __minimal_python_version(self):
     """
     Check Python version
     """
     logger.info("Check if Python is in minimum " +
                 self.python_minimal_version + " version")
     if self.python_version >= self.python_minimal_version:
         logger.info("Good! Python " + self.python_version + " on " +
                     platform.system())
     else:
         __text_error = "You must run the program under Python 3 minimum!"
         logger.critical(__text_error)
         utils.stop_program()
    def __vbox_sdk_get_latest_stable_version(self):
        """
        Get latest stable version of vbox sdk
        :return: Last stable release, retrieved from the internet
        """
        __latest_stable_version_request = urllib.request.Request(self.sdk_url_latest_stable_version)
        with urllib.request.urlopen(__latest_stable_version_request) as response:
            __latest_stable_version_response = response.read().decode().rstrip()

        __regex_test = self.__vbox_sdk_version_test_regex(__latest_stable_version_response)

        if __regex_test is None:
            logger.critical(
                'The value of the version does not match the regex, it must contain 3 numbers separated by points. '
                'Check with the following URL: ' + self.sdk_url_latest_stable_version)
            utils.stop_program()

        return __latest_stable_version_response
 def __verify_vbox_sdk(self):
     """
     Prerequisites vbox sdk
     """
     if self.sdk_version.lower() == "latest":  # If the last one was set in the configuration
         self.sdk_latest_stable_version = self.__vbox_sdk_get_latest_stable_version()
         self.sdk_version = self.sdk_latest_stable_version
         logger.info("Starting the vbox configuration using the latest version of the SDK: " +
                     self.sdk_latest_stable_version)
         self.__vbox_sdk_exist(self.sdk_latest_stable_version)
     else:
         __regex_test = self.__vbox_sdk_version_test_regex(self.sdk_version)
         if __regex_test is None:
             logger.critical(
                 'Please check the value of "vbox_sdk" in the configuration file, it must contain 3 numbers '
                 'separated by points')
             utils.stop_program()
         elif __regex_test is not None:
             logger.debug("Validated SDK version: " + __regex_test.group())
             self.__vbox_sdk_exist(self.sdk_version)
Exemplo n.º 4
0
def run_python_script(command, path_to_run=os.getcwd(), output=False):
    """
    Launch a python script
    :param output: Output of the command executed
    :param path_to_run: Directory where the script is to be started. Default: current directory
    :param command: Command to launch the script
    """
    try:
        __current_dir = os.getcwd()  # Save the current execution directory
    except FileNotFoundError:
        __text_error = "The current execution directory does not exist"
        logger.critical(__text_error)
        stop_program()

    try:
        os.chdir(path_to_run)  # Change the execution directory
    except FileNotFoundError:
        __text_error = "The specified folder can not be found: " + path_to_run
        logger.critical(__text_error)
        stop_program()

    if output is False:
        subprocess.call(command,
                        stdout=subprocess.DEVNULL,
                        stderr=subprocess.DEVNULL,
                        shell=True)
    else:
        subprocess.call(command, shell=True)

    try:
        os.chdir(__current_dir)  # Reset execution directory
    except FileNotFoundError:
        __text_error = "The specified folder can not be found: " + path_to_run
        logger.critical(__text_error)
        stop_program()
Exemplo n.º 5
0
def unzip_file(file, to):
    """
    Unzip file
    :param file: Zip file (to extract)
    :param to: Location of extraction
    """
    if zipfile.is_zipfile(file) is False:
        __text_error = "Invalid ZIP file: " + file
        logger.critical(__text_error)
        stop_program()

    try:
        with zipfile.ZipFile(file, 'r') as zip_ref:
            zip_ref.extractall(to)
    except zipfile.BadZipFile:
        __text_error = "Invalid ZIP file: " + file
        logger.critical(__text_error)
        stop_program()
    except zipfile.LargeZipFile:
        __text_error = "ZIP file size exceeds 4GB: " + file
        logger.critical(__text_error)
        stop_program()
    def __vbox_sdk_download(self, version):
        """
        Download VBOX SDK
        :param version: Specify version download
        :return: Path of the downloaded file
        """
        logger.info("Starting the download process...")

        __vbox_sdk_url_repo = cfg_vbox['sdk_virtualbox']['vbox_url_repo'] + version + "/"
        __vbox_sdk_url_repo_index_html = __vbox_sdk_url_repo + "index.html"

        try:
            __vbox_sdk_url_repo_read_index = urllib.request.urlopen(__vbox_sdk_url_repo_index_html).read()
        except urllib.error.URLError as e:
            __text_error = "Can not access the following URL: " + __vbox_sdk_url_repo_index_html
            logger.critical(__text_error)
            utils.stop_program()
        except urllib.error.HTTPError as e:
            __text_error = "Can not access the following URL: " + __vbox_sdk_url_repo_index_html + " (HTTPError code: " \
                           + str(e.code) + ")"
            logger.critical(__text_error)
            utils.stop_program()

        __vbox_sdk_url_download_search = re.search(b'(VirtualBoxSDK-)(.*?)(zip)', __vbox_sdk_url_repo_read_index)

        if __vbox_sdk_url_download_search is None:
            __text_error = "Can not find SDK download link"
            logger.critical(__text_error)
            utils.stop_program()

        __vbox_sdk_url_download = __vbox_sdk_url_download_search.group().decode()
        __url_download = __vbox_sdk_url_repo + __vbox_sdk_url_download

        logger.debug("Check if the directory (" + self.tmp_directory + ") already exists")

        if os.path.isdir(self.tmp_directory):
            logger.debug("Directory '" + self.tmp_directory + "' already exists")
        else:
            os.mkdir(self.tmp_directory, 0o777)
            logger.debug("Directory '" + self.tmp_directory + "' created")

        logger.info("Downloading the " + __url_download + " file in progress...")

        try:
            __file = urllib.request.urlretrieve(__url_download, self.tmp_directory + "/" + __vbox_sdk_url_download)
        except urllib.error.URLError as e:
            __text_error = "Can not access the following URL: " + __url_download
            logger.critical(__text_error)
            utils.stop_program()
        except urllib.error.HTTPError as e:
            __text_error = "Can not access the following URL: " + __url_download + " (HTTPError code: " \
                           + str(e.code) + ")"
            logger.critical(__text_error)
        except urllib.error.ContentTooShortError as e:
            __text_error = "There was a problem downloading the file"
            logger.critical(__text_error)
            utils.stop_program()

        logger.info("The file has been downloaded")
        return __file[0]
Exemplo n.º 7
0
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

from automanagemachine.components import utils
from automanagemachine.components.machine.machine_vbox import MachineVbox
from automanagemachine.components.requirements.requirements_vbox_sdk import RequirementsVboxSdk
from automanagemachine.core import cfg, logger

print(cfg['app']['name'] + " | version: " + cfg['app']['version'])

virtual_environment = cfg['machine']['virtual_environment']
if virtual_environment.lower() == "vbox":
    requirements = RequirementsVboxSdk()
    machine = MachineVbox()
elif virtual_environment.lower() == "aws":
    logger.warning("AWS not implemented.")
    utils.stop_program()
else:
    logger.critical("Not implemented.")
    utils.stop_program()

machine.name = cfg['machine']['name']
machine.cpu = cfg['machine']['cpu']
machine.virtual_memory = cfg['machine']['virtual_memory']
machine.create()
machine.run()