From d3048e7cc72d452d4b3cc709d1513b6379f26057 Mon Sep 17 00:00:00 2001 From: Patrick Wright Date: Wed, 16 Aug 2017 12:55:08 -0700 Subject: [PATCH] install_strategy for install.ps1 Signed-off-by: Patrick Wright --- .../acceptance-cookbook/.gitignore | 2 + .../acceptance-cookbook/metadata.rb | 2 + .../acceptance-cookbook/recipes/destroy.rb | 3 + .../acceptance-cookbook/recipes/provision.rb | 12 +++ .../acceptance-cookbook/recipes/verify.rb | 14 +++ .../inspec/verify.rb | 7 ++ .../terraform/application.tf | 89 +++++++++++++++++++ .../terraform/aws.tf | 8 ++ .../terraform/variables.tf | 16 ++++ .../powershell/scripts/helpers.ps1.erb | 2 +- .../powershell/scripts/install_project.ps1 | 16 +++- spec/unit/mixlib/install/generator_spec.rb | 7 +- 12 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/.gitignore create mode 100644 acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/metadata.rb create mode 100644 acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/destroy.rb create mode 100644 acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/provision.rb create mode 100644 acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/verify.rb create mode 100644 acceptance/windows-server-2012r2-once/inspec/verify.rb create mode 100644 acceptance/windows-server-2012r2-once/terraform/application.tf create mode 100644 acceptance/windows-server-2012r2-once/terraform/aws.tf create mode 100644 acceptance/windows-server-2012r2-once/terraform/variables.tf diff --git a/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/.gitignore b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/.gitignore new file mode 100644 index 00000000..041413b0 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/.gitignore @@ -0,0 +1,2 @@ +nodes/ +tmp/ diff --git a/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/metadata.rb b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/metadata.rb new file mode 100644 index 00000000..ece66a84 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/metadata.rb @@ -0,0 +1,2 @@ +name 'acceptance-cookbook' + diff --git a/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/destroy.rb b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/destroy.rb new file mode 100644 index 00000000..69264798 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/destroy.rb @@ -0,0 +1,3 @@ +execute "terraform destroy -force" do + cwd "#{node['chef-acceptance']['suite-dir']}/terraform" +end diff --git a/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/provision.rb b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/provision.rb new file mode 100644 index 00000000..971bcbd9 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/provision.rb @@ -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 + diff --git a/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/verify.rb b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/verify.rb new file mode 100644 index 00000000..924fbaa5 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/.acceptance/acceptance-cookbook/recipes/verify.rb @@ -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 diff --git a/acceptance/windows-server-2012r2-once/inspec/verify.rb b/acceptance/windows-server-2012r2-once/inspec/verify.rb new file mode 100644 index 00000000..65af9bda --- /dev/null +++ b/acceptance/windows-server-2012r2-once/inspec/verify.rb @@ -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 diff --git a/acceptance/windows-server-2012r2-once/terraform/application.tf b/acceptance/windows-server-2012r2-once/terraform/application.tf new file mode 100644 index 00000000..dadf56d2 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/terraform/application.tf @@ -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 = < + 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"} + + + 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}") + +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", + ] + } +} diff --git a/acceptance/windows-server-2012r2-once/terraform/aws.tf b/acceptance/windows-server-2012r2-once/terraform/aws.tf new file mode 100644 index 00000000..85b319a2 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/terraform/aws.tf @@ -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" +} diff --git a/acceptance/windows-server-2012r2-once/terraform/variables.tf b/acceptance/windows-server-2012r2-once/terraform/variables.tf new file mode 100644 index 00000000..d3d6ba43 --- /dev/null +++ b/acceptance/windows-server-2012r2-once/terraform/variables.tf @@ -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" +} diff --git a/lib/mixlib/install/generator/powershell/scripts/helpers.ps1.erb b/lib/mixlib/install/generator/powershell/scripts/helpers.ps1.erb index 46e459c4..404889da 100644 --- a/lib/mixlib/install/generator/powershell/scripts/helpers.ps1.erb +++ b/lib/mixlib/install/generator/powershell/scripts/helpers.ps1.erb @@ -174,4 +174,4 @@ function Get-WMIQuery { else { Get-WmiObject $class } -} \ No newline at end of file +} diff --git a/lib/mixlib/install/generator/powershell/scripts/install_project.ps1 b/lib/mixlib/install/generator/powershell/scripts/install_project.ps1 index 5fdb643b..18489c5f 100644 --- a/lib/mixlib/install/generator/powershell/scripts/install_project.ps1 +++ b/lib/mixlib/install/generator/powershell/scripts/install_project.ps1 @@ -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 diff --git a/spec/unit/mixlib/install/generator_spec.rb b/spec/unit/mixlib/install/generator_spec.rb index 3d4a8594..fca41d74 100644 --- a/spec/unit/mixlib/install/generator_spec.rb +++ b/spec/unit/mixlib/install/generator_spec.rb @@ -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 @@ -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 @@ -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