This utility can be used to configure an environment for a virtual
cluster. It sets up entries in /etc/hosts, creates the necessary
directory structure, and generates helper scripts.

Documentation for virtual clusters will come in a later patch.
---
 .gitignore              |    1 +
 Makefile.am             |    9 +++
 tools/vcluster-setup.in |  167 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 177 insertions(+), 0 deletions(-)
 create mode 100644 tools/vcluster-setup.in

diff --git a/.gitignore b/.gitignore
index 393495e..2571df7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -93,6 +93,7 @@
 # tools
 /tools/kvm-ifup
 /tools/ensure-dirs
+/tools/vcluster-setup
 
 # scripts
 /scripts/gnt-backup
diff --git a/Makefile.am b/Makefile.am
index 0cd3cfa..e002f85 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -160,6 +160,7 @@ CLEANFILES = \
        $(man_MANS) \
        $(manhtml) \
        tools/kvm-ifup \
+       tools/vcluster-setup
        stamp-directories \
        stamp-srclinks \
        $(nodist_pkgpython_PYTHON) \
@@ -665,6 +666,9 @@ dist_tools_SCRIPTS = \
        tools/xm-console-wrapper \
        tools/master-ip-setup
 
+nodist_tools_SCRIPTS = \
+       tools/vcluster-setup
+
 pkglib_python_scripts = \
        daemons/import-export \
        tools/check-cert-expired
@@ -706,6 +710,7 @@ EXTRA_DIST = \
        $(pkglib_python_scripts) \
        devel/upload.in \
        tools/kvm-ifup.in \
+       tools/vcluster-setup.in \
        $(docdot) \
        $(docpng) \
        $(docrst) \
@@ -1037,6 +1042,10 @@ devel/upload: devel/upload.in $(REPLACE_VARS_SED)
        sed -f $(REPLACE_VARS_SED) < $< > $@
        chmod u+x $@
 
+tools/vcluster-setup: tools/vcluster-setup.in $(REPLACE_VARS_SED)
+       sed -f $(REPLACE_VARS_SED) < $< > $@
+       chmod +x $@
+
 daemons/%:: daemons/%.in $(REPLACE_VARS_SED)
        sed -f $(REPLACE_VARS_SED) < $< > $@
        chmod +x $@
diff --git a/tools/vcluster-setup.in b/tools/vcluster-setup.in
new file mode 100644
index 0000000..03bc572
--- /dev/null
+++ b/tools/vcluster-setup.in
@@ -0,0 +1,167 @@
+#!/bin/bash
+#
+
+# Copyright (C) 2012 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+set -e -u -o pipefail
+
+readonly self=$(readlink -f $0)
+readonly ensure_dirs=@PKGLIBDIR@/ensure-dirs
+
+usage() {
+  echo "Usage: $0 <directory>"
+  #echo
+  #echo 'Options:'
+  #echo "  -H  Hostname"
+  #echo "  -P  Port (defaults to $default_port)"
+}
+
+# Variables for options
+nodecount=5
+networkprefix=192.0.2
+
+# Parse options
+while getopts :h opt; do
+  case "$opt" in
+    h)
+      usage
+      exit 0
+    ;;
+    \?)
+      echo "Invalid option: -$OPTARG" >&2
+      usage >&2
+      exit 1
+      ;;
+    :)
+      echo "Option -$OPTARG requires an argument" >&2
+      usage >&2
+      exit 1
+      ;;
+  esac
+done
+
+shift $((OPTIND - 1))
+
+if [[ "$#" != 1 ]]; then
+  usage
+  exit 1
+fi
+
+readonly rootdir=$1; shift
+
+if [[ ! -d "$rootdir" ]]; then
+  echo "Directory '$rootdir' does not exist!" >&2
+  exit 1
+fi
+
+node_hostname() {
+  local -r number="$1"
+
+  echo "node$((number + 1))"
+}
+
+node_ipaddr() {
+  local -r number="$1"
+
+  echo "$networkprefix.$((number + 10))"
+}
+
+setup_node() {
+  local -r number="$1"
+  local -r nodedir=$rootdir/$(node_hostname $number)
+
+  if [[ ! -d $nodedir ]]; then
+    mkdir $nodedir
+  fi
+
+  mkdir -vp \
+    $nodedir/etc/default \
+    $nodedir/var/lock\
+    $nodedir/var/{lib,log,run}/ganeti
+
+  GANETI_HOSTNAME=$(node_hostname $number) \
+  GANETI_ROOTDIR=$nodedir \
+  $ensure_dirs
+
+  local -r daemon_args="-b $(node_ipaddr $number)"
+
+  cat > $nodedir/etc/default/ganeti <<EOF
+  # Default settings for virtual node $i
+  NODED_ARGS='$daemon_args'
+  MASTERD_ARGS=''
+  RAPI_ARGS='$daemon_args'
+  CONFD_ARGS='$daemon_args'
+
+  export GANETI_ROOTDIR='$nodedir'
+  export GANETI_HOSTNAME='$(node_hostname $number)'
+EOF
+
+  cat > $nodedir/cmd <<EOF
+#!/bin/bash
+
+export GANETI_ROOTDIR='$nodedir'
+export GANETI_HOSTNAME='$(node_hostname $number)'
+
+bash -c "\$*"
+EOF
+  chmod +x $nodedir/cmd
+}
+
+setup_all_nodes() {
+  for ((i=0; i < nodecount; ++i)); do
+    setup_node $i
+  done
+}
+
+setup_etc_hosts() {
+  local -r tmpfile=/etc/hosts.vcluster.$$.$RANDOM
+  {
+    egrep -v "^$networkprefix.[[:digit:]]+[[:space:]]" /etc/hosts
+    echo "$networkprefix.1 cluster"
+    for ((i=0; i < nodecount; ++i)); do
+      echo "$(node_ipaddr $i) $(node_hostname $i)"
+    done
+    for ((i=1; i < 10; ++i)); do
+      echo "$networkprefix.$((i + 50)) instance$i"
+    done
+  } > $tmpfile && \
+  chmod 0644 $tmpfile && \
+  mv -v $tmpfile /etc/hosts
+}
+
+setup_network_interfaces() {
+  local ipaddr
+
+  #ip addr del "$networkprefix.1" dev eth0 || :
+  #ip addr add "$networkprefix.1" dev eth0
+
+  for ((i=0; i < nodecount; ++i)); do
+    ipaddr="$(node_ipaddr $i)/32"
+    ip addr del "$ipaddr" dev eth0 || :
+    ip addr add "$ipaddr" dev eth0
+  done
+}
+
+#set -x
+setup_all_nodes
+setup_etc_hosts
+setup_network_interfaces
+
+exit 0
+
+# vim: set sw=2 sts=2 et :
-- 
1.7.6

Reply via email to