Skip to content

Commit

Permalink
Merge pull request #36 from tribe29/devel
Browse files Browse the repository at this point in the history
Minor refactoring and fixes
  • Loading branch information
robin-checkmk authored Apr 22, 2022
2 parents 5ad150c + 7def9ea commit 44b2157
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vagrant
.*.swp
.*.swo
*~
Expand Down
39 changes: 39 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|

# Ubuntu
config.vm.define "ansible-collection", primary: true do |srv|
srv.vm.box = "ubuntu/focal64"
srv.vm.network "private_network", ip: "IP_ADDRESS"
srv.ssh.insert_key = false
srv.vm.provider "virtualbox" do |v|
v.name = 'ansible-collection'
v.memory = 4096
v.cpus = 2
end
$script = <<-SCRIPT
apt-get update
apt-get install -y python3-pip
pip install -r /vagrant/requirements.txt
ansible-galaxy collection install -f -r /vagrant/requirements.yml
wget "https://download.checkmk.com/checkmk/2.0.0p23/check-mk-raw-2.0.0p23_0.focal_amd64.deb" -O /tmp/checkmk-stable.deb
wget "https://download.checkmk.com/checkmk/2.1.0b5/check-mk-raw-2.1.0b5_0.focal_amd64.deb" -O /tmp/checkmk-beta.deb
apt-get install -y /tmp/checkmk-stable.deb
omd create --admin-password 'cmk' stable
apt-get install -y /tmp/checkmk-beta.deb
omd create --admin-password 'cmk' beta
omd status -b stable || omd start stable
omd status -b beta || omd start beta
SCRIPT
srv.vm.provision "shell", inline: $script
# srv.vm.provision "shell" do |s|
# s.path = "./setup.sh"
# end
end
end
49 changes: 49 additions & 0 deletions changelogs/fragments/activation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# https://docs.ansible.com/ansible/latest/community/development_process.html#changelogs-how-to
minor_changes:
- Improve construction of headers and base_url variables in activation module.
- Clean up variable assignments in activation module.
- Add hint, that running the activation module is required only once and not per host.

# known_issues:
# - This release is still in development and a heavy work in progress.
# - Discovery module is not feature complete yet.
# - Downtime module is not fully idempotent yet. This affects service downtimes and deletions.

## Line Format
# When writing a changelog entry, use the following format:

# - scope - description starting with a lowercase letter and ending with a period at the very end. Multiple sentences are allowed (https://github.com/reference/to/an/issue or, if there is no issue, reference to a pull request itself).

# The scope is usually a module or plugin name or group of modules or plugins, for example, lookup plugins. While module names can (and should) be mentioned directly (foo_module), plugin names should always be followed by the type (foo inventory plugin).

# For changes that are not really scoped (for example, which affect a whole collection), use the following format:

# - Description starting with an uppercase letter and ending with a dot at the very end. Multiple sentences are allowed (https://github.com/reference/to/an/issue or, if there is no issue, reference to a pull request itself).


## Possible keys:

# breaking_changes

# Changes that break existing playbooks or roles. This includes any change to existing behavior that forces users to update tasks. Displayed in both the changelogs and the Porting Guides.
# major_changes

# Major changes to Ansible itself. Generally does not include module or plugin changes. Displayed in both the changelogs and the Porting Guides.
# minor_changes

# Minor changes to Ansible, modules, or plugins. This includes new features, new parameters added to modules, or behavior changes to existing parameters.
# deprecated_features

# Features that have been deprecated and are scheduled for removal in a future release. Displayed in both the changelogs and the Porting Guides.
# removed_features

# Features that were previously deprecated and are now removed. Displayed in both the changelogs and the Porting Guides.
# security_fixes

# Fixes that address CVEs or resolve security concerns. Include links to CVE information.
# bugfixes

# Fixes that resolve issues.
# known_issues

# Known issues that are currently not fixed or will not be fixed.
49 changes: 49 additions & 0 deletions changelogs/fragments/discovery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# https://docs.ansible.com/ansible/latest/community/development_process.html#changelogs-how-to
minor_changes:
- Improve construction of headers and base_url variables in discovery module.
- Clean up variable assignments in discovery module.
- Introduce quick fix for handling of HTTP 500 errors in discovery module.

# known_issues:
# - This release is still in development and a heavy work in progress.
# - Discovery module is not feature complete yet.
# - Downtime module is not fully idempotent yet. This affects service downtimes and deletions.

## Line Format
# When writing a changelog entry, use the following format:

# - scope - description starting with a lowercase letter and ending with a period at the very end. Multiple sentences are allowed (https://github.com/reference/to/an/issue or, if there is no issue, reference to a pull request itself).

# The scope is usually a module or plugin name or group of modules or plugins, for example, lookup plugins. While module names can (and should) be mentioned directly (foo_module), plugin names should always be followed by the type (foo inventory plugin).

# For changes that are not really scoped (for example, which affect a whole collection), use the following format:

# - Description starting with an uppercase letter and ending with a dot at the very end. Multiple sentences are allowed (https://github.com/reference/to/an/issue or, if there is no issue, reference to a pull request itself).


## Possible keys:

# breaking_changes

# Changes that break existing playbooks or roles. This includes any change to existing behavior that forces users to update tasks. Displayed in both the changelogs and the Porting Guides.
# major_changes

# Major changes to Ansible itself. Generally does not include module or plugin changes. Displayed in both the changelogs and the Porting Guides.
# minor_changes

# Minor changes to Ansible, modules, or plugins. This includes new features, new parameters added to modules, or behavior changes to existing parameters.
# deprecated_features

# Features that have been deprecated and are scheduled for removal in a future release. Displayed in both the changelogs and the Porting Guides.
# removed_features

# Features that were previously deprecated and are now removed. Displayed in both the changelogs and the Porting Guides.
# security_fixes

# Fixes that address CVEs or resolve security concerns. Include links to CVE information.
# bugfixes

# Fixes that resolve issues.
# known_issues

# Known issues that are currently not fixed or will not be fixed.
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace: tribe29
name: checkmk

# The version of the collection. Must be compatible with semantic versioning
version: 0.2.0
version: 0.2.1

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
28 changes: 16 additions & 12 deletions plugins/modules/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
description:
- Activate changes within Checkmk.
- This module only needs to be run once and not for every host. Use C(run_once).
extends_documentation_fragment: [tribe29.checkmk.common]
Expand All @@ -43,6 +44,7 @@
site: "my_site"
automation_user: "automation"
automation_secret: "$SECRET"
run_once: 'true'
- name: "Activate changes on a specific site."
tribe29.checkmk.activation:
Expand All @@ -52,6 +54,7 @@
automation_secret: "$SECRET"
sites:
- "my_site"
run_once: 'true'
- name: "Activate changes including foreign changes."
tribe29.checkmk.activation:
Expand All @@ -60,6 +63,7 @@
automation_user: "automation"
automation_secret: "$SECRET"
force_foreign_changes: 'true'
run_once: 'true'
'''

RETURN = r'''
Expand Down Expand Up @@ -98,14 +102,9 @@ def run_module():
changed = False
failed = False
http_code = ''
server_url = module.params['server_url']
site = module.params['site']
automation_user = module.params['automation_user']
automation_secret = module.params['automation_secret']
sites = module.params['sites']
if sites == {}:
sites = []
force_foreign_changes = module.params['force_foreign_changes']

http_code_mapping = {
# http_code: (changed, failed, "Message")
Expand All @@ -127,21 +126,26 @@ def run_module():

# Declare headers including authentication to send to the Checkmk API
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + automation_user + ' ' + automation_secret
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer %s %s"
% (
module.params.get("automation_user", ""),
module.params.get("automation_secret", ""),
),
}

params = {
'force_foreign_changes': force_foreign_changes,
'force_foreign_changes': module.params.get("force_foreign_changes", ""),
'redirect': True, # ToDo: Do we need this? Does it need to be configurable?
'sites': sites
'sites': sites,
}

base_url = "%s/%s/check_mk/api/1.0" % (
server_url,
site,
module.params.get("server_url", ""),
module.params.get("site", ""),
)

api_endpoint = '/domain-types/activation_run/actions/activate-changes/invoke'
url = base_url + api_endpoint
response, info = fetch_url(module, url, module.jsonify(params), headers=headers, method='POST')
Expand Down
25 changes: 14 additions & 11 deletions plugins/modules/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ def run_module():
changed = False
failed = False
http_code = ''
server_url = module.params['server_url']
site = module.params['site']
automation_user = module.params['automation_user']
automation_secret = module.params['automation_secret']
host_name = module.params['host_name']
state = module.params['state']

http_code_mapping = {
# http_code: (changed, failed, "Message")
Expand All @@ -107,24 +101,33 @@ def run_module():
404: (False, True, "Not Found: Host could not be found."),
406: (False, True, "Not Acceptable."),
415: (False, True, "Unsupported Media Type."),
500:
(False, True,
"General Server Error. This might be related to a host not being discoverable at all. Check the affected host in the UI."
),
}

# Declare headers including authentication to send to the Checkmk API
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + automation_user + ' ' + automation_secret
"Authorization": "Bearer %s %s"
% (
module.params.get("automation_user", ""),
module.params.get("automation_secret", ""),
),
}

params = {
'mode': state,
'mode': module.params.get("state", ""),
}

base_url = "%s/%s/check_mk/api/1.0" % (
server_url,
site,
module.params.get("server_url", ""),
module.params.get("site", ""),
)
api_endpoint = '/objects/host/' + host_name + '/actions/discover_services/invoke'

api_endpoint = '/objects/host/' + module.params.get("host_name") + '/actions/discover_services/invoke'
url = base_url + api_endpoint
response, info = fetch_url(module, url, module.jsonify(params), headers=headers, method='POST')
http_code = info['status']
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/targets/activation/tasks/prep.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
- name: "Download Checkmk."
- name: "Download Checkmk {{ checkmk_version }}."
ansible.builtin.get_url:
url: "{{ download_url }}"
dest: /tmp/checkmk-server.deb

- name: "Install Checkmk."
- name: "Install Checkmk {{ checkmk_version }}."
ansible.builtin.apt:
deb: /tmp/checkmk-server.deb
state: present

- name: "Create site."
- name: "Create site {{ site }}."
ansible.builtin.command: "omd create --no-tmpfs --admin-password {{ automation_secret }} {{ site }}"
args:
creates: "/omd/sites/{{ site }}"

- name: "Start site."
- name: "Start site {{ site }}."
ansible.builtin.shell: "omd status -b {{ site }} || omd start {{ site }}"
3 changes: 2 additions & 1 deletion tests/integration/targets/activation/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
download_url: "https://download.checkmk.com/checkmk/2.0.0p20/check-mk-raw-2.0.0p20_0.bionic_amd64.deb"
checkmk_version: "2.0.0p23"
download_url: "https://download.checkmk.com/checkmk/{{ checkmk_version }}/check-mk-raw-{{ checkmk_version }}_0.bionic_amd64.deb"
site: "test"
server_url: "http://127.0.0.1/"
automation_user: "cmkadmin"
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/targets/discovery/tasks/prep.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
- name: "Download Checkmk."
- name: "Download Checkmk {{ checkmk_version }}."
ansible.builtin.get_url:
url: "{{ download_url }}"
dest: /tmp/checkmk-server.deb

- name: "Install Checkmk."
- name: "Install Checkmk {{ checkmk_version }}."
ansible.builtin.apt:
deb: /tmp/checkmk-server.deb
state: present

- name: "Create site."
- name: "Create site {{ site }}."
ansible.builtin.command: "omd create --no-tmpfs --admin-password {{ automation_secret }} {{ site }}"
args:
creates: "/omd/sites/{{ site }}"

- name: "Start site."
- name: "Start site {{ site }}."
ansible.builtin.shell: "omd status -b {{ site }} || omd start {{ site }}"
3 changes: 2 additions & 1 deletion tests/integration/targets/discovery/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
download_url: "https://download.checkmk.com/checkmk/2.0.0p20/check-mk-raw-2.0.0p20_0.bionic_amd64.deb"
checkmk_version: "2.0.0p23"
download_url: "https://download.checkmk.com/checkmk/{{ checkmk_version }}/check-mk-raw-{{ checkmk_version }}_0.bionic_amd64.deb"
site: "test"
server_url: "http://127.0.0.1/"
automation_user: "cmkadmin"
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/targets/downtime/tasks/prep.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
- name: "Download Checkmk."
- name: "Download Checkmk {{ checkmk_version }}."
ansible.builtin.get_url:
url: "{{ download_url }}"
dest: /tmp/checkmk-server.deb

- name: "Install Checkmk."
- name: "Install Checkmk {{ checkmk_version }}."
ansible.builtin.apt:
deb: /tmp/checkmk-server.deb
state: present

- name: "Create site."
- name: "Create site {{ site }}."
ansible.builtin.command: "omd create --no-tmpfs --admin-password {{ automation_secret }} {{ site }}"
args:
creates: "/omd/sites/{{ site }}"

- name: "Start site."
- name: "Start site {{ site }}."
ansible.builtin.shell: "omd status -b {{ site }} || omd start {{ site }}"
3 changes: 2 additions & 1 deletion tests/integration/targets/downtime/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
download_url: "https://download.checkmk.com/checkmk/2.0.0p20/check-mk-raw-2.0.0p20_0.bionic_amd64.deb"
checkmk_version: "2.0.0p23"
download_url: "https://download.checkmk.com/checkmk/{{ checkmk_version }}/check-mk-raw-{{ checkmk_version }}_0.bionic_amd64.deb"
site: "test"
server_url: "http://127.0.0.1/"
automation_user: "cmkadmin"
Expand Down
Loading

0 comments on commit 44b2157

Please sign in to comment.