Exemplo n.º 1
0
def pdu_power_status(request, id):
    """
    Get status of all power ports for an asset in
    network controlled PDU datacenter.
    """
    try:
        asset = Asset.objects.get(id=id)
    except ObjectDoesNotExist:
        return JsonResponse(
            {
                "failure_message":
                Status.ERROR.value + "Asset" +
                GenericFailure.DOES_NOT_EXIST.value
            },
            status=HTTPStatus.BAD_REQUEST,
        )
    if not is_asset_power_controllable_by_pdu(asset):
        return JsonResponse(
            {
                "failure_message":
                Status.ERROR.value +
                "Power is not network controllable on this rack."
            },
            status=HTTPStatus.BAD_REQUEST,
        )
    power_connections = serialize_power_connections(PowerPort, asset)
    # Get string parameter representing rack number (i.e. A01<L/R>)
    rack_str = str(asset.rack.row_letter)
    if (asset.rack.rack_num / 10) < 1:
        rack_str = rack_str + "0"
    rack_str = rack_str + str(asset.rack.rack_num)
    power_status = dict()
    for power_connection in power_connections:
        try:
            html = requests.get(
                PDU_URL + GET_PDU + rack_str +
                str(power_connections[power_connection]["left_right"]),
                timeout=5,
            )
        except ConnectionError:
            return JsonResponse(
                {
                    "failure_message":
                    Status.CONNECTION.value + PowerFailure.CONNECTION.value
                },
                status=HTTPStatus.REQUEST_TIMEOUT,
            )
        power_status[power_connection] = regex_power_status(
            html.text, power_connections[power_connection]["port_number"])[0]
    return JsonResponse(
        {
            "power_connections": power_connections,
            "power_status": power_status
        },
        status=HTTPStatus.OK,
    )
Exemplo n.º 2
0
def pdu_power_off(request):
    """
    Turn on power to specified port
    """
    try:
        asset = get_pdu_power_request_parameters(request)
    except PowerManagementException as error:
        return JsonResponse(
            {"failure_message": Status.ERROR.value + str(error)},
            status=HTTPStatus.BAD_REQUEST,
        )
    except UserPowerPermissionException as error:
        return JsonResponse(
            {
                "failure_message":
                Status.AUTH_ERROR.value + AuthFailure.POWER.value,
                "errors": str(error),
            },
            status=HTTPStatus.UNAUTHORIZED,
        )
    power_connections = serialize_power_connections(PowerPort, asset)
    # Check power is off
    for connection in power_connections:
        try:
            html = requests.get(PDU_URL + GET_PDU + get_pdu_status_ext(
                asset, str(power_connections[connection]["left_right"])))
        except ConnectionError:
            return JsonResponse(
                {
                    "failure_message":
                    Status.CONNECTION.value + PowerFailure.CONNECTION.value
                },
                status=HTTPStatus.REQUEST_TIMEOUT,
            )
        power_status = regex_power_status(
            html.text, power_connections[connection]["port_number"])[0]
        if power_status == "ON":
            try:
                toggle_pdu_power(asset, connection, "off")
            except ConnectionError:
                return JsonResponse(
                    {
                        "failure_message":
                        Status.CONNECTION.value + PowerFailure.CONNECTION.value
                    },
                    status=HTTPStatus.REQUEST_TIMEOUT,
                )
    log_power_action(
        request.user,
        PowerAction.OFF,
        asset,
    )
    return JsonResponse(
        {"success_message": Status.SUCCESS.value + "Power turned off."},
        status=HTTPStatus.OK,
    )
Exemplo n.º 3
0
def toggle_pdu_power(asset, asset_port_number, goal_state):
    power_connections = serialize_power_connections(PowerPort, asset)
    pdu_port = power_connections[asset_port_number]["port_number"]
    pdu = "hpdu-rtp1-" + get_pdu_status_ext(
        asset, str(power_connections[asset_port_number]["left_right"]))
    try:
        requests.post(PDU_URL + TOGGLE_PDU, {
            "pdu": pdu,
            "port": pdu_port,
            "v": goal_state
        })
    except ConnectionError:
        raise ConnectionError("Cannot contact PDU Network Controller")
    return
Exemplo n.º 4
0
def pdu_power_cycle(request):
    try:
        asset = get_pdu_power_request_parameters(request)
    except PowerManagementException as error:
        return JsonResponse(
            {"failure_message": Status.ERROR.value + str(error)},
            status=HTTPStatus.BAD_REQUEST,
        )
    except UserPowerPermissionException as error:
        return JsonResponse(
            {
                "failure_message":
                Status.AUTH_ERROR.value + AuthFailure.POWER.value,
                "errors": str(error),
            },
            status=HTTPStatus.UNAUTHORIZED,
        )
    power_connections = serialize_power_connections(PowerPort, asset)
    for connection in power_connections:
        try:
            toggle_pdu_power(asset, connection, "off")
        except ConnectionError:
            return JsonResponse(
                {
                    "failure_message":
                    Status.CONNECTION.value + PowerFailure.CONNECTION.value
                },
                status=HTTPStatus.REQUEST_TIMEOUT,
            )
    time.sleep(2)
    for connection in power_connections:
        try:
            toggle_pdu_power(asset, connection, "on")
        except ConnectionError:
            return JsonResponse(
                {
                    "failure_message":
                    Status.CONNECTION.value + PowerFailure.CONNECTION.value
                },
                status=HTTPStatus.REQUEST_TIMEOUT,
            )
    log_power_action(request.user, PowerAction.CYCLE, asset)
    return JsonResponse(
        {
            "success_message":
            Status.SUCCESS.value + "Power cycled, all asset power ports reset."
        },
        status=HTTPStatus.OK,
    )
Exemplo n.º 5
0
def remove_unavailable_pdu_ports(available_left, available_right, assets,
                                 power_port_model):
    for asset in assets:
        asset_power = serialize_power_connections(power_port_model, asset)
        for port_num in asset_power.keys():
            if asset_power[port_num]["left_right"] == "L":
                try:
                    available_left.remove(asset_power[port_num]["port_number"])
                except ValueError:
                    raise Exception("Port " +
                                    asset_power[port_num]["port_number"] +
                                    " does not exist on PDU. ")
            else:
                try:
                    available_right.remove(
                        asset_power[port_num]["port_number"])
                except ValueError:
                    raise Exception("Port " +
                                    asset_power[port_num]["port_number"] +
                                    " does not exist on PDU. ")