-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The flush test sends an SNMP request to PDU to reset the power on the hardware PC. As the flush test sends only one type of request we don't need to parse it and can just wait for any UDP packet and perform a hard reset of the VM. Signed-off-by: Kostiantyn Kostiuk <[email protected]>
- Loading branch information
1 parent
963fa60
commit 54d73fa
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# Intended to be usable as a standalone script | ||
# it is not a part of the AutoHCK module | ||
|
||
require 'json' | ||
require 'logger' | ||
require 'socket' | ||
|
||
class SnmpReset | ||
def initialize(logger, socket_file) | ||
@logger = logger | ||
|
||
@socket_file = socket_file | ||
|
||
@qmp_connected = false | ||
end | ||
|
||
def connect_socket | ||
@logger.info "Initializing QMP session for #{@socket_file}" | ||
@qmp_socket = UNIXSocket.new(@socket_file) | ||
end | ||
|
||
def run | ||
@logger.info 'Initializing UDP socket' | ||
UDPSocket.open do |socket| | ||
socket.bind('0.0.0.0', 'snmp') | ||
@logger.info "Listening on #{socket.local_address.inspect_sockaddr}" | ||
|
||
count = 0 | ||
|
||
loop do | ||
_, addr = socket.recvfrom(512) | ||
|
||
@logger.info "Received data from #{addr[3]}:#{addr[1]}" | ||
|
||
unless @qmp_connected | ||
connect_socket | ||
send_cmd 'qmp_capabilities' | ||
@qmp_connected = true | ||
end | ||
|
||
count += 1 | ||
send_cmd 'system_reset' | ||
@logger.info "Rebooting... #{count}" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def send_cmd(cmd) | ||
@logger.info "Sending #{cmd} via QMP" | ||
@qmp_socket.write JSON.dump({ 'execute' => cmd }) | ||
@qmp_socket.flush | ||
|
||
loop do | ||
response = JSON.parse(@qmp_socket.readline) | ||
break response['return'] if response.key?('return') | ||
raise response['error'].to_s if response.key?('error') | ||
end | ||
end | ||
end | ||
|
||
log = Logger.new('fake-snmp-reset.txt') | ||
log.level = Logger::DEBUG | ||
log.info "START #{ARGV}" | ||
|
||
snmp = SnmpReset.new(log, ARGV[0]) | ||
snmp.run |