only use the slice's public IP if NAT is enabled
[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 def write_hostnamefile(wholestring, hostfile_pathname):
61     for eachline in wholestring:
62       if eachline == "\n" or eachline == "":
63         continue
64       line = eachline.split()
65       if line[0] == "vsys_ipList:":
66         print "writing", hostfile_pathname
67         g = open(hostfile_pathname, "w+")
68         nodeNames = line[1]
69         nodes = nodeNames.split(",")
70         for i in nodes[1:]:
71           g.write(i + "\n")
72         g.close()
73         return
74     print "failed to parse vsys_iplist. no hostnamefile created. aborting"
75     print "dump wholestring: ", str(wholestring)
76     sys.exit(-1)
77
78 parser = OptionParser()
79 (options, args) = parser.parse_args()
80 hostfileName = "hostfile"
81 fileList = args[0:len(args)]
82
83 directory = os.getcwd()
84
85 f = open("/etc/slicename", "r")
86 sliceName = f.read()
87 homedir = "/home/" + sliceName
88
89 cmd = "sudo chown -R " + sliceName + " /vsys"
90 os.system(cmd)
91
92 outpath = '/vsys/slice_config.out'
93 inpath = '/vsys/slice_config.in'
94
95 ###
96 ### If vsys is not set up, wait a bit and try again
97
98 counter = 1
99 while True:
100         signal.signal(signal.SIGALRM, alarm)
101         output = fetch_output_nofail(inpath,outpath)
102         if (output is not None or counter==INSANE):
103             break
104         print "No access to vsys. Looping (%d)..."%counter
105         counter = counter+1
106
107 if (output is None):
108     print "Could not connect to Vsys. Giving up."
109     exit(1)
110
111 outfd = os.open(outpath, os.O_RDONLY|os.O_NONBLOCK)
112 infile = open(inpath, 'w')
113
114 flags = fcntl.fcntl(outfd, fcntl.F_GETFL)
115 fcntl.fcntl(outfd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
116 outfile = os.fdopen(outfd)
117
118 infile.write('1')
119 output = ''
120
121 while (True):
122         batch = outfile.read()
123         if (batch):
124                 output+=batch
125         else:
126                 break
127
128 wholestring = output.split("\n")
129 hostfile_pathname = homedir + "/" + hostfileName
130 write_hostnamefile(wholestring, hostfile_pathname)
131
132 nodes = open(hostfile_pathname, "r")
133
134 username = getpass.getuser()
135 wholefile = nodes.readlines()
136
137 for n in wholefile[1:]:
138   node = n.rstrip()
139   # wait until node is alive
140   while (True):
141     cmd = "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + username + "@" + node + " true"
142     x = os.system(cmd)
143     if x == 0:
144       break
145     else:
146       os.system("sleep 10")
147   cmd = "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + username + "@" + node + " \"if [ ! -d " + directory + " ]; then mkdir " + directory + "; fi\""
148   os.system(cmd)
149   for eachfile in fileList:
150      cmd = "scp -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + eachfile + " " + username + "@" + node + ":" + directory
151      os.system(cmd)