forked from InfraSIM/obsoleted-vpduserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·793 lines (681 loc) · 26.7 KB
/
server.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#!/usr/bin/python
'''
*********************************************************
Copyright @ 2015 EMC Corporation All Rights Reserved
*********************************************************
'''
import SocketServer
import time
import sys
import os
import signal
import subprocess
import socket
import struct
import fcntl
import ctypes
import math
import shutil
import getopt
install_data_dir = [
os.path.join(os.environ['HOME'], '.pdusim'),
os.path.join(sys.prefix, 'pdusim'),
os.path.join(sys.prefix, 'share', 'pdusim'),
os.path.join(os.path.split(__file__)[0], 'pdusim'),
os.path.dirname(os.path.abspath(__file__))
]
for dir in install_data_dir:
path = os.path.join(dir, 'third-party')
if os.path.exists(path):
for d in os.listdir(path):
sys.path.insert(0, os.path.join(path, d))
from texttable import Texttable, get_color_string, bcolors
import pdusim.password as password
import pdusim.reportip
from pdusim.sss import SNMPSimService
import pdusim.common.logger as logger
from pdusim.common.colors import bcolors as colors
from pdusim.common.sshsrv import SSHHandler, command
import pdusim.common.config as config
import pdusim.common.daemon
import pdusim.mapping_file as mapping_file
server_pid_file = "/var/run/vpdud/server.pid"
SIOCGIFINDEX = 0x8933
SIOCGIFFLAGS = 0x8913
SIOCSIFFLAGS = 0x8914
SIOCGIFHWADDR = 0x8927
SIOCSIFHWADDR = 0x8924
SIOCGIFADDR = 0x8915
SIOCSIFADDR = 0x8916
SIOCGIFNETMASK = 0x891B
SIOCSIFNETMASK = 0x891C
SIOCETHTOOL = 0x8946
config_home_dir = ""
# From linux/if.h
IFF_UP = 0x1
# From linux/socket.h
AF_UNIX = 1
AF_INET = 2
def get_vpdu_pid():
try:
fd = open("/var/run/vpdud/vpdud.pid", "r")
line = fd.readline()
pid = line.strip(os.linesep)
fd.close()
return int(pid)
except:
pass
return -1
def start_vpdu():
vpdu_command = "vpdud.py"
if not os.path.exists("/usr/bin/vpdud.py") or \
not os.path.exists("/usr/local/bin"):
vpdu_command = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"vpdud.py")
command = vpdu_command + " " + \
"-d --logging-method=file:/var/log/vpdud/vpdud.log"
logger.info(command)
retcode = subprocess.call(command, shell=True)
# wait service started
time.sleep(10)
return retcode
def get_ip_address(ifname):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = struct.pack('16sH14s', ifname, AF_UNIX, '\x00'*14)
res = fcntl.ioctl(s.fileno(), SIOCGIFADDR, ifreq)
ip = struct.unpack('16sH2x4s8x', res)[2]
s.close()
return socket.inet_ntoa(ip)
except:
logger.error("Failed to get ip on %s" % ifname)
return ""
def set_ip_address(ifname, ip):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
bin_ip = socket.inet_aton(ip)
ifreq = struct.pack('16sH2s4s8s', ifname, socket.AF_INET,
'\x00'*2, bin_ip, '\x00'*8)
fcntl.ioctl(s, SIOCSIFADDR, ifreq)
s.close()
except:
logger.error("Failed to set ip %s on %s" % (ip, ifname))
print "Failed to set ip %s on %s" % (ip, ifname)
def get_netmask_int(netmask):
ret = 0
for n in range(0, netmask):
ret |= 1 << (31 - n)
return ret
def get_mask(mask):
n = 0
while True:
if mask == 0:
break
mask &= (mask - 1)
n += 1
return n
def convert_ip_to_int(ip):
ip_items = ip.split('.')
ip_int = 0
for item in ip_items:
ip_int = ip_int * 256 + int(item)
return ip_int
def convert_int_to_ip(ip_int):
ip_items = ['0', '0', '0', '0']
for i in range(0, 4):
ip_items[3-i] = str(ip_int % 256)
ip_int = int((int(ip_int) - int(ip_items[3-i])) / 256)
return '.'.join(ip_items)
def link_up(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = struct.pack("16sh", ifname, 0)
flags = struct.unpack('16sh',
fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, ifreq))[1]
flags = flags | IFF_UP
ifreq = struct.pack('16sh', ifname, flags)
fcntl.ioctl(s.fileno(), SIOCSIFFLAGS, ifreq)
s.close()
def link_down(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = struct.pack("16sh", ifname, 0)
flags = struct.unpack('16sh',
fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, ifreq))[1]
flags = flags & ~IFF_UP
ifreq = struct.pack('16sh', ifname, flags)
fcntl.ioctl(s.fileno(), SIOCSIFFLAGS, ifreq)
s.close()
def link_status(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = struct.pack("16sh", ifname, 0)
flags = struct.unpack('16sh',
fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, ifreq))[1]
if flags & IFF_UP:
return True
else:
return False
s.close()
def get_netmask(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = struct.pack('16sH14s', ifname, socket.AF_INET, '\x00'*14)
try:
res = fcntl.ioctl(s.fileno(), SIOCGIFNETMASK, ifreq)
except IOError:
s.close()
return ""
netmask = socket.ntohl(struct.unpack('16sH2xI8x', res)[2])
s.close()
return 32 - int(round(math.log(ctypes.c_uint32(~netmask).value + 1, 2), 1))
def set_netmask(ifname, netmask):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
netmask = ctypes.c_uint32(~((2 ** (32 - netmask)) - 1)).value
nmbytes = socket.htonl(netmask)
ifreq = struct.pack('16sH2si8s', ifname, socket.AF_INET,
'\x00'*2, nmbytes, '\x00'*8)
fcntl.ioctl(s.fileno(), SIOCSIFNETMASK, ifreq)
s.close()
def get_net_interfaces():
return os.listdir("/sys/class/net")
class vPDUHandler(SSHHandler):
WELCOME = "Welcome to vPDU server"
PROMPT = "(vPDU) "
def __init__(self):
super(vPDUHandler, self).__init__()
self.config_instance = config.get_conf_instance()
self.mapping_file_handle = mapping_file.get_mapping_file_handle()
@command(['config'])
def command_config(self, params):
'''<esxi|pdu> [<list/update/add/delete> | <set/get>] [<param.1> ... < param.n>]
configure vPDU
----------------------------------
config pdu set <name>
- set PDU name
e.g.
config pdu set hawk
config pdu set database <database file>
- set pdu database file name
e.g.
config pdu set database ipia.db
config pdu set datadir <snmp data dir>
- set snmp data directory name
e.g.
config pdu set datadir hawk
config pdu list
-list pdu configurations
config esxi
--------------------------------------
config esxi list
- list configuration
config esxi update <option name> <value>
- update configuration
e.g.
Update esxi ip address in configuration file, run below command:
config esxi update host 10.62.59.124
Update esxi host "username"
config esxi update uesrname root
Update esxi host "password"
config esxi update password root
config esxi add <host> <uesrname> <password>
- add configuration
e.g.
Add an ESXi host information including ip, username and passowrd
config esxi add 10.62.59.128 root 1234567
config esxi delete
- delete configuration
e.g.
Delete section "esxihost"
config esxi delete esxihost
Note: After update/add the configuration, please run 'config list' to
be sure that the changes you made are correct.
'''
if len(params) == 0:
return
if params[0] == "pdu":
if params[1] == 'set':
if params[2] == 'name':
self.config_instance.pdu_name = params[3]
elif params[2] == 'database':
self.config_instance.db_file = params[3]
elif params[2] == 'datadir':
self.config_instance.snmp_data_dir = params[3]
self.config_instance.update()
elif params[1] == 'get':
self.config_instance.init()
table = Texttable()
table.add_row(['name', self.config_instance.pdu_name])
table.add_row(['database', self.config_instance.db_file])
table.add_row(['snmp data dir',
self.config_instance.snmp_data_dir])
table_str = table.draw()
self.writeresponse(table_str)
logger.info("\n" + table_str)
else:
logger.error("Unknown command {0}".format(params[0]))
elif params[0] == "esxi":
if params[1] == "list":
self.config_instance.init()
esxi_info = self.config_instance.esxi_info
if esxi_info is not None:
table = Texttable()
table.header(["esxi host", "username", "password"])
table.add_row([
get_color_string(bcolors.GREEN, esxi_info['host']),
get_color_string(bcolors.GREEN, esxi_info["username"]),
get_color_string(bcolors.GREEN, esxi_info['password'])
])
table_str = table.draw()
self.writeresponse(table_str)
logger.info("\n" + table_str)
return
else:
self.writeresponse("%sNo ESXi host info in configuration \
file.%s" % (colors.RED, colors.NORMAL))
elif params[1] == "update":
if len(params[2:]) == 2:
esxi_info = self.config_instance.esxi_info
if esxi_info is not None:
esxi_info[params[2]] = params[3]
self.config_instance.update()
else:
self.writeresponse("%sNo %s found in configuration \
file.%s" % (colors.RED, params[1], colors.NORMAL))
elif params[1] == "add":
if len(params[2:]) != 3:
return
if self.config_instance.esxi_info is None:
esxi_info = {}
logger.info("Adding esxi host: {0}, {1}, {2}"
.format(params[2], params[3], params[4]))
esxi_info['host'] = params[2]
esxi_info['username'] = params[3]
esxi_info['password'] = params[4]
self.config_instance.esxi_info = esxi_info
self.config_instance.update()
else:
self.writeresponse("ESXi info already exists.")
elif params[1] == "delete":
if self.config_instance.esxi_info is not None:
self.config_instance.delete()
else:
self.writeresponse("ESXi info already deleted.")
else:
self.writeresponse("unknown parameters.")
else:
self.writeresponse("unknown parameters: {0}.".format(params[0]))
@command(['map'])
def command_map(self, params):
'''[<add/list/delete/update>] [<param.1> ... <param.n>]
list/update/add mappings between VM name and PDU port
map add <datastore> <vm> <pdu> <port>
- Add an entry for VM and vPDU port
e.g.:
Add an entry.
map add datastore1 vquanta_auto1 1 2
map update <datastore> <vm> <pdu> <port>
- update an entry for VM and vPDU port
e.g.:
Update an existing datastore entry
map update datastore1 vquanta_auto1 3 1
map delete <datastore> <vm>
- Delete a datastore or a mapping for vm
e.g.:
Delete "datastore1"
map delete datastore1
Delete a mapping "vquanta_auto1 = 2" in datastore1
map delete datastore1 vquanta_auto1
map list
- List all mappings between VMs and vPDU ports
Note: when you are done to make changes, please run 'map list' to be
sure eveything is correct.
'''
if len(params) == 0:
return
if params[0] == "add" or params[0] == "update":
if len(params) != 5:
self.writeresponse(
colors.RED + "Invalid parameters." + colors.NORMAL
)
return
self.mapping_file_handle.update(params[1], params[2],
params[3], params[4])
elif params[0] == "delete":
if len(params) == 2:
self.mapping_file_handle.delete(params[1])
elif len(params) == 3:
self.mapping_file_handle.delete(params[1], params[2])
else:
self.writeresponse("Invalid parameters.")
elif params[0] == "list":
table = Texttable()
table.header(["PDU", "Port", "VM Name", "Datastore"])
table.set_cols_align(['c', 'c', 'c', 'c'])
for node_list in self.mapping_file_handle.nodes_list:
datastore = node_list.keys()[0]
for ni in node_list[datastore]:
table.add_row([
get_color_string(bcolors.GREEN, ni["control_pdu"]),
get_color_string(bcolors.GREEN, ni["control_port"]),
get_color_string(bcolors.GREEN, ni['node_name']),
get_color_string(bcolors.GREEN, datastore)
])
self.writeresponse(table.draw())
@command(['ip'])
def command_ip(self, params):
''' [<set/get/link>] [<param.1> ... <param.n>]
set/get interface IP address
link up/down will bring up/down interface link.
ip set <intf> <addr> <netmask>
e.g.:
Set enp0s8 address to 10.0.1.2
ip set enp0s8 10.0.1.2 255.255.255.0
ip get <intf>
e.g.:
Get enp0s8 address
ip get enp0s8
The output will be:
ip address: 10.0.1.2, netmask: 255.255.255.0
ip link list
- list all avaialbe ethernet interfaces
ip link <intf> status
- Get specific interface link status
ip link <intf> up
- Bring up link for interface
ip link <intf> down
- Bring down link for interface
'''
if len(params) == 0:
return
ifname_list = get_net_interfaces()
if params[0] == 'link':
if params[1] == 'list':
self.writeresponse(
colors.CYAN + "Available interfaces:" + colors.NORMAL
)
self.writeresponse(
colors.RED + ' '.join(ifname_list) + colors.NORMAL
)
return
else:
ifname = params[1]
if ifname not in ifname_list:
logger.error("%s not exists." % ifname)
self.writeresponse("%s%s not exists.%s" %
(colors.RED, ifname, colors.NORMAL))
return
subcmd = params[2]
if subcmd == 'up':
link_up(ifname)
elif subcmd == 'down':
link_down(ifname)
elif subcmd == 'status':
ret = link_status(ifname)
if ret:
self.writeresponse("%s link up" % ifname)
else:
self.writeresponse("%s link down" % ifname)
else:
logger.error("unknown parameters.")
else:
ifname = params[1]
if ifname not in ifname_list:
logger.error("%s not exists." % ifname)
self.writeresponse("%s%s not exists.%s" %
(colors.RED, ifname, colors.NORMAL))
return
if params[0] == 'get':
ip_address = get_ip_address(ifname)
netmask = get_netmask(ifname)
self.writeresponse(
colors.GREEN + "ip address: " + ip_address +
", " + "netmask: " +
convert_int_to_ip(get_netmask_int(netmask)) +
colors.NORMAL
)
elif params[0] == 'set':
set_ip_address(ifname, params[2])
if len(params) > 3:
netmask = params[3]
int_netmask = convert_ip_to_int(netmask)
set_netmask(ifname, get_mask(int_netmask))
status = link_status(ifname)
if not status:
link_up(ifname)
else:
logger.error("unknown parameters.")
@command(['vpdu'])
def command_vpdu(self, params):
'''[<start/stop/restart/status>]
Control vpdu service and get vpdu service status.
vpdu start - Start vPDU and SNMP simulator service
vpdu stop - Stop vPDU and SNMP simulator service
vpdu restart - Restart vPDU and SNMP simulator service
vpdu status - Get vPDU and SNMP simulator status
'''
if len(params) == 0:
return
logger.info("Executing action: %s" % params[0])
if params[0] == 'start':
pid = get_vpdu_pid()
logger.info("vpdu pid: %d" % pid)
if pid > 0:
self.writeresponse("%svpdu service [%d] is alredy running.%s"
% (colors.GREEN, pid, colors.NORMAL))
else:
ret = start_vpdu()
if ret == 0:
pid = get_vpdu_pid()
if pid > 0:
self.writeresponse("%svpdu service [%d] is started.%s"
% (colors.GREEN, pid, colors.NORMAL))
return
else:
# check if the snmpsim service is already running.
snmpsim_pid = SNMPSimService.getpid()
if snmpsim_pid > 0:
os.kill(snmpsim_pid, signal.SIGTERM)
self.writeresponse("%sFailed to start vpdu service.%s"
% (colors.RED, colors.NORMAL))
logger.error("Failed to start vpdu service.")
elif params[0] == 'stop':
pid = get_vpdu_pid()
if pid < 0:
self.writeresponse("%svpdu service is already stopped.%s"
% (colors.GREEN, colors.NORMAL))
return
if pid > 0:
os.kill(pid, signal.SIGTERM)
# Wait 1 second for snmpsim exit, and then end then check again
time.sleep(1)
snmpsim_pid = SNMPSimService.getpid()
if snmpsim_pid > 0:
os.kill(snmpsim_pid, signal.SIGTERM)
pid = get_vpdu_pid()
snmpsim_pid = SNMPSimService.getpid()
if pid < 0 and snmpsim_pid < 0:
self.writeresponse("%svpdu service is stopped.%s"
% (colors.GREEN, colors.NORMAL))
else:
self.writeresponse("%sCannot stop vpdu service.vpdu pid %d, \
snmpsim pid %d.%s"
% (colors.RED, pid, snmpsim_pid,
colors.NORMAL))
logger.error("Cannot stop vpdu service. vpdu pid %d, \
snmpsim pid %d" % (pid, snmpsim_pid))
elif params[0] == 'restart':
pid = get_vpdu_pid()
if pid > 0:
os.kill(pid, signal.SIGTERM)
# Wait 1 second for snmpsim exit, and then end then check again
time.sleep(1)
snmpsim_pid = SNMPSimService.getpid()
if snmpsim_pid > 0:
os.kill(snmpsim_pid, signal.SIGTERM)
self.writeresponse("{0}vpdu service is stopped.{1}"
.format(colors.GREEN, colors.NORMAL))
logger.info("vpdu service is stopped.")
ret = start_vpdu()
if ret == 0:
pid = get_vpdu_pid()
snmpsim_pid = SNMPSimService.getpid()
if pid > 0 and snmpsim_pid > 0:
self.writeresponse("{0}vpdu service [{1}] is restarted.{2}"
.format(colors.GREEN, pid, colors.NORMAL)
)
logger.info("vpdu service [%d] is restarted." % pid)
return
self.writeresponse("{0}Cannot restart vpdu service. pid {1}, \
snmpsimd pid {2}{3}".format(colors.RED, pid,
snmpsim_pid,
colors.NORMAL)
)
logger.error("Cannot restart vpdu service.")
elif params[0] == 'status':
vpdu_pid = get_vpdu_pid()
snmpsim_pid = SNMPSimService.getpid()
response = ""
if vpdu_pid > 0 and snmpsim_pid > 0:
response = "%svpdu service [%d] is running.%s" % \
(colors.GREEN, vpdu_pid, colors.NORMAL)
self.writeresponse(response)
logger.info(response)
elif vpdu_pid < 0 and snmpsim_pid < 0:
response = \
"%svpdu service is not running.%s" % (colors.RED,
colors.NORMAL)
self.writeresponse(response)
logger.info(response)
else:
response = "{0}There is an exception, \
vpdu pid {1}, snmp sim pid {2}.{3}"\
.format(colors.RED, vpdu_pid, snmpsim_pid, colors.NORMAL)
self.writeresponse(response)
logger.info(response)
@command(['password', 'pass'])
def command_password(self, params):
'''[set/get/list] <pdu> <port> <password>
password set <pdu> <port> <password>
Set port password on pdu
e.g.
- Set password "A01" for port 1 on pdu 1
password set 1 1 A01
password get <pdu> <port>
Get port password on pdu
e.g.
- Get password for port 1 on pdu 1
password get 1 1
password list <pdu>
Display password of all ports on pdu
e.g.
- Display all ports password on pdu 1
password list 1
'''
subcommand = params[0]
if subcommand == 'set':
if len(params) != 4:
self.writeresponse("Invalid parameters.")
return
pdu = int(params[1]) * 3 - 2
port = int(params[2])
passwd = params[3]
password.write_password(pdu, port, passwd)
elif subcommand == 'get':
if len(params) != 3:
self.writeresponse("Invalid parameters.")
return
pdu = int(params[1]) * 3 - 2
port = int(params[2])
password_str = password.read_password(pdu, port)
if password_str == "":
self.writeresponse("Not found password.")
return
response = "Password is: " + password_str
self.writeresponse(response)
elif subcommand == 'list':
if len(params) != 2:
self.writeresponse("Invalid parameters.")
return
pdu = int(params[1]) * 3 - 2
table = Texttable()
table.header(["Port", "Password"])
table.set_cols_dtype(['d', 't'])
table.set_cols_align(['c', 'c'])
for port_index in range(24):
passwd = password.read_password(pdu, port_index + 1)
if passwd == "":
continue
table.add_row([port_index + 1, passwd])
self.writeresponse(table.draw())
@command(["save"])
def command_confs(self, params):
'''
save
save all configurations including password, mappings, host configuration
'''
# save configuration file to writable disk
if not os.path.exists("/boot/conf"):
shutil.copytree(os.path.join(config_home_dir, "conf"), "/boot/conf")
else:
for path, _, files in os.walk(
os.path.join(config_home_dir, "conf")
):
for f in files:
shutil.copy(os.path.join(path, f), "/boot/conf")
class vPDUServer(SocketServer.TCPServer):
SocketServer.TCPServer.allow_reuse_address = True
def signal_handler(signum, frame):
vpdu_pid = get_vpdu_pid()
if vpdu_pid > 0:
os.kill(vpdu_pid, signal.SIGTERM)
snmpsim_pid = SNMPSimService.getpid()
if snmpsim_pid > 0:
os.kill(snmpsim_pid, signal.SIGTERM)
logger.info("Exit server.")
sys.exit(0)
def init_signal():
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
def usage():
print("Usage: server.py [OPTIONS]")
print("Options are:")
print("-d Run in daemon")
print("-h Help")
print("--logging-method=<file:file_name|stderr|stdout>")
if __name__ == '__main__':
daemon = False
logger.initialize("vpdud", "stdout")
try:
opts, args = getopt.getopt(sys.argv[1:],
"dh",
["daemonize", "help", "logging-method="])
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(1)
elif opt in ("-d", "--daemonize"):
daemon = True
elif opt == "--logging-method":
logger.initialize("vpdud", *arg.split(':'))
except getopt.GetoptError:
usage()
sys.exit(1)
if daemon:
pdusim.common.daemon.daemonize(server_pid_file)
init_signal()
# report_ip_thread = threading.Thread(target = pdusim.reportip.rptClient)
# report_ip_thread.start()
# Find the conf home dir
for dir in install_data_dir:
path = os.path.join(dir, 'conf', 'host.conf')
if os.path.exists(path):
config_home_dir = dir
break
if config_home_dir == "":
logger.error("Cann't find conf dir.")
sys.exit(1)
conf = config.Config(config_home_dir)
config.set_conf_instance(conf)
mapping_file.set_mapping_file_handle(
mapping_file.MappingFileHandle(config_home_dir)
)
logger.info("Server started")
server = vPDUHandler()
server.serve_forever()