From 80269b764e7e7bc3c6dbcaa799e345e477525e17 Mon Sep 17 00:00:00 2001 From: cbanciu Date: Fri, 11 Aug 2017 17:53:42 +0300 Subject: [PATCH] WindowsSSM installation and other changes, please adapt as needed --- attributes/default.rb | 27 +++++++++++++--------- files/default/install_win_ssm.ps1 | 27 ++++++++++++++++++++++ recipes/default.rb | 37 +++++++++++++++++++++++++++++-- recipes/install.rb | 24 -------------------- 4 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 files/default/install_win_ssm.ps1 delete mode 100644 recipes/install.rb diff --git a/attributes/default.rb b/attributes/default.rb index cc74230..2951c42 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -15,9 +15,13 @@ config['region'], config['package']['version'], value_for_platform_family('rhel' => 'linux_amd64', - 'debian' => 'debian_amd64'), + 'amazon' => 'linux_amd64', + 'debian' => 'debian_amd64', + 'windows' => 'windows_amd64'), value_for_platform_family('rhel' => 'amazon-ssm-agent.rpm', - 'debian' => 'amazon-ssm-agent.deb') + 'amazon' => 'amazon-ssm-agent.rpm', + 'debian' => 'amazon-ssm-agent.deb', + 'windows' => 'AmazonSSMAgentSetup.exe') ) # Path where the package is downloaded to @@ -25,7 +29,9 @@ config['package']['path'] = ::File.join( Chef::Config['file_cache_path'], value_for_platform_family('rhel' => 'amazon-ssm-agent.rpm', - 'debian' => 'amazon-ssm-agent.deb') + 'amazon' => 'amazon-ssm-agent.rpm', + 'debian' => 'amazon-ssm-agent.deb', + 'windows' => 'AmazonSSMAgentSetup.exe') ) # Checksum of the package @@ -39,12 +45,13 @@ # '5052f18e58' # ) - # Name of the agent service + # set agent on windows # @since 0.1.0 - config['service']['name'] = 'amazon-ssm-agent' - - # Actions to set the agent to - # * Note: We set this to disable / start to provide faster boot times - # @since 0.1.0 - config['service']['actions'] = %w(disable start) + if node['platform'] == 'windows' + config['service']['name'] = 'AmazonSSMAgent' + config['service']['actions'] = %w(enable start) + else + config['service']['name'] = 'amazon-ssm-agent' + config['service']['actions'] = %w(enable start) + end end diff --git a/files/default/install_win_ssm.ps1 b/files/default/install_win_ssm.ps1 new file mode 100644 index 0000000..ef9eb5e --- /dev/null +++ b/files/default/install_win_ssm.ps1 @@ -0,0 +1,27 @@ +Clear-Host +$Name = "AmazonSSMAgent" +$Service = Get-Service -display $Name -ErrorAction SilentlyContinue +If (-Not $Service) { + If (Test-Path -Path C:\chef\cache\EC2Install) { + Remove-item C:\chef\cache\EC2*.* + Remove-item C:\chef\cache\EC2Install -recurse + } + Add-Type -AssemblyName System.IO.Compression.FileSystem + function Unzip + { + param([string]$zipfile, [string]$outpath) + [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath) + } + Invoke-WebRequest -Uri https://s3.amazonaws.com/ec2-downloads-windows/EC2Config/EC2Install.zip -UseBasicParsing -OutFile C:\chef\cache\EC2Install.zip + Unzip C:\chef\cache\EC2Install.zip C:\chef\cache\EC2Install + $Name = "Ec2Config" + $Service = Get-Service -display $Name -ErrorAction SilentlyContinue + If (-Not $Service) { + C:\chef\cache\EC2Install\EC2Install.exe /install /quiet /norestart + } else { + C:\chef\cache\EC2Install\EC2Install.exe /repair /quiet /norestart + } +} +Else { + 'SSM Agent Service exists' +} diff --git a/recipes/default.rb b/recipes/default.rb index 24ebc47..5482576 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -1,2 +1,35 @@ -include_recipe "#{cookbook_name}::install" -include_recipe "#{cookbook_name}::logrotate" +# deploy ssm agent on Linux or windows. Cosmin 07.2017 + +if node['platform'] == 'windows' + include_recipe 'windows' + include_recipe 'chef_handler' + script_path = win_friendly_path(File.join(Chef::Config[:file_cache_path],'install_win_ssm.ps1')) + install_ssm = 'install_win_ssm.ps1' + execute install_ssm do + command "powershell -ExecutionPolicy ByPass -File \"#{script_path}\"" + action :nothing + end + cookbook_file script_path do + source 'install_win_ssm.ps1' + action :create + notifies :run, "execute[#{install_ssm}]", :immediately + end +else + include_recipe "#{cookbook_name}::logrotate" + remote_file 'amazon-ssm-agent' do + path node['ssm_agent']['package']['path'] + source node['ssm_agent']['package']['url'] + checksum node['ssm_agent']['package']['checksum'] + mode 0644 + end + package 'amazon-ssm-agent' do + source node['ssm_agent']['package']['path'] + provider value_for_platform_family( + 'rhel' => Chef::Provider::Package::Yum, + 'amazon' => Chef::Provider::Package::Yum, + 'debian' => Chef::Provider::Package::Dpkg) + end + service node['ssm_agent']['service']['name'] do + action node['ssm_agent']['service']['actions'] + end +end diff --git a/recipes/install.rb b/recipes/install.rb deleted file mode 100644 index bab98b6..0000000 --- a/recipes/install.rb +++ /dev/null @@ -1,24 +0,0 @@ -# Download the installer -# @since 0.1.0 -remote_file 'amazon-ssm-agent' do - path node['ssm_agent']['package']['path'] - source node['ssm_agent']['package']['url'] - checksum node['ssm_agent']['package']['checksum'] - mode 0644 -end - -# Install the pakage -# @since 0.1.0 -package 'amazon-ssm-agent' do - source node['ssm_agent']['package']['path'] - provider value_for_platform_family( - 'rhel' => Chef::Provider::Package::Yum, - 'debian' => Chef::Provider::Package::Dpkg - ) -end - -# Ensure service state -# @since 0.1.0 -service node['ssm_agent']['service']['name'] do - action node['ssm_agent']['service']['actions'] -end