4 min read

How I auto-start my dev environment on Windows 10

Starting all components for development is hectic whenever you restart your machine, this thing can be automated using batch script.

Developers face lot of challenges with infrastructure setup and other non-dev activities for local development. For non-local environments you should have infrastructure provisioning knowledge or DevOps support.


Whenever I start development of any new project I start with creation of virtual environment for the same, you can provision virtual environment on any operating system but I prefer Unix/Linux based OS for development and can rely on windows for other tooling. So I prefer installing windows and making virtual machine with Linux based OS for development.

Let's list out all software's and tools required for development:

  1. Windows 10 - Base operating system for booting machine
  2. Virtual Box - Virtualization software for installing Linux
  3. Docker - For running my software in container
  4. Putty - For accessing linux shell from windows

Everytime whenever you start your machine you need to start all components to start the development. In DevOps world we have saying "Automate the task which you are doing twice!"

Start virtual machine:

We can configure to start each component in a bash script. First of all we need to start our virtual box machine.

  1. Add virtualbox bin path to PATH variable. It is virtualbox installation directory and should look like: C:\Program Files\Oracle\VirtualBox. This path should have VBoxManage executable. For more information on adding path variable, click here
  2. After adding path variable you should be able to run VBoxManage command from the command prompt, if the command is not recognized try to restart command prompt and recheck path variable.
  3. Using VBoxManage you can manage your virtual machines i.e. Start, Stop, Restart particular virtual machine
VBoxManage command

Enabling Docker on Virtual Machine

Once virtual machine is started you need to start docker and other required services which are needed for you development.

Once you have installed docker you can enable it's service to start it on boot-up using below command:

#Enable docker on linux Centos/RHEL
systemctl enable docker

Enable Docker to Start at boot

Setup password-less SSH Connection

Password less connectivity between windows machine and Linux is required for having prompt-less connection.

  1. Generate public-private key pair on windows machine using ssh-keygen utility
  2. Copy public key id_rsa.pub and paste at the bottom of authorized_keys file on linux machine

Configuring Putty

Once your virtual machine is started we need shell to interact with the machine. For that we need to first configure session in putty and load the configuration with it.

  1. Open putty, enter username@ip-address of your virtual machine in host name section
  2. Enter name for saved session in Saved Sessions box and hit save
  3. Go to Connection > SSH > Auth and select private key which you have created while setting up password-less connection
  4. Go to Session and save it

Configure Startup script in windows

Now all setup is done you can create a text file with extension ".bat" which is executable script in windows, you can instruct machine to do automated things using batch script.

  • Make output silent and start virtual machine, here you need to mention virtualbox machine name in my case it's ubuntu-devops, you can find it in settings window
VirtualBox Settings
  • Headless start will make the machine start in background without any UI (Dont' worry we will access terminal from Putty)
@echo off
VBoxManage startvm ubuntu-devops --type headless

Start VM in headless mode

  • This command will trigger startup of machine and we need to make sure if it's started or not for that we can use ping mechanism till machine starts.
ping -n "192.168.0.110" | find "TTL=" > nul

Ping virtual machine

  • If machine is not pingable then we need to try it again and if it's up and running then we need to open putty session. For this purpose we need to use looping in batch script like below
:pingpong
ping -n 3 192.168.0.110 | find "TTL=" >nul
if errorlevel 1 (
    echo VM is not reachable
	goto pingpong
) else (
    Rem Start putty command here
	goto end
)
:end

Looping in Batch scripting

  • For starting up putty session get the path of putty executable and with load option you can start the saved session from putty. Note that this will be password-less as we have set it up to use password-less ssh.
start C:\Users\Suhas\Desktop\putty.exe -load "<saved-session-name>"

Start putty from command line

  • Our final batch script will look like below, save it.
@echo off
VBoxManage startvm ubuntu-devops --type headless

:pingpong
ping -n 3 suhasvm | find "TTL=" >nul
if errorlevel 1 (
    echo suhasvm is not reachable
	goto pingpong
) else (
    start C:\Users\Suhas\Desktop\putty.exe -load "suhasvm"
	goto end
)
:end
exit 0

Startup Batch script

Configure script to start at Windows Startup

You can configure any software or batch script to start at windows startup by copying the script in Startup program.

To open configuration for startup goto run and type shell:startup, one window with startup programs will open, you can copy created batch script in this directory.

All Set! Just restart your windows machine and test the configuration. For any issues you can reach out to me.