#!/usr/bin/python

# Copyright (c) 2013 Isilon Systems LLC. All rights reserved.

import sys
import syslog
import os

import isi.hw.bay as bay
import isi.hw.sed as sed
import isi.hw.hal as hal
import isi.hw.bootstrap as bs
import isi.hw.psi as psi

BAY_ENABLE_KEY = "hw.attributes.bay.%s.slot_enabled"

def main():
    pids = []
    result = 0
  
    if not hal.supportsSelfEncryptingDrives():
        sys.exit(0)

    for baynum in range(1, bay.maxBays() + 1):
        try:
            pid = os.fork()
        except OSError, e:
            result = 1
            # For a fork failure, append -1 to keep the PID list one-to-one
            # with respect to bays, so each PID refers to bay (list index + 1)
            pids.append(-1)
            syslog.syslog(syslog.LOG_ERR, "Fork error %s locking bay%s" %
                          (e.errno, baynum))
            continue
        if (pid != 0):
            # Parent process
            pids.append(pid)
            continue
        else:
            # Child process

            # On Infinity nodes without fully populated sleds, we expect to
            # see some slots empty, per the 'slot enabled' config value, and bays
            # 1 and 2 which are always considered 'enabled' to accomodate SSD upgrades.
            # Skip calling isi_sed on these bays to not generate errors.  If a SED
            # is present in a slot that we expect to be empty, we will still lock it.
            # When all legacy nodes are transitioned to configurations this infinity
            # check can be removed and we should consider having 'report errors for
            # empty bays' be a separate configuration item.
            skip_bay = False
            if (bs.isInfinityMotherboard() and
               (psi.getBool(BAY_ENABLE_KEY % baynum) is False or baynum < 3)):
                # See if we can find a device for this bay.  Otherwise skip it.
                if bay.bay2dev(baynum) is None:
                    skip_bay = True
            if (skip_bay):
                result = sed.SED_RESULT_SUCCESS
            else:
                result = sed.lock_bay(baynum)
            exit(result)

    for index, pid in enumerate(pids):
        # Only get status from children who were actually forked
        if (pid > 0):
            status = os.waitpid(pid, 0)[1]
            if (os.WIFEXITED(status)):
                ret = os.WEXITSTATUS(status)
                if ret not in [sed.SED_RESULT_SUCCESS,
                    sed.SED_RESULT_DRIVE_UNOWNED]:
                    result = 1
                    syslog.syslog(syslog.LOG_ERR,
                                 "failed to lock bay %s (%s)" %
                                 (index + 1, sed.result2str(ret)))
            else:
                    syslog.syslog(syslog.LOG_ERR,
                                  "failed to lock bay %s: "
                                  "child (pid %s) terminated: status %s" %
                                  (index + 1, pid, status))
                    result = 1

    sys.exit(result)

if __name__ == '__main__':
    main()
