Skip to content

Commit

Permalink
install_strategy for install.ps1
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Wright <[email protected]>
  • Loading branch information
Patrick Wright committed Aug 16, 2017
1 parent d52b50c commit d3048e7
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodes/
tmp/
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name 'acceptance-cookbook'

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
execute "terraform destroy -force" do
cwd "#{node['chef-acceptance']['suite-dir']}/terraform"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
execute "bundle exec ruby -e \"require 'mixlib/install'; puts Mixlib::Install.install_ps1, 'install -install_strategy once'\" > ../.acceptance_data/powershell_install_once.ps1" do
cwd node['chef-acceptance']['suite-dir']
end

execute "terraform plan" do
cwd "#{node['chef-acceptance']['suite-dir']}/terraform"
end

execute "terraform apply" do
cwd "#{node['chef-acceptance']['suite-dir']}/terraform"
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ruby_block "get ip" do
block do
tf_state = JSON.parse(File.read("#{node['chef-acceptance']['suite-dir']}/terraform/terraform.tfstate"))
node.default["ip"] = tf_state["modules"].first["resources"]["aws_instance.mixlib_install_ps1"]["primary"]["attributes"]["public_ip"]
end
end

execute "run inspec" do
command lazy { "inspec exec verify.rb -t winrm://Administrator@#{node['ip']} --password $WINDOWS_PASSWORD" }
cwd "#{node['chef-acceptance']['suite-dir']}/inspec"
environment(
"WINDOWS_PASSWORD" => ENV["TF_VAR_admin_password"] || "Pas5w0rD"
)
end
7 changes: 7 additions & 0 deletions acceptance/windows-server-2012r2-once/inspec/verify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe package("Chef Client v13*") do
it { should be_installed }
end

describe command("powershell.exe -file /tmp/install.ps1") do
its("stdout") { should match "Nothing to install" }
end
89 changes: 89 additions & 0 deletions acceptance/windows-server-2012r2-once/terraform/application.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
data "aws_ami" "windows_ami" {
most_recent = true

filter {
name = "owner-alias"
values = ["amazon"]
}

filter {
name = "name"
values = ["Windows_Server-2012-R2*-English-*-Base-*"]
}

filter {
name = "architecture"
values = ["x86_64"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}

filter {
name = "image-type"
values = ["machine"]
}
}

resource "aws_instance" "mixlib_install_ps1" {
count = 1

ami = "${data.aws_ami.windows_ami.id}"
instance_type = "${var.aws_instance_type}"
key_name = "es-infrastructure"

associate_public_ip_address = true

subnet_id = "subnet-11ac0174" # Planet Releng Public Subnet
source_dest_check = false

vpc_security_group_ids = [
"sg-96274af3",
]

connection {
type = "winrm"
user = "Administrator"
password = "${var.admin_password}"
timeout = "10m"
}

user_data = <<EOF
<script>
winrm quickconfig -q & winrm set winrm/config/winrs @{MaxMemoryPerShellMB="300"} & winrm set winrm/config @{MaxTimeoutms="1800000"} & winrm set winrm/config/service @{AllowUnencrypted="true"} & winrm set winrm/config/service/auth @{Basic="true"}
</script>
<powershell>
Set-ExecutionPolicy -ExecutionPolicy Bypass
netsh advfirewall firewall add rule name="WinRM in" protocol=TCP dir=in profile=any localport=5985 remoteip=any localip=any action=allow
$admin = [adsi]("WinNT://./administrator, user")
$admin.psbase.invoke("SetPassword", "${var.admin_password}")
</powershell>
EOF

tags {
# ChefOps's AWS standard tags:
X-Dept = "EngServ"
X-Contact = "pwright"
X-Production = "false"
X-Environment = "acceptance"
X-Application = "mixlib-install"
}

provisioner "file" {
source = "../../.acceptance_data/powershell_install_once.ps1"
destination = "/tmp/install.ps1"
}

provisioner "remote-exec" {
inline = [
"powershell.exe -file /tmp/install.ps1",
]
}
}
8 changes: 8 additions & 0 deletions acceptance/windows-server-2012r2-once/terraform/aws.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Restrict operation of terraform to chef-es profile so that
# we do not create resources in other aws profiles.
# We assume user has configured standard aws credentials
# under ~/.aws/credentials or with $AWS_SHARED_CREDENTIALS_FILE
provider "aws" {
region = "${var.aws_region}"
profile = "chef-aws"
}
16 changes: 16 additions & 0 deletions acceptance/windows-server-2012r2-once/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Region to create infrastructure in
variable "aws_region" {
type = "string"
default = "us-west-2"
}

variable "aws_instance_type" {
type = "string"
default = "t2.micro"
}

variable "admin_password" {
description = "Set Windows Administrator password"
type = "string"
default = "Pas5w0rD"
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ function Get-WMIQuery {
else {
Get-WmiObject $class
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,25 @@ function Install-Project {
$daemon = 'auto',
[string]
$http_proxy,
[string]
$download_url_override,
# Specify an alternate download url, must also include checksum
[string]
$checksum
$download_url_override,
# SHA256 checksum of the download file
# Must be present when using download_url_override
[string]
$checksum,
# Set to 'once' to skip install if project is detected
[string]
$install_strategy
)

if ((Test-Path "$env:systemdrive\opscode\$project\embedded") -and ($install_strategy -eq 'once')) {
Write-Host "$project installation detected"
Write-Host "install_strategy set to 'once'"
Write-Host "Nothing to install"
exit
}

# Set http_proxy as env var
if(-not [string]::IsNullOrEmpty($http_proxy)) {
$env:http_proxy = $http_proxy
Expand Down
7 changes: 6 additions & 1 deletion spec/unit/mixlib/install/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
http_proxy: "http://sam:iam@greeneggsandham:1111",
download_url_override: "https://packages.chef.io/files/stable/chef/12.19.36/windows/2012/chef-client-12.19.36-1-x64.msi",
checksum: "1baed41a777d298a08fc0a34dd1eaaa76143bde222fd22c31aa709c7911dec48",
install_strategy: "once",
}
end

Expand All @@ -197,6 +198,10 @@
it "adds checksum var" do
expect(install_script).to match(/checksum '#{install_command_options[:checksum]}'/)
end

it "adds install_strategy var" do
expect(install_script).to match(/install_strategy '#{install_command_options[:install_strategy]}'/)
end
end

context "for bourne install params" do
Expand Down Expand Up @@ -227,7 +232,7 @@
expect(install_script).to match(/checksum='#{install_command_options[:checksum]}'/)
end

it "adds checksum var" do
it "adds install_strategy var" do
expect(install_script).to match(/install_strategy='#{install_command_options[:install_strategy]}'/)
end
end
Expand Down

0 comments on commit d3048e7

Please sign in to comment.