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