def send_files(self, src_dir, dst_dir, filestocopy): with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: self._run_vm(vm, "login_in_guest", self.user, self.passwd) memo = [] for filetocopy in filestocopy: d, f = filetocopy.split("/") src = "%s/%s/%s" % (src_dir, d, f) if d == ".": dst = "%s\\%s" % (dst_dir, f) else: dst = "%s\\%s\\%s" % (dst_dir, d, f) rdir = "%s\\%s" % (dst_dir, d) if not rdir in memo: logging.debug("making directory %s " % (rdir)) self._run_vm(vm, "make_directory", rdir) memo.append(rdir) try: logging.debug("copy %s -> %s" % (src, dst)) self._run_vm(vm, "send_file", src, dst) except: logging.debug("resending file %s -> %s" % (src, dst)) self._run_vm(vm, "send_file", src, dst)
def list_directory(self, dir_path): with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: try: self._run_vm(vm, "login_in_guest", self.user, self.passwd) return self._run_vm(vm, "list_files", dir_path) # except VIApiException: # logging.error("path %s is not found on vm %s") % (dir_path, vm) # raise Exception("path %s is not found on vm %s" % (dir_path, vm)) except Exception as e: print "EXCEPTION %s" % e return None
def _run_cmd(self, func, *params): try: with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: f = getattr(vm, func) if len(params) is None: return f else: return f(*params) except Exception as e: logging.error("%s, ERROR: Problem running %s. Reason: %s" % (self.name, func, e)) raise
def _run_task(self, func, *params): def wait_for(task): s = task.wait_for_state(['success', 'error']) if s == 'error': logging.error("ERROR: problem with task %s: %s" % (func, task.get_error_message())) return False return True try: with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: f = getattr(vm, func) if len(params) is None: task = f(sync_run=False) else: task = f(sync_run=False, *params) return wait_for(task) except Exception as e: logging.error("%s, ERROR: Problem running %s. Reason: %s" % (self.name, func, e)) raise
def get_file(self, src_file, dst_file): with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: self._run_vm(vm, "login_in_guest", self.user, self.passwd) return self._run_vm(vm, "get_file", src_file, dst_file)
def make_directory(self, dst_dir): with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: self._run_vm(vm, "login_in_guest", self.user, self.passwd) return self._run_vm(vm, "make_directory", dst_dir)