1#!/usr/local/bin/python2.6
2#
3# This file is part of PerconaFT.
4# Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
5#
6
7import sys
8import os
9import optparse
10import getpass
11import pexpect
12import time
13from multiprocessing import Process
14
15# options
16parser = optparse.OptionParser()
17parser.add_option('--test', dest='test', type='string', default=None, help="name of stress test to run")
18parser.add_option('--iterations', dest='iterations', type='int', default=1, help="Number of test iterations (default = 1)")
19parser.add_option('--verbose', dest='verbose', action="store_true", default=False, help="Verbose printing (default = FALSE)")
20parser.add_option('--client', dest='client', type='string', default='192.168.1.107', help='client machine being power failed (default=tick)')
21parser.add_option('--sandbox_dir', dest='sbdir', type='string', default='/tpch/mysb/msb_3.0.2-beta_16948/', help='sandbox directory (default = None)')
22options,remainder = parser.parse_args()
23
24nameaddr='mysql@'+options.client
25password='mytokudb'
26
27ipm_nameaddr='admn@192.168.1.254'
28ipm_passwd='admn'
29
30def IPM_cmd(cmds):
31    # password handling
32    ssh_newkey = 'Are you sure you want to continue connecting'
33    p=pexpect.spawn('ssh %s' % ipm_nameaddr, timeout=60)
34    i=p.expect([ssh_newkey,'Password:',pexpect.EOF])
35    if i==0:
36        p.sendline('yes')
37        i=p.expect([ssh_newkey,'Password:',pexpect.EOF])
38    if i==1:
39        p.sendline(ipm_passwd)
40    elif i==2:
41        print "I either got key or connection timeout"
42        pass
43
44    # run command(s)
45    i = p.expect('Sentry:')
46    for cmd in cmds:
47        if i==0:
48            p.sendline(cmd)
49        else:
50            print 'p.expect saw', p.before
51        i = p.expect('Sentry:')
52        print p.before
53
54    # close session
55    p.sendline('quit')
56    p.expect(pexpect.EOF)
57    return 0
58
59def IPM_power_on():
60    IPM_cmd(['on all'])
61
62def IPM_power_off():
63    IPM_cmd(['off all'])
64
65def ssh_cmd(cmd, verbose=True, timeout=30):
66
67  ssh_newkey = 'Are you sure you want to continue connecting'
68  p=pexpect.spawn('ssh %s %s' % (nameaddr, cmd), timeout=timeout)
69
70  i=p.expect([ssh_newkey,'password:',pexpect.EOF])
71  if i==0:
72    p.sendline('yes')
73    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
74  if i==1:
75    if verbose:
76      print 'ssh %s %s' % (nameaddr, cmd)
77    p.sendline(password)
78    p.expect(pexpect.EOF)
79  elif i==2:
80    print "I either got key or connection timeout"
81    pass
82  if verbose:
83    print p.before
84  return p.before
85
86def client_cmd(cmd, verbose=True, timeout=3600):
87    ssh_cmd(cmd, verbose, timeout)
88
89def ping_server(name):
90    p=pexpect.spawn("ping -c 1 "+name)
91    i=p.expect(['1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms',
92                '1 packets transmitted, 1 received, 0% packet loss, time 0ms',
93                pexpect.EOF])
94    return i
95
96
97def test_it():
98    cmd = "/home/wells/svn/iibench/py/iibench.py --db_config_file=%smy.sandbox.cnf --max_rows=1000000000 --engine=tokudb --outfile=/tmp/pf_%d" % (options.sbdir, options.iterations)
99    print "CMD = ", cmd
100    client_cmd(cmd, timeout=3600)
101
102def run_test():
103#    cmd = options.test
104#    if ( options.verbose ): cmd += ' -v'
105#    for i in range(options.iterations):
106
107    t0 = Process(target=test_it, args=())
108    for iter in range(options.iterations + 1):
109        print "Turn On Power to Server"
110        IPM_power_on()
111        i = ping_server(options.client)
112        while ( i != 1 ):
113            i = ping_server(options.client)
114        print "Server rebooted, wait 30 seconds to restart MySQL"
115        time.sleep(30)
116        print "Start MySQL"
117        client_cmd(options.sbdir+'stop')  # clears out flags from previous start
118        client_cmd(options.sbdir+'start')
119        if iter < options.iterations:
120            print "Run Test"
121            t0.start()
122            print "Sleep(%d)" % (300 + iter)
123            time.sleep(300 + iter)
124            print "Turn Off Power to Server"
125            IPM_power_off()
126            t0.terminate()
127        else:
128            # last loop through, just cleanup
129            client_cmd(options.sbdir+'stop')
130
131def main(argv):
132    run_test()
133    return 0
134
135if __name__ == '__main__':
136    usage = sys.modules["__main__"].__doc__
137    parser.set_usage(usage)
138    unused_flags, new_argv = parser.parse_args(args=sys.argv[1:], values=options)
139    sys.exit(main([sys.argv[0]] + new_argv))
140
141