Wednesday, November 29, 2017

After a while - automating VM builds

2 years since the last blog.
Many things changed. Professinaly and in life.

Anywasy quick to topic
The type of work i do these days is automating- some call it devops.
But I feel its the natural progression from development to more productive tasks.
Less code more automate.

I was given task of building VM’s -. Which normally is a hated job. Who would sit and run the scripts and setup a vm . Who would love the job.
My initial thought was to export it from AWS AMI.

We already had the automation doen to create AMI’s on the fly. This was done through hashicopr packer and ansible. Where ansibe did the configurations while packer did the build.
And a quick search took me to https://aws.amazon.com/ec2/vm-import/ . A service in AWS which helps you to import and export to VM form AMI . I was taken by this feature first without realising the hidden pits . So AWS only allows to export the AMI’s that was built form imported VM’s.
So what type of VM’s they allow to import - ‘windows VMs’ . hmm tricky business. icloud interoperability is still in dark ages.

Luckily we already had our automation in packer.
I had to modify the packer scripts to build the image in VMWare.

{
"builders": [
{
"boot_wait": "2s",
"vm_name": "xxxx",
"vmdk_name": "xxxxx",
"type": "vmware-iso",
"iso_url": "/Users/test/Downloads/CentOS-7-x86_64-Minimal-1708.iso",
"iso_checksum": "aae20c8052a55cf179af88d9dd35f1a889cd5773",
"iso_checksum_type": "sha1"
"ssh_username": "root",
"ssh_password": "xxxx",
"communicator": "ssh",
"ssh_pty": "true",
"headless": false,
"disk_size": 8000,
"guest_os_type": "linux",
"vmx_data": {
"cpuid.coresPerSocket": "1",
"memsize": "8000",
"numvcpus": "1"
},
"http_directory": "http",
"boot_command": [
" <tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
],
"ssh_wait_timeout": "10000s",
"shutdown_command": "/sbin/halt -p"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"sleep 50",
"echo 'done'"
]
}
]
}

And i came to know about hte kickstart file in linux. sometime back i have heard about it. But never had the time to actually explore it. The kickstart file is used to automate the intstallation process in linux. Its nothing but a set of command and configurations for the linux installation to consume and carry on automatically.

So to assist the script above I had to build a kickstart file. A typical kickstart fiel looks like


install
cdrom
lang en_US.UTF-8
keyboard us
timezone UTC
network --bootproto=dhcp
rootpw --plaintext xxx123
auth --enableshadow --passalgo=sha512 --kickstart
firewall --disabled --ssh --service=ssh
selinux --disabled
bootloader --location=mbr

text
skipx
zerombr

clearpart --all --initlabel
autopart

firstboot --disabled
reboot

%packages --instLangs=en_US.utf8 --nobase --ignoremissing --excludedocs
openssh-clients

@core
%end


%post --log=/root/ks.log

%end


Some of the issue si faced

==> vmware-iso: Error starting VM: VMware error: 2017-11-29T20:58:30.791| ServiceImpl_Opener: PID 19380
==> vmware-iso: Error: The operation was canceled
==> vmware-iso: Waiting 1.384998s to give VMware time to clean up...
==> vmware-iso: Deleting output directory...
Build 'vmware-iso' errored: Error starting VM: VMware error: 2017-11-29T20:58:30.791| ServiceImpl_Opener: PID 19380
Error: The operation was canceled

This happened the first few times i tried to run the packer 'build command’ . This normally happens when VMware cannot create the vm that you have mentioned. normally its issue with the CPU, no of virtual core, RAM . In my case it was the ram which i set to 32GB and since my lappie was only 32GB it couldnt create the VM as such.

The next error i faced was

==> vmware-iso: Waiting 2s for boot...
==> vmware-iso: Connecting to VM via VNC
==> vmware-iso: Typing the boot command over VNC...
==> vmware-iso: Waiting for SSH to become available...
^C==> vmware-iso: Stopping virtual machine...
==> vmware-iso: Deleting output directory...
Build 'vmware-iso' errored: Build was cancelled.
Cleanly cancelled builds after being interrupted.


Here the screen just keep waiting for ssh connection to run the ssh provisioner. Actually the packer here waits for the os installation to complete in the VM.
You can test whats happeneing by setting the headless: false . That way when packer run it spins up the terminal in VMware where all the action happens. Now in my case the issue was that the boot_command was not proper and it dint kickstart the installation of OS. I had to murk around with the centos specicif boot command to kickstart the kickstart file.

Once that was done - it was the momnet of bliss. The entire VM creation is now automated. without a single button click. I use to rember how tedious it use to be when i first tried to install linux. Today this can be done with customisation like 3-4 times in a couple of minutes. Automation rocks :)