#!/usr/bin/python

# this script does a clean start of the soft ioc

import subprocess
import getpass
import sys
import telnetlib
import time
from epics import caget, caput

print "Killing all chiller sessions..."
print "Wait for few minutes for connection to restore...."

# First, check to see if chiller soft ioc is running
process=[]
HOST="laci@lcls-daemon1"
COMMAND="ps -ef | grep sioc-dmp1-rf02 | grep chiller"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                       shell=False,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print result
if result == []:
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    for line in result:
        process.append(line.split()[1])

ssh.kill()
ssh.terminate()

# if soft ioc is running, stop it first and ensure there are no processes running for chiller
if len(process) > 0:
    COMMAND="/etc/init.d/st.sioc-dmp1-rf02 stop; sleep 5.0; ps -ef | grep sioc-dmp1-rf02 | grep chiller"
    ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result = ssh.stdout.readlines()
    print result
    ssh.kill()
    ssh.terminate()

# if the soft ioc stop script leaves lingering process.....
COMMAND="ps -ef | grep sioc-dmp1-rf02 | grep chiller"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print result

ssh.kill()
ssh.terminate()

# kill it explicitly
if result != []:
    for line in result:
        COMMAND="kill -KILL %s" % line.split()[1]
        ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        result = ssh.stdout.readlines()
        print result
    ssh.kill()
    ssh.terminate()

# now telnet to the digi and kill all open sessions on port 7 which is connected to the chiller rs232 port
OLD_HOST = "ts-b921-nw09"
HOST = "ts-b921-nw08"
OLD_SESSION = "serial 1"
SESSION = "tty07"
INT_SESSION = "tty01"
OLD_KILL_CMD = "kill %s\n"
KILL_CMD = "kill tty=%s\n"

tn = telnetlib.Telnet(HOST)
tn.read_until("login:")
tn.write("root\n")
tn.read_until("password:")
tn.write("b1G*B00k\n")
time.sleep( 5) 
tn.write("who\n")
time.sleep( 5) 
tn.write("exit\n")
time.sleep( 5)
sessions = tn.read_all().split('\r\n')
print sessions

tty_sessions = []
for session in sessions:
    if SESSION in session:
        tty_sessions.append(session.split()[0])

if tty_sessions:
    tn = telnetlib.Telnet(HOST)
    tn.read_until("login:")
    tn.write("root\n")
    tn.read_until("password:")
    tn.write("b1G*B00k\n")
    time.sleep( 5)
    for tty in tty_sessions:
        tn.write(OLD_KILL_CMD % tty)
        time.sleep( 5)
    tn.write("who\n")
    time.sleep( 5)
    tn.write("exit\n")
    time.sleep( 5)
    sessions = tn.read_all().split('\r\n')
    print sessions	

# after all open sessions on port 7 have been killed, restart the soft ioc and ensure it is running
HOST="laci@lcls-daemon1"
COMMAND="/etc/init.d/st.sioc-dmp1-rf02 start; sleep 5.0; ps -ef | grep sioc-dmp1-rf02 | grep chiller"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print result

ssh.kill()
ssh.terminate()
