Annotation of pkgsrc/sysutils/openstack_init/files/openstack_init.py, revision 1.1

1.1     ! manu        1: #!/usr/pkg/bin/python2.7
        !             2: 
        !             3: import json
        !             4: import base64
        !             5: import subprocess
        !             6: import tempfile
        !             7: import os
        !             8: import sys
        !             9: 
        !            10: def printf(format, *args):
        !            11:     sys.stdout.write(format % args)
        !            12: 
        !            13: disks = list()
        !            14: config = dict()
        !            15: 
        !            16: # shut down /dev/stderr
        !            17: os.close(2)
        !            18: 
        !            19: cmd = "/sbin/sysctl -n hw.disknames"
        !            20: for disk in subprocess.check_output(cmd.split(" ")).split(" "):
        !            21:        cmd = "/sbin/disklabel " + disk
        !            22:        p = subprocess.Popen(cmd.split(" "), stdout=subprocess.PIPE)
        !            23:        disklabel = p.communicate()[0].split("\n")
        !            24:        for line in disklabel:
        !            25:                if line and line.find("ISO9660") != -1:
        !            26:                        letter = line.split(":")[0].replace(" ", "")
        !            27:                        disks.append("/dev/" + disk + letter)
        !            28: 
        !            29: for n, dev in enumerate(disks):
        !            30:        mount_cmd = "/sbin/mount " + dev + " /mnt"
        !            31:        umount_cmd = "/sbin/umount " + dev
        !            32:        meta_data_file = "/mnt/openstack/latest/meta_data.json"
        !            33:        try:
        !            34:                subprocess.call(mount_cmd.split(" "))
        !            35:        except:
        !            36:                next
        !            37: 
        !            38:        try:
        !            39:                config = json.load(open(meta_data_file))
        !            40:        except:
        !            41:                try:
        !            42:                        subprocess.call(umount_cmd.split(" "))
        !            43:                except:
        !            44:                        next
        !            45:                next
        !            46: 
        !            47:        try:
        !            48:                subprocess.call(umount_cmd.split(" "))
        !            49:        except:
        !            50:                next
        !            51: 
        !            52:        # or hostname?
        !            53:        if "name" in config:
        !            54:                printf("hostname %s;\n", config["name"])
        !            55:                printf("echo '%s' > /etc/myname;\n", config["name"])
        !            56: 
        !            57:        if "random_seed" in config:
        !            58:                tmp = tempfile.mkstemp()
        !            59:                os.write(tmp[0], base64.b64decode(config["random_seed"]))
        !            60:                printf("rndctl -L %s;\n", tmp[1])
        !            61:                os.close(tmp[0])
        !            62: 
        !            63:        if "public_keys" in config:
        !            64:                printf("mkdir -p /root/.ssh\n");
        !            65:                for n, k in enumerate(config['public_keys']):
        !            66:                        cmd = "echo '%s %s' >> /root/.ssh/authorized_keys;\n"
        !            67:                        printf(cmd, config['public_keys'][k], k)
        !            68: 
        !            69:        if "admin_pass" in config:
        !            70:                key = config["admin_pass"] + "\n"
        !            71:                PIPE = subprocess.PIPE
        !            72:                p = subprocess.Popen("pwhash", stdin=PIPE, stdout=PIPE)
        !            73:                hash = p.communicate(key)[0].replace("\n", "")
        !            74:                
        !            75:                ptmp = open("/etc/ptmp", "wx")
        !            76:                pwd = open("/etc/master.passwd", "r")
        !            77:                lines = pwd.readlines()
        !            78:                pwd.close()
        !            79:                for line in lines:
        !            80:                        if line.find("root:") == 0:
        !            81:                                fields = line.split(":")
        !            82:                                fields[1] = hash
        !            83:                                line = ":".join(fields)
        !            84:                        ptmp.write(line)
        !            85:                ptmp.close()
        !            86:                printf("pwd_mkdb -p /etc/ptmp;\n")
        !            87:                
        !            88: 
        !            89: 

CVSweb <webmaster@jp.NetBSD.org>