check in dave's init script
smbaker [Sun, 9 Dec 2012 20:43:18 +0000 (12:43 -0800)]
scripts/init.py [new file with mode: 0644]

diff --git a/scripts/init.py b/scripts/init.py
new file mode 100644 (file)
index 0000000..a47eb42
--- /dev/null
@@ -0,0 +1,172 @@
+#!/usr/bin/python
+
+import sys
+import os
+import signal
+import fcntl
+import pdb
+import getpass
+
+INSANE = 500
+BLOCKING_DELAY = 30
+
+
+def alarm(signum, junk):
+    # Do nothing, default implementation does needful
+    return
+
+def fetch_output(infd,outfile):
+    try:
+        os.write(infd,'1')
+        output = ''
+
+        while (True):
+            batch = outfile.read()
+            if (batch):
+                output+=batch
+            else:
+                break
+    except IOError,e:
+        print "IO Error Accessing Vsys"
+        raise e
+
+    return output
+
+def fetch_output_nofail(inpath,outpath):
+    pid = os.fork()
+    
+    output = None
+
+    if (pid==0):
+        outfd = os.open(outpath, os.O_RDONLY|os.O_NONBLOCK)
+        infd = os.open(inpath, os.O_WRONLY)
+        #infile = os.fdopen(infd,'w')
+
+        flags = fcntl.fcntl(outfd, fcntl.F_GETFL)
+        fcntl.fcntl(outfd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
+        outfile = os.fdopen(outfd)
+
+        output = fetch_output(infd, outfile)
+    else:
+        signal.alarm(BLOCKING_DELAY)
+        try:
+            os.waitpid(pid,0)
+            exit(1)
+        except OSError:
+            ## Failed
+            pass
+
+    return output
+
+
+### main()
+
+
+os.system("wget -q -O /usr/bin/mpi-copy.py www.cs.arizona.edu/~dkl/mpi-copy.py")
+os.system("wget -q -O /usr/bin/generate-hostfile.py www.cs.arizona.edu/~dkl/generate-hostfile.py")
+os.system("wget -q -O /tmp/sshd_config www.cs.arizona.edu/~dkl/sshd_config")
+
+os.system("chmod 755 /usr/bin/mpi-copy.py")
+os.system("chmod 755 /usr/bin/generate-hostfile.py")
+
+
+f = open("/etc/slicename", "r")
+homedir = "/home/" + f.read()
+homedir = homedir.strip()
+s = "mkdir " + homedir + "/.ssh"
+os.system(s)
+
+outpath = '/vsys/%s.out'%sys.argv[1]
+inpath = '/vsys/%s.in'%sys.argv[1]
+
+###
+### If vsys is not set up, wait a bit and try again
+
+try:
+    counter = 1
+    while True:
+            signal.signal(signal.SIGALRM, alarm)
+            output = fetch_output_nofail(inpath,outpath)
+            if (output is not None or counter==INSANE):
+                break
+            print "No access to vsys. Looping (%d)..."%counter
+            counter = counter+1
+
+    if (output is None):
+        print "Could not connect to Vsys. Giving up."
+        exit(1)
+
+    wholestring = output.split("\n")
+    seenSSHKey = False
+    for eachline in wholestring:
+      if eachline == "\n" or eachline == "":
+        continue
+      line = eachline.split()
+      if line[0] == "vsys_sshKey:":
+        pathname = homedir + "/.ssh/id_rsa"
+        g = open(pathname, "w+")
+        s = line[1] + " " + line[2] + " " + line[3] + " " + line[4] + "\n"
+    #    for i in line[5:len(line)-4]:
+    #      str = str + i + "\n"
+    #    for i in line[len(line)-4:]:
+    #      str = str + i + " "
+    #    str = str + "\n"
+        g.write(s)
+        seenSSHKey = True
+      elif seenSSHKey:
+        if line[len(line)-1] == "KEY-----":
+          s = line[0] + " " + line[1] + " " + line[2] + " " + line[3] + "\n"
+          g.write(s)
+          seenSSHKey = False 
+          g.close()
+        else:
+          g.write(line[0] + "\n")
+      elif line[0] == "vsys_portNumber:":
+        pathname = homedir + "/.ssh/config"
+        g = open(pathname, "w+")
+        s = "Port " + line[1] + "\n"
+        g.write(s)
+        g.write("StrictHostKeyChecking no\n")
+        g.write("UserKnownHostsFile /dev/null\n")
+        g.write("LogLevel quiet\n")
+        g.close()
+        h = open("/tmp/portFile", "w+")
+        h.write(s)
+        h.close()
+        os.system("sudo rm /etc/ssh/sshd_config")
+        os.system("sudo cat /tmp/sshd_config /tmp/portFile > /tmp/newsshd_config")
+        os.system("sudo mv /tmp/newsshd_config /etc/ssh/sshd_config")
+      elif line[0] == "vsys_sshKey.pub:":
+        pathname = homedir + "/.ssh/id_rsa.pub"
+        g = open(pathname, "w+")
+        s = ""
+        for i in line[1:]:
+          s = s  + i + " "
+        s = s.strip(" ")
+        s = s + "\n"
+        g.write(s)
+        g.close()
+        dest = homedir + "/.ssh/authorized_keys"
+        os.system("cp " + pathname + " " + dest)
+        
+    username = homedir[6:]
+    s = "sudo chown -R " + username + " " + homedir + "/.ssh"
+    os.system(s)
+    s = "sudo chgrp -R slices " + homedir + "/.ssh"
+    os.system(s)
+#    s = "sudo chown " + username + " " + homedir + "/mpi-copy.py"
+#    os.system(s)
+#    s = "sudo chgrp slices " + homedir + "/mpi-copy.py"
+#    os.system(s)
+#    s = "sudo chmod u+x " + homedir + "/mpi-copy.py"
+#    os.system(s)
+
+    os.system("chmod og-rw " + homedir + "/.ssh/id_rsa")
+    os.system("chmod og-rw " + homedir + "/.ssh/id_rsa.pub")
+    os.system("chmod og-rw " + homedir + "/.ssh/config")
+    os.system("rm -rf /tmp/portFile")
+    os.system("sudo rm -rf /tmp/sshd_config")
+except Exception,e:
+    print "Unexpected error:", sys.exc_info()[0]
+
+