#!/usr/bin/env python

# Copyright C 2008-2013 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

import sys

import isi.app.lib.procs as procs
import isi.misc as isi_misc
import isi.hw.hal as hal
import isi.hw.sed as sed

def release_seds():
    result = 0
    print 'Releasing ownership of all Self Encrypting Drives (SEDs)'
    print 'Un-mounting /ifs...'
    (err, out) = procs.get_cmd_output("/sbin/umount -f /ifs")
    if err:
        print 'Unable to un-mount /ifs'
        return 1
    drives = isi_misc.hd_drives()
    for drive in drives:
        ret = sed.release_ownership(drive[0])
        if ret not in [sed.SED_RESULT_SUCCESS, sed.SED_RESULT_DRIVE_UNOWNED]:
            print 'Unable to release ownership of drive "%s"' % drive[0]
            result = 1
        else:
            print 'Releasing ownership of drive "%s" was successful' % drive[0]
    return result

def main():
    result = 0
    if hal.supportsSelfEncryptingDrives(): 
        result = release_seds()
        if result:
            print 'Unable to release ownership of all Self Encrypting Drives.'
        else:
            print ('Releasing ownership of all Self Encrypting drives was'
                   ' successful.')
        sys.exit(result and 1 or 0)
    else:
        print "Node does not support Self Encrypting Drives."
        sys.exit(0)

if __name__ == "__main__":
    main()
