Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the ansible collection #2

Merged
merged 22 commits into from
Dec 9, 2024
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
# autoware-github-runner-ansible

## Installation

### Install ansible

```bash
sudo apt update
sudo apt dist-upgrade -y

# Remove apt installed ansible (In Ubuntu 22.04, ansible the version is old)
sudo apt-get purge ansible

# Install pipx
sudo apt-get -y install pipx

# Add pipx to the system PATH
python3 -m pipx ensurepath

# Install ansible
pipx install --include-deps --force ansible
```

### Install ansible collections

```bash
ansible-galaxy install -f -r requirements.yaml
```

### Playbooks

#### Docker setup

```bash
ansible-playbook autoware.github_runner.docker_setup --ask-become-pass

# Restart to apply post-installation changes
sudo reboot
```

#### Runner setup

- 🔴 Modify the PAT according to <https://github.com/MonolithProjects/ansible-github_actions_runner?tab=readme-ov-file#requirements> .
- 🔴 Modify the runner name.
- 🔴 Modify the GitHub account.

```bash
export PERSONAL_ACCESS_TOKEN=<your_personal_access_token>

ansible-playbook autoware.github_runner.runner_setup --ask-become-pass --extra-vars "runner_name=ovh-runner-01 reinstall_runner=true github_account=xmfcx"
```

Set up the clean-up script.

```bash
ansible-playbook autoware.github_runner.runner_configuration --ask-become-pass

# Restart and check if everything is working
sudo reboot
```
15 changes: 15 additions & 0 deletions galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace: autoware
name: github_runner
version: 0.1.0
readme: README.md
authors:
- M. Fatih Cırıt <[email protected]>
description: Sets up the GitHub runner for Autoware repositories
license:
- Apache-2.0
tags:
- autoware
- github-runner
repository: https://github.com/autowarefoundation/autoware-github-runner-ansible
homepage: https://www.autoware.org/
issues: https://github.com/autowarefoundation/autoware-github-runner-ansible/issues
4 changes: 4 additions & 0 deletions playbooks/docker_setup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: Install docker engine
hosts: localhost
roles:
- autoware.github_runner.docker_engine
4 changes: 4 additions & 0 deletions playbooks/runner_configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: Configure the runner
hosts: localhost
roles:
- autoware.github_runner.runner_configuration
11 changes: 11 additions & 0 deletions playbooks/runner_setup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- name: Install GitHub Actions Runner
hosts: localhost
runner_user: ubuntu
become: true
vars:
- github_owner: autowarefoundation
- runner_org: true
- runner_group: Default
- runner_labels: [self-hosted, Linux, X64]
roles:
- role: monolithprojects.github_actions_runner

Check warning on line 11 in playbooks/runner_setup.yaml

View workflow job for this annotation

GitHub Actions / spell-check-differential

Unknown word (monolithprojects)
7 changes: 7 additions & 0 deletions requirements.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
roles:
- name: monolithprojects.github_actions_runner

Check warning on line 2 in requirements.yaml

View workflow job for this annotation

GitHub Actions / spell-check-differential

Unknown word (monolithprojects)
version: 1.21.1
src: https://github.com/MonolithProjects/ansible-github_actions_runner
collections:
- source: ./
type: dir
63 changes: 63 additions & 0 deletions roles/docker_engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# docker_engine

This role installs [Docker Engine](https://docs.docker.com/engine/) following the [installation guide](https://docs.docker.com/engine/install/ubuntu/) and sets up execution from non-root users following the [manual](https://docs.docker.com/engine/install/linux-postinstall/).

## Inputs

None.

## Manual Installation

Install Docker Engine:

```bash
# Taken from: https://docs.docker.com/engine/install/ubuntu/
# And: https://docs.docker.com/engine/install/linux-postinstall/

# Uninstall old versions
sudo apt-get remove docker docker-engine docker.io containerd runc

# Install using the repository
sudo apt-get update

sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release

# Add Docker’s official GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Use the following command to set up the repository:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

# Verify that Docker Engine is installed correctly by running the hello-world image.
sudo docker run hello-world
# Note: This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.
```

Perform the post-installation steps:

```bash
# Post-installation steps for Linux

# Create the docker group.
sudo groupadd docker

# Add your user to the docker group.
sudo usermod -aG docker $USER

# Log out and log back in so that your group membership is re-evaluated.

# Verify that you can run docker commands without sudo
docker run hello-world
# Note: This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.
```
Empty file.
Empty file.
91 changes: 91 additions & 0 deletions roles/docker_engine/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
- name: Uninstall old versions
become: true
ansible.builtin.apt:
name:
- docker
- docker-engine
- docker.io
- containerd
- runc
- docker.io
- docker-compose
- docker-compose-v2
- docker-doc
- podman-docker
state: absent
update_cache: true

- name: Install dependencies for setting up apt sources
become: true
ansible.builtin.apt:
name:
- ca-certificates
- curl
- gnupg
- lsb-release
update_cache: true

# sudo mkdir -p /etc/apt/keyrings
- name: Create the directory for keyrings
become: true
ansible.builtin.file:
state: directory
path: /etc/apt/keyrings
mode: 0755

# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- name: Authorize Docker GPG key
become: true
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
keyring: /etc/apt/keyrings/docker.gpg

- name: Save result of 'dpkg --print-architecture'
ansible.builtin.command: dpkg --print-architecture
register: docker_engine__deb_architecture
changed_when: false

- name: Save result of 'lsb_release -cs'
ansible.builtin.command: lsb_release -cs
register: docker_engine__lsb_release_cs
changed_when: false

- name: Save result of 'lsb_release -is'
ansible.builtin.command: lsb_release -is
register: docker_engine__lsb_release_is
changed_when: false

# echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- name: Add Docker apt repository to source list
become: true
ansible.builtin.apt_repository:
repo: deb [arch={{ docker_engine__deb_architecture.stdout }} signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/{{ docker_engine__lsb_release_is.stdout | lower }} {{ docker_engine__lsb_release_cs.stdout }} stable
filename: docker
state: present
update_cache: true

- name: Install Docker Engine
become: true
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
update_cache: true

# sudo groupadd docker
- name: Add docker group
become: true
ansible.builtin.group:
name: docker
state: present

# sudo usermod -aG docker $USER
- name: Add user to docker group
become: true
ansible.builtin.user:
name: "{{ ansible_user_id }}"
groups: docker
append: true
1 change: 1 addition & 0 deletions roles/runner_configuration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# runner_configuration
Empty file.
5 changes: 5 additions & 0 deletions roles/runner_configuration/files/cleanup_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

keep_last_x=4
# List all images, sort by creation date, get the image IDs, skip the last x, and remove the rest
docker images --format "{{.CreatedAt}} {{.ID}}" | sort -r | awk '{print $5}' | tail -n +$((keep_last_x + 1)) | xargs -r docker rmi -f
Empty file.
20 changes: 20 additions & 0 deletions roles/runner_configuration/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- name: Create directory for runner scripts
become: true
ansible.builtin.file:
path: /opt/runner-scripts
state: directory
mode: "0755"

- name: Install the cleanup script
become: true
ansible.builtin.copy:
src: "{{ role_path }}/files/cleanup_script.sh"
dest: /opt/runner-scripts/cleanup_script.sh
mode: "0755"

- name: Append cleanup script to actions-runner .env
become: true
ansible.builtin.lineinfile:
path: /opt/actions-runner/.env
line: ACTIONS_RUNNER_HOOK_JOB_STARTED=/opt/runner-scripts/cleanup_script.sh
create: true
Loading