Skip to content

Commit 00082b3

Browse files
authored
Merge pull request #67 from r0x0d/make-it-more-generic
Update the worker to make it more generic
2 parents b59b767 + ba53505 commit 00082b3

18 files changed

+115
-82
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
with:
2828
platforms: 'linux/amd64'
2929
package: 'src'
30-
name: 'rhc-worker-bash-${{ steps.tagName.outputs.tag }}'
30+
name: 'rhc-worker-script-${{ steps.tagName.outputs.tag }}'
3131
compress: 'true'
3232
dest: 'dist'
3333

.packit.yaml

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
specfile_path: packaging/rhc-worker-bash.spec
2-
upstream_package_name: rhc-worker-bash
3-
downstream_package_name: rhc-worker-bash
4-
upstream_project_url: https://github.com/oamg/rhc-worker-bash
1+
specfile_path: packaging/rhc-worker-script.spec
2+
upstream_package_name: rhc-worker-script
3+
downstream_package_name: rhc-worker-script
4+
upstream_project_url: https://github.com/oamg/rhc-worker-script
55

66
srpm_build_deps: [golang, make]
77

@@ -10,22 +10,22 @@ jobs:
1010
- job: copr_build
1111
enable_net: true
1212
owner: "@oamg"
13-
project: "rhc-worker-bash"
13+
project: "rhc-worker-script"
1414
targets:
1515
- epel-7-x86_64
1616
trigger: pull_request
1717
actions:
1818
# do not get the version from a tag (git describe) but from the spec file
1919
get-current-version:
20-
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-bash.spec
20+
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-script.spec
2121
create-archive:
2222
- "make distribution-tarball"
23-
- bash -c "ls -1 ./rhc-worker-bash-*.tar.gz"
23+
- bash -c "ls -1 ./rhc-worker-script-*.tar.gz"
2424

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

3939
# do not get the version from a tag (git describe) but from the spec file
4040
get-current-version:
41-
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-bash.spec
41+
- grep -oP '^Version:\s+\K\S+' packaging/rhc-worker-script.spec
4242
create-archive:
4343
- "make distribution-tarball"
44-
- bash -c "ls -1 ./rhc-worker-bash-*.tar.gz"
44+
- bash -c "ls -1 ./rhc-worker-script-*.tar.gz"

CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Contributing to RHC Worker Bash
1+
# Contributing to RHC Worker Script
22

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

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

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

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

150150
- Unit testing

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# Project constants
1414
VERSION ?= 0.2
15-
PKGNAME ?= rhc-worker-bash
15+
PKGNAME ?= rhc-worker-script
1616
GO_SOURCES := $(wildcard src/*.go)
1717
PYTHON ?= python3
1818
PIP ?= pip3
@@ -45,7 +45,7 @@ clean:
4545

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

5050
distribution-tarball:
5151
go mod vendor

README.md

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
![Tests](https://github.com/oamg/rhc-worker-bash/actions/workflows/tests.yml/badge.svg)
2-
[![codecov](https://codecov.io/github/oamg/rhc-worker-bash/branch/main/graph/badge.svg?token=6MRLOJS2SJ)](https://codecov.io/github/oamg/rhc-worker-bash)
1+
![Tests](https://github.com/oamg/rhc-worker-script/actions/workflows/tests.yml/badge.svg)
2+
[![codecov](https://codecov.io/github/oamg/rhc-worker-script/branch/main/graph/badge.svg?token=6MRLOJS2SJ)](https://codecov.io/github/oamg/rhc-worker-script)
33

4-
# RHC Worker Bash
4+
# RHC Worker
55

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

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

2323
## General workflow of the worker
2424

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

8484
### Worker playbooks
8585

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

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

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

99100
#### Custom playbook
100101

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

113114
## FAQ
114115

115-
### Are there special environment variables used by `rhc-worker-bash`?
116+
### Are there special environment variables used by `rhc-worker-script`?
116117

117118
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.
118119

119120
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.
120121

121-
### Can I change behavior of `rhc-worker-bash`?
122+
### Can I change behavior of `rhc-worker-script`?
122123

123-
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.
124+
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.
124125

125126
Example of full config (with default values):
126127

127128
```yaml
128-
# rhc-worker-bash configuration
129+
# rhc-worker-script configuration
129130

130131
# recipient directive to register with dispatcher
131-
directive: "rhc-worker-bash"
132+
directive: "rhc-worker-script"
132133

133134
# whether to verify incoming yaml files
134135
verify_yaml: true
@@ -137,10 +138,10 @@ verify_yaml: true
137138
insights_core_gpg_check: true
138139

139140
# temporary directory in which the temporary files with executed bash scripts are created
140-
temporary_worker_directory: "/var/lib/rhc-worker-bash"
141+
temporary_worker_directory: "/var/lib/rhc-worker-script"
141142
```
142143
143-
### Can I change the location of `rhc-worker-bash` config?
144+
### Can I change the location of `rhc-worker-script` config?
144145

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

development/nginx/data/example.yml development/nginx/data/example_bash.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
insights_signature: |
55
ascii_armored gpg signature
66
insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
7+
interpreter: /bin/bash
78
content: |
8-
#!/bin/sh
9-
echo "Hello, world!"
9+
echo "Hello, world!" > /root/bash.txt
1010
content_vars:
1111
# variables that will be handed to the script as environment vars
1212
# will be prefixed with RHC_WORKER_*
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- name: Hello World Example
2+
vars:
3+
# Signature to validate that no one tampered with script
4+
insights_signature: |
5+
ascii_armored gpg signature
6+
insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
7+
interpreter: /usr/bin/python
8+
content: |
9+
def main():
10+
print("Hello, Python!")
11+
12+
with open("/root/python.txt", "w") as handler:
13+
handler.write("Hello, Python!")
14+
15+
main()
16+
content_vars:
17+
# variables that will be handed to the script as environment vars
18+
# will be prefixed with RHC_WORKER_*
19+
FOO: bar
20+
BAR: foo

development/python/mqtt_publish.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ def get_ip_address():
1212
return host_ip
1313

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

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

2323
MESSAGE = {
2424
"type": "data",
2525
"message_id": str(uuid.uuid4()),
2626
"version": 1,
2727
"sent": "2021-01-12T14:58:13+00:00", # str(datetime.datetime.now().isoformat()),
28-
"directive": 'rhc-worker-bash',
28+
"directive": 'rhc-worker-script',
2929
"content": f'http://{get_ip_address()}:8000/data/{SERVED_FILENAME}',
3030
"metadata": {
3131
"correlation_id": "00000000-0000-0000-0000-000000000000",

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/oamg/rhc-worker-bash
1+
module github.com/oamg/rhc-worker-script
22

33
go 1.16
44

packaging/rhc-worker-bash.spec packaging/rhc-worker-script.spec

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
%define debug_package %{nil}
55

66
%global repo_orgname oamg
7-
%global repo_name rhc-worker-bash
8-
%global binary_name rhc-bash-worker
7+
%global repo_name rhc-worker-script
98
%global rhc_libexecdir %{_libexecdir}/rhc
109
%{!?_root_sysconfdir:%global _root_sysconfdir %{_sysconfdir}}
1110
%global rhc_worker_conf_dir %{_root_sysconfdir}/rhc/workers
@@ -22,7 +21,7 @@
2221
Name: %{repo_name}
2322
Version: 0.2
2423
Release: 1%{?dist}
25-
Summary: Worker executing bash scripts on hosts managed by Red Hat Insights
24+
Summary: Worker executing scripts on hosts managed by Red Hat Insights
2625

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

3938
%description
40-
Remote Host Configuration (rhc) worker for executing bash scripts on hosts
39+
Remote Host Configuration (rhc) worker for executing scripts on hosts
4140
managed by Red Hat Insights.
4241

4342
%prep
4443
%setup -q
4544

4645
%build
4746
mkdir -p _gopath/src
48-
ln -fs $(pwd)/src _gopath/src/%{binary_name}-%{version}
49-
ln -fs $(pwd)/vendor _gopath/src/%{binary_name}-%{version}/vendor
47+
ln -fs $(pwd)/src _gopath/src/%{repo_name}-%{version}
48+
ln -fs $(pwd)/vendor _gopath/src/%{repo_name}-%{version}/vendor
5049
export GOPATH=$(pwd)/_gopath
51-
pushd _gopath/src/%{binary_name}-%{version}
50+
pushd _gopath/src/%{repo_name}-%{version}
5251
%if %{use_go_toolset_1_16}
5352
scl enable go-toolset-1.16 -- %{gobuild}
5453
%else
5554
%{gobuild}
5655
%endif
57-
strip %{binary_name}-%{version}
56+
strip %{repo_name}-%{version}
5857
popd
5958

6059

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

65-
install -D -m 755 _gopath/src/%{binary_name}-%{version}/%{binary_name}-%{version} %{buildroot}%{rhc_libexecdir}/%{binary_name}
64+
install -D -m 755 _gopath/src/%{repo_name}-%{version}/%{repo_name}-%{version} %{buildroot}%{rhc_libexecdir}/%{repo_name}
6665
install -D -d -m 755 %{buildroot}%{rhc_worker_conf_dir}
6766

6867
%files
69-
%{rhc_libexecdir}/%{binary_name}
68+
%{rhc_libexecdir}/%{repo_name}
7069
%license LICENSE
7170
%doc README.md
7271

rhc-worker.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# recipient directive to register with dispatcher
2+
directive: "rhc-worker-script"
3+
4+
# whether to verify incoming yaml files
5+
verify_yaml: false
6+
7+
# perform the insights-client GPG check on the insights-core egg
8+
insights_core_gpg_check: false
9+
10+
# temporary directory in which the temporary script will be placed and executed.
11+
temporary_worker_directory: "/var/lib/rhc-worker-script"

src/fixtures_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var ExampleYamlData = []byte(
77
vars:
88
insights_signature: "ascii_armored gpg signature"
99
insights_signature_exclude: "/vars/insights_signature,/vars/content_vars"
10+
interpreter: /bin/bash
1011
content: |
1112
#!/bin/sh
1213
echo "$RHC_WORKER_FOO $RHC_WORKER_BAR!"

src/logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

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

1515
// SetupLogger sets up the logger for the application and returns the log file.
1616
// It creates a log folder if it doesn't exist, opens a log file, sets the log level

src/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
)
1515

1616
// Initialized in main
17-
const configFilePath = "/etc/rhc/workers/rhc-worker-bash.yml"
18-
const logDir = "/var/log/rhc-worker-bash"
19-
const logFileName = "rhc-worker-bash.log"
17+
const configFilePath = "/etc/rhc/workers/rhc-worker-script.yml"
18+
const logDir = "/var/log/rhc-worker-script"
19+
const logFileName = "rhc-worker-script.log"
2020

2121
var yggdDispatchSocketAddr string
2222
var config *Config
@@ -52,11 +52,11 @@ func main() {
5252
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
5353
defer cancel()
5454

55-
// Register as a handler of the "rhc-worker-bash" type.
55+
// Register as a handler of the "rhc-worker-script" type.
5656
r, err := c.Register(
5757
ctx,
5858
&pb.RegistrationRequest{
59-
Handler: "rhc-worker-bash",
59+
Handler: "rhc-worker-script",
6060
Pid: int64(os.Getpid()),
6161
DetachedContent: true,
6262
})

0 commit comments

Comments
 (0)