Esempio n. 1
0
 def try_connect(ssh):
     """ Recursive function to enable multiple connection attempts """
     try:
         ssh.connect(self.ssh_server,
                     port=int(self.ssh_port),
                     username=self.ssh_user,
                     password=self.ssh_password)
         STREAM.success(" -> Connection established")
     except Exception as err:
         STREAM.warning(" -> Fail (%s)" % err)
         if "ecdsakey" in str(err):
             STREAM.warning("ECDSAKey error, try to fix.")
             Popen('ssh-keygen -f %s -R "[%s]:%s"' % (os.path.join(
                 os.path.expanduser("~"),
                 ".ssh/known_hosts"), self.ssh_server, self.ssh_port),
                   shell=True,
                   stdout=PIPE,
                   stderr=PIPE).communicate()
         if self.connect_tries > 20:
             raise paramiko.ssh_exception.SSHException(
                 "Connection retries limit exceed!")
         self.connect_tries += 1
         STREAM.info(" -> Connection retry %s:" % self.connect_tries)
         sleep(15)
         try_connect(ssh)
Esempio n. 2
0
 def run_playbook(self, playbook, inventory):
     options = self.parse_options()
     STREAM.info("==> Execute Ansible playbook: %s" % playbook)
     # initialize needed objects
     loader = DataLoader()
     passwords = {}
     inventory = InventoryManager(loader=loader, sources=inventory)
     variable_manager = VariableManager(loader=loader, inventory=inventory)
     # create the playbook executor, which manages running the plays via a task queue manager
     pbex = PlaybookExecutor(playbooks=[playbook],
                             inventory=inventory,
                             variable_manager=variable_manager,
                             loader=loader,
                             options=options,
                             passwords=passwords)
     # run playbook and return exit_code
     results = pbex.run()
     if results == 0:
         STREAM.success(" -> Successfully executed.")
     else:
         raise Exception(
             " -> Ansible playbook(%s) exited with error_code: %s" %
             (playbook, results))
     # Clean ansible temp files
     shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Esempio n. 3
0
    def connect_to_vm(self):
        """ Method connects to VirtualMachine via ssh """
        def try_connect(ssh):
            """ Recursive function to enable multiple connection attempts """
            self.connect_tries += 1
            try:
                ssh.connect(self.ssh_server, port=int(self.ssh_port), username=self.ssh_user, password=self.ssh_password)
                STREAM.success(" -> Connection established")
            except Exception as err:
                STREAM.warning(" -> Fail (%s)" % err)
                if "ecdsakey" in str(err):
                    STREAM.warning("ECDSAKey error, try to fix.")
                    Popen('ssh-keygen -f %s -R "[%s]:%s"' %
                          (os.path.join(os.path.expanduser("~"), ".ssh/known_hosts"), self.ssh_server, self.ssh_port),
                          shell=True, stdout=PIPE, stderr=PIPE).communicate()
                if self.connect_tries == self.connections_limit:
                    raise paramiko.ssh_exception.SSHException("Connection retries limit(%s) exceed!"
                                                              % self.connections_limit)
                STREAM.info(" -> Connection retry %s:" % self.connect_tries)
                sleep(15)
                try_connect(ssh)

        STREAM.info("==> Connecting to VirtualMachine (port = %s)." % self.ssh_port)

        ssh = paramiko.SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.connect_tries = 0
        try_connect(ssh)
        del self.connect_tries
        return ssh
Esempio n. 4
0
 def renew_vm(self):
     """ Method to replace the old box """
     for fil in os.listdir(self.work_dir):
         if fil.endswith(".box"):
             STREAM.info("==> Renew old box...")
             os.remove(os.path.join(self.work_dir, fil))
     os.rename(os.path.join(self.work_dir, self.boxname),
               os.path.join(self.work_dir, self.boxname[:-5]))
Esempio n. 5
0
 def create_box(self):
     """ Method to create vagrant box from exported configuration """
     STREAM.info("==> Creating box...")
     with tarfile.open(os.path.join(self.work_dir, self.boxname),
                       "w") as tar:
         for fil in os.listdir(self.tmp_dir):
             tar.add(os.path.join(self.tmp_dir, fil), arcname=fil)
     STREAM.debug(" -> Clearing temporary files")
     shutil.rmtree(self.tmp_dir)
Esempio n. 6
0
 def load_keywords(self):
     lst_of_keywords = self.enabled_keywords
     STREAM.info("==> Checking and loading keywords...")
     for keyword in lst_of_keywords:
         KeywordController.check_keyword(keyword)
     loaded_keywords = {}
     for keyword in lst_of_keywords:
         loaded_keywords[keyword] = self.load_keyword(keyword)
     return loaded_keywords
Esempio n. 7
0
 def clearing(self):
     image_id_file = os.path.join(LoadSettings.WORK_DIR, ".openstack_export.tmp")
     if os.path.exists(image_id_file):
         with open(image_id_file, "r") as tmp:
             old_image_id = tmp.read()
         target_cluster = self.openstack_credentials_harvester()
         glance = self.cluster_connect(target_cluster)
         self.delete_image(glance, old_image_id)
         STREAM.info(" -> Removed just created image with id %s" % old_image_id)
         os.remove(image_id_file)
     else:
         STREAM.info(" -> Nothing to clean.")
Esempio n. 8
0
 def main(self):
     # - Attributes taken from config
     self.vm_name = self.vm_name
     # self.forwarding_ports input format: name:guest:host, ... ex: vm_ssh:22:2020, icap:1344:1234
     self.forwarding_ports = self.forwarding_ports
     self.management_type = self.management_type
     # ------------------------------------
     STREAM.info("==> Forwarding ports.")
     if self.check_vm_status():
         raise Exception(
             "Unable to forwarding ports, VirtualMachine is booted.")
     self.forward()
Esempio n. 9
0
 def check_connection(self):
     STREAM.info("==> Waiting for the availability of the port: %s:%s" %
                 (self.ANSIBLE_SERVER, self.ANSIBLE_PORT))
     STREAM.debug(" -> Connection timeout set to: %s" %
                  self.CONNECTION_TIMEOUT)
     start_time_point = time()
     while True:
         if self._port_check(self.ANSIBLE_SERVER, self.ANSIBLE_PORT):
             STREAM.info(" -> Port available.")
             break
         if time() - start_time_point > self.CONNECTION_TIMEOUT:
             STREAM.error(" -> Port unavailable.")
             break
         sleep(1)
Esempio n. 10
0
 def main(self):
     # - Attributes taken from config
     self.openstack_cluster = self.openstack_cluster
     self.openstack_image_name = self.openstack_image_name
     self.openstack_flavor = self.openstack_flavor
     self.openstack_network = self.openstack_network
     # - Optional attribute taken from config
     try:
         self.openstack_availability_zone = getattr(
             self, "openstack_availability_zone")
         nodes = self.get_nodes(self.openstack_availability_zone)
     except:
         self.openstack_availability_zone = None
         nodes = None
     # --------------------------------
     # List of available clusters
     self.clusters = {}
     target_cluster = self.openstack_credentials_harvester()
     nova = self.cluster_connect(target_cluster)
     STREAM.info("==> Creating cache for image %s" %
                 self.openstack_image_name)
     # Check for already created instance with current name
     STREAM.debug(" -> Check for running instances with the same name")
     self.check_for_running_instances(nova)
     if self.openstack_availability_zone is None:
         STREAM.info(" -> Running cache on random node.")
         self.cache_image(nova)
     else:
         STREAM.info(" -> Running parallel cache on specified nodes.")
         STREAM.info(" -> Number of worker processes: %s" % self.WORKERS)
         self.cache_image_multi_nodes(nova, nodes)
Esempio n. 11
0
    def vbox_guestadditions_update(self, ssh):
        """ Method to update Virtual Box Guest Additions in VirtualMachine """

        STREAM.info("==> Updating VboxGuestAdditions.")
        if not self.mount_vbox_guestadditions(ssh):
            return
        STREAM.debug(" -> Execute update GuestAdditions.")
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            "/mnt/dvd/VBoxLinuxAdditions.run 2>&1",
            environment={"LC_ALL": "C"})
        ssh_stdin.write("y\n")
        ssh_stdin.flush()
        stdout = ssh_stdout.read()
        if "Running kernel modules will not be replaced" in stdout:
            STREAM.success(" -> VboxGuestAdditions updated")
        else:
            STREAM.error(stdout)
Esempio n. 12
0
 def command_exec(self, ssh, command, stdin=""):
     """ Method to execute remote command via ssh connection """
     STREAM.info(" -> Executing command: %s" % command)
     ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
     ssh_stdin.write(stdin)
     ssh_stdin.flush()
     stdout = ssh_stdout.read()
     stderr = ssh_stderr.read()
     STREAM.debug(self.get_decoded(stdout))
     if len(stderr) > 0:
         STREAM.debug(self.get_decoded(stderr))
     exit_code = ssh_stdout.channel.recv_exit_status()
     STREAM.debug(" -> Command exitcode: %s" % exit_code)
     if exit_code == 0:
         STREAM.success(" -> Command executed successfully")
     else:
         raise Exception("Executed command exit status not 0")
Esempio n. 13
0
 def clearing(self):
     """ Clearing method, invoked if job is interrupted or complete unsuccessfully.
         Reverse already done actions by invoking 'clearing' method in each keyword.
         If 'clearing' method not implemented in keyword, doing nothing. """
     LoggerOptions.set_action("clearing")
     for action in reversed(self.actions_progress):
         try:
             invoke = self.invoke_keyword(action)
             getattr(invoke, "clearing")
         except AttributeError:
             STREAM.debug("==> Reverse action '%s':" % action)
             STREAM.debug(
                 " -> Method 'clearing' not implemented in keyword, nothing to do."
             )
         else:
             STREAM.info("==> Reverse action '%s':" % action)
             invoke().clearing()
     LoggerOptions.set_action(None)
Esempio n. 14
0
 def get_vbox_guestadditions_iso(self, version):
     """ Method to download VirtualBoxGuestAdditions.iso from Virtual Box server """
     filename = "VBoxGuestAdditions_%s.iso" % version
     download_path = os.path.join(LoadSettings.WORK_DIR, filename)
     if os.path.exists(download_path):
         return download_path
     Popen('rm -rf %s' % os.path.join(LoadSettings.WORK_DIR, "*.iso"),
           shell=True,
           stdout=PIPE,
           stderr=PIPE).communicate()
     download_link = self.vbox_url + version + "/" + filename
     STREAM.debug(" -> download link: %s" % download_link)
     iso = requests.get(download_link).content
     STREAM.info(" -> Downloading VboxGuestAdditions...")
     with open(download_path, "wb") as ga:
         ga.write(iso)
     STREAM.success(" -> Downloaded: %s" % download_path)
     return download_path
Esempio n. 15
0
 def restore_from_snapshot(self):
     STREAM.info("==> Restore VirtualMachine state from snapshot: '%s'" %
                 self.snapshot_name)
     result = Popen('VBoxManage snapshot %s restore %s' %
                    (self.vm_name, self.snapshot_name),
                    shell=True,
                    stdout=PIPE,
                    stderr=PIPE).communicate()
     if len(result[1]) > 0:
         if "0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%" not in result[
                 1]:
             if "VBOX_E_OBJECT_NOT_FOUND" in result[1]:
                 raise Exception(
                     "Snapshot with name '%s' not found for this VirtualMachine"
                     % self.snapshot_name)
             else:
                 raise Exception(result[1])
     STREAM.debug(result)
     STREAM.success(" -> Restore complete.")
Esempio n. 16
0
 def noforce_stop(self):
     STREAM.info("==> Attempting to gracefull shutdown VirtualMachine")
     if not self.check_vm_status():
         STREAM.info(" -> VirtualMachine is already stopped")
         return
     process = Popen("VBoxManage controlvm %s acpipowerbutton" %
                     self.vm_name,
                     shell=True,
                     stdout=PIPE,
                     stderr=PIPE).communicate()
     stderr = process[1]
     if len(stderr) > 0:
         raise Exception(stderr)
     while 1:
         rvms = Popen("VBoxManage list runningvms | awk '{print $1}'",
                      shell=True,
                      stdout=PIPE,
                      stderr=PIPE)
         data = rvms.stdout.read()
         if self.vm_name not in data:
             break
Esempio n. 17
0
 def openstack_credentials_harvester(self):
     """ Method to get cluster's connection settings from the configuration file """
     STREAM.info("==> Get Openstack cluster connection settings")
     try:
         configfile, section = self.openstack_cluster.split("::")
         STREAM.debug(" -> Using user configuration file %s" % configfile)
     except ValueError:
         configfile = LoadSettings.GENERAL_CONFIG
         STREAM.debug(" -> Using general configuration file %s" %
                      configfile)
         section = self.openstack_cluster
     config = ConfigParser()
     config.read(configfile)
     args = {key: value.strip() for key, value in config.items(section)}
     self.clusters[section] = args
     if self.clusters == {}:
         STREAM.error(
             " -> There are no connection settings for the Openstack clusters found!\nMake sure"
             "that parameter(openstack_cluster) specified correctly.")
         STREAM.error(" -> Export in Openstack passed.")
         sys.exit(1)
     STREAM.info(" -> Found connection settings for the Openstack cluster")
     STREAM.info(" -> Target Openstack cluster set to: %s" % section)
     target_cluster_name = section
     return target_cluster_name
Esempio n. 18
0
 def upload_image(self, connection):
     """ Method to upload image to the openstack cluster """
     args = self.get_image_properties()
     args["name"] = self.openstack_image_name
     STREAM.info("==> Uploading image...")
     STREAM.debug(" -> Image properties: %s" % args)
     # Find where vm files are located
     vm_dir = self.find_vm_files()
     if vm_dir is None:
         return
     # Find specified disk format in vm directory.
     disk = None
     for fil in os.listdir(vm_dir):
         if fil.endswith(args["disk_format"]):
             disk = os.path.join(vm_dir, fil)
     if disk is None:
         STREAM.error("%s disk not found in %s\nMake sure that you are specify a right disk_format "
                      "in parameter(openstack_image_properties) or the disk exists." % (args["disk_format"], vm_dir))
         STREAM.error("Export in openstack canceled.")
         return
     STREAM.debug(" -> VirtualMachine's virtual hard drive location: %s" % disk)
     # Get image id, if image with specified name already exists
     old_image_id = self.image_exists(connection, args["name"])
     # Create image object with specified properties.
     image = connection.images.create(**args)
     # create file with created image id
     image_id_file = os.path.join(LoadSettings.WORK_DIR, ".openstack_export.tmp")
     with open(image_id_file, "w") as tmp:
         tmp.write(image.id)
     # Uploading image.
     connection.images.upload(image.id, open(disk, 'rb'))
     STREAM.success(" -> Uploading complete.")
     # if all ok remove tmp file
     os.remove(image_id_file)
     if old_image_id is not None:
         STREAM.info(" -> Remove old image.")
         self.delete_image(connection, old_image_id)
         STREAM.debug(" -> Removed image with id: %s" % old_image_id)
         STREAM.success(" -> Removed.")
Esempio n. 19
0
    def command_exec(self, ssh, command, stdin="", get_pty=False):
        """ Method to execute remote command via ssh connection """
        def line_buffered(f):
            """ Iterator object to get output in realtime from stdout buffer """
            while not f.channel.exit_status_ready():
                yield self.get_decoded(f.readline().strip())

        STREAM.info(" -> Executing command: %s" % command)
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command,
                                                             get_pty=get_pty)
        ssh_stdout._set_mode("rb")
        ssh_stdin.write(stdin)
        ssh_stdin.flush()
        for l in line_buffered(ssh_stdout):
            STREAM.debug(l)
        stderr = ssh_stderr.read()
        if len(stderr) > 0:
            try:
                raise Exception(stderr)
            except UnicodeDecodeError:
                raise Exception(self.get_decoded(stderr))
        STREAM.success(" -> Command executed successfully")
Esempio n. 20
0
 def main(self):
     # - Attributes taken from config
     self.vm_name = self.vm_name
     self.forwarding_ports = self.forwarding_ports
     self.credentials = self.credentials
     self.management_type = self.management_type
     # -------------------------------------------
     # Setting attributes to invoked keywords
     vbox_stop.vm_name = self.vm_name
     vbox_start.vm_name = self.vm_name
     STREAM.info("==> Updating VirtualMachine.")
     self.get_connection_settings()
     if self.management_type == "ssh":
         ssh = self.ssh_connect_to_vm()
     else:
         raise Exception(
             "Don't know how to connect to vm! (parameter 'management_type' has unknown value)"
         )
     self.detected_os = self.get_vm_platform(ssh)
     # Invoke update method
     update_method = getattr(self, "update_%s" % self.detected_os)
     update_method(ssh)
     STREAM.success(" -> VirtualMachine has been updated.")
Esempio n. 21
0
 def upload_script_and_execute(self, ssh, parameter):
     Platform = self.get_platform(ssh)
     if parameter.strip().startswith("script:"):
         parameter = parameter[7:]
     try:
         shell, filepath = parameter.strip().split(":")
     except ValueError:
         shell = None
         filepath = parameter.strip()
     STREAM.info(" -> Executing script: %s" % filepath)
     if Platform == "win-like":
         STREAM.debug(" -> Remote system probably is windows type")
         temppath = os.path.join("C:\Windows\Temp", os.path.basename(filepath))
         default_shell = r"C:\Windows\System32\cmd.exe /c start"
     else:
         STREAM.debug(" -> Remote system probably is unix type")
         temppath = os.path.join("/tmp", os.path.basename(filepath))
         default_shell = "bash"
     if shell is None:
         STREAM.debug(" -> Shell is not specified, using default: %s" % default_shell)
         shell = default_shell
     scp = SCPClient(ssh.get_transport())
     scp.put(filepath, temppath)
     scp.close()
     ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("%s %s" % (shell, temppath))
     stdout = ssh_stdout.read()
     stderr = ssh_stderr.read()
     STREAM.debug(self.get_decoded(stdout))
     if len(stderr) > 0:
         STREAM.debug(self.get_decoded(stderr))
     exit_code = ssh_stdout.channel.recv_exit_status()
     STREAM.debug(" -> Script exitcode: %s" % exit_code)
     if exit_code == 0:
         STREAM.success(" -> Script executed successfully")
     else:
         raise Exception("Executed script exit status not 0")
Esempio n. 22
0
 def start(self):
     STREAM.info("==> Starting VirtualMachine...")
     if self.check_vm_status():
         STREAM.info(" -> VirtualMachine is already booted")
         return
     process = Popen("VBoxManage startvm %s --type headless" % self.vm_name,
                     shell=True,
                     stdout=PIPE,
                     stderr=PIPE).communicate()
     stderr = process[1]
     if len(stderr) > 0:
         raise Exception(stderr)
     while 1:
         sleep(10)
         rvms = Popen("VBoxManage list runningvms | awk '{print $1}'",
                      shell=True,
                      stdout=PIPE,
                      stderr=PIPE)
         data = rvms.stdout.read()
         if self.vm_name in data:
             break
     STREAM.info(" -> VirtualMachine successfully booted.")
Esempio n. 23
0
 def check_attributes_dependencies(self):
     STREAM.info("==> Checking for keywords required attributes.")
     for vm in self.config_sequence:
         # Set of required attributes for all Keywords used in the VirtualMachine
         req_args = set()
         STREAM.debug("==> VirtualMachine: %s" % vm)
         for action in self.config[vm].actions:
             try:
                 # List of required attributes for the Keyword to work
                 req_attr = self.loaded_keywords[
                     action].REQUIRED_CONFIG_ATTRS
                 # Add required attributes of current action to summary set
                 req_args = set(req_args) | set(req_attr)
             except KeyError:
                 # If action not in executions section, check for aliases
                 if action not in self.executions.keys():
                     # Check aliases actions for required attributes
                     try:
                         aliases = self.config[vm].aliases[action]
                     # Intercept if VirtualMachine have no aliases
                     except KeyError as key:
                         STREAM.error(
                             "The keyword (%s) you use in the configuration file does not exist or is not enabled."
                             % key)
                         STREAM.warning(
                             "You can't use this keyword until you turn it on in .vmaker.ini"
                         )
                         sys.exit(1)
                     # Intercept if VirtualMachine have no aliases
                     except AttributeError:
                         STREAM.error(
                             "The keyword (u'%s') you use in the configuration file does not exist or is not enabled."
                             % action)
                         STREAM.warning(
                             "You can't use this keyword until you turn it on in .vmaker.ini"
                         )
                         sys.exit(1)
                     for act in aliases:
                         req_attr = self.loaded_keywords[
                             act].REQUIRED_CONFIG_ATTRS
                         req_args = set(req_args) | set(req_attr)
         vm_attrs = [
             name for name in dir(self.config[vm])
             if not name.startswith('__')
         ]
         req_args = set(req_args)
         vm_attrs = set(vm_attrs)
         STREAM.debug(" -> [%s] Required attributes for actions: %s" %
                      (vm, req_args))
         STREAM.debug(" -> [%s] VirtualMachines attributes: %s" %
                      (vm, vm_attrs))
         # Attributes comparison
         result = req_args - vm_attrs
         if len(result) > 0:
             STREAM.error(
                 " -> Section <%s> missed required attributes %s." %
                 (vm, list(result)))
             STREAM.error(
                 " -> This causes problems in the operation of some keywords. Check your user configuration file."
             )
             sys.exit()
     STREAM.success(" -> All attributes are present.")
Esempio n. 24
0
 def load_config(self):
     STREAM.info("==> Loading user configuration file...")
     config = ConfigParser()
     config.read(self.CONFIG_FILE)
     aliases, groups, vms, cmds = {}, {}, {}, {}
     # - Generating aliases objects
     STREAM.debug("==> Generating alias objects...")
     for sec in config.sections():
         STREAM.debug(" -> Loading section '%s'" % sec)
         try:
             if config[sec]["type"] == "aliases":
                 STREAM.debug("    [%s] Section seems like alias object" %
                              sec)
                 args = {
                     key: [val.strip() for val in value.split(",")]
                     for key, value in config.items(sec) if key != "type"
                 }
                 STREAM.debug("    [%s] -> Section attributes: %s" %
                              (sec, args))
                 if config.has_option(sec, "group"):
                     STREAM.debug(
                         "    [%s] -> Section have <group> attribute: assigned to group %s"
                         % (sec, str(config[sec]["group"])))
                     aliases[str(config[sec]["group"])] = type(
                         str(config[sec]["group"]), (object, ),
                         {"aliases": args})
                     STREAM.debug(
                         "    [%s] -> Object attributes: %s" %
                         (sec, dir(aliases[str(config[sec]["group"])])))
                 else:
                     STREAM.debug(
                         "    [%s] -> Section don't have <group> attribute: assigned to global context"
                         % sec)
                     aliases["global"] = type("global", (object, ),
                                              {"aliases": args})
                     STREAM.debug("    [%s] -> Object attributes: %s" %
                                  (sec, dir(aliases["global"])))
             else:
                 STREAM.debug(
                     "    [%s] Section doesn't seem like alias object. Passed..."
                     % sec)
         except KeyError as wrong_key:
             STREAM.error(
                 " -> Config Error: Wrong section '%s' Key %s not specified"
                 % (sec, wrong_key))
             sys.exit()
     STREAM.debug("[*] ==> Generated alias objects: %s\n" % aliases)
     # - Generating group objects
     STREAM.debug("==> Generating group objects...")
     for sec in config.sections():
         STREAM.debug(" -> Loading section '%s'" % sec)
         try:
             if config[sec]["type"] == "group":
                 STREAM.debug("    [%s] Section seems like group object" %
                              sec)
                 args = {
                     key: value
                     for key, value in config.items(sec) if key != "type"
                 }
                 STREAM.debug("    [%s] -> Section attributes: %s" %
                              (sec, args))
                 if aliases != {}:
                     STREAM.debug(
                         "    [%s] -> Alias objects detected: object will generated with alias inheritance"
                         % sec)
                     if aliases.get(sec) is None and aliases.get(
                             "global") is None:
                         # => alias null
                         STREAM.debug(
                             "    [%s] -> Group alias: False, Global alias: False -> object will generated without alias inheritance"
                             % sec)
                         groups[sec] = type(str(sec), (object, ), args)
                         STREAM.debug("    [%s] -> Object attrs: %s" %
                                      (sec, groups[sec].aliases))
                     elif aliases.get(sec) is not None and aliases.get(
                             "global") is not None:
                         # => alias group + global
                         STREAM.debug(
                             "    [%s] -> Group alias: True, Global alias: True -> alias group + global alias inheritance"
                             % sec)
                         complex_alias = dict(
                             aliases.get(sec).aliases,
                             **aliases.get("global").aliases)
                         aliases.get(sec).aliases = complex_alias
                         groups[sec] = type(str(sec), (aliases.get(sec), ),
                                            args)
                         STREAM.debug("    [%s] -> Object aliases: %s" %
                                      (sec, groups[sec].aliases))
                     elif aliases.get(sec) is not None:
                         # => alias group
                         STREAM.debug(
                             "    [%s] -> Group alias: True, Global alias: False -> alias group inheritance"
                             % sec)
                         groups[sec] = type(str(sec), (aliases.get(sec), ),
                                            args)
                         STREAM.debug("    [%s] -> Object attrs: %s" %
                                      (sec, groups[sec].aliases))
                     elif aliases.get("global") is not None:
                         # => alias global
                         STREAM.debug(
                             "    [%s] -> Group alias: False, Global alias: True -> global alias inheritance"
                             % sec)
                         groups[sec] = type(str(sec),
                                            (aliases.get("global"), ), args)
                         STREAM.debug("    [%s] -> Object attrs: %s" %
                                      (sec, groups[sec].aliases))
                 else:
                     STREAM.debug(
                         "    [%s] -> Alias objects not detected: object will generated without alias inheritance"
                         % sec)
                     # => alias null
                     groups[sec] = type(str(sec), (object, ), args)
             else:
                 STREAM.debug(
                     "    [%s] Section doesn't seem like group object. Passed..."
                     % sec)
         except KeyError as wrong_key:
             STREAM.error(
                 " -> Config Error: Wrong section '%s' Key '%s' not specified"
                 % (sec, wrong_key))
             sys.exit()
     STREAM.debug("[*] ==> Generated group objects: %s\n" % groups)
     # - Generating VM objects
     STREAM.debug("==> Generating vm objects...")
     vms_work_sequence = []
     for sec in config.sections():
         STREAM.debug(" -> Loading section '%s'" % sec)
         try:
             if config[sec]["type"] == "vm":
                 STREAM.debug("    [%s] Section seems like vm object" % sec)
                 args = {
                     key: value
                     for key, value in config.items(sec) if key != "type"
                     and key != "group" and key != "actions"
                 }
                 STREAM.debug("    [%s] -> Section attributes: %s" %
                              (sec, args))
                 # firstly check if vm section exists action attr
                 # then below check maybe it inherit from group
                 try:
                     act = config[sec]["actions"]
                     args["actions"] = act
                 except KeyError:
                     pass
                 # alias inheritance added in group generation step
                 if config.has_option(sec, "group") and groups.get(
                         config[sec]["group"]) is not None:
                     STREAM.debug(
                         "    [%s] Assigned group detected: inherit attributes "
                         "from group '%s'" % (sec, config[sec]["group"]))
                     vms[sec] = type(str(sec),
                                     (groups.get(config[sec]["group"]), ),
                                     args)
                 else:
                     # if group doesn't exist or no group, adding alias inheritance
                     STREAM.debug(
                         "    [%s] Assigned group not detected: assign aliases"
                         % sec)
                     if aliases.get("global") is None:
                         STREAM.debug(
                             "    [%s] Aliases not assigned: no aliases" %
                             sec)
                         # => alias null
                         vms[sec] = type(str(sec), (), args)
                     else:
                         STREAM.debug("    [%s] Aliases assigned: global" %
                                      sec)
                         # => alias global
                         vms[sec] = type(str(sec),
                                         (aliases.get("global"), ), args)
                 # Check if 'action' attr was inherited from group
                 try:
                     acts = getattr(vms[sec], "actions")
                     setattr(vms[sec], "actions",
                             [action.strip() for action in acts.split(",")])
                     retro = "    [%s] Section inheritance retrospective:"
                     final_attrs = {
                         attr
                         for attr in dir(vms[sec])
                         if not attr.startswith('__')
                     }
                     for attr in final_attrs:
                         val = getattr(vms[sec], attr)
                         retro += "\n\t\t\t\t\t\t%s = %s" % (attr, val)
                     STREAM.debug(retro % sec)
                     vms_work_sequence.append(sec)
                 except AttributeError as wrong_key:
                     STREAM.error(" -> Config Error: Wrong section '%s'"
                                  " Key %s not specified" %
                                  (sec, str(wrong_key).split(" ")[-1]))
                     del vms[sec]
                     sys.exit()
             else:
                 STREAM.debug(
                     "    [%s] Section doesn't seem like vm object. Passed..."
                     % sec)
         except KeyError as wrong_key:
             STREAM.error(
                 " -> Config Error: Wrong section '%s' Key '%s' not specified"
                 % (sec, wrong_key))
             sys.exit()
     STREAM.debug("[*] ==> Generated vm objects: %s" % vms)
     STREAM.debug("[*] ==> Generated vm objects work sequence: %s" %
                  vms_work_sequence)
     STREAM.debug("==> Finding sections with executions...")
     for sec in config.sections():
         try:
             if config[sec]["type"] == "executions":
                 STREAM.debug(" -> Found section '%s'" % sec)
                 args = {
                     key: value
                     for key, value in config.items(sec) if key != "type"
                 }
                 cmds = dict(cmds, **args)
         except KeyError as wrong_key:
             STREAM.error(
                 " -> Config Error: Wrong section '%s' Key '%s' not specified"
                 % (sec, wrong_key))
             sys.exit()
     STREAM.debug("[*] ==> Found executions aliases: %s" % cmds)
     STREAM.success(" -> User configuration file loaded")
     return vms, vms_work_sequence, cmds
Esempio n. 25
0
 def create_snapshot(self):
     STREAM.info("==> Create a snapshot with name: '%s'" % self.snapshot_name)
     result = Popen('VBoxManage snapshot %s take %s' % (self.vm_name, self.snapshot_name),
                    shell=True, stdout=PIPE, stderr=PIPE).communicate()
     STREAM.debug(result)
     STREAM.success(" -> Snapshot created")
Esempio n. 26
0
 def export_vm_configuration(self):
     """ Method to export VirtualMachine configuration from Virtual Vox """
     STREAM.info("==> Checking if vm exists...")
     vms = Popen("vboxmanage list vms |awk '{print $1}'",
                 shell=True,
                 stdout=PIPE,
                 stderr=PIPE).communicate()
     vms = vms[0]
     if not self.vm_name in vms:
         STREAM.error(
             " -> VirtualMachine doesn't exist!\nMake sure that the VirtualMachine"
             " you specified in the parameter(vm_name) are exists.")
         return False
     STREAM.success(" -> Exists: True")
     STREAM.info("==> Exporting configuration...")
     STREAM.debug(" -> vagrant catalog directory: %s" %
                  self.vagrant_catalog)
     if not os.path.exists(self.vagrant_catalog):
         STREAM.error(
             " -> Vagrant catalog (%s) does not exist!\nMake sure that the catalog"
             " you specified in the parameter(vagrant_catalog) are exists."
             % self.vagrant_catalog)
         STREAM.warning(" -> Export in vagrant, passed.")
         return False
     self.work_dir = os.path.join(self.vagrant_catalog, self.vm_name)
     self.tmp_dir = os.path.join(self.vagrant_catalog, self.vm_name, "tmp")
     try:
         os.makedirs(self.tmp_dir)
     except OSError as errno:
         if "Errno 17" in str(errno):
             STREAM.debug(
                 "==> Temporary directory detected, cleaning before start..."
             )
             shutil.rmtree(self.tmp_dir)
             STREAM.debug(" -> Removed: %s" % self.tmp_dir)
             os.makedirs(self.tmp_dir)
         else:
             STREAM.error(errno)
             return False
     result = Popen(
         'VBoxManage export %s --output %s' %
         (self.vm_name, os.path.join(self.tmp_dir, self.vm_name + ".ovf")),
         shell=True,
         stdout=PIPE,
         stderr=PIPE).communicate()
     if len(result[1]) > 0:
         if "0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%" not in result[
                 1]:
             raise Exception(result[1])
     diskname = ""
     for fil in os.listdir(self.tmp_dir):
         if fil.endswith(".vmdk"):
             diskname = fil
             os.rename(os.path.join(self.tmp_dir, fil),
                       os.path.join(self.tmp_dir, "box-disk.vmdk"))
         elif fil.endswith(".ovf"):
             os.rename(os.path.join(self.tmp_dir, fil),
                       os.path.join(self.tmp_dir, "box.ovf"))
     with open(os.path.join(self.tmp_dir, "box.ovf"), "r") as ovf:
         ovf_file = ovf.read()
     with open(os.path.join(self.tmp_dir, "box.ovf"), "w") as ovf:
         ovf.write(ovf_file.replace(diskname, "box-disk.vmdk"))
     return True
Esempio n. 27
0
    def generate_default_config(config_file):
        template = """; You can create vm objects and assign them any actions.
; Specify preffered section name.
[debian9-x86-template]
; Mandatory keys.
;   Key specifies, which type of object will be created (vm, group, alias).
type = vm
;   Key specifies Keywords which will be performed for this VirtualMachine
actions = port_forwarding, vbox_start, execute_command, vbox_stop
; Variable keys
;   Key specifies to which group this object belongs.
group = linux
; You may specify email to receive notifications about Keyword's errors.
;alert = [email protected]
; That description will be shown in subject of the email
;alert_description = install curl in vm
; Attributes needed for the correct work of a Keyword's
; name of the virtual machine in VirtualBox.
vm_name = debian9-x86-template
; Command which will be executed in VirtualMachine by Keyword "execute_command"
execute_command = apt-get install -y clamav

[fedora27-amd64]
type = vm
; actions will be inherited from group
group = linux
vm_name = fedora27-amd64
execute_command = dnf install -y clamav
 
[freebsd10-amd64]
type = vm
group = linux
vm_name = freebsd10-amd64
execute_command = pkg install -y clamav

; You can create groups and combine it with other objects.
;   Groups support attribute inheritance (groups attributes have a lower priority than vm attributes).
;   Specify name of the group.
[linux]
; Mandatory key.
type = group
;   Key specifies keywords which will be performed for the group of VirtualMachines.
actions = port_forwarding, vbox_start, execute_command, vbox_stop
; You can specify a timeout for each Keyword after which the process will be terminated (ex: <Keyword_name>_timeout)
execute_command_timeout = 10

; You can combine some Keywords in one action, named alias.
[linux_aliases]
type = alias
; By default aliases extends to all objects, but you can assign aliases at specific group
;group = linux
reboot = vbox_stop, vbox_start
"""
        STREAM.info("==> Generating default configuration file...")
        if os.path.exists(config_file):
            STREAM.warning(" -> File %s already exists!" % config_file)
            STREAM.warning(" -> Do you want to overwrite it? (y/n): ")
            answers = ["y", "n"]
            while 1:
                choice = raw_input().lower()
                if choice in answers:
                    break
                STREAM.error("Choose y or n! : ")
            if choice == answers[0]:
                with open(config_file, "w") as config:
                    config.write(template)
                STREAM.success(" -> Generated %s" % config_file)
            else:
                STREAM.notice(" -> Cancelled by user.")
                sys.exit()
        else:
            with open(config_file, "w") as config:
                config.write(template)
            STREAM.success(" -> Generated %s" % config_file)
Esempio n. 28
0
 def main(self):
     """ Mandatory method, invoked by vmaker Core process, which represents an entrypoint of the keyword. """
     STREAM.info("keyword's start.")