-
-
Save bdemers/5ec7723c6bf116cb1619 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
teamcity_bin_dir = "#{node['teamcity']['agent']['home']}/bin" | |
service_install_bat = "#{teamcity_bin_dir}/service.install.bat" | |
# unzip the teamcity agent bundle | |
# on linux just use unzip, because it is easier to set the user (and the mode will already be set) | |
case node[:os] | |
when 'windows' | |
zipp node[:teamcity][:agent][:url] do | |
backup false | |
not_if {::File.exists?(node[:teamcity][:agent][:properties])} | |
destination node[:teamcity][:agent][:home] | |
action :decompress | |
end | |
execute "Install Service" do | |
command service_install_bat | |
action :run | |
cwd teamcity_bin_dir | |
not_if {Win32::Service.exists?('TCBuildAgent')} | |
notifies :run, "execute[TCagent login Update]", :immediately | |
end | |
# runas user info | |
tcCreds = Chef::EncryptedDataBagItem.load("TCaccount", node[:teamcity][:agent][:user][:databag]) | |
execute "TCagent login Update" do | |
command "#{ENV['WINDIR']}/system32/sc.exe config TCBuildAgent obj= #{tcCreds['tcLogin']} password= #{tcCreds['password']} TYPE= own" | |
action :nothing | |
cwd teamcity_bin_dir | |
end | |
when 'linux' | |
# Create the users' group | |
group node[:teamcity][:agent][:user][:group] do | |
end | |
# Create the user | |
user node[:teamcity][:agent][:user][:name] do | |
comment "Team City User" | |
gid node[:teamcity][:agent][:user][:group] | |
home "/home/#{node[:teamcity][:agent][:user][:group]}" | |
end | |
directory node[:teamcity][:agent][:home] do | |
action :create | |
recursive true | |
not_if { File.exists?(node[:teamcity][:agent][:home])} | |
user node[:teamcity][:agent][:user][:name] | |
group user node[:teamcity][:agent][:user][:group] | |
end | |
remote_file "#{Chef::Config[:file_cache_path]}/teamcity-agent.zip" do | |
source node[:teamcity][:agent][:url] | |
mode 0555 | |
end | |
execute "unzip #{Chef::Config[:file_cache_path]}/teamcity-agent.zip -d #{node[:teamcity][:agent][:home]}" do | |
user node[:teamcity][:agent][:user][:name] | |
creates node[:teamcity][:agent][:properties] | |
not_if {::File.exists?(node[:teamcity][:agent][:properties])} | |
end | |
# as of TeamCity 6.5.4 the zip does NOT contain the file mode | |
file ::File.join( node[:teamcity][:agent][:home], 'launcher/bin/TeamCityAgentService-linux-x86-64') do | |
mode 0755 | |
end | |
file ::File.join( node[:teamcity][:agent][:home], 'launcher/bin/TeamCityAgentService-linux-x86-32') do | |
mode 0755 | |
end | |
file ::File.join( node[:teamcity][:agent][:home], 'launcher/bin/TeamCityAgentService-linux-ppc-64') do | |
mode 0755 | |
end | |
# template init.d script | |
template 'teamcity-service-script' do | |
path "/etc/init.d/#{node[:teamcity][:agent][:service][:name]}" | |
source "teamcity-agent.erb" | |
variables( | |
:teamcity_home => node[:teamcity][:agent][:home], | |
:teamcity_user => node[:teamcity][:agent][:user][:name] | |
) | |
mode "0755" | |
end | |
end | |
auth_token = '' | |
begin | |
databag = Chef::EncryptedDataBagItem.load("TCTokens", node.name.gsub('.','')) | |
auth_token = databag['token'] | |
rescue | |
log ("TeamCity auth token for node #{node.name} was not found in databag TCTokens. You need to create the item.") | |
end | |
# buildAgent.properties (TeamCity will restart if this file is changed) | |
template node[:teamcity][:agent][:properties] do | |
source "buildAgent.properties.erb" | |
variables( | |
.... | |
) | |
mode 0644 | |
end | |
# set the java version used to run TeamCity | |
template 'wrapper.conf' do | |
path "#{node['teamcity']['agent']['home']}/launcher/conf/wrapper.conf" | |
source "wrapper.conf.erb" | |
variables( | |
:java_exe => node[:java][:exe] | |
) | |
mode 0644 | |
end | |
# use a cookbook file as the settings.xml | |
cookbook_file node[:teamcity][:agent][:m2_settings] do | |
source "settings.xml" | |
end | |
case node[:os] | |
when 'windows' | |
service node[:teamcity][:agent][:service][:name] do | |
action [ :enable, :start ] | |
subscribes :restart, resources("template[wrapper.conf]" | |
), :delayed | |
end | |
when 'linux' | |
service node[:teamcity][:agent][:service][:name] do | |
action [ :start, :enable ] | |
supports :status => true, :restart => true | |
status_command "/etc/init.d/#{node[:teamcity][:agent][:service][:name]} status | grep \"is running\"" | |
#provider Chef::Provider::Service::Init | |
subscribes :restart, resources("template[wrapper.conf]", | |
"template[teamcity-service-script]" | |
), :delayed | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment