def os_type(self,display=True): """ Show OS type ( LFS, RedHat CentOS ... ) [display] True by default """ if util._is_host_up(env.host, int(env.port)) is False: return False OS = util._run("bash -c 'head -n 1 /etc/{{system,redhat,centos}-release,release} 2>/dev/null' | sort -u | grep -v '^==>'") if re.match("LC",OS,flags=re.IGNORECASE): if display: print env.host + "| LFS" return "lfs" if re.match("Red Hat",OS,flags=re.IGNORECASE): if display: print env.host + "| redhat" return "redhat" if re.match("Centos",OS,flags=re.IGNORECASE): if display: print env.host + "| centos" return "centos" if re.match("Solaris",OS,flags=re.IGNORECASE): if display: print env.host + "| solaris" return "solaris" else: if display: print env.host + "| UNKNOWN_OS" return "UNKNOWN_OS"
def arch(self): """ Show architecture x86_64, i386 ... """ if not util._is_host_up(env.host, int(env.port)): return False print env.host + "|" + util._run("uname --hardware-platform")
def uptime(self): """ Show uptime of the host """ if not util._is_host_up(env.host, int(env.port)): return False print env.host + "|" + util._run('uptime')
def id(self): """ Get the current id of the user on remote host """ id = util._run("id") print env.host+"|"+id return id
def kernel_version(self): """ Display the kernel version running on the host """ if util._is_host_up(env.host, int(env.port)) is False: return False k_version = util._run('uname -r') print env.host + "|" + k_version
def _fabrun(self,*args): puts("FABRUN SUDO ="+str(self.needSudo)) if self.needSudo: puts("Using sudo for "+str(args)) return util._sudo(*args) else: puts("Using run for "+str(args)) return util._run(*args)
def hostname(self): """ Return configured hostname """ if not util._is_host_up(env.host, int(env.port)): return False result = util._run('hostname -f') if result.succeeded: print env.host + "|" + result return result.succeeded
def date(self): """ Show date of the host """ if not util._is_host_up(env.host, int(env.port)): return False result = util._run('date') if result.succeeded: print env.host + "|" + result return result.succeeded
def check_tcp(self,rhost,rport,timeout=5): """ Test <rhost> <rport> tcp connectivity from host using python with [timeout] (default: 5s) """ if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = env.host puts("Proxy %s port %s " % (rhost,rport)) tcp_check = util._run("python -c \"import socket;sock=socket.socket();sock.settimeout("+str(timeout)+");sock.connect(('"+rhost+"',"+rport+"));sock.close()\"") if tcp_check.succeeded: print HOSTNAME + "|" + rhost + "|" + rport + "|" + green("Success") else: print HOSTNAME + "|" + rhost + "|" + rport + "|" + red("Failed",True) return tcp_check.succeeded
def version_rpm(self,name): """ Display rpm package <name> version """ if util._is_host_up(env.host, int(env.port)) is False: return False #rpms = util._run('rpm -qa | egrep \"'+name+'\"') os_version = self.os_release(display=False) rpms = util._run('rpm -q '+name+' --queryformat "%{NAME}|%{VERSION}-%{RELEASE}.%{ARCH}|%{INSTALLTIME:date}\n"') rpms_list = rpms.splitlines() if not self.first.has_key(util._func_name()): self.first[util._func_name()] = True util._print_task_header("HOSTNAME|OS_RELEASE|PACKAGE_NAME|VERSION|INSTALL_DATE") for rpm in rpms_list: print env.host + "|" + os_version + "|" + rpm
def os_short(self): """ Show truncated OS version ( LFS, RedHat CentOS ... ) """ if util._is_host_up(env.host, int(env.port)) is False: return False OS = util._run("bash -c 'head -n 1 /etc/{{system,redhat,centos}-release,release} 2>/dev/null' | sort -u | grep -v '^==>'") if re.match("LC",OS,flags=re.IGNORECASE): print env.host + "|LFS " + OS if re.match("Red Hat",OS,flags=re.IGNORECASE): print env.host + "|" + OS if re.match("Centos",OS,flags=re.IGNORECASE): print env.host + "|" + OS if re.match("Solaris",OS,flags=re.IGNORECASE): print env.host + "|" + OS else: print env.host + "|UNKNOW_OS" + OS
def os_release(self,display=True): """ Show complete OS release ( LFS, RedHat ... ) """ if util._is_host_up(env.host, int(env.port)) is False: return False OS = util._run("bash -c 'head -n 1 /etc/{{system,redhat,centos}-release,release} 2>/dev/null' | sort -u | grep -v '^==>'") OS_list = OS.splitlines() OS_hash = {} for entry in OS_list: OS_hash[entry]=1 OS = "" for os in OS_hash.iterkeys(): OS+=os+' ' if display: print env.host + "|" + OS else: return OS
def check_tcp(self, rhost, rport, timeout=5): """ Test <rhost> <rport> tcp connectivity from host using python with [timeout] (default: 5s) """ if util._is_host_up(env.host, int(env.port)) is False: return False HOSTNAME = env.host puts("Proxy %s port %s " % (rhost, rport)) tcp_check = util._run( "python -c \"import socket;sock=socket.socket();sock.settimeout(" + str(timeout) + ");sock.connect(('" + rhost + "'," + rport + "));sock.close()\"") if tcp_check.succeeded: print HOSTNAME + "|" + rhost + "|" + rport + "|" + green("Success") else: print HOSTNAME + "|" + rhost + "|" + rport + "|" + red( "Failed", True) return tcp_check.succeeded
def run(image, cmd=None, daemon=False, exposeports=True): ''' Equivalent to `docker run` but a bit simplified ''' basecmd = ['docker', 'run', image] if cmd is None: # TODO add default args to `docker run` pass else: basecmd.extend(cmd) if exposeports: basecmd.insert(2, '-P') if daemon: basecmd.insert(2, '-d') # TODO add expose ports functionality if _run(basecmd) != 0: raise Exception('Failed `%s`' % str(basecmd))
def _fabrun(self, *args): if self.needSudo: return util._sudo(*args) else: return util._run(*args)
def init_repo(self): if not self.is_repo(): cmd = ['git', 'init'] if _run(cmd, cwd=self.localpath) != 0: raise Exception('Failed to init repo @ %s' % self.localpath)
def is_repo(self): cmd = ['git', 'status'] return 0 == _run(cmd, cwd=self.localpath)
def clone(self, url, cwd=None): cmd = ['git', 'clone', url] if cwd is not None: cmd.append(cwd) if _run(cmd, cwd=self.localpath) != 0: raise Exception(''.join(['Failed to clone repo ', url]))
def delete_branch(self, branchname): if branchname == self.current_branch(): raise Exception('Cannot delete branch %s because you are currently on that branch' % branchname) cmd = ['git', 'branch', '-D', branchname] if _run(cmd, cwd=self.localpath) != 0: raise Exception(''.join(['Failed to delete branch ', branchname, '@', self.localpath]))
def fetch(self, url, ref): cmd = ['git', 'fetch', url, ref] if _run(cmd, cwd=self.localpath) != 0: raise Exception(''.join(['Failed to fetch ref ', ref, '@', url]))
def new_branch(self, name): cmd = ['git', 'checkout', '-b', name] if _run(cmd, cwd=self.localpath) != 0: raise Exception(''.join(['Failed to checkout branch ', name, '@', self.localpath]))
def checkout(self, branch): cmd = ['git', 'checkout', branch] if _run(cmd, cwd=self.localpath) != 0: raise Exception(''.join(['Failed to checkout branch ', branch, '@', self.localpath]))
def _fabrun(self,*args): if self.needSudo: return util._sudo(*args) else: return util._run(*args)