Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #40 from Telefonica/add_env_support
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarsj authored May 25, 2022
2 parents af60a05 + de74c03 commit bbc1f4c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 37 deletions.
43 changes: 25 additions & 18 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,33 @@
# Optional[String], Comma separated list of hosts that should not use a proxy. More information at https://docs.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners
#
# * path
# Optional[Array[String]], List of paths to be used as PATH env in the instance runner. If not defined, this file will be kept as created by the runner scripts. Default value: undef
# Optional[Array[String]], List of paths to be used as PATH env in the instance runner.
# If not defined, file ".path" will be kept as created by the runner scripts. Default value: undef
#
# * env
# Optional[Hash[String, String]], List of variables to be used as env variables in the instance runner.
# If not defined, file ".env" will be kept as created
# by the runner scripts. (Default: Value set by github_actions_runner Class)
#
class github_actions_runner (
Enum['present', 'absent'] $ensure,
Stdlib::Absolutepath $base_dir_name,
String[1] $personal_access_token,
String[1] $package_name,
String[1] $package_ensure,
String[1] $repository_url,
String[1] $user,
String[1] $group,
Hash[String[1], Hash] $instances,
String[1] $github_domain,
String[1] $github_api,
Optional[String[1]] $enterprise_name = undef,
Optional[String[1]] $org_name = undef,
Optional[String[1]] $http_proxy = undef,
Optional[String[1]] $https_proxy = undef,
Optional[String[1]] $no_proxy = undef,
Optional[Array[String]] $path = undef,
Enum['present', 'absent'] $ensure,
Stdlib::Absolutepath $base_dir_name,
String[1] $personal_access_token,
String[1] $package_name,
String[1] $package_ensure,
String[1] $repository_url,
String[1] $user,
String[1] $group,
Hash[String[1], Hash] $instances,
String[1] $github_domain,
String[1] $github_api,
Optional[String[1]] $enterprise_name = undef,
Optional[String[1]] $org_name = undef,
Optional[String[1]] $http_proxy = undef,
Optional[String[1]] $https_proxy = undef,
Optional[String[1]] $no_proxy = undef,
Optional[Array[String]] $path = undef,
Optional[Hash[String, String]] $env = undef,
) {

$root_dir = "${github_actions_runner::base_dir_name}-${github_actions_runner::package_ensure}"
Expand Down
59 changes: 42 additions & 17 deletions manifests/instance.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,32 @@
# Optional[Array[String]], A list of costum lables to add to a runner.
#
# * path
# Optional[Array[String]], List of paths to be used as PATH env in the instance runner. If not defined, this file will be kept as created
# Optional[Array[String]], List of paths to be used as PATH env in the instance runner. If not defined, file ".path" will be kept as created
# by the runner scripts. (Default: Value set by github_actions_runner Class)
#
# * env
# Optional[Hash[String, String]], List of variables to be used as env variables in the instance runner.
# If not defined, file ".env" will be kept as created
# by the runner scripts. (Default: Value set by github_actions_runner Class)
#
define github_actions_runner::instance (
Enum['present', 'absent'] $ensure = 'present',
String[1] $personal_access_token = $github_actions_runner::personal_access_token,
String[1] $user = $github_actions_runner::user,
String[1] $group = $github_actions_runner::group,
String[1] $hostname = $::facts['hostname'],
String[1] $instance_name = $title,
String[1] $github_domain = $github_actions_runner::github_domain,
String[1] $github_api = $github_actions_runner::github_api,
Optional[String[1]] $http_proxy = $github_actions_runner::http_proxy,
Optional[String[1]] $https_proxy = $github_actions_runner::https_proxy,
Optional[String[1]] $no_proxy = $github_actions_runner::no_proxy,
Optional[Array[String[1]]] $labels = undef,
Optional[String[1]] $enterprise_name = $github_actions_runner::enterprise_name,
Optional[String[1]] $org_name = $github_actions_runner::org_name,
Optional[String[1]] $repo_name = undef,
Optional[Array[String]] $path = $github_actions_runner::path,
Enum['present', 'absent'] $ensure = 'present',
String[1] $personal_access_token = $github_actions_runner::personal_access_token,
String[1] $user = $github_actions_runner::user,
String[1] $group = $github_actions_runner::group,
String[1] $hostname = $::facts['hostname'],
String[1] $instance_name = $title,
String[1] $github_domain = $github_actions_runner::github_domain,
String[1] $github_api = $github_actions_runner::github_api,
Optional[String[1]] $http_proxy = $github_actions_runner::http_proxy,
Optional[String[1]] $https_proxy = $github_actions_runner::https_proxy,
Optional[String[1]] $no_proxy = $github_actions_runner::no_proxy,
Optional[Array[String[1]]] $labels = undef,
Optional[String[1]] $enterprise_name = $github_actions_runner::enterprise_name,
Optional[String[1]] $org_name = $github_actions_runner::org_name,
Optional[String[1]] $repo_name = undef,
Optional[Array[String]] $path = $github_actions_runner::path,
Optional[Hash[String, String]] $env = $github_actions_runner::env,
) {

if $labels {
Expand Down Expand Up @@ -183,6 +189,25 @@
notify => Systemd::Unit_file["github-actions-runner.${instance_name}.service"]
}

$content_env = $env ? {
undef => undef,
default => epp('github_actions_runner/env.epp', {
envs => $env,
})
}

file { "${github_actions_runner::root_dir}/${name}/.env":
ensure => $ensure,
mode => '0644',
owner => $user,
group => $group,
content => $content_env,
require => [Archive["${instance_name}-${archive_name}"],
Exec["${instance_name}-run_configure_install_runner.sh"],
],
notify => Systemd::Unit_file["github-actions-runner.${instance_name}.service"]
}

$active_service = $ensure ? {
'present' => true,
'absent' => false,
Expand Down
75 changes: 73 additions & 2 deletions spec/classes/github_actions_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
end
end

context 'is expected to create a .path file in an instance setting path at global level' do
context 'is expected to create a .path file in an instance, setting path at global level' do
let(:params) do
super().merge(
'path' => [
Expand All @@ -391,7 +391,7 @@
end
end

context 'is expected to create a .path file in an instance setting the path at instance level' do
context 'is expected to create a .path file in an instance, setting the path at instance level' do
let(:params) do
super().merge(
'path' => [
Expand Down Expand Up @@ -420,6 +420,77 @@
end
end

# .env
context 'is expected to create a .env file with a specific requires and notifies' do
it do
is_expected.to contain_file('/some_dir/actions-runner-2.272.0/first_runner/.env')
is_expected.to contain_file('/some_dir/actions-runner-2.272.0/first_runner/.env').that_requires(['Archive[first_runner-actions-runner-linux-x64-2.272.0.tar.gz]',
'Exec[first_runner-run_configure_install_runner.sh]'])
is_expected.to contain_file('/some_dir/actions-runner-2.272.0/first_runner/.env').that_notifies('Systemd::Unit_file[github-actions-runner.first_runner.service]')
end
end

context 'is expected to create a .env file in an instance with default env hash (nil content)' do
it do
is_expected.to contain_file('/some_dir/actions-runner-2.272.0/first_runner/.env').with(
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0644',
'content' => nil,
)
end
end

context 'is expected to create a .env file in an instance, setting env at global level' do
let(:params) do
super().merge(
'env' => {
'foo' => 'bar',
'key' => 'value',
},
)
end

it do
is_expected.to contain_file('/some_dir/actions-runner-2.272.0/first_runner/.env').with(
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0644',
'content' => "foo=bar\nkey=value\n",
)
end
end

context 'is expected to create a .env file in an instance, setting the env at instance level' do
let(:params) do
super().merge(
'env' => {
'foo' => 'bar',
'key' => 'value',
},
'instances' => {
'first_runner' => {
'env' => {
'other' => 'value',
}
},
},
)
end

it do
is_expected.to contain_file('/some_dir/actions-runner-2.272.0/first_runner/.env').with(
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0644',
'content' => "other=value\n",
)
end
end

context 'is expected to remove github_actions_runner unit_file and other resources' do
let(:params) do
super().merge(
Expand Down
5 changes: 5 additions & 0 deletions templates/env.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%- | Hash[String, String] $envs,
| -%>
<%- $envs.each |$key, $value| { -%>
<%= $key %>=<%= $value %>
<%- } -%>

0 comments on commit bbc1f4c

Please sign in to comment.