set executable bit
[mpi-image.git] / scripts / mpi-copy.py
1 #!/usr/bin/python
2
3 from optparse import OptionParser
4 import os
5 import signal
6 import sys
7 import fcntl
8 import getpass
9
10 INSANE = 500
11 BLOCKING_DELAY = 30
12
13 def alarm(signum, junk):
14     # Do nothing, default implementation does needful
15     return
16
17 def fetch_output(infd,outfile):
18     try:
19         os.write(infd,'1')
20         output = ''
21
22         while (True):
23             batch = outfile.read()
24             if (batch):
25                 output+=batch
26             else:
27                 break
28     except IOError,e:
29         print "IO Error Accessing Vsys"
30         raise e
31
32     return output
33
34 def fetch_output_nofail(inpath,outpath):
35     pid = os.fork()
36
37     output = None
38
39     if (pid==0):
40         outfd = os.open(outpath, os.O_RDONLY|os.O_NONBLOCK)
41         infd = os.open(inpath, os.O_WRONLY)
42 #        infile = os.fdopen(infd,'w')
43
44         flags = fcntl.fcntl(outfd, fcntl.F_GETFL)
45         fcntl.fcntl(outfd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
46         outfile = os.fdopen(outfd)
47
48         output = fetch_output(infd, outfile)
49     else:
50         signal.alarm(BLOCKING_DELAY)
51         try:
52             os.waitpid(pid,0)
53             exit(1)
54         except OSError:
55             ## Failed
56             pass
57
58     return output
59
60 parser = OptionParser()
61 (options, args) = parser.parse_args()
62 hostfileName = "hostfile"
63 fileList = args[0:len(args)]
64
65 directory = os.getcwd()
66
67 f = open("/etc/slicename", "r")
68 sliceName = f.read()
69 homedir = "/home/" + sliceName
70
71 str = "sudo chown -R " + sliceName + " /vsys"
72 os.system(str)
73
74 outpath = '/vsys/slice_config.out'
75 inpath = '/vsys/slice_config.in'
76
77 ###
78 ### If vsys is not set up, wait a bit and try again
79
80 counter = 1
81 while True:
82         signal.signal(signal.SIGALRM, alarm)
83         output = fetch_output_nofail(inpath,outpath)
84         if (output is not None or counter==INSANE):
85             break
86         print "No access to vsys. Looping (%d)..."%counter
87         counter = counter+1
88
89 if (output is None):
90     print "Could not connect to Vsys. Giving up."
91     exit(1)
92
93 outfd = os.open(outpath, os.O_RDONLY|os.O_NONBLOCK)
94 infile = open(inpath, 'w')
95
96 flags = fcntl.fcntl(outfd, fcntl.F_GETFL)
97 fcntl.fcntl(outfd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
98 outfile = os.fdopen(outfd)
99
100 infile.write('1')
101 output = ''
102
103 while (True):
104         batch = outfile.read()
105         if (batch):
106                 output+=batch
107         else:
108                 break
109
110 wholestring = output.split("\n")
111 pathname = homedir + "/" + hostfileName
112 for eachline in wholestring:
113   if eachline == "\n" or eachline == "":
114     continue
115   line = eachline.split()
116   if line[0] == "vsys_ipList:":
117     g = open(hostfileName, "w+")
118     nodeNames = line[1]
119     nodes = nodeNames.split(",")
120     for i in nodes:
121       g.write(i + "\n")
122     g.close()
123
124 nodes = open(hostfileName, "r")
125
126 username = getpass.getuser()
127 wholefile = nodes.readlines()
128
129 for n in wholefile[1:]:
130   node = n.rstrip()
131   # wait until node is alive
132   while (True):
133     str = "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + username + "@" + node + " true"
134     x = os.system(str)
135     if x == 0:
136       break
137     else:
138       os.system("sleep 10")
139   str = "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + username + "@" + node + " \"if [ ! -d " + directory + " ]; then mkdir " + directory + "; fi\""
140   os.system(str)
141   for eachfile in fileList:
142      str = "scp -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + eachfile + " " + username + "@" + node + ":" + directory
143      os.system(str)