Showing posts with label batch. Show all posts
Showing posts with label batch. Show all posts

Wednesday, June 12, 2013

Command line installation of vSphere Auto Deploy

After a long absence, partially motivated by changing my job while organizing a VMUG meeting, I`ve found a bit of time to restart working at the blog.
In  this post I am presenting command line installation of Auto Deploy. It is assumed that vCenter Server exists (in this case called vcsrv01.domain.test). The installation file is also needed - it can be found in vCenter ISO at vctools\VMware-autodeploy.exe. Before running any command, copy the exe file on the server where it will be installed or on a centralized repository (file share). Open a command prompt and run the command line installation:

start /wait D:\VC\vctools\VMware-autodeploy.exe /L1033 /v" /qn VM_PATH_DATA=D:\DATA VM_DATA_SIZE=4 VM_PORT=6501 VM_PORTADD=6502 VC_SERVER_ADDRESS=vcsrv01.domain.test VC_SERVER_PORT=80  VC_SERVER_USER=VC-Admin VC_SERVER_PASSWORD=AdminPass" 

Two types of arguments are passed in the command line:
  • first arguments for windows installer
    • /L1033 - english language
    • /qn - no user interface 
    • /v" " - passes arguments to msi
  •  second - software specific arguments:
    • VM_PATH_DATA - root destination data directory for the installation 
    • VM_DATA_SIZE - space for VM_PATH_DATA in GB
    • VM_PORT - Auto Deploy service TCP port number
    • VM_PORTADD - Auto Deploy management TCP port number
    • VC_SERVER_ADDRESS - IP or FQDN for vCenter Server
    • VC_SERVER_PORT - vCenter Server port number
    • VC_SERVER_USER - vCenter Server user name
    • VC_SERVER_PASSWORD - vCenter Server user password
Several VMware components can be installed or upgraded using command line parameters. For more information see the following technical paper from VMware.

Friday, February 22, 2013

Guest customization - vCloud Director and vSphere

One nice feature that VMware Tools brings is the possibility to inject and run scripts directly in the virtual machine - be it bash or batch. I`ve been using this feature called guest customization when deploying new virtual machines in either a vCloud organization or directly in vSphere environment.

Guest customization in vSphere using PowerCLI

I`ll start with vSphere, since I`ve done more customization in this environment than in vCloud. The task is simple: deploy a linux VM from a template and provide ssh connectivity. After template deployment, the VM runs and empty OS - no IPs, no routes, DNS, repositories... nothing. The solution is the following PowerCLI cmdlet: Invoke-VMScript. I will not explain the syntax since it can be found here. I`ll just show what it can do in next piece of code:

Start-VM $vmname
 

### check VMtools status 
$vmtoolsstatus = ""
do {
Start-Sleep -Seconds 5
$vm = Get-VM $vmname
$vmtoolsstatus = $vm.Guest.ExtensionData.ToolsRunningStatus
echo "Tools not running... waiting 5 seconds"
} while ($vmtoolsstatus -ne "guestToolsRunning")

echo "configuring hostname"

$hostnamecfg = "sed 's/HOSTNAME\(.*\)/HOSTNAME=${vmname}.${domain}/g' -i /etc/sysconfig/network; hostname $vmname"

Invoke-VMScript -VM $(Get-VM $vmname) -GuestUser $GuestUser -GuestPassword $GuestPassword  -ScriptType bash -ScriptText $hostnamecfg

First the VM is started. Then a loop checks that VMware Tools have started. After, the script is being build in text variable $hostnamecfg - in this case a sed in /etc/sysconfig/network file (RedHat based distro). Last, Invoke-VMScript cmdlet injects $hostnamecfg script in the guest OS and runs it. This way you can set up all network interfaces, routes, yum repositories, ntp, dns, ssh, start-stop services. For example changing yum repo for CentOS to a local repository:


echo "configuring YUM repo"
if ( $template -eq "template-CentOS6") {
$yumcfg = " sed -i '/mirrorlist=http:\/\/mirrorlist\.centos\.org/s/^/# /' /etc/yum.repos.d/CentOS-Base.repo;"
$yumcfg += " sed -i 's/#baseurl=http:\/\/mirror\.centos\.org/baseurl=http:\/\/yumlocal\.vmlab\.local/' /etc/yum.repos.d/CentOS-Base.repo;"
}

Invoke-VMScript -VM $(Get-VM $vmname) -GuestUser $GuestUser -GuestPassword $GuestPassword  -ScriptType bash -ScriptText $yumcfg 


Guest customization in vCloud Director


vCloud Director brings guest customization directly in the portal. After the VM has been deployed from a vApp template in virtual machine properties - Guest OS Customization, Customization Script field:


#!/bin/bash
if [ -f /etc/sysconfig/network-scripts/route-eth0 ]
then
sed -i 's/\.5\.1/\.52\.1/g' /etc/sysconfig/network-scripts/route-eth0
else
echo "192.168.60.0/24 via 192.168.52.1" >> /etc/sysconfig/network-scripts/route-eth0
fi


In the example above, a bash script checks to see if route file for eth0 exists. If it does, then it changes gateway for existing route (sed), if it does not it creates route-eth0  and adds the route. Upon Power on (or Power on and force recustomization) the script will be run inside guest OS. 

Adding a route for Windows OS will replace the bash script with a batch script:

@echo off
(
route add 192.168.60.0 mask 255.255.255.0 192.168.52.1 -p

)

The same rule as for vSphere applies: you can customize guest os (Linux or Windows) with whatever configuration you need and how much scripting allows it. I am not discussing workflow automation tools (vCenter Orchestrator), nor configuration management software (Puppet). It is just about making life easier for some repetitive tasks using basic scripting.