Skip to content

Commit

Permalink
Get started with vmop-agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Feb 25, 2025
1 parent d2c39dc commit 4a7a309
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
2 changes: 2 additions & 0 deletions dev-example/vmop-agent/99-vmop-agent.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUBSYSTEM=="virtio-ports", ATTR{name}=="org.jdrupes.vmop_agent.0", \
TAG+="systemd" ENV{SYSTEMD_WANTS}="vmop-agent.service"
19 changes: 19 additions & 0 deletions dev-example/vmop-agent/vmop-agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/bash

hostSerial="/dev/virtio-ports/org.jdrupes.vmop_agent.0"

if [ ! -w "$hostSerial" ]; then
echo >&2 "Device $hostSerial not writable"
exit 1
fi

if ! exec {con}<>"$hostSerial"; then
echo >&2 "Cannot open device $hostSerial"
exit 1
fi

echo >&${con} "220 Hello"

while read line <&${con}; do
true
done
15 changes: 15 additions & 0 deletions dev-example/vmop-agent/vmop-agent.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=VM-Operator (Guest) Agent
BindsTo=dev-virtio\x2dports-org.jdrupes.vmop_agent.0.device
After=dev-virtio\x2dports-org.jdrupes.vmop_agent.0.device multi-user.target
IgnoreOnIsolate=True

[Service]
UMask=0077
#EnvironmentFile=/etc/sysconfig/vmop-agent
ExecStart=/usr/local/libexec/vmop-agent
Restart=always
RestartSec=0

[Install]
WantedBy=dev-virtio\x2dports-org.jdrupes.vmop_agent.0.device
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.jdrupes.vmoperator.runner.qemu.events.RunnerStateChange;
import org.jdrupes.vmoperator.runner.qemu.events.RunnerStateChange.RunState;
import org.jdrupes.vmoperator.runner.qemu.events.ShutdownEvent;
import org.jdrupes.vmoperator.runner.qemu.events.VmopAgentConnected;
import org.jdrupes.vmoperator.util.GsonPtr;
import org.jgrapes.core.Channel;
import org.jgrapes.core.annotation.Handler;
Expand Down Expand Up @@ -192,20 +193,21 @@ public void onRunnerStateChanged(RunnerStateChange event)
status.addProperty("ram", GsonPtr.to(from.data())
.getAsString("spec", "vm", "maximumRam").orElse("0"));
status.addProperty("cpus", 1);

// In case we had an irregular shutdown
status.remove("osinfo");
} else if (event.runState() == RunState.STOPPED) {
status.addProperty("ram", "0");
status.addProperty("cpus", 0);
status.remove("osinfo");
}

// In case console connection was still present
if (!running) {
// In case console connection was still present
status.addProperty("consoleClient", "");
updateCondition(from, status, "ConsoleConnected", false,
"VmStopped", "The VM has been shut down");
"VmStopped", "The VM is not running");

// In case we had an irregular shutdown
status.remove("osinfo");
updateCondition(vmDef, vmDef.statusJson(), "VmopAgentConnected",
false, "VmStopped", "The VM is not running");
}
return status;
});
Expand Down Expand Up @@ -322,4 +324,24 @@ public void onOsinfo(OsinfoEvent event) throws ApiException {
});

}

/**
* @param event the event
* @throws ApiException
*/
@Handler
@SuppressWarnings("PMD.AssignmentInOperand")
public void onVmopAgentConnected(VmopAgentConnected event)
throws ApiException {
VmDefinition vmDef;
if (vmStub == null || (vmDef = vmStub.model().orElse(null)) == null) {
return;
}
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
updateCondition(vmDef, status, "VmopAgentConnected",
true, "VmopAgentStarted", "The VM operator agent is running");
return status;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.jdrupes.vmoperator.runner.qemu;

import java.io.IOException;
import org.jdrupes.vmoperator.runner.qemu.events.VmopAgentConnected;
import org.jgrapes.core.Channel;

/**
Expand All @@ -42,7 +43,9 @@ public VmopAgentClient(Channel componentChannel) throws IOException {

@Override
protected void processInput(String line) throws IOException {
// TODO Auto-generated method stub
if (line.startsWith("220 ")) {
rep().fire(new VmopAgentConnected());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* VM-Operator
* Copyright (C) 2025 Michael N. Lipp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.jdrupes.vmoperator.runner.qemu.events;

import org.jgrapes.core.Event;

/**
* Signals information about the guest OS.
*/
public class VmopAgentConnected extends Event<Void> {
}

0 comments on commit 4a7a309

Please sign in to comment.