ユーザ用ツール

サイト用ツール


サイドバー

Index

はじめてのおつかい





DokuWiki整形記法


PlayGround



serverapps:configmgmt:xen_autometer_perl

xenserver-autometerとPerlでVMを一気に構築してみる

knife-xenserverとxenserver-autometerでVMを一気に構築してみるでchefのknifeを使っているが諸般の事情でchefの利用は延期となったため、素のxenserver-automaterとperlで代用のコマンドを作成します。

テンプレートの用意までは以下を参照してください

FreeBSDにxenserver-automatorを移植するのスクリプト利用すると、FreeBSDでも同様にVMを一気に構築することができます。

Requirement

perlと以下のモジュールを使用しています。

使い方

 xen-domu-net-config --xensv_host hostname|IP adress
                     [--xensv_user user]
                     [--xensv_password SECRET]
                     --vm_template tempname|UUID
                     [--vm_name vmhost] --vm_ip 192.168.11.111
                     [--vm_netmask 255.255.255.0] [--vm_gateway 192.168.11.1]
                     [--vm_dns 192.168.11.2] [--vm_domain example.com]
オプション説明デフォルト必須
–xensv_hostXenサーバホスト名またはIPxensv 192.168.11.5なし必須
–xensv_userXenサーバLoginユーザ名usernameroot
–xensv_passwordXenサーバLoginユーザPasswordSECRET指定しない場合問い合わせ
–vm_template使用するTemplate名またはUUID534e2779-310c-4dbc-bb40-0ab8a2da11e8なし必須
–vm_name作成するVMのホスト名vm009ランダム文字列
–vm_ipVMに付与するIP192.168.11.99なし必須
–vm_netmaskネットマスク255.255.255.0255.255.255.0
–vm_gatewayデフォルトゲートウェイ192.168.11.1なし
–vm_dnsDNSサーバIP192.168.11.2なし
–vm_domainVMのドメインexample.comなし

実行例

実際には1行です

# xen-domu-net-config  --xensv_host xensv01 --xensv_password SECRET
 --vm_template 534e2928-efb4-4b0d-8e5b-63ef69de295a
 --vm_name vm001 --vm_ip 192.168.11.100 --vm_gateway 192.168.11.1 
 --vm_dns 192.168.11.2 --vm_domain example.com

Connecting Xen Server Dom0.
create new vm: vm001 UUID: 534e2a1e-4e28-488a-9660-34df646c3cf1
Booting new vm: vm001

作成されたVMで確認

# cat /etc/hosts
127.0.0.1       localhost
192.168.11.100     vm001.example.com   vm001

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="static"
#BROADCAST=
DNS1="192.168.11.2"
GATEWAY="192.168.11.1"
#HWADDR=
IPADDR="192.168.11.100"
NETMASK="255.255.255.0"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
#UUID=

ソース

xen-domu-net-config.pl
#!/usr/bin/perl
#
# XenServer DomU vm-install with IP configuration
#
# Written by T.Hayashi/Rookie Inc.
#   ver 1.0     2014.04.14  Init
#
#
 
use strict;
use warnings;
use utf8;
use 5.010;
 
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat);
use Pod::Usage;
 
use Net::OpenSSH;
use String::Random;
 
 
# #######################################
#
# Get commandline option (コマンドラインオプション取得)
#
GetOptions(
    \my %opt, qw/
    man
    help
    xensv_host=s
    xensv_user=s
    xensv_password=s
    vm_template=s
    vm_name=s
    vm_ip=s
    vm_netmask=s
    vm_gateway=s
    vm_dns=s
    vm_domain=s
/) or pod2usage(2);
 
# HELP 表示
pod2usage(1) if $opt{help};
pod2usage(-exitstatus => 0, -verbose => 2) if $opt{man};
 
 
# 必須オプションのチェック
my @required_options = qw/xensv_host vm_template vm_ip/;
pod2usage(2) if grep {!exists $opt{$_}} @required_options;
 
# デフォルト値を指定
$opt{xensv_user}="root" unless $opt{xensv_user};
$opt{vm_name}= String::Random->new->randregex('[A-Za-z0-9]{8}') unless $opt{vm_name};
$opt{vm_netmask}= "255.255.255.0" unless $opt{vm_netmask};
$opt{vm_gateway}= "" unless $opt{vm_gateway};
$opt{vm_dns}= "" unless $opt{vm_dns};
$opt{vm_domain}= "" unless $opt{vm_domain};
 
# ログイン認証
say 'Connecting Xen Server Dom0.';
my $ssh;
if ($opt{xensv_password}){
    $ssh = Net::OpenSSH->new(
          $opt{xensv_host}
        ,(user => $opt{xensv_user},password => $opt{xensv_password})
        , master_opts => [-o => "StrictHostKeyChecking=no"]
    );
}else{
    $ssh = Net::OpenSSH->new(
          $opt{xensv_host}
        ,(user => $opt{xensv_user})
        , master_opts => [-o => "StrictHostKeyChecking=no"]
    );
}
$ssh->error and die "Can't ssh to ". $opt{xensv_host} .": " . $ssh->error;
 
 
# create new vm
my $vmuuid = $ssh->capture('xe vm-install template=' . $opt{vm_template} . ' new-name-label=' . $opt{vm_name})
    or die "xe vm-install failed: " . $ssh->error;
chomp($vmuuid);
 
say 'create new vm: ' . $opt{vm_name} . ' UUID: ' . $vmuuid;
 
 
# XenStore param set
# IP
$ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/ip=' . $opt{vm_ip})
    or die "xe vm-param-set failed: " . $ssh->error;
# Gateway
$ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/gw=' . $opt{vm_gateway})
    or die "xe vm-param-set failed: " . $ssh->error;
# Netmask
$ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/nm=' . $opt{vm_netmask})
    or die "xe vm-param-set failed: " . $ssh->error;
# Nameserver
$ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/ns=' . $opt{vm_dns})
    or die "xe vm-param-set failed: " . $ssh->error;
# domain
$ssh->system('xe vm-param-set uuid=' . $vmuuid . ' xenstore-data:vm-data/dm=' . $opt{vm_domain})
    or die "xe vm-param-set failed: " . $ssh->error;
 
 
# Boot new vm
$ssh->system('xe vm-start uuid=' . $vmuuid)
    or die "xe vm-start failed: " . $ssh->error;
 
say 'Booting new vm: ' . $opt{vm_name};
 
 
#
#-------- END -------------------------------------------------------------
#
__END__
 
=head1 NAME
 
xen-domu-net-config - XenServer DomU vm-install with IP configuration
 
=head1 SYNOPSIS
 
 xen-domu-net-config --xensv_host hostname|IP adress
                     [--xensv_user user]
                     [--xensv_password SECRET]
                     --vm_template tempname|UUID
                     [--vm_name vmhost] --vm_ip 192.168.11.111
                     [--vm_netmask 255.255.255.0] [--vm_gateway 192.168.11.1]
                     [--vm_dns 192.168.11.2] [--vm_domain example.com]
 
 
 Options:
 
   --xensv_host         XenServer Hostname or IP address
   --xensv_user         XenServer Logon User name(Default root)
   --xensv_password     XenServer Logon password
                        (if not exist this option, inquire Password)
   --vm_template        Used VM template name or UUID
   --vm_name            New VM hostname(Default Random name)
   --vm_ip              New VM IP address
   --vm_netmask         New VM Netmask(Default 255.255.255.0)
   --vm_gateway         New VM gateway IP address(Default '')
   --vm_dns             New VM dns IP address(Default '')
   --vm_domain          New VM domain name(Default '')
 
   --help               brief help message
   --man                full documentation
 
=head1 OPTIONS
 
=over 4
 
=item B<--xensv_host hostname>
 
XenServer Hostname or IP address
 
=item B<--xensv_user user >
 
XenServer Logon User name(Default root)
 
=item B<--xensv_password Secret >
 
XenServer Logon password
 
=item B<--vm_template 53452a7b-4e10-403d-9ed5-042382b9e59d >
 
Used VM template name or UUID
 
=item B<--vm_name vmhost>
 
New VM hostname
 
=item B<--vm_ip 192.168.11.111 >
 
New VM IP address
 
=item B<--vm_netmask 255.255.255.0>
 
New VM Netmask
 
=item B<--vm_gateway 192.168.11.1 >
 
New VM gateway IP address
 
=item B<--vm_dns 192.168.11.2>
 
New VM dns IP address
 
=item B<--vm_domain example.com>
 
New VM domain name
 
=back
 
=head1 DESCRIPTION
 
B<This program> create new vm from template.
 
=head1 License(BSD License)
 
Copyright(C) 2014 Toshinori Hayashi <hayashi@rookie-inc.com>/Rookie Inc.
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 
=over 4
 
=item * Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.
 
=item * Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 
=item * Neither the name of the Toshinori Hayashi or Rookie Inc nor the names of
its contributors may be used to endorse or promote products derived from 
this software without specific prior written permission.
 
=back
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
 
=cut
serverapps/configmgmt/xen_autometer_perl.txt · 最終更新: 2017/04/14 14:21 (外部編集)