def sync_node_time(cluster): hosts = C_Host.objects.filter( Q(project_id=cluster.id) & ~Q(name='localhost') & ~Q(name='127.0.0.1') & ~Q(name='::1')) data = [] times = [] result = {'success': True, 'data': []} for host in hosts: ssh_config = SshConfig(host=host.ip, port=host.port, username=host.username, password=host.password, private_key=None) ssh_client = SSHClient(ssh_config) res = ssh_client.run_cmd('date') gmt_date = res[0] GMT_FORMAT = '%a %b %d %H:%M:%S CST %Y' date = time.strptime(gmt_date, GMT_FORMAT) timeStamp = int(time.mktime(date)) times.append(timeStamp) show_time = time.strftime('%Y-%m-%d %H:%M:%S', date) time_data = { 'hostname': host.name, 'date': show_time, } data.append(time_data) result['data'] = data max = builtins.max(times) min = builtins.min(times) # 如果最大值减最小值超过5分钟 则判断有错 if (max - min) > 300000: result['success'] = False return result
def is_host_connected(ip, port, credential): config = SshConfig(host=ip, username=credential.username, password=credential.password, private_key=credential.private_key, port=port) client = SSHClient(config) return client.ping()
def test_ssh(ssh_config): result = True client = SSHClient(ssh_config) try: client.ping() except Exception as e: result = False return result, e return result, None
def gather_auth_token(self): cmd = "kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep tiller | awk '{print $1}') | grep token: | awk '{print $2}'" master = self.cluster.group_set.get(name='master').hosts.first() ssh_config = parse_host_to_ssh_config(master) ssh_client = SSHClient(ssh_config) connected = ssh_client.ping() if not connected: raise Exception("ssh connect error!") else: out, code = ssh_client.run_cmd(cmd) if code == 0: self.cache_auth_token(out) return out else: raise Exception('exec cmd error:' + out)
def get_gpu_device(ssh_config): client = SSHClient(ssh_config) cmd = "lspci | grep -i nvidia" result, code = client.run_cmd(cmd) if code == 0: return result