Files
chef/site-cookbooks/kosmos_gitea/recipes/runner.rb
T

112 lines
2.9 KiB
Ruby

#
# Cookbook:: kosmos_gitea
# Recipe:: runner
#
version = node["gitea"]["runner"]["version"]
download_url = "https://dl.gitea.com/gitea-runner/#{version}/gitea-runner-#{version}-linux-amd64"
working_directory = node["gitea"]["working_directory"]
gitea_credentials = data_bag_item("credentials", "gitea")
runners = gitea_credentials["runners"]
gitea_host = "https://#{node["gitea"]["domain"]}"
apt_repository 'docker' do
uri 'https://download.docker.com/linux/ubuntu'
key 'https://download.docker.com/linux/ubuntu/gpg'
components ['stable']
end
%w[
docker-ce
docker-ce-cli
containerd.io
docker-buildx-plugin
].each do |apt_pkg|
package apt_pkg
end
remote_file "/usr/local/bin/gitea_runner" do
source download_url
checksum node["gitea"]["runner"]["checksum"]
mode "0750"
end
directory "#{working_directory}/runners" do
mode "0700"
end
execute "create_gitea_actions_network" do
command "docker network create --subnet 172.20.0.0/16 gitea-actions"
not_if "docker network inspect gitea-actions"
end
runners.each_with_index do |runner, index|
runner_name = "gitea-runner-#{runner["org"]}"
runner_dir = "#{working_directory}/runners/#{runner["org"]}"
cache_port = node["gitea"]["runner"]["cache_base_port"] + index
directory runner_dir do
mode "0700"
end
runner_config = node["gitea"]["runner"]["config"].to_hash
runner_config["cache"] = runner_config["cache"].merge("port" => cache_port)
firewall_rule "runner_cache_#{runner["org"]}" do
command :allow
port cache_port
protocol :tcp
source "172.20.0.0/16"
end
template "#{runner_dir}/config.yaml" do
source "runner.config.yaml.erb"
mode "0640"
owner "root"
group "root"
variables(config: runner_config)
notifies :restart, "service[#{runner_name}]", :delayed
end
bash "register_#{runner["org"]}_runner" do
cwd runner_dir
code <<-EOF
gitea_runner register \
--no-interactive \
--instance #{gitea_host} \
--name #{runner_name} \
--token #{runner["token"]}
EOF
not_if { File.exist?("#{runner_dir}/.runner") }
end
systemd_unit "#{runner_name}.service" do
content({
Unit: {
Description: "Gitea Actions Runner for '#{runner["org"]}' org",
Documentation: ["https://gitea.com/gitea/runner"],
Requires: "gitea.service",
After: "syslog.target network.target"
},
Service: {
Type: "simple",
WorkingDirectory: runner_dir,
Environment: "HOME=/root",
ExecStart: "/usr/local/bin/gitea_runner daemon --config #{runner_dir}/config.yaml",
ExecStartPre: "/bin/sleep 3", # Wait for Gitea's API to be up when restarting at the same time
Restart: "always",
},
Install: {
WantedBy: "multi-user.target"
}
})
verify false
triggers_reload true
action [:create]
end
service runner_name do
action [:enable, :start]
end
end