#!/usr/bin/python

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

import sys
import syslog

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

mesh = disk.mesh

def main(disknames):
    # The intent of isi_reformat_wipe_disk is to have a isi_reformat_node
    # specific wipe disk script. The difference between isi_reformat_wipe_disk
    # and isi_wipe_disk is that it will retain the root mirrored partitions for
    # a given drive, whereas isi_wipe_disk will work as it always has and
    # is intended to wipe out all the partitions on your drives.
    exit_status = 0
    skip = []
    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))):

            if bootstrap.isInfinityMotherboard():
                skip = map(lambda x: x.get("name"), mirror.mirror_defs())
            ret = sed.release_ownership(dp.name)
            if ret in [sed.SED_RESULT_SUCCESS, sed.SED_RESULT_DRIVE_UNOWNED]:
                disk.wipe_disk_provider(dp, skip)
            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, skip)

    sys.exit(exit_status)

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