-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·125 lines (87 loc) · 3.04 KB
/
Copy pathserver.py
File metadata and controls
executable file
·125 lines (87 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python2
# coding: utf-8
import re
import socket
from collections import defaultdict
import psutil
from pykit import fsutil
from pykit import net
from pykit import strutil
from .server_status import ServerStatus
from .drive_status import DriveStatus
def _make_mountpoints_info():
mps = fsutil.get_all_mountpoint()
prt_by_mp = fsutil.get_disk_partitions()
mps_info = defaultdict(dict)
for m in mps:
p = prt_by_mp.get(m, None)
if p is None:
continue
capacity = fsutil.get_path_usage(m)['total']
mps_info[m]['fs'] = p['fstype']
mps_info[m]['capacity'] = capacity
return dict(mps_info)
def _get_allocated_drive(allocated_drive_pre, mountpoints):
rst = {}
max_mount_idx = 0
for m in mountpoints:
if not m.startswith(allocated_drive_pre):
continue
rst[m] = {'status': DriveStatus.NORMAL}
idx = int(m.split('/')[-1])
max_mount_idx = max_mount_idx if max_mount_idx > idx else idx
return rst, max_mount_idx
def make_serverrec(server_id, idc, idc_type, roles, allocated_drive_pre, **argkv):
serverrec = {}
ips = net.get_host_ip4(exclude_prefix="docker")
inn_ips = net.choose_inn(ips)
pub_ips = net.choose_pub(ips)
memory = psutil.virtual_memory().total
cpu_info = {}
# count of logical cpus
cpu_info['count'] = psutil.cpu_count()
# Mhz
if hasattr(psutil, 'cpu_freq'):
cpu_info['frequency'] = psutil.cpu_freq().max
serverrec['server_id'] = server_id
serverrec['pub_ips'] = pub_ips
serverrec['inn_ips'] = inn_ips
serverrec['hostname'] = socket.gethostname()
serverrec['memory'] = memory
serverrec['cpu'] = cpu_info
serverrec['idc'] = idc
serverrec['idc_type'] = idc_type
serverrec['roles'] = roles
mps = _make_mountpoints_info()
serverrec['mountpoints'] = mps
allocated_drive, max_mount_idx = _get_allocated_drive(allocated_drive_pre, mps)
serverrec['next_mount_index'] = max_mount_idx + 1
serverrec['allocated_drive'] = allocated_drive
serverrec['status'] = ServerStatus.def_status()
serverrec.update(argkv)
return serverrec
def get_serverrec_str(serverrec):
rst = []
for k in ('server_id', 'idc', 'idc_type', 'roles'):
rst.append('{k}: {v}'.format(k=k, v=serverrec[k]))
rst.append('mountpoints_count: {cnt}'.format(
cnt=len(serverrec['mountpoints'])))
rst.append('allocated_drive_count: {cnt}'.format(
cnt=len(serverrec['allocated_drive'])))
return '; '.join(rst)
def validate_idc(idc):
# deprecated, use IDCID(idc)
if not isinstance(idc, basestring):
return False
if len(idc) > 0 and not idc.startswith('.'):
return False
names = idc.split('.')
for n in names:
if re.match("^[0-9a-zA-Z]*$", n) is None:
return False
return True
def idc_distance(idc_a, idc_b):
if idc_a == idc_b:
return 0
pre_cnt = len(strutil.common_prefix(idc_a.split('.'), idc_b.split('.')))
return 32 / (2 ** pre_cnt)