#!/bin/bash
# Copyright (c) 2008 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# 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.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# fail on first error and on unset variables
set -u -e

GENMAC=/usr/share/logilab-vm/tools/gen_mac.pl

#BEGINPARAM
#NAME= # Name of vm
#MEMORY=512000 # Memory allocated
#HOSTNAME=
#DOMAINNAME=
#TEMPLATE=debian-4.0-x86 # Which template the system is based on
#NET1_IP=
#NET1_CIDR=24
#NET2_IP=
#NET2_CIDR=24
#GATEWAY=
#NAMESERVER1=213.215.7.51
#NAMESERVER2=213.215.7.52
#ENDPARAM

NET1_MAC_INT=$($GENMAC -n 2 -i $NET1_IP | awk 'NR == 1 { print $0 }')
NET1_MAC_EXT=$($GENMAC -n 2 -i $NET1_IP | awk 'NR == 2 { print $0 }')
NET2_MAC_INT=$($GENMAC -n 2 -i $NET2_IP | awk 'NR == 1 { print $0 }')
NET2_MAC_EXT=$($GENMAC -n 2 -i $NET2_IP | awk 'NR == 2 { print $0 }')
logilab-vm-create --verbose \
    --type openvz \
    --sys name=$NAME,mem=$MEMORY,host=$HOSTNAME,domain=$DOMAINNAME,ostemplate=$TEMPLATE,ns=$NAMESERVER1,ns=$NAMESERVER2,gw=$GATEWAY \
    --dev template=$TEMPLATE \
    --net method=user,mac=$NET1_MAC_EXT,mac_int=$NET1_MAC_INT,ip=$NET1_IP,cidr=$NET1_CIDR \
    --net method=user,mac=$NET2_MAC_EXT,mac_int=$NET2_MAC_INT,ip=$NET2_IP,cidr=$NET2_CIDR
