#!/usr/bin/env python
# Copyright (c) 2006-2011 Arastra, Inc.  All rights reserved.
# Arastra, Inc. Confidential and Proprietary.

'''
Eos initialization script, installed as /usr/bin/EosInit. It is
executed by /etc/rc.d/init.d/Eos early in the system bootup process.
'''

import Ark
import Cell
import EosInit
import Tac
from VeosHypervisor import runningInDocker

def EosStart():
   platform = Ark.getPlatform()
   if platform != "ceoslab" and not ( platform == "veos" and runningInDocker() ):
      # Load modules required by Eos packages.
      Tac.run( [ "/sbin/modprobe", "tun" ], input="" )
      Tac.run( [ "/sbin/modprobe", "8021q" ], input="" )
      Tac.run( [ "/sbin/modprobe", "rbfd" ], input="" )
      Tac.run( [ "/sbin/modprobe", "nf_defrag_ipv6" ], input="" )
      if "scd" not in open( '/proc/modules' ).read():
         Tac.run( [ "/sbin/modprobe", "kshim" ], input="" )

   if platform == "veos" and not runningInDocker():
      # On veos platforms, we would like to start acpid service
      # to process to listen to virsh shutdown command and gracefully
      # shutdown the vm.
      try:
         Tac.run( [ "/sbin/modprobe", "button" ], input="" )
         Tac.run( [ "service", "acpid", "restart" ], stdout=Tac.CAPTURE )
      except Tac.SystemCommandError as e:
         print "veos: %s" % ( e.output )

   # Create a symlink Cli and FastCli since Cli has been deprecated
   Tac.run( [ "ln", "-s", "-f", "/usr/bin/FastCli",
              "/usr/bin/Cli" ], stdout=Tac.CAPTURE )

   cellType = file( "/etc/celltype" ).read()
   if cellType in ( "supervisor", "fixed", "generic" ):
      Tac.run( [ "chkagent", "--add", "Sysdb" ], stdout=Tac.CAPTURE )
      Tac.run( [ "chkagent", "--add", "Launcher" ], stdout=Tac.CAPTURE )
      Tac.run( [ "chkagent", "--add", "Fru" ], stdout=Tac.CAPTURE )
      Tac.run( [ "chkagent", "--add", "StageMgr" ], stdout=Tac.CAPTURE )
      Tac.run( [ "chkagent", "--add", "ConfigAgent" ], stdout=Tac.CAPTURE )
      if Cell.electionMgrSupported():
         Tac.run( [ "chkagent", "--add", "ElectionMgr" ], stdout=Tac.CAPTURE )
   elif cellType == "card":
      Tac.run( [ "chkagent", "--add", "SysdbProxy" ], stdout=Tac.CAPTURE )
      Tac.run( [ "chkagent", "--add", "Launcher" ], stdout=Tac.CAPTURE )
   else:
      print "Unknown cell type.  No agents started by %s" % __file__

   # Userspace kernel canaries
   Tac.run( [ "chkagent", "--add", "SlabMonitor" ], stdout=Tac.CAPTURE )

   # Restart ProcMgr, so it adopts these new agent config files
   Tac.run( [ "service", "ProcMgr", "reload" ], stdout=Tac.CAPTURE )

def stage1Init():
   print "Starting stage 1 EOS initialization"
   EosInit.restoreASUBootImage()

def stage2Init():
   print "Starting stage 2 EOS initialization"
   EosStart()

if __name__ == "__main__":
   import optparse
   op = optparse.OptionParser( usage = "%prog [options]" )
   op.add_option( "--stage", help="initialization stage to run",
                  action="store", choices=["1", "2"] )
   opts, args = op.parse_args()
   if args:
      op.error( "Unexpected arguments" )
   if not opts.stage:
      op.error( "You must specify the initialization stage to run" )
   stage = int( opts.stage )
   if stage == 1:
      stage1Init()
   elif stage == 2:
      stage2Init()
