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

Fix galaxy.yaml #56

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ broken in the latest version (please report an issue in this repository). Use th
ansible-galaxy collection install dynatrace.oneagent:==1.0.0
```

See [using Ansible collections](https://docs.ansible.com/ansible/devel/user_guide/collections_using.html) for more
details.
See [using Ansible collections](https://docs.ansible.com/ansible/devel/user_guide/collections_using.html) for more details.

## Use Cases

See [OneAgent role README](roles/oneagent/README.md) for more details.

## Testing

The collection was tested against Ansible sanity tests and component tests. The latter runs regular deployment with
the installer and checks veriety of installation scenarios.
See [OneAgent role tests README](roles/oneagent/tests/README.md) for more details.

## Support
Expand Down
15 changes: 10 additions & 5 deletions galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ readme: README.md
authors:
- Dynatrace LLC
description: Module to install and configure Dynatrace OneAgent deployment
license:
- MIT
license_file: LICENSE
tags:
- dynatrace
- oneagent
- monitoring
- agent
WStechura marked this conversation as resolved.
Show resolved Hide resolved
- deployment
- monitoring
- infrastructure
- linux
- windows
repository: https://github.com/Dynatrace/Dynatrace-OneAgent-Ansible
documentation: https://docs.dynatrace.com/docs/setup-and-configuration/dynatrace-oneagent/deployment-orchestration/ansible
build_ignore:
- roles/oneagent/tests
- roles/oneagent/examples
- .github
- .pre-commit-config.yaml
- .gitignore
- venv
4 changes: 1 addition & 3 deletions roles/oneagent/examples/advanced_config/advanced_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Download OneAgent installer in specific version to a custom directory with additional OneAgent install parameters.
Both linux_other and linux_arm have different user specified by platform args parameter.
hosts: linux_other,linux_arm
collections:
- dynatrace.oneagent
# credentials.yml file contains oneagent_environment_url and
# oneagent_paas_token variables that needs to be stored securely
vars_files:
Expand All @@ -22,4 +20,4 @@
tasks:
- name: Import Dynatrace OneAgent role
ansible.builtin.import_role:
name: oneagent
name: dynatrace.oneagent.oneagent
4 changes: 1 addition & 3 deletions roles/oneagent/examples/local_installer/local_installer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
Basic OneAgent installation using a local installer. Hosts placed in unix hosts groups have its local installer paths defined in inventory file.
Main node communicates with Windows hosts over SSH.
hosts: windows,unix
collections:
- dynatrace.oneagent
tasks:
- name: Import Dynatrace OneAgent role
ansible.builtin.import_role:
name: oneagent
name: dynatrace.oneagent.oneagent
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
- name: Apply host level configuration with oneagentctl
hosts: linux_other
collections:
- dynatrace.oneagent
vars:
oneagent_install_args:
- --set-host-name=new_host_name
Expand All @@ -12,4 +10,4 @@
tasks:
- name: Import Dynatrace OneAgent role
ansible.builtin.import_role:
name: oneagent
name: dynatrace.oneagent.oneagent
7 changes: 5 additions & 2 deletions roles/oneagent/tests/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Component tests

The tests support two types of deployment:
- local - the tests are run on the same Unix machine as main node;
- remote - the tests are run on a remote Windows (Unix is not supported at the moment) machine;
Currently, there is no option to mix these two types of deployment and the tests must be run for one platform at a time.

## Remote deployment

For remote deployment, regular OneAgent installers are used, which are downloaded from the Dynatrace environment during
the tests. To use this type of deployment, the following parameters must be provided:
- `--user` - username for the remote machine;
Expand All @@ -15,12 +17,14 @@ the tests. To use this type of deployment, the following parameters must be prov
Failing to provide any of these parameters will result in failure.

## Local deployment

For local deployment, the tests are using mocked version of the OneAgent installer, which simulates its basic behavior -
returning version, deploying `uninstall.sh` script and creating `oneagentctl`, used for configuring installation.
To use this type of deployment, the only required parameter is `--linux_x86=localhost`. In case, multiple platforms for
local deployment are specified or any other platforms is used along with local one, only the first local platform is used.

## Requirements

- Python 3.10+
- pip 21.0+
- venv 20.0+
Expand All @@ -36,8 +40,7 @@ Upon downloading the collection
$ apt-get install -y python3-venv python3-pip sshpass

# Create virtual environment
$ python -m venv venv
$ source venv/bin/activate
$ python -m venv venv && source venv/bin/activate

# Install requirements
$ pip install -r roles/oneagent/tests/requirements.txt
Expand Down
35 changes: 20 additions & 15 deletions roles/oneagent/tests/ansible/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
ANSIBLE_PASS_KEY,
ANSIBLE_RESOURCE_DIR,
ANSIBLE_USER_KEY,
COLLECTION_NAME,
COLLECTION_NAMESPACE,
CREDENTIALS_FILE_NAME,
HOSTS_TEMPLATE_FILE_NAME,
INSTALLED_COLLECTIONS_DIR,
INVENTORY_FILE,
PLAYBOOK_FILE,
PLAYBOOK_TEMPLATE_FILE_NAME,
ROLE_NAME,
TEST_COLLECTIONS_DIR,
TEST_SIGNATURE_FILE,
)

from util.common_utils import read_yaml_file, write_yaml_file
from util.constants.common_constants import TEST_DIRECTORY, INSTALLERS_DIRECTORY, INSTALLER_CERTIFICATE_FILE_NAME
from util.constants.common_constants import TEST_DIRECTORY
from util.test_data_types import DeploymentPlatform, PlatformCollection


Expand All @@ -30,17 +26,18 @@ def _prepare_collection() -> None:


def _prepare_playbook_file() -> None:
shutil.copy(
str(ANSIBLE_RESOURCE_DIR / PLAYBOOK_TEMPLATE_FILE_NAME), str(TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME)
)
shutil.copy(str(ANSIBLE_RESOURCE_DIR / PLAYBOOK_TEMPLATE_FILE_NAME),
str(TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME))


def _prepare_inventory_file(user: str, platforms: PlatformCollection) -> None:
host_file = TEST_DIRECTORY / HOSTS_TEMPLATE_FILE_NAME
shutil.copy(str(ANSIBLE_RESOURCE_DIR / HOSTS_TEMPLATE_FILE_NAME), str(host_file))
shutil.copy(str(ANSIBLE_RESOURCE_DIR /
HOSTS_TEMPLATE_FILE_NAME), str(host_file))
data = read_yaml_file(INVENTORY_FILE)
for platform, hosts in platforms.items():
group_data = data["all"]["children"][platform.family()]["children"][platform.value]
group_data = data["all"]["children"][platform.family()
]["children"][platform.value]
group_data["hosts"] = {k: None for k in hosts}
group_data["vars"][ANSIBLE_USER_KEY] = user
# TODO: Add condition to fail test if localhost is used with multiple platforms
Expand All @@ -53,7 +50,8 @@ def _prepare_inventory_file(user: str, platforms: PlatformCollection) -> None:

def _prepare_credentials_file(user: str, password: str) -> None:
credentials_file = TEST_DIRECTORY / CREDENTIALS_FILE_NAME
shutil.copy(str(ANSIBLE_RESOURCE_DIR / CREDENTIALS_FILE_NAME), str(credentials_file))
shutil.copy(str(ANSIBLE_RESOURCE_DIR / CREDENTIALS_FILE_NAME),
str(credentials_file))
data = read_yaml_file(credentials_file)
data[ANSIBLE_USER_KEY] = user
data[ANSIBLE_PASS_KEY] = password
Expand Down Expand Up @@ -86,7 +84,8 @@ class AnsibleConfig:
INSTALLER_ARCH_KEY = "oneagent_installer_arch"
INSTALLER_PLATFORM_ARGS_KEY = "oneagent_platform_install_args"

def __init__(self, user: str, password: str, platforms: PlatformCollection):
def __init__(self, user: str, password: str,
platforms: PlatformCollection):
self.user = user
self.password = password
self.platforms = platforms
Expand All @@ -102,9 +101,14 @@ def set_common_parameter(self, key: str, value: Any) -> None:
data[0][AnsibleConfig.PARAM_SECTION_KEY][key] = value
write_yaml_file(PLAYBOOK_FILE, data)

def set_platform_parameter(self, platform: DeploymentPlatform, key: str, value: Any) -> None:
def set_platform_parameter(
self,
platform: DeploymentPlatform,
key: str,
value: Any) -> None:
data = read_yaml_file(INVENTORY_FILE)
group_data = data["all"]["children"][platform.family()]["children"][platform.value]
group_data = data["all"]["children"][platform.family()
]["children"][platform.value]
group_data[self.PARAM_SECTION_KEY][key] = value
write_yaml_file(INVENTORY_FILE, data)

Expand All @@ -119,6 +123,7 @@ def clear_parameters_section(self) -> None:
write_yaml_file(PLAYBOOK_FILE, data)
data = read_yaml_file(INVENTORY_FILE)
for platform in DeploymentPlatform:
group_data = data["all"]["children"][platform.family()]["children"][platform.value]
group_data = data["all"]["children"][platform.family(
)]["children"][platform.value]
group_data[self.PARAM_SECTION_KEY] = {}
write_yaml_file(INVENTORY_FILE, data)
7 changes: 4 additions & 3 deletions roles/oneagent/tests/ansible/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
# we need to get the script's user home dir to access installed collection.
# Parents[-3] for __file__ will return "/home/<user>"
INSTALLED_COLLECTIONS_DIR = Path.home() / ".ansible" / "collections"
NAMESPACE_DIR = INSTALLED_COLLECTIONS_DIR / "ansible_collections" / COLLECTION_NAMESPACE
NAMESPACE_DIR = INSTALLED_COLLECTIONS_DIR / \
"ansible_collections" / COLLECTION_NAMESPACE
ROLE_DIR = NAMESPACE_DIR / COLLECTION_NAME / "roles" / ROLE_NAME

PLAYBOOK_FILE = TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME
INVENTORY_FILE = TEST_DIRECTORY / HOSTS_TEMPLATE_FILE_NAME
TEST_COLLECTIONS_DIR = TEST_DIRECTORY / "collections"
TEST_SIGNATURE_FILE = TEST_COLLECTIONS_DIR / "ansible_collections" / COLLECTION_NAMESPACE /\
COLLECTION_NAME / "roles" / ROLE_NAME / "files" / INSTALLER_CERTIFICATE_FILE_NAME
TEST_SIGNATURE_FILE = TEST_COLLECTIONS_DIR / "ansible_collections" / COLLECTION_NAMESPACE / \
COLLECTION_NAME / "roles" / ROLE_NAME / "files" / INSTALLER_CERTIFICATE_FILE_NAME

# Public
LOCAL_INSTALLERS_LOCATION = INSTALLERS_DIRECTORY
Expand Down
28 changes: 17 additions & 11 deletions roles/oneagent/tests/ansible/runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import subprocess
import logging

from ansible.constants import HOSTS_TEMPLATE_FILE_NAME, PLAYBOOK_TEMPLATE_FILE_NAME, CREDENTIALS_FILE_NAME, TEST_DIRECTORY
from ansible.constants import (HOSTS_TEMPLATE_FILE_NAME, PLAYBOOK_TEMPLATE_FILE_NAME, CREDENTIALS_FILE_NAME,
TEST_DIRECTORY)
from util.test_data_types import CommandResult, DeploymentResult


Expand All @@ -12,19 +13,24 @@ def __init__(self, user: str, password: str):

def run_deployment(self) -> DeploymentResult:
with open(TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME, 'r') as f:
logging.debug(f"Running playbook ({PLAYBOOK_TEMPLATE_FILE_NAME}):\n{f.read()}")
logging.debug(
"Running playbook (%s):\n%s", PLAYBOOK_TEMPLATE_FILE_NAME, f.read())

with open(TEST_DIRECTORY / HOSTS_TEMPLATE_FILE_NAME, 'r') as f:
logging.debug(f"Inventory file ({HOSTS_TEMPLATE_FILE_NAME}):\n{f.read()}")
logging.debug(
"Inventory file (%s):\n%s", HOSTS_TEMPLATE_FILE_NAME, f.read())

with open(TEST_DIRECTORY / CREDENTIALS_FILE_NAME, 'r') as f:
logging.debug(f"Credentials file ({CREDENTIALS_FILE_NAME}):\n{f.read()}")
logging.debug(
"Credentials file (%s):\n%s", CREDENTIALS_FILE_NAME, f.read())

res = subprocess.run(
["ansible-playbook", "-i", TEST_DIRECTORY / HOSTS_TEMPLATE_FILE_NAME, TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=False,
)
res = subprocess.run(["ansible-playbook",
"-i",
TEST_DIRECTORY / HOSTS_TEMPLATE_FILE_NAME,
TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=False,
)
return [CommandResult(res.returncode, res.stdout, res.stderr)]
6 changes: 5 additions & 1 deletion roles/oneagent/tests/command/command_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ def create_directory(self, address: str, directory: Path) -> CommandResult:
raise NotImplementedError

@abstractmethod
def run_command(self, address: str, command: str, *args: str) -> CommandResult:
def run_command(
self,
address: str,
command: str,
*args: str) -> CommandResult:
raise NotImplementedError
53 changes: 40 additions & 13 deletions roles/oneagent/tests/command/platform_command_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,49 @@ def __init__(self, user: str, password: str):
self.unix_command_wrapper = UnixCommandWrapper(user, password)
self.windows_command_wrapper = WindowsCommandWrapper(user, password)

def _get_command_wrapper(self, platform: DeploymentPlatform) -> CommandWrapper:
def _get_command_wrapper(
self,
platform: DeploymentPlatform) -> CommandWrapper:
if platform == DeploymentPlatform.WINDOWS_X86:
return self.windows_command_wrapper
return self.unix_command_wrapper

def get_file_content(self, platform: DeploymentPlatform, address: str, file: Path) -> CommandResult:
return self._get_command_wrapper(platform).get_file_content(address, file)

def file_exists(self, platform: DeploymentPlatform, address: str, file: Path) -> CommandResult:
def get_file_content(
self,
platform: DeploymentPlatform,
address: str,
file: Path) -> CommandResult:
return self._get_command_wrapper(
platform).get_file_content(address, file)

def file_exists(
self,
platform: DeploymentPlatform,
address: str,
file: Path) -> CommandResult:
return self._get_command_wrapper(platform).file_exists(address, file)

def directory_exists(self, platform: DeploymentPlatform, address: str, directory: Path) -> CommandResult:
return self._get_command_wrapper(platform).directory_exists(address, directory)

def create_directory(self, platform: DeploymentPlatform, address: str, directory: Path) -> CommandResult:
return self._get_command_wrapper(platform).create_directory(address, directory)

def run_command(self, platform: DeploymentPlatform, address: str, command: str, *args: str) -> CommandResult:
return self._get_command_wrapper(platform).run_command(address, command, *args)
def directory_exists(
self,
platform: DeploymentPlatform,
address: str,
directory: Path) -> CommandResult:
return self._get_command_wrapper(
platform).directory_exists(address, directory)

def create_directory(
self,
platform: DeploymentPlatform,
address: str,
directory: Path) -> CommandResult:
return self._get_command_wrapper(
platform).create_directory(address, directory)

def run_command(
self,
platform: DeploymentPlatform,
address: str,
command: str,
*args: str) -> CommandResult:
return self._get_command_wrapper(
platform).run_command(address, command, *args)
25 changes: 19 additions & 6 deletions roles/oneagent/tests/command/unix/unix_command_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ class UnixCommandWrapper(CommandWrapper):
def __init__(self, user: str, password: str):
self.password = password

def _execute(self, address: str, command: str, *args: str) -> CommandResult:
out = subprocess.run(
" ".join([command, *args]), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, check=False, shell=True
)
def _execute(
self,
address: str,
command: str,
*args: str) -> CommandResult:
out = subprocess.run(" ".join([command,
*args]),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=False,
shell=True)
return CommandResult(out.returncode, out.stdout, out.stderr)

def get_file_content(self, address: str, file: Path) -> CommandResult:
Expand All @@ -28,5 +36,10 @@ def directory_exists(self, address: str, directory: Path) -> CommandResult:
def create_directory(self, address: str, directory: Path) -> CommandResult:
return self._execute(address, "mkdir", "-p", str(directory))

def run_command(self, address: str, command: str, *args: str) -> CommandResult:
return self._execute(address, f"echo {self.password} | sudo -S {command}", *args)
def run_command(
self,
address: str,
command: str,
*args: str) -> CommandResult:
return self._execute(
address, f"echo {self.password} | sudo -S {command}", *args)
Loading
Loading