Skip to content

Commit

Permalink
Merge pull request #319 from JulianMuehmelt/master
Browse files Browse the repository at this point in the history
add runners.machine and runners.machine.autoscaling support
  • Loading branch information
riemers authored Apr 24, 2024
2 parents 1430ed5 + e1e5b36 commit 4d19989
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ansible.cfg
.idea
.vscode
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ gitlab_runner:
## Autoscale Runner Machine vars for AWS (optional)
- `gitlab_runner_machine_options: []` - Foremost you need to pass an array of dedicated vars in the machine_options to configure your scaling runner:
- `MachineOptions: []` - Foremost you need to pass an array of dedicated vars in the machine_options to configure your scaling runner:

+ `amazonec2-access-key` and `amazonec2-secret-key` the keys of the dedicated IAM user with permission for EC2
+ `amazonec2-zone`
Expand All @@ -62,12 +62,10 @@ gitlab_runner:
- `MachineName` - Name of the machine. It **must** contain `%s`, which will be replaced with a unique machine identifier.
- `IdleCount` - Number of machines, that need to be created and waiting in Idle state.
- `IdleTime` - Time (in seconds) for machine to be in Idle state before it is removed.

In addition you could set *off peak* settings. This lets you select a regular time periods when no work is done. For example most of commercial companies are working from Monday to Friday in a fixed hours, eg. from 10am to 6pm. In the rest of the week - from Monday to Friday at 12am-9am and 6pm-11pm and whole Saturday and Sunday - no one is working. These time periods we’re naming here as Off Peak.

- `gitlab_runner_machine_off_peak_periods`
- `gitlab_runner_machine_off_peak_idle_time`
- `gitlab_runner_machine_off_peak_idle_count`
- `MaxGrowthRate` - The maximum number of machines that can be added to the runner in parallel. Default is 0 (no limit).
- `MaxBuilds` - Maximum job (build) count before machine is removed.
- `IdleScaleFactor` - (Experimental) The number of Idle machines as a factor of the number of machines currently in use. Must be in float number format. See the autoscale documentation for more details. Defaults to 0.0.
- `IdleCountMin` - Minimal number of machines that need to be created and waiting in Idle state when the IdleScaleFactor is in use. Default is 1.

### Read Sources
For details follow these links:
Expand Down Expand Up @@ -134,15 +132,21 @@ gitlab_runner_runners:
docker_image: 'alpine'
# Indicates whether this runner can pick jobs without tags.
run_untagged: true
extra_configs:
runners.machine:
IdleCount: 1
IdleTime: 1800
MaxBuilds: 10
MachineDriver: 'amazonec2'
MachineName: 'git-runner-%s'
MachineOptions: ["amazonec2-access-key={{ lookup('env','AWS_IAM_ACCESS_KEY') }}", "amazonec2-secret-key={{ lookup('env','AWS_IAM_SECRET_KEY') }}", "amazonec2-zone={{ lookup('env','AWS_EC2_ZONE') }}", "amazonec2-region={{ lookup('env','AWS_EC2_REGION') }}", "amazonec2-vpc-id={{ lookup('env','AWS_VPC_ID') }}", "amazonec2-subnet-id={{ lookup('env','AWS_SUBNET_ID') }}", "amazonec2-use-private-address=true", "amazonec2-tags=gitlab-runner", "amazonec2-security-group={{ lookup('env','AWS_EC2_SECURITY_GROUP') }}", "amazonec2-instance-type={{ lookup('env','AWS_EC2_INSTANCE_TYPE') }}"]
machine_IdleCount: 1
machine_IdleTime: 1800
machine_MaxBuilds: 10
machine_MachineDriver: 'amazonec2'
machine_MachineName: 'git-runner-%s'
machine_MachineOptions: ["amazonec2-access-key={{ lookup('env','AWS_IAM_ACCESS_KEY') }}", "amazonec2-secret-key={{ lookup('env','AWS_IAM_SECRET_KEY') }}", "amazonec2-zone={{ lookup('env','AWS_EC2_ZONE') }}", "amazonec2-region={{ lookup('env','AWS_EC2_REGION') }}", "amazonec2-vpc-id={{ lookup('env','AWS_VPC_ID') }}", "amazonec2-subnet-id={{ lookup('env','AWS_SUBNET_ID') }}", "amazonec2-use-private-address=true", "amazonec2-tags=gitlab-runner", "amazonec2-security-group={{ lookup('env','AWS_EC2_SECURITY_GROUP') }}", "amazonec2-instance-type={{ lookup('env','AWS_EC2_INSTANCE_TYPE') }}"]
machine_autoscaling:
- Periods: ["* * 7-18 * * mon-fri *"]
Timezone: "UTC"
IdleCount: 3
IdleTime: 900
- Periods: ["* * * * * sat,sun *"]
Timezone: "UTC"
IdleCount: 0
IdleTime: 300
```

### NOTE
Expand Down
156 changes: 156 additions & 0 deletions tasks/update-config-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,162 @@
- restart_gitlab_runner
- restart_gitlab_runner_macos

#### [runners.machine] section ####
- name: "{{ runn_name_prefix }} Set machine section"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^\s*\[runners\.machine\]'
line: ' [runners.machine]'
state: present
insertafter: EOF
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine MaxGrowthRate"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ MaxGrowthRate ='
line: ' MaxGrowthRate = {{ gitlab_runner.machine_MaxGrowthRate|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_MaxGrowthRate is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine IdleCount"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ IdleCount ='
line: ' IdleCount = {{ gitlab_runner.machine_IdleCount|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_IdleCount is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine IdleScaleFactor"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ IdleScaleFactor ='
line: ' IdleScaleFactor = {{ gitlab_runner.machine_IdleScaleFactor|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_IdleScaleFactor is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine IdleCountMin"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ IdleCountMin ='
line: ' IdleCountMin = {{ gitlab_runner.machine_IdleCountMin|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_IdleCountMin is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine IdleTime"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ IdleTime ='
line: ' IdleTime = {{ gitlab_runner.machine_IdleTime|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_IdleTime is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine MaxBuilds"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ MaxBuilds ='
line: ' MaxBuilds = {{ gitlab_runner.machine_MaxBuilds|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_MaxBuilds is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine MachineName"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ MachineName ='
line: ' MachineName = {{ gitlab_runner.machine_MachineName|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_MachineName is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine MachineDriver"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ MachineDriver ='
line: ' MachineDriver = {{ gitlab_runner.machine_MachineDriver|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_MachineDriver is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set machine MachineOptions"
lineinfile:
dest: "{{ temp_runner_config.path }}"
regexp: '^ MachineOptions ='
line: ' MachineOptions = {{ gitlab_runner.machine_MachineOptions|default("") | to_json }}'
state: "{{ 'present' if gitlab_runner.machine_MachineOptions is defined else 'absent' }}"
insertafter: '^\s*\[runners\.machine\]'
backrefs: no
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

#### [[runners.machine.autoscaling]] section ####

- name: "{{ runn_name_prefix }} Set additional autoscaling"
blockinfile:
dest: "{{ temp_runner_config.path }}"
content: "{{ lookup('template', 'config.runners.machine.autoscaling.j2') if gitlab_runner.machine_autoscaling is defined }}"
state: "{{ 'present' if gitlab_runner.machine_autoscaling is defined else 'absent' }}"
marker: "# {mark} runners.machine.autoscaling"
insertafter: EOF
check_mode: no
no_log: "{{ gitlab_runner_no_log_secrets | default(omit) }}"
notify:
- restart_gitlab_runner
- restart_gitlab_runner_macos

- name: "{{ runn_name_prefix }} Set builds dir file option"
lineinfile:
dest: "{{ temp_runner_config.path }}"
Expand Down
6 changes: 6 additions & 0 deletions templates/config.runners.machine.autoscaling.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% for machine in gitlab_runner.machine_autoscaling %}
[[runners.machine.autoscaling]]
{% for attr in machine %}
{{ attr }} = {{ machine[attr] | to_json }}
{% endfor %}
{% endfor %}

0 comments on commit 4d19989

Please sign in to comment.