#!/usr/bin/python

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

import sys
import syslog

import isi.sys.disk as disk
import isi.hw.hal as hal
import isi.hw.bay as bay
import isi.hw.sed as sed

mesh = disk.mesh

def main(disknames):
    exit_status = 0
    for diskname in disknames:
        if diskname.startswith('/dev/'):
            diskname = diskname[len('/dev/'):]
        mesh.refresh()
        dp = mesh.disk_provider(diskname)
        if (hal.supportsSelfEncryptingDrives() and 
            bay.isSelfEncryptingDrive(bay.dev2bay(diskname))):

            ret = disk.sed_release_ownership(dp.name)
            if ret in [sed.SED_RESULT_SUCCESS, sed.SED_RESULT_DRIVE_UNOWNED]:
                disk.wipe_disk_provider(dp)
            else:
                msg = ('Unable to release ownership of "%s", result "%s"' %
                    (dp.name, sed.result2str(ret)))
                print msg
                sys.stdout.flush()
                # XXX FIXME DSS: We may want to raise an alert here for
                #                those cases where we were not called
                #                by isi_reformat_node, since in those
                #                cases, the node will still be part of
                #                the cluster and an alert would provide
                #                some visibility to this issue.
                syslog.syslog(syslog.LOG_EMERG, msg)
                exit_status = 1
        else:
            disk.wipe_disk_provider(dp)

    sys.exit(exit_status)

if __name__ == '__main__':
    main(sys.argv[1:])
