Add OpenRC agent script

Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
This commit is contained in:
Darko Draskovic
2023-04-18 16:07:51 +02:00
parent 780c620b30
commit 306fa302dc
4 changed files with 66 additions and 10 deletions
+10
View File
@@ -22,11 +22,16 @@ $(SERVICES):
QCOW2_PATH = ~/go/src/github.com/ultravioletrs/manager/cmd/manager/img/boot.img
HOST_AGENT_PATH = ~/ultravioletrs/agent/build/cocos-agent
GUEST_AGENT_PATH = /root/agent
HOST_AGENT_SH_PATH = ~/go/src/github.com/ultravioletrs/agent/alpine/agent.sh
GUEST_AGENT_SH_PATH = /root/agent/
HOST_AGENT_RC_SH_PATH = ~/go/src/github.com/ultravioletrs/agent/alpine/agent
GUEST_AGENT_RC_SH_PATH=/etc/init.d/
# Copy the agent binary to the guest VM
copy-agent:
sudo virt-copy-in -a $(QCOW2_PATH) $(HOST_AGENT_PATH) $(GUEST_AGENT_PATH)
@@ -35,6 +40,11 @@ copy-agent:
copy-agent-sh:
sudo virt-copy-in -a $(QCOW2_PATH) $(HOST_AGENT_SH_PATH) $(GUEST_AGENT_SH_PATH)
# Copy the agent-rc init sh script to the guest VM
copy-agent-rc-sh:
chmod +x $(HOST_AGENT_RC_SH_PATH)
sudo virt-copy-in -a $(QCOW2_PATH) $(HOST_AGENT_RC_SH_PATH) $(GUEST_AGENT_RC_SH_PATH)
# docker_mfxkit:
# docker build --no-cache --tag=mainflux/mfxkit -f docker/Dockerfile .
+21 -1
View File
@@ -47,7 +47,9 @@ To see the change in action, run
curl -i -X POST -H "Content-Type: application/json" localhost:9022/mfxkit -d '{"secret":"secret2"}'
```
## Alpine linux
## Alpine
### cron
To schedula a task _via_ `cron`
@@ -64,3 +66,21 @@ To check whether the program is executing, run
```sh
cat /var/log/messages
```
### OpenRC
OpenRC is an Alpine's service manager.
Once the `agent` script is copied in `/etc/init.d/` on the guest system, log into the guest system and run
```sh
rc-update add agent default
```
and reboot.
### curl
```sh
curl -i -X POST -H "Content-Type: application/json" 192.168.122.251:9021/agent -d '{"secret":"secret"}'
```
Executable
+20
View File
@@ -0,0 +1,20 @@
#!/sbin/openrc-run
command="/root/agent/cocos-agent"
command_args=""
depend() {
need net
}
start() {
ebegin "Starting cocos-agent"
start-stop-daemon -S -q -b -m -p /run/cocos-agent.pid -x $command -- $command_args
eend $?
}
stop() {
ebegin "Stopping cocos-agent"
start-stop-daemon -K -q -p /run/cocos-agent.pid
eend $?
}
+15 -9
View File
@@ -3,14 +3,20 @@
AGENT_NAME="cocos-agent"
AGENT_PATH="$HOME/agent/$AGENT_NAME"
# find the process IDs of running processes based on their name
if pgrep -x "$AGENT_NAME" >/dev/null; then
echo "Executable $AGENT_NAME is already running."
else
if [ -x "$AGENT_PATH" ]; then
echo "Executing $AGENT_NAME..."
"$AGENT_PATH"
while true
do
# find the process IDs of running processes based on their name
if pgrep -x "$AGENT_NAME" >/dev/null; then
echo "Executable $AGENT_NAME is already running."
else
echo "Executable $AGENT_NAME not found at path $AGENT_PATH."
if [ -x "$AGENT_PATH" ]; then
echo "Executing $AGENT_NAME..."
"$AGENT_PATH"
else
echo "Executable $AGENT_NAME not found at path $AGENT_PATH."
fi
fi
fi
# wait for 15 seconds before running the loop again
sleep 15
done