summaryrefslogtreecommitdiff
path: root/sysutils
diff options
context:
space:
mode:
authormanu <manu>2014-05-10 16:38:03 +0000
committermanu <manu>2014-05-10 16:38:03 +0000
commit51e4fe0a1af3857d24be0e206006fc21728e6eb6 (patch)
treebeb85af8b557f3380434057a643f5ff0e0f572fa /sysutils
parent96b5154a0b9a5d860b1bb16deefbb89bc4ace7c9 (diff)
downloadpkgsrc-51e4fe0a1af3857d24be0e206006fc21728e6eb6.tar.gz
Autoconfigure NetBSD VM on OpenStack
Diffstat (limited to 'sysutils')
-rw-r--r--sysutils/Makefile3
-rw-r--r--sysutils/openstack_init/DESCR2
-rw-r--r--sysutils/openstack_init/MESSAGE12
-rw-r--r--sysutils/openstack_init/Makefile29
-rw-r--r--sysutils/openstack_init/PLIST5
-rwxr-xr-xsysutils/openstack_init/files/openstack_init.py89
-rwxr-xr-xsysutils/openstack_init/files/openstack_init.sh19
-rwxr-xr-xsysutils/openstack_init/files/xen_network.py59
-rw-r--r--sysutils/openstack_init/files/xen_network.sh18
9 files changed, 235 insertions, 1 deletions
diff --git a/sysutils/Makefile b/sysutils/Makefile
index 5dd42c6ed78..0c753e52ff6 100644
--- a/sysutils/Makefile
+++ b/sysutils/Makefile
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.596 2014/05/03 19:11:08 wiz Exp $
+# $NetBSD: Makefile,v 1.597 2014/05/10 16:39:06 manu Exp $
#
COMMENT= System utilities
@@ -334,6 +334,7 @@ SUBDIR+= nvnet
SUBDIR+= nvramtool
SUBDIR+= oak
SUBDIR+= open-vm-tools
+SUBDIR+= openstack_init
SUBDIR+= openxenmanager
SUBDIR+= osname
SUBDIR+= p5-File-Copy-Recursive
diff --git a/sysutils/openstack_init/DESCR b/sysutils/openstack_init/DESCR
new file mode 100644
index 00000000000..fb82c7fb5d8
--- /dev/null
+++ b/sysutils/openstack_init/DESCR
@@ -0,0 +1,2 @@
+This package provides two startup scripts to autoconfigure
+a NetBSD VM on Openstack.
diff --git a/sysutils/openstack_init/MESSAGE b/sysutils/openstack_init/MESSAGE
new file mode 100644
index 00000000000..4d8af7b8e52
--- /dev/null
+++ b/sysutils/openstack_init/MESSAGE
@@ -0,0 +1,12 @@
+===========================================================================
+$NetBSD: MESSAGE,v 1.1 2014/05/10 16:38:03 manu Exp $
+
+Add the following in /etc/rc.conf
+
+# to configure hostname, root password and SSH keys from CD-ROM (run once)
+openstack_init=YES
+
+# to autoconfigure the network from hypervisor information (run on each boot)
+xen_network=YES
+
+===========================================================================
diff --git a/sysutils/openstack_init/Makefile b/sysutils/openstack_init/Makefile
new file mode 100644
index 00000000000..70f10636d24
--- /dev/null
+++ b/sysutils/openstack_init/Makefile
@@ -0,0 +1,29 @@
+# $NetBSD: Makefile,v 1.1 2014/05/10 16:38:03 manu Exp $
+#
+
+DISTNAME= openstack_init-1.0
+#PKGREVISION= 1
+CATEGORIES= sysutils
+MASTER_SITES= # none
+DISTFILES= # none
+
+MAINTAINER= pkgsrc-users@NetBSD.org
+COMMENT= Autoconfigure NetBSD VM on OpenStack
+LICENSE= original-bsd
+
+WRKSRC= ${WRKDIR}
+NO_BUILD= yes
+USE_LANGUAGES= # none
+
+do-extract:
+ cd ${FILESDIR} && cp *.py *.sh ${WRKSRC}/
+
+RCD_SCRIPTS= xen_network openstack_init
+
+do-install:
+ ${INSTALL_PROGRAM_DIR} ${DESTDIR}${PREFIX}/sbin
+ ${INSTALL_SCRIPT} ${WRKSRC}/xen_network.py ${DESTDIR}${PREFIX}/sbin/
+ ${INSTALL_SCRIPT} ${WRKSRC}/openstack_init.py ${DESTDIR}${PREFIX}/sbin/
+
+.include "../../lang/python/application.mk"
+.include "../../mk/bsd.pkg.mk"
diff --git a/sysutils/openstack_init/PLIST b/sysutils/openstack_init/PLIST
new file mode 100644
index 00000000000..0870f8c6354
--- /dev/null
+++ b/sysutils/openstack_init/PLIST
@@ -0,0 +1,5 @@
+@comment $NetBSD: PLIST,v 1.1 2014/05/10 16:38:03 manu Exp $
+sbin/xen_network.py
+share/examples/rc.d/xen_network
+sbin/openstack_init.py
+share/examples/rc.d/openstack_init
diff --git a/sysutils/openstack_init/files/openstack_init.py b/sysutils/openstack_init/files/openstack_init.py
new file mode 100755
index 00000000000..13c074e2ded
--- /dev/null
+++ b/sysutils/openstack_init/files/openstack_init.py
@@ -0,0 +1,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")
+
+
+
diff --git a/sysutils/openstack_init/files/openstack_init.sh b/sysutils/openstack_init/files/openstack_init.sh
new file mode 100755
index 00000000000..5ce29ae640c
--- /dev/null
+++ b/sysutils/openstack_init/files/openstack_init.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+# PROVIDE: openstack_init
+# REQUIRE: mountcritlocal
+# BEFORE: SERVERS
+
+$_rc_subr_loaded . /etc/rc.subr
+
+name="openstack_init"
+start_cmd="openstack_init"
+
+openstack_init()
+{
+ test "x`hostname`" = "x" && eval $( /usr/pkg/sbin/openstack_init.py )
+}
+
+load_rc_config $name
+run_rc_command "$1"
+
diff --git a/sysutils/openstack_init/files/xen_network.py b/sysutils/openstack_init/files/xen_network.py
new file mode 100755
index 00000000000..3f2c91be48f
--- /dev/null
+++ b/sysutils/openstack_init/files/xen_network.py
@@ -0,0 +1,59 @@
+#!/usr/pkg/bin/python2.7
+
+import json
+import subprocess
+import time
+import sys
+
+def printf(format, *args):
+ sys.stdout.write(format % args)
+
+ifs = dict()
+dns = list()
+
+for line in subprocess.check_output(["/sbin/ifconfig", "-a"]).split("\n"):
+ if line and line[0] != "\t":
+ ifname = line.split(":")[0]
+ mac = None
+ if line and line.find("address:") != -1:
+ mac = line.split(": ")[1].replace(":", "").upper()
+ ifs[mac] = ifname
+
+
+for n, k in enumerate(ifs):
+ cmd = "/usr/pkg/bin/xenstore-read"
+ path = "vm-data/networking/" + k
+ ifconfig = json.loads(subprocess.check_output([cmd, path]))
+
+ if "dns" in ifconfig:
+ for p, l in enumerate(ifconfig["dns"]):
+ if not l in dns:
+ dns.append(l)
+
+ if "ips" in ifconfig:
+ for p, l in enumerate(ifconfig['ips']):
+ if l['enabled']:
+ cmd = "ifconfig %s inet %s netmask %s;\n"
+ printf(cmd, ifs[k], l['ip'], l['netmask'])
+
+ if "ip6s" in ifconfig:
+ for p, l in enumerate(ifconfig['ip6s']):
+ if l['enabled']:
+ cmd = "ifconfig %s inet6 %s/%d;\n"
+ printf(cmd, ifs[k], l['ip'], l['netmask'])
+
+ if "routes" in ifconfig:
+ for p, l in enumerate(ifconfig["routes"]):
+ cmd = "route add -net %s -netmask %s %s;\n"
+ printf(cmd, l["route"], l["netmask"], l["gateway"])
+
+ if "gateway" in ifconfig and ifconfig["gateway"]:
+ printf("route add default %s;\n", ifconfig["gateway"])
+
+ if "gateway_v6" in ifconfig:
+ printf("route add -inet6 default %s;\n", ifconfig["gateway_v6"])
+
+date = time.strftime("%Y-%m-%d %H:%M:%S")
+printf("echo '# autogenerated on %s' > /etc/resolv.conf;\n", date)
+for n, k in enumerate(dns):
+ printf("echo 'nameserver %s' >> /etc/resolv.conf;\n", k)
diff --git a/sysutils/openstack_init/files/xen_network.sh b/sysutils/openstack_init/files/xen_network.sh
new file mode 100644
index 00000000000..4122ffd92ce
--- /dev/null
+++ b/sysutils/openstack_init/files/xen_network.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+# PROVIDE: xen_network
+# REQUIRE: xenguest network
+
+$_rc_subr_loaded . /etc/rc.subr
+
+name="xen_network"
+start_cmd="network_start"
+
+network_start()
+{
+ eval $( @PREFIX@/sbin/xen_network.py )
+}
+
+load_rc_config $name
+run_rc_command "$1"
+