Open tomzo opened 9 years ago
I'm going to add the recipes here instead of on #52 since this feels more appropriate.
Recipe to install go agent via zip file.
install_path = 'C:\go-agent-15.2.0'
directory install_path do
rights :full_control, 'gocd', :applies_to_children => true
end
server_search_query = "chef_environment:#{node.chef_environment} AND recipes:go-server\\:\\:default"
Chef::Log.info("Search query: #{server_search_query}")
go_servers = search(:node, server_search_query)
if go_servers.count == 0
Chef::Log.warn("No Go servers found on any of the nodes running chef client.")
else
go_server = go_servers.first
go_server_host = go_server['ipaddress']
if go_servers.count > 1
Chef::Log.warn("Multiple Go servers found on Chef server. Using first returned server '#{go_server_host}' for server instance configuration.")
end
Chef::Log.info("Found Go server at ip address #{go_server_host} with automatic agent registration")
if autoregister_key = go_server['gocd']['server']['autoregister_key']
Chef::Log.warn("Agent auto-registration enabled. This agent will not require approval to become active.")
end
end
if go_server_host.nil?
go_server_host = '127.0.0.1'
Chef::Log.warn("Go server not found on Chef server. Defaulting Go server to #{go_server_host}")
end
if autoregister_key
directory "#{install_path}/config" do
recursive true
end
template "#{install_path}/config/autoregister.properties" do
not_if { File.exists? ("#{install_path}/config/agent.jks") }
variables({
key: autoregister_key,
hostname: node['gocd']['agent']['autoregister']['hostname'],
environments: node['gocd']['agent']['autoregister']['environments'],
resources: node['gocd']['agent']['autoregister']['resources'],
})
end
end
env 'GO_SERVER' do
value go_server_host
end
include_recipe '7-zip'
zipfile = "#{Chef::Config[:file_cache_path]}\\go-agent-15.2.0-2248.zip"
remote_file zipfile do
source 'https://download.go.cd/gocd/go-agent-15.2.0-2248.zip'
checksum '72fb2ed793401b46d73236d9c7a92e7e991f8b8b5f832c545a99eeff1fb0c8d9'
not_if {::File.exist?('C:\go-agent-15.2.0\agent.cmd') }
end
windows_batch 'Unzip Go Agent' do
code <<-EOH
"C:\\Program Files (x86)\\Git\\bin\\unzip.exe" -qo #{zipfile} -d C:\\
EOH
creates 'C:\go-agent-15.2.0\agent.cmd'
end
java_home = win_friendly_path(node['java']['java_home'])
env 'GO_AGENT_JAVA_HOME' do
value java_home
notifies :reboot_now, 'reboot[reboot-on-provision]', :immediately
end
Recipe to install go agent via installer
install_path = 'C:\GoAgent'
if Chef::Config['solo'] || node['gocd']['agent']['go_server_host']
Chef::Log.info("Attempting to use node['gocd']['agent']['go_server_host'] attribute for server host")
go_server_host = node['gocd']['agent']['go_server_host']
autoregister_key = node['gocd']['agent']['autoregister']['key']
else
server_search_query = node['gocd']['agent']['server_search_query']
Chef::Log.info("Search query: #{server_search_query}")
go_servers = search(:node, server_search_query)
if go_servers.count == 0
Chef::Log.warn("No Go servers found on any of the nodes running chef client.")
else
go_server = go_servers.first
go_server_host = go_server['ipaddress']
if go_servers.count > 1
Chef::Log.warn("Multiple Go servers found on Chef server. Using first returned server '#{go_server_host}' for server instance configuration.")
end
Chef::Log.info("Found Go server at ip address #{go_server_host} with automatic agent registration")
if autoregister_key = go_server['gocd']['server']['autoregister_key']
Chef::Log.warn("Agent auto-registration enabled. This agent will not require approval to become active.")
end
end
end
if go_server_host.nil?
go_server_host = '127.0.0.1'
Chef::Log.warn("Go server not found on Chef server or not specifed via node['gocd']['agent']['go_server_host'] attribute, defaulting Go server to #{go_server_host}")
end
if autoregister_key
directory "#{install_path}/config" do
recursive true
end
template "#{install_path}/config/autoregister.properties" do
not_if { File.exists? ("#{install_path}/config/agent.jks") }
if node['gocd']['agent']['windows']['enable_service']
notifies :restart, "windows_service[Go Agent]"
end
variables({
key: autoregister_key,
hostname: node['gocd']['agent']['autoregister']['hostname'],
environments: node['gocd']['agent']['autoregister']['environments'],
resources: node['gocd']['agent']['autoregister']['resources'],
})
end
end
opts = []
opts << "/SERVERIP=#{go_server_host}"
opts << "/D=#{install_path}"
windows_package 'Go Agent' do
source "https://download.go.cd/gocd/go-agent-#{node['gocd']['agent']['version']}-setup.exe"
options opts.join(' ')
action :install
end
windows_service 'Go Agent' do
if node['gocd']['agent']['windows']['enable_service']
action :configure_startup
startup_type :automatic
else
action :stop
startup_type :disabled
end
end
Clearly there's a lot of copy paste that has happened, and it should be possible to DRY up and improve the recipes.
@ketan I'll pick this up. Thanks for these recipes. They are definitely something to start with.
If you had some issues when deploying windows recently then I would appreciate if you mentioned them.
Did you do deployments from your develop
branch + some wrapper?
there was no wrapper, just a frankenstein recipe based on merging the linux agent recipe(for the autoregistration) and the existing agent windows recipe.
What I've provided is an exact copy of what's running on our servers. We tried the install via installer method but instead wrote the install via zip recipe for various reasons (run as service, desktop interaction, selenium IE driver).
Both of them "work" in that they install and configure the agent as expected.
I have dealt with linux cases already.
Installations are supported from
It makes sense to zip, especially in windows. I will work on it soon.
Currently source of Go installation is hard-coded to official thoughtworks repository.
It could be possible to install from alternative repository (e.g. mirror) or from raw package files.