Skip to content

Commit

Permalink
feat: Add cadvisor role
Browse files Browse the repository at this point in the history
Add a role to deploy https://github.com/google/cadvisor.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Jan 8, 2024
1 parent 6b1847f commit c9ec47d
Show file tree
Hide file tree
Showing 24 changed files with 641 additions and 0 deletions.
53 changes: 53 additions & 0 deletions roles/cadvisor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p>

# Ansible Role: cadvisor

## Description

Deploy [cadvisor](https://github.com/google/cadvisor) using ansible.

## Requirements

- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)

## Role Variables

All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml).
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/cadvisor_role.html) for description and default values of the variables.

## Example

### Playbook

Use it in a playbook as follows:
```yaml
- hosts: all
roles:
- prometheus.prometheus.cadvisor
```
### Demo site
We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/superq/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com).
## Local Testing
The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`.

## Continuous Integration

Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient.

## Contributing

See [contributor guideline](CONTRIBUTING.md).

## Troubleshooting

See [troubleshooting](TROUBLESHOOTING.md).

## License

This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
20 changes: 20 additions & 0 deletions roles/cadvisor/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Troubleshooting

## Bad requests (HTTP 400)

This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur.

This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers.

Unless netrc is needed by your playbook and ansible roles, please unset the var like so:

```
$ NETRC= ansible-playbook ...
```

Or:

```
$ export NETRC=
$ ansible-playbook ...
```
14 changes: 14 additions & 0 deletions roles/cadvisor/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
cadvisor_version: 0.47.2
cadvisor_binary_local_dir: ""
cadvisor_binary_url: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/\
cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}"
cadvisor_skip_install: false

cadvisor_listen_ip: "0.0.0.0"
cadvisor_port: "8080"
cadvisor_prometheus_endpoint: "/metrics"

cadvisor_binary_install_dir: "/usr/local/bin"
cadvisor_system_group: "root"
cadvisor_system_user: "{{ cadvisor_system_group }}"
10 changes: 10 additions & 0 deletions roles/cadvisor/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart cadvisor
listen: "restart cadvisor"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: cadvisor
state: restarted
when:
- not ansible_check_mode
49 changes: 49 additions & 0 deletions roles/cadvisor/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "cAdvisor"
description:
- "Deploy L(cadvisor,https://github.com/google/cadvisor) using ansible"
author:
- "Prometheus Community"
options:
cadvisor_version:
description: "cAdvisor package version. Also accepts latest as parameter."
default: "0.47.2"
cadvisor_skip_install:
description: "cAdvisor installation tasks gets skipped when set to true."
type: bool
default: false
cadvisor_binary_local_dir:
description:
- "Enables the use of local packages instead of those distributed on github."
- "The parameter may be set to a directory where the C(cadvisor) binary is stored on the host where ansible is run."
- "This overrides the I(cadvisor_version) parameter"
cadvisor_binary_url:
description: "URL of the cadvisor binary file"
default: "https://github.com/{{ _cadvisor_repo }}/releases/download/v{{ cadvisor_version }}/cadvisor-{{ cadvisor_version }}-linux-{{ go_arch }}"
cadvisor_listen_ip:
description: "Address on which cadvisor will listen"
default: "0.0.0.0"
cadvisor_port:
description: "Port on which cadvisor will listen"
default: "8080"
cadvisor_prometheus_endpoint:
description: "Path under which to expose metrics"
default: "/metrics"
cadvisor_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install cadvisor binary"
default: "/usr/local/bin"
cadvisor_system_group:
description:
- "I(Advanced)"
- "System group for cadvisor"
default: "root"
cadvisor_system_user:
description:
- "I(Advanced)"
- "cAdvisor user"
default: "root"
30 changes: 30 additions & 0 deletions roles/cadvisor/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
galaxy_info:
author: "cAdvisor Authors"
description: "cAdvisor"
license: "Apache"
min_ansible_version: "2.9"
platforms:
- name: "Ubuntu"
versions:
- "focal"
- "jammy"
- name: "Debian"
versions:
- "bullseye"
- "buster"
- name: "EL"
versions:
- "7"
- "8"
- "9"
- name: "Fedora"
versions:
- "37"
- '38'
galaxy_tags:
- "monitoring"
- "prometheus"
- "exporter"
- "metrics"
- "system"
9 changes: 9 additions & 0 deletions roles/cadvisor/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
provisioner:
inventory:
group_vars:
all:
cadvisor_binary_local_dir: "/tmp"
cadvisor_port: "8000"
go_arch: amd64
cadvisor_version: 0.47.0
26 changes: 26 additions & 0 deletions roles/cadvisor/molecule/alternative/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: Run local preparation
hosts: localhost
vars_files:

Check failure on line 4 in roles/cadvisor/molecule/alternative/prepare.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

yaml[trailing-spaces]

Trailing spaces
- ../defaults/main.yml
gather_facts: false
tasks:
- name: Download cadvisor binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ cadvisor_binary_url }}"
dest: "/tmp/cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}"
mode: 0644
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
check_mode: false

- name: Link to cadvisor binaries directory
become: false
ansible.builtin.file:
src: "/tmp/cadvisor-v{{ cadvisor_version }}-linux-{{ go_arch }}"
dest: "/tmp/cadvisor"
state: link
check_mode: false
35 changes: 35 additions & 0 deletions roles/cadvisor/molecule/alternative/tests/test_alternative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner
import pytest

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_service(host):
s = host.service("cadvisor")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u cadvisor --since "1 hour ago"')
print("\n==== journalctl -u cadvisor Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("cadvisor")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


@pytest.mark.parametrize("sockets", [
"tcp://127.0.0.1:8000",
])
def test_socket(host, sockets):
assert host.socket(sockets).is_listening
6 changes: 6 additions & 0 deletions roles/cadvisor/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
cadvisor_port: "8080"
69 changes: 69 additions & 0 deletions roles/cadvisor/molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_files(host):
files = [
"/etc/systemd/system/cadvisor.service",
"/usr/local/bin/cadvisor"
]
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file


def test_permissions_didnt_change(host):
dirs = [
"/etc",
"/root",
"/usr",
"/var"
]
for file in dirs:
f = host.file(file)
assert f.exists
assert f.is_directory
assert f.user == "root"
assert f.group == "root"


def test_user(host):
assert host.group("root").exists
assert "root" in host.user("root").groups
assert host.user("root").shell == "/bin/bash"
assert host.user("root").home == "/root"


def test_service(host):
s = host.service("cadvisor")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u cadvisor --since "1 hour ago"')
print("\n==== journalctl -u cadvisor Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("cadvisor")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
sockets = [
"tcp://127.0.0.1:8080"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
6 changes: 6 additions & 0 deletions roles/cadvisor/molecule/latest/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
cadvisor_version: latest
43 changes: 43 additions & 0 deletions roles/cadvisor/molecule/latest/tests/test_alternative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner
import pytest

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.mark.parametrize("files", [
"/etc/systemd/system/cadvisor.service",
"/usr/local/bin/cadvisor"
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file


def test_service(host):
s = host.service("cadvisor")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u cadvisor --since "1 hour ago"')
print("\n==== journalctl -u cadvisor Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("cadvisor")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
s = host.socket("tcp://0.0.0.0:8080")
assert s.is_listening
Loading

0 comments on commit c9ec47d

Please sign in to comment.