def main():
    args = get_args()
    conn_params = get_connection_params(args)
    with SmartConnection(**conn_params) as si:
        vm = get_vm_by_uuid(si, args.uuid)
        datastore = get_datastore_by_name(si, args.datastore)
        migrate_vm_to_datastore(vm, datastore)
Example #2
0
 def _list(self):
     """List virtual machines on vSphere host"""
     ret = []
     with SmartConnection(**self.connect_opts) as conn:
         for vm in self._get_virtual_machines(conn):
             ret.append(vm.summary.config.name)
     return ret
Example #3
0
    def _list(self):
        """List virtual machines on vSphere host"""
        with SmartConnection(**self.connect_opts) as conn:
            vmlist = [vm.summary.config.name for vm in
                      self._get_virtual_machines(conn)]

        return vmlist
def main():
    args = get_args()
    conn_params = get_connection_params(args)
    with SmartConnection(**conn_params) as si:
        api = VmwareApi(si,
                        datacenter_name=args.datacenter,
                        cluster_name=args.cluster)
        provision_vm(api, args)
Example #5
0
def main():
    args = get_args()
    conn_params = get_connection_params(args)
    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGHUP, signal_handler)
    with SmartConnection(**conn_params) as si:
        api = VmwareApi(si,
                        datacenter_name=args.datacenter,
                        cluster_name=args.cluster)
        provision_vm(api, args)
Example #6
0
 def stop(self, label):
     """Stop a machine.
     @param label: machine name.
     @raise CuckooMachineError: if unable to stop machine
     """
     with SmartConnection(**self.connect_opts) as conn:
         vm = self._get_virtual_machine_by_label(conn, label)
         if vm:
             self._stop_virtual_machine(vm)
         else:
             raise CuckooMachineError(f"Machine {label} not found on host")
Example #7
0
 def start(self, label):
     """Start a machine.
     @param label: machine name.
     @raise CuckooMachineError: if unable to start machine.
     """
     name = self.db.view_machine_by_label(label).snapshot
     with SmartConnection(**self.connect_opts) as conn:
         vm = self._get_virtual_machine_by_label(conn, label)
         if vm:
             self._revert_snapshot(vm, name)
         else:
             raise CuckooMachineError(f"Machine {label} not found on host")
Example #8
0
 def stop(self, label):
     """Stop a machine.
     @param label: machine name.
     @raise CuckooMachineError: if unable to stop machine
     """
     with SmartConnection(**self.connect_opts) as conn:
         vm = self._get_virtual_machine_by_label(conn, label)
         if vm:
             self._stop_virtual_machine(vm)
             self.set_status(label, vm.runtime.powerState)
         else:
             raise CuckooMachineError("Machine %s not found on host" %
                                      label)
Example #9
0
    def _status(self, label):
        """Get power state of vm from vSphere host.
        @param label: virtual machine name
        @raise CuckooMachineError: if error getting status or machine not found
        """
        with SmartConnection(**self.connect_opts) as conn:
            vm = self._get_virtual_machine_by_label(conn, label)
            if not vm:
                raise CuckooMachineError(
                    "Machine {0} not found on server".format(label))

            status = vm.runtime.powerState
            self.set_status(label, status)
            return status
Example #10
0
 def dump_memory(self, label, path):
     """Take a memory dump of a machine.
     @param path: path to where to store the memory dump
     @raise CuckooMachineError: if error taking the memory dump
     """
     name = f"cuckoo_memdump_{random.randint(100000, 999999)}"
     with SmartConnection(**self.connect_opts) as conn:
         vm = self._get_virtual_machine_by_label(conn, label)
         if vm:
             self._create_snapshot(vm, name)
             self._download_snapshot(conn, vm, name, path)
             self._delete_snapshot(vm, name)
         else:
             raise CuckooMachineError(f"Machine {label} not found on host")
Example #11
0
 def start(self, label, task, revert=True):
     """Start a machine.
     @param label: machine name.
     @param task: task object.
     @param revert: Revert machine to snapshot
     @raise CuckooMachineError: if unable to start machine.
     """
     name = self.db.view_machine_by_label(label).snapshot
     with SmartConnection(**self.connect_opts) as conn:
         vm = self._get_virtual_machine_by_label(conn, label)
         if vm:
             self._revert_snapshot(vm, name)
         else:
             raise CuckooMachineError("Machine %s not found on host" %
                                      label)
Example #12
0
    def _vsphere_connection(self):
        """ContextManager for connecting/authenticating to vSphere"""

        password = self.get_option('password')
        try:
            password = self._compose(password)
        except AnsibleError:
            display.debug("... vSphere password is not templated")

        opts = dict(
            host=self.get_option('hostname'),
            user=self.get_option('username'),
            pwd=password,
        )

        return SmartConnection(**opts)
def main():
    args = get_args()
    print('Report start: %s' % time.ctime())

    with SmartConnection(host=args.vc_host,
                         user=args.vc_user,
                         pwd=args.vc_pass,
                         port=args.vc_port) as service_instance:
        content = service_instance.RetrieveContent()

        dvs = get_dvs_by_uuid(content, args.dvs_uuid)
        dvs_ports = get_dvs_ports(dvs)
        vms_filter_spec = get_filter_spec(dvs, vim.DistributedVirtualSwitch,
                                          'summary.vm', vim.VirtualMachine,
                                          ['config.instanceUuid', 'network'])
        vm_ref_to_props = get_mo_ref_to_props(content, vms_filter_spec)

        pgs_filter_spec = get_filter_spec(dvs, vim.DistributedVirtualSwitch,
                                          'portgroup',
                                          vim.dvs.DistributedVirtualPortgroup,
                                          ['name', 'key'])
        pg_ref_to_props = get_mo_ref_to_props(content, pgs_filter_spec)

        port_reports.report_dvs_port_name_duplications(dvs_ports)

        with os_connection.Connection(cloud='envvars') as os_conn:
            os_ports = os_conn.list_ports(
                {'binding:host_id': args.os_compute_host})
            os_ports = [
                p for p in os_ports if p.get('binding:vif_type') == 'dvs'
            ]
            port_reports.report_connectee_consistency(dvs_ports, os_ports,
                                                      vm_ref_to_props)
            port_reports.report_pg_sg_consistency(args.dvs_uuid, os_ports,
                                                  pg_ref_to_props,
                                                  vm_ref_to_props)
            port_reports.report_port_mapping(dvs_ports, os_ports)

            if args.align_vc:
                port_resolver.align_vc_with_os(args.dvs_uuid, dvs_ports,
                                               os_ports, pg_ref_to_props,
                                               vm_ref_to_props, dvs,
                                               service_instance)

    print('\nReport end: %s' % time.ctime())
Example #14
0
    def _initialize_check(self):
        """Runs checks against virtualization software when a machine manager
        is initialized.
        @raise CuckooCriticalError: if a misconfiguration or unsupported state
                                    is found.
        """
        self.connect_opts = {}

        if self.options.vsphere.host:
            self.connect_opts["host"] = self.options.vsphere.host
        else:
            raise CuckooCriticalError(
                "vSphere host address setting not found, "
                "please add it to the config file.")

        if self.options.vsphere.port:
            self.connect_opts["port"] = self.options.vsphere.port
        else:
            raise CuckooCriticalError("vSphere port setting not found, "
                                      "please add it to the config file.")

        if self.options.vsphere.user:
            self.connect_opts["user"] = self.options.vsphere.user
        else:
            raise CuckooCriticalError("vSphere username setting not found, "
                                      "please add it to the config file.")

        if self.options.vsphere.pwd:
            self.connect_opts["pwd"] = self.options.vsphere.pwd
        else:
            raise CuckooCriticalError("vSphere password setting not found, "
                                      "please add it to the config file.")

        # Workaround for PEP-0476 issues in recent Python versions
        if self.options.vsphere.unverified_ssl:
            import ssl

            sslContext = ssl._create_unverified_context()
            self.connect_opts["sslContext"] = sslContext
            log.warn("Turning off SSL certificate verification!")

        # Check that a snapshot is configured for each machine
        # and that it was taken in a powered-on state
        try:
            with SmartConnection(**self.connect_opts) as conn:
                for machine in self.machines():
                    if not machine.snapshot:
                        raise CuckooCriticalError(
                            "Snapshot name not specified "
                            "for machine {0}, please add "
                            "it to the config file.".format(machine.label))
                    vm = self._get_virtual_machine_by_label(
                        conn, machine.label)
                    if not vm:
                        raise CuckooCriticalError(
                            "Unable to find machine {0} "
                            "on vSphere host, please "
                            "update your configuration.".format(machine.label))
                    state = self._get_snapshot_power_state(
                        vm, machine.snapshot)
                    if not state:
                        raise CuckooCriticalError(
                            "Unable to find snapshot {0} "
                            "for machine {1}, please "
                            "update your configuration.".format(
                                machine.snapshot, machine.label))
                    if state != self.RUNNING:
                        raise CuckooCriticalError(
                            "Snapshot for machine {0} not "
                            "in powered-on state, please "
                            "create one.".format(machine.label))

        except CuckooCriticalError:
            raise

        except Exception as e:
            raise CuckooCriticalError("Couldn't connect to vSphere host")

        super(vSphere, self)._initialize_check()
Example #15
0
    def _initialize_check(self):
        """Runs checks against virtualization software when a machine manager
        is initialized.
        @raise CuckooCriticalError: if a misconfiguration or unsupported state
                                    is found.
        """
        self.connect_opts = {}

        if self.options.vsphere.host:
            self.connect_opts["host"] = self.options.vsphere.host
        else:
            raise CuckooCriticalError(
                "vSphere host address setting not found, "
                "please add it to the config file.")

        if self.options.vsphere.port:
            self.connect_opts["port"] = self.options.vsphere.port
        else:
            raise CuckooCriticalError("vSphere port setting not found, "
                                      "please add it to the config file.")

        if self.options.vsphere.user:
            self.connect_opts["user"] = self.options.vsphere.user
        else:
            raise CuckooCriticalError("vSphere username setting not found, "
                                      "please add it to the config file.")

        if self.options.vsphere.pwd:
            self.connect_opts["pwd"] = self.options.vsphere.pwd
        else:
            raise CuckooCriticalError("vSphere password setting not found, "
                                      "please add it to the config file.")

        # Verify that connect options are valid
        try:
            with SmartConnection(**self.connect_opts):
                pass
        except Exception as e:
            raise CuckooCriticalError(
                "Couldn't connect to vSphere host: {0}".format(e))

        # Check that a snapshot is configured for each machine
        # and that it was taken in a powered-on state
        with SmartConnection(**self.connect_opts) as conn:
            for machine in self.machines():
                if not machine.snapshot:
                    raise CuckooCriticalError("Snapshot name not specified "
                                              "for machine {0}, please add "
                                              "it to the config file.".format(
                                                  machine.label))
                vm = self._get_virtual_machine_by_label(conn, machine.label)
                if not vm:
                    raise CuckooCriticalError(
                        "Unable to find machine {0} "
                        "on vSphere host, please "
                        "update your configuration.".format(machine.label))
                state = self._get_snapshot_power_state(vm, machine.snapshot)
                if state != self.RUNNING:
                    raise CuckooCriticalError("Snapshot for machine {0} not "
                                              "in powered-on state, please "
                                              "create one.".format(
                                                  machine.label))

        super(vSphere, self)._initialize_check()