summaryrefslogtreecommitdiffstats
path: root/bitbake
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2015-12-17 16:48:46 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2015-12-18 13:51:53 +0000
commit847b93596dcb8bb154bada121ed5f878070fdc4b (patch)
tree13d878d3360d26325baa2400b17628ecd91a00ca /bitbake
parent9f3681dca555c1520ca33ca25ff75e8ba3caee80 (diff)
downloadpoky-847b93596dcb8bb154bada121ed5f878070fdc4b.tar.gz
bitbake: toaster: implement checksocket command
Implemented new management command to check if it's possible to listen on specified address:port. [YOCTO #8775] (Bitbake rev: 0339b90842fd7c878c511b4b89ebcaee9a431bba) Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Elliot Smith <elliot.smith@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake')
-rw-r--r--bitbake/lib/toaster/toastermain/management/commands/checksocket.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/toastermain/management/commands/checksocket.py b/bitbake/lib/toaster/toastermain/management/commands/checksocket.py
new file mode 100644
index 0000000000..0399b8659b
--- /dev/null
+++ b/bitbake/lib/toaster/toastermain/management/commands/checksocket.py
@@ -0,0 +1,69 @@
1#!/usr/bin/env python
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# BitBake Toaster Implementation
6#
7# Copyright (C) 2015 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22"""Custom management command checksocket."""
23
24import errno
25import socket
26
27from django.core.management.base import BaseCommand, CommandError
28from django.utils.encoding import force_text
29
30DEFAULT_ADDRPORT = "0.0.0.0:8000"
31
32class Command(BaseCommand):
33 """Custom management command."""
34
35 help = 'Check if Toaster can listen on address:port'
36
37 def add_arguments(self, parser):
38 parser.add_argument('addrport', nargs='?', default=DEFAULT_ADDRPORT,
39 help='ipaddr:port to check, %s by default' % \
40 DEFAULT_ADDRPORT)
41
42 def handle(self, *args, **options):
43 addrport = options['addrport']
44 if ':' not in addrport:
45 raise CommandError('Invalid addr:port specified: %s' % addrport)
46 splitted = addrport.split(':')
47 try:
48 splitted[1] = int(splitted[1])
49 except ValueError:
50 raise CommandError('Invalid port specified: %s' % splitted[1])
51 self.stdout.write('Check if toaster can listen on %s' % addrport)
52 try:
53 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
54 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
55 sock.bind(tuple(splitted))
56 except (socket.error, OverflowError) as err:
57 errors = {
58 errno.EACCES: 'You don\'t have permission to access port %s' \
59 % splitted[1],
60 errno.EADDRINUSE: 'Port %s is already in use' % splitted[1],
61 errno.EADDRNOTAVAIL: 'IP address can\'t be assigned to',
62 }
63 if hasattr(err, 'errno') and err.errno in errors:
64 errtext = errors[err.errno]
65 else:
66 errtext = force_text(err)
67 raise CommandError(errtext)
68
69 self.stdout.write("OK")