#!/usr/bin/env python

# Copyright C 2002-2004 by Isilon Systems, Inc. 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 os, sys, string

import isi.auth as auth
import isi.config.ifs as ifs
import isi.config.mcp
import syslog

oldHostname = string.strip(os.popen("/bin/hostname -s").readlines()[0])
newHostname = sys.argv[1]

if oldHostname != newHostname:
    # Set hostname:
    os.system("/bin/hostname %s" % newHostname)

    # We've reset the hostname, so update /etc/hosts
    os.system("/etc/mcp/scripts/hostupdate.py /etc/hosts")

    clusterName = ifs.get()['name']
    if not oldHostname:
        devID = stats.get('id')
        if not clusterName or not devID:
            sys.exit()
        oldHostname = "%s-%d" % (clusterName, devID) 

    if oldHostname != newHostname:
        try:
            from isi.config.auth import refresh_configuration
            refresh_configuration()
        except Exception as e:
            syslog.syslog(syslog.LOG_NOTICE, "Failed to refresh lsassd " +\
                    "in isi_set_hostname: %s"%e)

        domain = None
        try:
            domain = auth.get_current_domain()
        except Exception, e:
            pass

        if domain is not None:
            try:
                auth.reset_schannel()
            except Exception, e:
                syslog.syslog(syslog.LOG_NOTICE, "Failed to reset secure channel " +\
                        "in isi_set_hostname: %s"%e)

