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
|
#! /usr/bin/python
# -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
#
# Copyright (C) 2010 Patrick Crews
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""port_management.py
code for dealing with the various tasks
around handing out and managing server ports
that we need to run tests
"""
# imports
import os
import sys
class portManager:
""" class for doing the work of handing out and tracking ports """
def __init__(self, system_manager, debug = 0):
# This is a file that can be read into a dictionary
# it is in port:owner format
self.skip_keys = [ 'port_file_delimiter'
, 'system_manager'
]
self.port_catalog = "/tmp/drizzle_test_port_catalog.dat"
self.port_file_delimiter = ':' # what we use to separate port:owner
self.debug = debug
self.logging = system_manager.logging
self.system_manager = system_manager
if self.debug:
self.logging.debug_class(self)
def get_port_block(self, requester, base_port, block_size):
""" Try to return a block of ports of size
block_size, starting with base_port
We take a target port and increment it
until we find an unused port. We make
no guarantee of continuous ports, only
that we will try to return block_size
ports for use
We can probably get fancier / smarter in the future
but this should work for now :-/
"""
assigned_ports = []
current_port = base_port
while len(assigned_ports) != block_size:
new_port = (self.get_port(requester, current_port))
assigned_ports.append(new_port)
current_port = new_port+1
return assigned_ports
def get_port(self, requester, desired_port):
""" Try to lock in the desired_port
if not, we increment the value until
we find an unused port.
We take max / min port values from test-run.pl
This is a bit bobo, but will work for now...
"""
searching_for_port = 1
attempts_remain = 100
max_port_value = 32767
min_port_value = 5001
while searching_for_port and attempts_remain:
# Check if the port is used
if self.check_port_status(desired_port):
# assign it
self.assign_port(requester, desired_port)
return desired_port
else: # increment the port and try again
desired_port = desired_port + 1
if desired_port >= max_port_value:
desired_port = min_port_value
attempts_remain = attempts_remain - 1
self.logging.error("Failed to assign a port in %d attempts")
sys.exit(1)
def check_port_status(self, port):
""" Check if a port is in use, via the catalog file
which all copies of dbqp.py should use
Not *really* sure how well this works with multiple
dbqp.py instances...we'll see if we even need it
to work
"""
# read the catalog file
port_catalog = self.process_port_catalog()
if port not in port_catalog and not self.is_port_used(port):
return 1
else:
return 0
def is_port_used(self, port):
""" See if a given port is used on the system """
retcode, output = self.system_manager.execute_cmd("netstat -lant")
# parse our output
entry_list = output.split("\n")
good_data = 0
for entry in entry_list:
if entry.startswith('Proto'):
good_data = 1
elif good_data:
# We try to catch additional output
# like we see with freebsd
if entry.startswith('Active'):
good_data = 0
pass
else:
if self.system_manager.cur_os in [ 'FreeBSD'
, 'Darwin'
]:
split_token = '.'
else:
split_token = ':'
port_candidate = entry.split()[3].split(split_token)[-1].strip()
if port_candidate.isdigit():
used_port = int(port_candidate)
else:
used_port = None # not a value we can use
if port == used_port:
if entry.split()[-1] != "TIME_WAIT":
return 1
return 0
def process_port_catalog(self):
""" Read in the catalog file so that we can see
if the port is in use or not
"""
port_catalog = {}
delimiter = ':'
if os.path.exists(self.port_catalog):
try:
port_file = open(self.port_catalog,'r')
for line in port_file:
line = line.strip()
port, owner = line.split(self.port_file_delimiter)
port_catalog[port] = owner
port_file.close()
except IOError, e:
self.logging.error("Problem opening port catalog file: %s" %(self.port_catalog))
self.logging.error("%s" %e)
sys.exit(1)
return port_catalog
def assign_port(self, owner, port):
"""Assigns a port - logs it in the port_catalog file"""
data_string = "%d:%s\n" %(port, owner)
try:
port_file = open(self.port_catalog,'a')
port_file.write(data_string)
port_file.close()
except IOError, e:
self.logging.error("Problem opening port catalog file: %s" %(self.port_catalog))
self.logging.error("%s" %e)
sys.exit(1)
def free_ports(self, portlist):
""" Clean up our port catalog """
for port in portlist:
self.free_port(port)
def free_port(self, port):
""" Free a single port from the catalog """
if self.debug:
self.logging.debug("Freeing port %d" %(port))
port_catalog = self.process_port_catalog()
port_catalog.pop(str(port),None)
self.write_port_catalog(port_catalog)
def write_port_catalog(self, port_catalog):
port_file = open(self.port_catalog, 'w')
for key, value in port_catalog.items():
port_file.write(("%s:%s\n" %(key, value)))
port_file.close()
|