~ubuntu-branches/ubuntu/vivid/manila/vivid-proposed

« back to all changes in this revision

Viewing changes to manila/openstack/common/eventlet_backdoor.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-01-15 11:55:33 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20150115115533-yshssd76s2uj3ybx
Tags: 2015.1~b1-0ubuntu1
* New upstream release:
  - d/control: Add new dependencies.
  - Install any new binaries and configuration to common package.
* d/watch: Update to use tarballs.openstack.org.
* d/control,compat: Bump debhelper compat level to 9.
* d/control: Bumped Standards-Version to 3.9.6, no changes.
* Systemd enablement:
  - d/rules,control: Enable use of dh-systemd and openstack-pkg-tools.
  - d/*.init.in: Write templates for generation of init, service and
    upstart configurations.
  - d/*.upstart: Drop in preference to above.
* d/*.logrotate: Move to single logrotate configuration in common package.
* d/rules: Ensure unit test suite failure fails package build.
* d/p/pep-0476.patch: Deal with SSL certification chain verification unit
  test failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
1
# Copyright (c) 2012 OpenStack Foundation.
4
2
# Administrator of the National Aeronautics and Space Administration.
5
3
# All Rights Reserved.
16
14
#    License for the specific language governing permissions and limitations
17
15
#    under the License.
18
16
 
 
17
from __future__ import print_function
 
18
 
 
19
import errno
19
20
import gc
 
21
import os
20
22
import pprint
 
23
import socket
21
24
import sys
22
25
import traceback
23
26
 
26
29
import greenlet
27
30
from oslo.config import cfg
28
31
 
 
32
from manila.openstack.common._i18n import _LI
 
33
from manila.openstack.common import log as logging
 
34
 
 
35
help_for_backdoor_port = (
 
36
    "Acceptable values are 0, <port>, and <start>:<end>, where 0 results "
 
37
    "in listening on a random tcp port number; <port> results in listening "
 
38
    "on the specified port number (and not enabling backdoor if that port "
 
39
    "is in use); and <start>:<end> results in listening on the smallest "
 
40
    "unused port number within the specified range of port numbers.  The "
 
41
    "chosen port is displayed in the service's log file.")
29
42
eventlet_backdoor_opts = [
30
 
    cfg.IntOpt('backdoor_port',
31
 
               default=None,
32
 
               help='port for eventlet backdoor to listen')
 
43
    cfg.StrOpt('backdoor_port',
 
44
               help="Enable eventlet backdoor.  %s" % help_for_backdoor_port)
33
45
]
34
46
 
35
47
CONF = cfg.CONF
36
48
CONF.register_opts(eventlet_backdoor_opts)
 
49
LOG = logging.getLogger(__name__)
 
50
 
 
51
 
 
52
class EventletBackdoorConfigValueError(Exception):
 
53
    def __init__(self, port_range, help_msg, ex):
 
54
        msg = ('Invalid backdoor_port configuration %(range)s: %(ex)s. '
 
55
               '%(help)s' %
 
56
               {'range': port_range, 'ex': ex, 'help': help_msg})
 
57
        super(EventletBackdoorConfigValueError, self).__init__(msg)
 
58
        self.port_range = port_range
37
59
 
38
60
 
39
61
def _dont_use_this():
40
 
    print "Don't use this, just disconnect instead"
 
62
    print("Don't use this, just disconnect instead")
41
63
 
42
64
 
43
65
def _find_objects(t):
44
 
    return filter(lambda o: isinstance(o, t), gc.get_objects())
 
66
    return [o for o in gc.get_objects() if isinstance(o, t)]
45
67
 
46
68
 
47
69
def _print_greenthreads():
48
70
    for i, gt in enumerate(_find_objects(greenlet.greenlet)):
49
 
        print i, gt
 
71
        print(i, gt)
50
72
        traceback.print_stack(gt.gr_frame)
51
 
        print
 
73
        print()
52
74
 
53
75
 
54
76
def _print_nativethreads():
55
77
    for threadId, stack in sys._current_frames().items():
56
 
        print threadId
 
78
        print(threadId)
57
79
        traceback.print_stack(stack)
58
 
        print
 
80
        print()
 
81
 
 
82
 
 
83
def _parse_port_range(port_range):
 
84
    if ':' not in port_range:
 
85
        start, end = port_range, port_range
 
86
    else:
 
87
        start, end = port_range.split(':', 1)
 
88
    try:
 
89
        start, end = int(start), int(end)
 
90
        if end < start:
 
91
            raise ValueError
 
92
        return start, end
 
93
    except ValueError as ex:
 
94
        raise EventletBackdoorConfigValueError(port_range, ex,
 
95
                                               help_for_backdoor_port)
 
96
 
 
97
 
 
98
def _listen(host, start_port, end_port, listen_func):
 
99
    try_port = start_port
 
100
    while True:
 
101
        try:
 
102
            return listen_func((host, try_port))
 
103
        except socket.error as exc:
 
104
            if (exc.errno != errno.EADDRINUSE or
 
105
               try_port >= end_port):
 
106
                raise
 
107
            try_port += 1
59
108
 
60
109
 
61
110
def initialize_if_enabled():
70
119
    if CONF.backdoor_port is None:
71
120
        return None
72
121
 
 
122
    start_port, end_port = _parse_port_range(str(CONF.backdoor_port))
 
123
 
73
124
    # NOTE(johannes): The standard sys.displayhook will print the value of
74
125
    # the last expression and set it to __builtin__._, which overwrites
75
126
    # the __builtin__._ that gettext sets. Let's switch to using pprint
80
131
            pprint.pprint(val)
81
132
    sys.displayhook = displayhook
82
133
 
83
 
    sock = eventlet.listen(('localhost', CONF.backdoor_port))
 
134
    sock = _listen('localhost', start_port, end_port, eventlet.listen)
 
135
 
 
136
    # In the case of backdoor port being zero, a port number is assigned by
 
137
    # listen().  In any case, pull the port number out here.
84
138
    port = sock.getsockname()[1]
 
139
    LOG.info(
 
140
        _LI('Eventlet backdoor listening on %(port)s for process %(pid)d') %
 
141
        {'port': port, 'pid': os.getpid()}
 
142
    )
85
143
    eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
86
144
                     locals=backdoor_locals)
87
145
    return port