196ff1984SBrad Bishop#!/usr/bin/env python3
2eb8dc403SDave Cobbley#
3eb8dc403SDave Cobbley# BitBake Toaster Implementation
4eb8dc403SDave Cobbley#
5eb8dc403SDave Cobbley# Copyright (C) 2015 Intel Corporation
6eb8dc403SDave Cobbley#
7c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
8eb8dc403SDave Cobbley#
9eb8dc403SDave Cobbley
10eb8dc403SDave Cobbley"""Custom management command checksocket."""
11eb8dc403SDave Cobbley
12eb8dc403SDave Cobbleyimport errno
13eb8dc403SDave Cobbleyimport socket
14eb8dc403SDave Cobbley
15eb8dc403SDave Cobbleyfrom django.core.management.base import BaseCommand, CommandError
16*5082cc7fSAndrew Geisslerfrom django.utils.encoding import force_str
17eb8dc403SDave Cobbley
18eb8dc403SDave CobbleyDEFAULT_ADDRPORT = "0.0.0.0:8000"
19eb8dc403SDave Cobbley
20eb8dc403SDave Cobbleyclass Command(BaseCommand):
21eb8dc403SDave Cobbley    """Custom management command."""
22eb8dc403SDave Cobbley
23eb8dc403SDave Cobbley    help = 'Check if Toaster can listen on address:port'
24eb8dc403SDave Cobbley
25eb8dc403SDave Cobbley    def add_arguments(self, parser):
26eb8dc403SDave Cobbley        parser.add_argument('addrport', nargs='?', default=DEFAULT_ADDRPORT,
27eb8dc403SDave Cobbley                            help='ipaddr:port to check, %s by default' % \
28eb8dc403SDave Cobbley                                 DEFAULT_ADDRPORT)
29eb8dc403SDave Cobbley
30eb8dc403SDave Cobbley    def handle(self, *args, **options):
31eb8dc403SDave Cobbley        addrport = options['addrport']
32eb8dc403SDave Cobbley        if ':' not in addrport:
33eb8dc403SDave Cobbley            raise CommandError('Invalid addr:port specified: %s' % addrport)
34eb8dc403SDave Cobbley        splitted = addrport.split(':')
35eb8dc403SDave Cobbley        try:
36eb8dc403SDave Cobbley            splitted[1] = int(splitted[1])
37eb8dc403SDave Cobbley        except ValueError:
38eb8dc403SDave Cobbley            raise CommandError('Invalid port specified: %s' % splitted[1])
39eb8dc403SDave Cobbley        self.stdout.write('Check if toaster can listen on %s' % addrport)
40eb8dc403SDave Cobbley        try:
41eb8dc403SDave Cobbley            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
42eb8dc403SDave Cobbley            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
43eb8dc403SDave Cobbley            sock.bind(tuple(splitted))
44eb8dc403SDave Cobbley        except (socket.error, OverflowError) as err:
45eb8dc403SDave Cobbley            errors = {
46eb8dc403SDave Cobbley                errno.EACCES: 'You don\'t have permission to access port %s' \
47eb8dc403SDave Cobbley                              % splitted[1],
48eb8dc403SDave Cobbley                errno.EADDRINUSE: 'Port %s is already in use' % splitted[1],
49eb8dc403SDave Cobbley                errno.EADDRNOTAVAIL: 'IP address can\'t be assigned to',
50eb8dc403SDave Cobbley            }
51eb8dc403SDave Cobbley            if hasattr(err, 'errno') and err.errno in errors:
52eb8dc403SDave Cobbley                errtext = errors[err.errno]
53eb8dc403SDave Cobbley            else:
54*5082cc7fSAndrew Geissler                errtext = force_str(err)
55eb8dc403SDave Cobbley            raise CommandError(errtext)
56eb8dc403SDave Cobbley
57eb8dc403SDave Cobbley        self.stdout.write("OK")
58