Vagrant, VitualBox, and Apache - Part 2

To continue from yesterdays post (ref: Vagrant, VitualBox, and Apache), we need to provision our vagrant windows vm to our system settings. For me, I like to have everything environment related in my base dir, and my actual web environment in a "wwwroot" directory. So my Apache Document Root needs to change to:

DocumentRoot "/vagrant/wwwroot"  

I could spin up Vagrant with the GUI turned on and change the settings required manually, but that defeats the purpose of having vagrant configure my vm.

There are several provisioners available out there, but I wanted to be quick about things, so I used Windows PowerShell which vagrant can pass to my windows guest vm. I am using PowerShell to stop Apache and ColdFusion, change there configuration files, and start them back up.

# Update Apache Config File
# Change DocumentRoot
# Change Error Log Paths
$setupApacheServerScript = <<SCRIPT
$Apache = "Apache2.2"
$httpdConf = "C:\\Apache2.2\\conf\\httpd.conf"

Write-Output "Stopping $Apache"  
Stop-Service $Apache  
Write-Output "$Apache Stopped"

Write-Output "Update Apache Configuration"  
(Get-Content $httpdConf) | Foreach-Object {$_ -replace "/Apache2.2/htdocs", '/vagrant/wwwroot'} | Set-Content $httpdConf 
(Get-Content $httpdConf) | Foreach-Object {$_ -replace '"[^\.]+/([a-zA-Z]+\.log)"', '"/vagrant/logs/apache/$1"'} | Set-Content $httpdConf 
Write-Output "Apache Configuration Updated"  
SCRIPT

# Start Apache
$startApacheScript = <<SCRIPT
$apache = "Apache2.2"
Write-Output "Starting $apache"  
Restart-Service $apache  
Write-Output "$apache is: Running"  
SCRIPT

config.vm.provision "shell", inline: $setupApacheServerScript  
config.vm.provision "shell", run: "always", inline: $startApacheScript  

As you may remember, we disabled apache from starting automatically in the last post when starting up the box. We did this because when we change the DocumentRoot to our sync'd folder, apache wont start on startup because vagrant adds those folder after windows, and it services (including apache), starts. Vagrant provisioning occurs after the sync'd folders are created. So the $startApacheScript script runs after the directories are created, so apache will start!.

I have written a similar script to handle the ColdFusion Logging.

PowerShell Service Warnings!

The main issue I needed to overcome was the warnings that are thrown by PowerShell when starting, stopping, and resetting a service. These messages are actually a warning produced by PowerShell saying that its still waiting for the action to be completed. you can stop these warnings with some additional parameters

...
Restart-Service $apache -WarningAction SilentlyContinue  
...

This will prevent those warnings from occurring.

The next challenge is was that I wanted to what for the process to be completed before continuing to the next process. This is solved with a Do-While script.

...
Restart-Service $apache -WarningAction SilentlyContinue  
do { Start-Sleep -Milliseconds 200} until ((get-service $apache).status -eq 'Running')  
...

Conclusion

I now have provisioning scripts to configure my vagrant vm, and restart services to ensure my VM is working every time.