From b66139a8e9b721256c0bc8b1bb55e4f9703cdb74 Mon Sep 17 00:00:00 2001 From: "Montague, Brent" Date: Thu, 25 Feb 2016 17:33:27 -0500 Subject: [PATCH] Add service configuration resource - Adds way to start or stop service at start --- .gitignore | 1 + .kitchen.yml | 5 +- .rubocop.yml | 5 +- libraries/server.rb | 8 ++ metadata.rb | 2 +- providers/server.rb | 73 +++++++++++++++++++ resources/server.rb | 12 ++- .../verify-octo/recipes/audit_server.rb | 14 ++++ test/cookbooks/verify-octo/recipes/server.rb | 4 +- 9 files changed, 115 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index bbe9430..814f800 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ Berksfile.lock .kitchen/ .kitchen.local.yml vendor/ +.rubocop-http* diff --git a/.kitchen.yml b/.kitchen.yml index b744caf..8d5a120 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -9,10 +9,7 @@ provisioner: audit_mode: :enabled platforms: - - name: windows-2008R2 - driver: - box: windows-2008r2-net451 - gui: false + - name: windows-2008R2-cvent suites: - name: server diff --git a/.rubocop.yml b/.rubocop.yml index cc32da4..77e6448 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1 +1,4 @@ -inherit_from: .rubocop_todo.yml +inherit_from: +- https://raw.githubusercontent.com/cvent/rubocop-rules-chef/master/rubocop.yml +Metrics/LineLength: + Max: 160 diff --git a/libraries/server.rb b/libraries/server.rb index 3957e4f..6feca41 100644 --- a/libraries/server.rb +++ b/libraries/server.rb @@ -25,6 +25,14 @@ def display_name 'Octopus Deploy Server' end + def service_name + 'OctopusDeploy' + end + + def server_install_location + 'C:\Program Files\Octopus Deploy\Octopus' + end + def installer_url(version) "https://download.octopusdeploy.com/octopus/Octopus.#{version}-x64.msi" end diff --git a/metadata.rb b/metadata.rb index 8c64ac6..403e3cb 100644 --- a/metadata.rb +++ b/metadata.rb @@ -6,7 +6,7 @@ long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) source_url 'https://github.com/cvent/octopus-deploy-cookbook' issues_url 'https://github.com/cvent/octopus-deploy-cookbook/issues' -version '0.4.8' +version '0.5.0' depends 'windows', '~> 1.38' supports 'windows' diff --git a/providers/server.rb b/providers/server.rb index 4678b42..6e36e6a 100644 --- a/providers/server.rb +++ b/providers/server.rb @@ -18,6 +18,7 @@ # limitations under the License. # +include OctopusDeploy::Shared include OctopusDeploy::Server use_inline_resources @@ -50,6 +51,78 @@ new_resource.updated_by_last_action(download.updated_by_last_action? || install.updated_by_last_action?) end +action :configure do + new_resource = @new_resource + name = new_resource.name + instance = new_resource.instance + checksum = new_resource.checksum + version = new_resource.version + home_path = new_resource.home_path + config_path = new_resource.config_path + connection_string = new_resource.connection_string + node_name = new_resource.node_name + admin_user = new_resource.admin_user + license = new_resource.license + create_database = new_resource.create_database + start_service = new_resource.start_service + + install = octopus_deploy_server name do + action :install + checksum checksum + version version + end + + create_instance = powershell_script "create-instance-#{instance}" do + action :run + cwd server_install_location + code <<-EOH + .\\Octopus.Server.exe create-instance --instance "#{instance}" --config "#{config_path}" --console + #{catch_powershell_error('Creating instance')} + EOH + not_if { ::File.exist?(config_path) } + end + + configure = powershell_script "configure-server-#{instance}" do # ~FC009 + action :run + cwd server_install_location + code <<-EOH + .\\Octopus.Server.exe configure --instance "#{instance}" --home "#{home_path}" --console + #{catch_powershell_error('Configuring Home Dir')} + .\\Octopus.Server.exe configure --instance "#{instance}" --storageConnectionString "#{connection_string}" --console + #{catch_powershell_error('Configuring Database Connection')} + .\\Octopus.Server.exe configure --instance "#{instance}" --upgradeCheck "True" --upgradeCheckWithStatistics "True" --console + #{catch_powershell_error('Configuring Upgrade Checks')} + .\\Octopus.Server.exe configure --instance "#{instance}" --webAuthenticationMode "Domain" --console + #{catch_powershell_error('Configuring authentication')} + .\\Octopus.Server.exe configure --instance "#{instance}" --serverNodeName "#{node_name}" --console + #{catch_powershell_error('Configuring Cluster Node Name')} + .\\Octopus.Server.exe configure --instance "#{instance}" --webForceSSL "False" --webListenPrefixes "http://localhost:80/" --commsListenPort "10943" --console + #{catch_powershell_error('Configuring Listen Ports')} + #{".\\Octopus.Server.exe database --instance \"#{instance}\" --create --console #{catch_powershell_error('Create Database')}" if create_database} + .\\Octopus.Server.exe service --instance "#{instance}" --stop --console + #{catch_powershell_error('Stop Service')} + #{".\\Octopus.Server.exe admin --instance \"#{instance}\" --username \"#{admin_user}\" --console #{catch_powershell_error('Set administrator')}" if admin_user} + #{".\\Octopus.Server.exe license --instance \"#{instance}\" --licenseBase64 \"#{Base64.encode64(license)}\"--console #{catch_powershell_error('Configuring License')}" if license} + .\\Octopus.Server.exe service --instance "#{instance}" --install --reconfigure --console + #{catch_powershell_error('Create Service')} + EOH + sensitive true + notifies :restart, "windows_service[#{service_name}]", :delayed + not_if { ::Win32::Service.exists?(service_name) } + end + + # Make sure enabled and started + service = windows_service service_name do + if start_service + action [:enable, :start] + else + action [:stop, :disable] + end + end + + new_resource.updated_by_last_action(actions_updated?([install, create_instance, configure, service])) +end + action :remove do new_resource = @new_resource version = new_resource.version diff --git a/resources/server.rb b/resources/server.rb index 3ee8caa..0deaf34 100644 --- a/resources/server.rb +++ b/resources/server.rb @@ -18,9 +18,17 @@ # limitations under the License. # -actions :install, :remove +actions :install, :configure, :remove default_action :install -attribute :instance, kind_of: String, name_attribute: true +attribute :instance, kind_of: String, default: 'OctopusServer' attribute :version, kind_of: String, required: true attribute :checksum, kind_of: String +attribute :home_path, kind_of: String, default: 'C:\Octopus' +attribute :config_path, kind_of: String, default: 'C:\Octopus\OctopusServer.config' +attribute :connection_string, kind_of: String +attribute :node_name, kind_of: String +attribute :create_database, kind_of: [TrueClass, FalseClass], default: false +attribute :admin_user, kind_of: String +attribute :license, kind_of: String +attribute :start_service, kind_of: [TrueClass, FalseClass], default: true diff --git a/test/cookbooks/verify-octo/recipes/audit_server.rb b/test/cookbooks/verify-octo/recipes/audit_server.rb index b829811..e4f9c08 100644 --- a/test/cookbooks/verify-octo/recipes/audit_server.rb +++ b/test/cookbooks/verify-octo/recipes/audit_server.rb @@ -30,4 +30,18 @@ expect(file('C:\Program Files\Octopus Deploy\Octopus\Octopus.Server.exe')).to be_version(install_version) end end + + control 'Octopus Deploy Server Configuration' do + it 'should have configured the service' do + expect(service('OctopusDeploy')).to be_installed + end + + it 'should have enabled the service' do + expect(service('OctopusDeploy')).to be_enabled + end + + it 'should have created the configuration file' do + expect(file('C:\Octopus\OctopusServer.config')).to be_file + end + end end diff --git a/test/cookbooks/verify-octo/recipes/server.rb b/test/cookbooks/verify-octo/recipes/server.rb index 66aa74b..52bba97 100644 --- a/test/cookbooks/verify-octo/recipes/server.rb +++ b/test/cookbooks/verify-octo/recipes/server.rb @@ -25,9 +25,11 @@ end octopus_deploy_server 'OctopusServer' do - action :install + action :configure version node['verify-octo']['server']['version'] checksum node['verify-octo']['server']['checksum'] + node_name 'octo-web-01' + connection_string 'Data Source=localhostdba;Initial Catalog=octo;Integrated Security=False;User ID=octo;Password=password' end include_recipe 'verify-octo::audit_server'