Skip to content

Commit

Permalink
Merge pull request #67 from r0x0d/make-it-more-generic
Browse files Browse the repository at this point in the history
Update the worker to make it more generic
  • Loading branch information
r0x0d authored Aug 9, 2023
2 parents b59b767 + ba53505 commit 00082b3
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
platforms: 'linux/amd64'
package: 'src'
name: 'rhc-worker-bash-${{ steps.tagName.outputs.tag }}'
name: 'rhc-worker-script-${{ steps.tagName.outputs.tag }}'
compress: 'true'
dest: 'dist'

Expand Down
22 changes: 11 additions & 11 deletions .packit.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
specfile_path: packaging/rhc-worker-bash.spec
upstream_package_name: rhc-worker-bash
downstream_package_name: rhc-worker-bash
upstream_project_url: https://github.com/oamg/rhc-worker-bash
specfile_path: packaging/rhc-worker-script.spec
upstream_package_name: rhc-worker-script
downstream_package_name: rhc-worker-script
upstream_project_url: https://github.com/oamg/rhc-worker-script

srpm_build_deps: [golang, make]

Expand All @@ -10,22 +10,22 @@ jobs:
- job: copr_build
enable_net: true
owner: "@oamg"
project: "rhc-worker-bash"
project: "rhc-worker-script"
targets:
- epel-7-x86_64
trigger: pull_request
actions:
# do not get the version from a tag (git describe) but from the spec file
get-current-version:
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-bash.spec
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-script.spec
create-archive:
- "make distribution-tarball"
- bash -c "ls -1 ./rhc-worker-bash-*.tar.gz"
- bash -c "ls -1 ./rhc-worker-script-*.tar.gz"

- job: copr_build
enable_net: true
owner: "@oamg"
project: "rhc-worker-bash"
project: "rhc-worker-script"
targets:
- epel-7-x86_64
trigger: commit
Expand All @@ -34,11 +34,11 @@ jobs:
# bump spec so we get release starting with 2 and hence all the default branch builds will
# have higher NVR than all the PR builds
post-upstream-clone:
- rpmdev-bumpspec --comment='latest upstream build' ./packaging/rhc-worker-bash.spec
- rpmdev-bumpspec --comment='latest upstream build' ./packaging/rhc-worker-script.spec

# do not get the version from a tag (git describe) but from the spec file
get-current-version:
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-bash.spec
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-script.spec
create-archive:
- "make distribution-tarball"
- bash -c "ls -1 ./rhc-worker-bash-*.tar.gz"
- bash -c "ls -1 ./rhc-worker-script-*.tar.gz"
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to RHC Worker Bash
# Contributing to RHC Worker Script

The following is a set of guidelines for contributing to RHC Worker Bash codebase,
The following is a set of guidelines for contributing to RHC Worker Script codebase,
which are hosted in the [OAMG Organization](https://github.com/oamg) on GitHub.
These are mostly guidelines, not rules.

Expand Down Expand Up @@ -98,7 +98,7 @@ can run a series of pre-defined hooks against our codebase to keep it clean and
maintainable. Here is an example of output from `pre-commit` being run:

```
(.venv3) [rhc-worker-bash]$ pre-commit run --all-files
(.venv3) [rhc-worker-script]$ pre-commit run --all-files
golangci-lint............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
Expand Down Expand Up @@ -144,7 +144,7 @@ pre-commit](https://pre-commit.com/#usage).
### Writing tests

Tests are an important part of the development process, they guarantee to us
that our code is working in the correct way as expected, and for RHC Worker Bash,
that our code is working in the correct way as expected, and for RHC Worker Script,
we separate these tests in two categories.

- Unit testing
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Project constants
VERSION ?= 0.2
PKGNAME ?= rhc-worker-bash
PKGNAME ?= rhc-worker-script
GO_SOURCES := $(wildcard src/*.go)
PYTHON ?= python3
PIP ?= pip3
Expand Down Expand Up @@ -45,7 +45,7 @@ clean:

build: $(GO_SOURCES)
mkdir -p build
CGO_ENABLED=0 go build -o build/rhc-bash-worker $^
CGO_ENABLED=0 go build -o build/rhc-worker-script $^

distribution-tarball:
go mod vendor
Expand Down
45 changes: 23 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
![Tests](https://github.com/oamg/rhc-worker-bash/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/github/oamg/rhc-worker-bash/branch/main/graph/badge.svg?token=6MRLOJS2SJ)](https://codecov.io/github/oamg/rhc-worker-bash)
![Tests](https://github.com/oamg/rhc-worker-script/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/github/oamg/rhc-worker-script/branch/main/graph/badge.svg?token=6MRLOJS2SJ)](https://codecov.io/github/oamg/rhc-worker-script)

# RHC Worker Bash
# RHC Worker

Remote Host Configuration (rhc) worker for executing bash scripts on hosts
managed by Red Hat Insights.
Remote Host Configuration (rhc) worker for executing scripts on hosts
managed by Red Hat Insights. Interpreter used to execute the script is defined inside the supplied yaml file - served by insights.

- [RHC Worker Bash](#rhc-worker-bash)
- [RHC Worker Script](#rhc-worker-script)
- [General workflow of the worker](#general-workflow-of-the-worker)
- [Getting started with local development](#getting-started-with-local-development)
- [Publish first message](#publish-first-message)
- [Worker playbooks](#worker-playbooks)
- [Custom playbook](#custom-playbook)
- [Convert2RHEL Playbook](#convert2rhel-playbook)
- [FAQ](#faq)
- [Are there special environment variables used by `rhc-worker-bash`?](#are-there-special-environment-variables-used-by-rhc-worker-bash)
- [Can I change behavior of `rhc-worker-bash`?](#can-i-change-behavior-of-rhc-worker-bash)
- [Can I change the location of `rhc-worker-bash` config?](#can-i-change-the-location-of-rhc-worker-bash-config)
- [Are there special environment variables used by `rhc-worker-script`?](#are-there-special-environment-variables-used-by-rhc-worker-script)
- [Can I change behavior of `rhc-worker-script`?](#can-i-change-behavior-of-rhc-worker-script)
- [Can I change the location of `rhc-worker-script` config?](#can-i-change-the-location-of-rhc-worker-script-config)
- [Contact](#contact)
- [Package maintainers](#package-maintainers)
- [Package maintainers](#package-fmaintainers)

## General workflow of the worker

Expand Down Expand Up @@ -83,18 +83,19 @@ vagrant ssh -- -t 'rhcd --log-level trace \

### Worker playbooks

There is an [example playbook](
https://github.com/oamg/rhc-worker-bash/blob/main/development/nginx/data/example.yaml)
There is an [example bash playbook](
https://github.com/oamg/rhc-worker-script/blob/main/development/nginx/data/example_bash.yaml)
available under `development/nginx/data`, with a minimal bash script to use
during the worker execution.

If there's a need to test any other playbook provided in this repository, one
must change what playbook will be used during the message consumption in the
[mqtt_publish.py](https://github.com/oamg/rhc-worker-bash/blob/main/development/python/mqtt_publish.py#L22)
[mqtt_publish.py](https://github.com/oamg/rhc-worker-script/blob/main/development/python/mqtt_publish.py#L22)
file with the name that corresponds the ones present in `development/nginx/data`. Currently, the ones available are:

1. [example.yaml](https://github.com/oamg/rhc-worker-bash/blob/main/development/nginx/data/example.yaml)
2. [convert2rhel.yaml](https://github.com/oamg/rhc-worker-bash/blob/main/development/nginx/data/convert2rhel.yaml)
1. [example_bash.yaml](https://github.com/oamg/rhc-worker-script/blob/main/development/nginx/data/example_bash.yaml)
2. [example_python.yaml](https://github.com/oamg/rhc-worker-script/blob/main/development/nginx/data/example_python.yaml)
3. [convert2rhel.yaml](https://github.com/oamg/rhc-worker-script/blob/main/development/nginx/data/convert2rhel.yaml)

#### Custom playbook

Expand All @@ -112,23 +113,23 @@ A specialized [Convert2RHEL](https://github.com/oamg/convert2rhel) playbook can

## FAQ

### Are there special environment variables used by `rhc-worker-bash`?
### Are there special environment variables used by `rhc-worker-script`?

There is one special variable that must be set in order to run our worker and that is `YGG_SOCKET_ADDR`, this variable value is set by `rhcd` via `--socket-addr` option.

Other than that there are no special variables, however if downloaded yaml file contained `content_vars` (like the example above), then before the execution of the bash script (`content`) all such variables are set as environment variables and prefixed with `RHC_WORKER_`, after script execution is done they are unset.

### Can I change behavior of `rhc-worker-bash`?
### Can I change behavior of `rhc-worker-script`?

Yes, some values can be changed if config exists at `/etc/rhc/workers/rhc-worker-bash.yml`, **the config must have valid yaml format**, see all available fields below.
Yes, some values can be changed if config exists at `/etc/rhc/workers/rhc-worker-script.yml`, **the config must have valid yaml format**, see all available fields below.

Example of full config (with default values):

```yaml
# rhc-worker-bash configuration
# rhc-worker-script configuration

# recipient directive to register with dispatcher
directive: "rhc-worker-bash"
directive: "rhc-worker-script"

# whether to verify incoming yaml files
verify_yaml: true
Expand All @@ -137,10 +138,10 @@ verify_yaml: true
insights_core_gpg_check: true

# temporary directory in which the temporary files with executed bash scripts are created
temporary_worker_directory: "/var/lib/rhc-worker-bash"
temporary_worker_directory: "/var/lib/rhc-worker-script"
```
### Can I change the location of `rhc-worker-bash` config?
### Can I change the location of `rhc-worker-script` config?

No, not right now. If you want this feature please create an issue or upvote already existing issue.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
insights_signature: |
ascii_armored gpg signature
insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
interpreter: /bin/bash
content: |
#!/bin/sh
echo "Hello, world!"
echo "Hello, world!" > /root/bash.txt
content_vars:
# variables that will be handed to the script as environment vars
# will be prefixed with RHC_WORKER_*
Expand Down
20 changes: 20 additions & 0 deletions development/nginx/data/example_python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- name: Hello World Example
vars:
# Signature to validate that no one tampered with script
insights_signature: |
ascii_armored gpg signature
insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
interpreter: /usr/bin/python
content: |
def main():
print("Hello, Python!")
with open("/root/python.txt", "w") as handler:
handler.write("Hello, Python!")
main()
content_vars:
# variables that will be handed to the script as environment vars
# will be prefixed with RHC_WORKER_*
FOO: bar
BAR: foo
6 changes: 3 additions & 3 deletions development/python/mqtt_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ def get_ip_address():
return host_ip

# This is changed everytime you refresh the box and register the machine again.
CLIENT_ID = "26468815-8407-4058-bcc8-6bcb4eae51c1"
CLIENT_ID = "f7fb5fa0-9580-4c18-9658-f95885cb31b5"
BROKER = '127.0.0.1'
BROKER_PORT = 1883
TOPIC = f"yggdrasil/{CLIENT_ID}/data/in"

# NOTE: currently can be whatever you placed inside devleopment/nginx/data folder
SERVED_FILENAME = "example.yml"
SERVED_FILENAME = "example_bash.yml"

MESSAGE = {
"type": "data",
"message_id": str(uuid.uuid4()),
"version": 1,
"sent": "2021-01-12T14:58:13+00:00", # str(datetime.datetime.now().isoformat()),
"directive": 'rhc-worker-bash',
"directive": 'rhc-worker-script',
"content": f'http://{get_ip_address()}:8000/data/{SERVED_FILENAME}',
"metadata": {
"correlation_id": "00000000-0000-0000-0000-000000000000",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/oamg/rhc-worker-bash
module github.com/oamg/rhc-worker-script

go 1.16

Expand Down
23 changes: 11 additions & 12 deletions packaging/rhc-worker-bash.spec → packaging/rhc-worker-script.spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
%define debug_package %{nil}

%global repo_orgname oamg
%global repo_name rhc-worker-bash
%global binary_name rhc-bash-worker
%global repo_name rhc-worker-script
%global rhc_libexecdir %{_libexecdir}/rhc
%{!?_root_sysconfdir:%global _root_sysconfdir %{_sysconfdir}}
%global rhc_worker_conf_dir %{_root_sysconfdir}/rhc/workers
Expand All @@ -22,7 +21,7 @@
Name: %{repo_name}
Version: 0.2
Release: 1%{?dist}
Summary: Worker executing bash scripts on hosts managed by Red Hat Insights
Summary: Worker executing scripts on hosts managed by Red Hat Insights

License: GPLv3+
URL: https://github.com/%{repo_orgname}/%{repo_name}
Expand All @@ -37,36 +36,36 @@ BuildRequires: golang
Requires: rhc

%description
Remote Host Configuration (rhc) worker for executing bash scripts on hosts
Remote Host Configuration (rhc) worker for executing scripts on hosts
managed by Red Hat Insights.

%prep
%setup -q

%build
mkdir -p _gopath/src
ln -fs $(pwd)/src _gopath/src/%{binary_name}-%{version}
ln -fs $(pwd)/vendor _gopath/src/%{binary_name}-%{version}/vendor
ln -fs $(pwd)/src _gopath/src/%{repo_name}-%{version}
ln -fs $(pwd)/vendor _gopath/src/%{repo_name}-%{version}/vendor
export GOPATH=$(pwd)/_gopath
pushd _gopath/src/%{binary_name}-%{version}
pushd _gopath/src/%{repo_name}-%{version}
%if %{use_go_toolset_1_16}
scl enable go-toolset-1.16 -- %{gobuild}
%else
%{gobuild}
%endif
strip %{binary_name}-%{version}
strip %{repo_name}-%{version}
popd


%install
# Create a temporary directory /var/lib/rhc-worker-bash - used mainly for storing temporary files
install -d %{buildroot}%{_sharedstatedir}/%{binary_name}/
# Create a temporary directory /var/lib/rhc-worker-script - used mainly for storing temporary files
install -d %{buildroot}%{_sharedstatedir}/%{repo_name}/

install -D -m 755 _gopath/src/%{binary_name}-%{version}/%{binary_name}-%{version} %{buildroot}%{rhc_libexecdir}/%{binary_name}
install -D -m 755 _gopath/src/%{repo_name}-%{version}/%{repo_name}-%{version} %{buildroot}%{rhc_libexecdir}/%{repo_name}
install -D -d -m 755 %{buildroot}%{rhc_worker_conf_dir}

%files
%{rhc_libexecdir}/%{binary_name}
%{rhc_libexecdir}/%{repo_name}
%license LICENSE
%doc README.md

Expand Down
11 changes: 11 additions & 0 deletions rhc-worker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# recipient directive to register with dispatcher
directive: "rhc-worker-script"

# whether to verify incoming yaml files
verify_yaml: false

# perform the insights-client GPG check on the insights-core egg
insights_core_gpg_check: false

# temporary directory in which the temporary script will be placed and executed.
temporary_worker_directory: "/var/lib/rhc-worker-script"
1 change: 1 addition & 0 deletions src/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var ExampleYamlData = []byte(
vars:
insights_signature: "ascii_armored gpg signature"
insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
interpreter: /bin/bash
content: |
#!/bin/sh
echo "$RHC_WORKER_FOO $RHC_WORKER_BAR!"
Expand Down
2 changes: 1 addition & 1 deletion src/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var sosReportFolder = "/etc/sos.extras.d"
var sosReportFile = "rhc-worker-logs"
var sosReportFile = "rhc-worker-script-logs"

// SetupLogger sets up the logger for the application and returns the log file.
// It creates a log folder if it doesn't exist, opens a log file, sets the log level
Expand Down
10 changes: 5 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
)

// Initialized in main
const configFilePath = "/etc/rhc/workers/rhc-worker-bash.yml"
const logDir = "/var/log/rhc-worker-bash"
const logFileName = "rhc-worker-bash.log"
const configFilePath = "/etc/rhc/workers/rhc-worker-script.yml"
const logDir = "/var/log/rhc-worker-script"
const logFileName = "rhc-worker-script.log"

var yggdDispatchSocketAddr string
var config *Config
Expand Down Expand Up @@ -52,11 +52,11 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

// Register as a handler of the "rhc-worker-bash" type.
// Register as a handler of the "rhc-worker-script" type.
r, err := c.Register(
ctx,
&pb.RegistrationRequest{
Handler: "rhc-worker-bash",
Handler: "rhc-worker-script",
Pid: int64(os.Getpid()),
DetachedContent: true,
})
Expand Down
Loading

0 comments on commit 00082b3

Please sign in to comment.