Skip to content

Instantly share code, notes, and snippets.

@mefellows
Last active March 1, 2021 15:07
Show Gist options
  • Save mefellows/7e25d5dba254d5d38b51 to your computer and use it in GitHub Desktop.
Save mefellows/7e25d5dba254d5d38b51 to your computer and use it in GitHub Desktop.
Machine Factories with Windows Part 1: Local Development with Vagrant
Configuration MyWebsite
{
param ($MachineName)
Import-DscResource -Module MyWebapp
Import-DscResource -Module cNetworking
Node $MachineName
{
WindowsFeature IIS
{
Ensure = "Present"
Name = "Web-Server"
}
WindowsFeature IISManagerFeature
{
Ensure = "Present"
Name = "Web-Mgmt-Tools"
}
WindowsFeature WebApp
{
Ensure = "Present"
Name = "Web-App-Dev"
IncludeAllSubFeature = $True
}
cFirewallRule webFirewall
{
Name = "WebFirewallOpen"
Direction = "Inbound"
LocalPort = "80"
Protocol = "TCP"
Action = "Allow"
Ensure = "Present"
}
SimpleWebsite sWebsite
{
WebAppPath = "c:\vagrant\urlsvc\ShortUrlWebApp"
DependsOn = '[cWebsite]DefaultWebsite'
}
}
}
packer build -only basebox-vbox vagrant.json
packer build -only devbox-vbox vagrant.json
vagrant plugin install vagrant-dsc
vagrant up
vagrant rsync-auto
Configuration SimpleWebsite
{
param
(
[String]$WebAppPath = "c:\myWebApp",
[String]$WebSiteName = "UrlSvc",
[String]$HostNameSuffix = "dev",
[String]$HostName = "api.urlsvc.${HostNameSuffix}",
[String]$ApiAppPoolName = "UrlSvc-API",
[HashTable]$AuthenticationInfo = @{Anonymous = "true"; Basic = "false"; Digest = "false"; Windows = "false"}
)
Import-DscResource -Module cWebAdministration
Import-DscResource -Module cNetworking
# Stop the default website
cWebsite DefaultSite
{
Ensure = "Absent"
Name = "Default Web Site"
State = "Stopped"
PhysicalPath = "C:\inetpub\wwwroot"
}
cWebAppPool APIAppPool
{
Name = $ApiAppPoolName
ApplicationName = "Default"
Ensure = "Present"
State = "Started"
IdentityType = "SpecificUser"
UserName = "vagrant"
Password = "vagrant"
}
cWebsite UrlSvcWebsite
{
Ensure = "Present"
Name = $WebSiteName
ApplicationPool = $ApiAppPoolName
BindingInfo = @(
SEEK_cWebBindingInformation
{
Protocol = "http"
Port = 80
HostName = $HostName
IPAddress = "*"
})
AuthenticationInfo = SEEK_cWebAuthenticationInformation
{
Anonymous = "true"
Basic = "false"
Digest = "false"
Windows = "false"
}
HostFileInfo = @(SEEK_cHostEntryFileInformation
{
RequireHostFileEntry = $True
HostEntryName = $HostName
HostIpAddress = "127.0.0.1"
})
PhysicalPath = $WebAppPath
State = "Started"
DependsOn = @("[cWebsite]DefaultSite")
}
}
{
"variables": {
"build_version": "1.0.0",
"ovf_source_path": "./output-basebox-vbox/machinefactory-api-1.0.0.ovf"
},
"builders": [
{
"type": "virtualbox-windows-iso",
"name": "basebox-vbox",
"vm_name": "machinefactory-api-{{user `build_version`}}",
"iso_url": "http://download.microsoft.com/download/6/2/A/62A76ABB-9990-4EFC-A4FE-C7D698DAEB96/9600.16384.WINBLUE_RTM.130821-1623_X64FRE_SERVER_EVAL_EN-US-IRM_SSS_X64FREE_EN-US_DV5.ISO",
"iso_checksum_type": "md5",
"iso_checksum": "458ff91f8abc21b75cb544744bf92e6a",
"winrm_username": "vagrant",
"winrm_password": "vagrant",
"winrm_wait_timeout": "10m",
"winrm_port":5985,
"headless": false,
"boot_wait": "1m",
"guest_additions_mode": "upload",
"shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
"guest_os_type": "Windows2012_64",
"disk_size": 61440,
"floppy_files": [
"./answer_files/2012_r2/Autounattend.xml",
"./scripts/microsoft-updates.bat",
"./scripts/win-updates.ps1",
"./scripts/oracle-cert.cer",
"./scripts/disable-password-complexity.ps1"
],
"vboxmanage": [
[
"modifyvm",
"{{.Name}}",
"--memory",
"2048"
],
[
"modifyvm",
"{{.Name}}",
"--cpus",
"2"
],
[
"modifyvm",
"{{.Name}}",
"--natpf1",
"guestwinrm,tcp,127.0.0.1,5985,,5985"
]
]
},
{
"type": "virtualbox-windows-ovf",
"name": "devbox-vbox",
"source_path": "{{user `ovf_source_path`}}",
"headless": false,
"guest_additions_mode": "disable",
"boot_wait": "30s",
"winrm_username": "vagrant",
"winrm_password": "vagrant",
"winrm_wait_timeout": "5m",
"winrm_port":5985,
"winrm_host":"localhost",
"shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
"shutdown_timeout": "10m",
"http_directory":"/tmp",
"vboxmanage": [
[
"modifyvm",
"{{.Name}}",
"--memory",
"2048"
],
[
"modifyvm",
"{{.Name}}",
"--vram",
"64"
],
[
"modifyvm",
"{{.Name}}",
"--cpus",
"2"
]
]
}
],
"provisioners": [
{
"type":"powershell",
"inline": [
"(iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')))>$null 2>&1",
"choco install 7zip"
]
},
{
"type": "powershell",
"scripts": [
"./scripts/virtualbox-guest-tools.ps1",
"./scripts/disable-password-complexity.ps1"
],
"only":["basebox-vbox"]
},
{
"type": "powershell",
"scripts": [ "./scripts/openssh.ps1" ],
"only":["devbox-vbox"]
},
{
"type": "powershell",
"elevated_user": "vagrant",
"elevated_password": "vagrant",
"scripts": [
"./scripts/provision.ps1",
"./scripts/provision-agent.ps1",
"./scripts/provision-ruby.ps1",
"./scripts/provision-dev.ps1"
],
"only":["devbox-vbox"]
},
{
"type":"windows-shell",
"scripts": [
"./scripts/rsync.bat",
"./scripts/enable-rdp.bat"
],
"only":["devbox-vbox"]
},
{
"type": "powershell",
"scripts": [
"./scripts/cleanup.ps1"
],
"only":["devbox-vbox"]
},
{
"type": "restart-windows",
"only":["devbox-vbox"]
},
{
"type": "powershell",
"pause_before":"2m",
"scripts": [
"./scripts/cleanup2.ps1"
],
"only":["devbox-vbox"]
}
],
"post-processors": [
{
"type": "vagrant",
"compression_level":1,
"keep_input_artifact": true,
"output": "machinefactory-api-{{.Provider}}-{{user `build_version`}}.box",
"vagrantfile_template": "vagrantfile-windows_2012_r2.template",
"only":["devbox-vbox"]
}
]
}
vagrant box add machinefactory-api-virtualbox-1.0.0 ./machinefactory-api-virtualbox-1.0.0.box
# -*- mode: ruby -*-
# vi: set ft=ruby :
$shell_script = <<SCRIPT
choco install mongodb
SCRIPT
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "machinefactory-api-virtualbox-1.0.0"
hostname = "urlsvc.dev"
ip_address = "10.0.0.30"
host_port = 5895
config.winrm.host = "localhost"
config.winrm.port = host_port
config.winrm.guest_port = host_port
config.vm.guest = :windows
config.vm.communicator = "winrm"
config.vm.network :forwarded_port, guest: 3389, host: 3399, id: "rdp", auto_correct: false
config.vm.network :forwarded_port, guest: 5985, host: host_port, id: "winrm", auto_correct: false
config.vm.network :forwarded_port, guest: 80, host: 8000, id: "web" # Port forward for IIS
config.vm.network :forwarded_port, guest: 443, host: 8443, id: "ssl" # Port forward for SSL IIS
config.vm.network :forwarded_port, guest: 4018, host: 4018, id: "remotevsdebug"
config.vm.network "private_network", ip: ip_address
config.vm.provider "virtualbox" do |v|
v.gui = true
end
if Vagrant.has_plugin?("vagrant-multi-hostsupdater")
config.multihostsupdater.aliases = {ip_address => [hostname]}
end
config.vm.synced_folder ".", "/vagrant", type: "rsync",
owner: "vagrant",
rsync__exclude: [".git", "*.box", "output-*"]
# Install Chocolatey and some basic DSC Resources
config.vm.provision "shell", inline: $shell_script
# Run DSC
config.vm.provision "dsc" do |dsc|
# Set of module paths relative to the Vagrantfile dir.
#
# These paths are added to the DSC Configuration running
# environment to enable local modules to be addressed.
#
# @return [Array] Set of relative module paths.
dsc.module_path = ["modules"]
# The path relative to `dsc.manifests_path` pointing to the Configuration file
dsc.configuration_file = "MyWebsite.ps1"
# The path relative to Vagrantfile pointing to the Configuration Data file
dsc.configuration_data_file = "manifests/MyConfig.psd1"
# The Configuration Command to run. Assumed to be the same as the `dsc.configuration_file`
# (sans extension) if not provided.
dsc.configuration_name = "MyWebsite"
# Relative path to a pre-generated MOF file.
#
# Path is relative to the folder containing the Vagrantfile.
# dsc.mof_path = "mof"
# Relative path to the folder containing the root Configuration manifest file.
# Defaults to 'manifests'.
#
# Path is relative to the folder containing the Vagrantfile.
dsc.manifests_path = "manifests"
# Commandline arguments to the Configuration run
#
# Set of Parameters to pass to the DSC Configuration.
dsc.configuration_params = {"-MachineName" => "localhost", "-WebAppPath" => "c:\\vagrant\\urlsvc", "-HostName" => hostname}
# The type of synced folders to use when sharing the data
# required for the provisioner to work properly.
#
# By default this will use the default synced folder type.
# For example, you can set this to "nfs" to use NFS synced folders.
# dsc.synced_folder_type = ""
# Temporary working directory on the guest machine.
# dsc.temp_dir = "c:/tmp/vagrant-dsc"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment