salt3006.9, redo reactors, use virt.shutdown

This commit is contained in:
m0duspwnens
2024-08-27 09:25:40 -04:00
parent d110503639
commit 21c3835322
13 changed files with 159 additions and 83 deletions

View File

@@ -0,0 +1,22 @@
#!py
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.
import logging
import salt.client
local = salt.client.LocalClient()
def run():
vm_name = data['name']
logging.error("setHostname reactor: start for: %s " % vm_name)
r = local.cmd(vm_name, 'state.apply', ['setup.virt.setHostname'])
logging.error("setHostname reactor: return for %s: %s " % (vm_name,r))
logging.error("setHostname reactor: end for: %s " % vm_name)
return {}

22
salt/reactor/setSalt.sls Normal file
View File

@@ -0,0 +1,22 @@
#!py
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.
import logging
import salt.client
local = salt.client.LocalClient()
def run():
vm_name = data['name']
logging.error("setSalt reactor: start for: %s " % vm_name)
r = local.cmd(vm_name, 'state.apply', ['setup.virt.setSalt'])
logging.error("setSalt reactor: return for: %s: %s " % (vm_name,r))
logging.error("setSalt reactor: end for: %s " % vm_name)
return {}

21
salt/reactor/sominion.sls Normal file
View File

@@ -0,0 +1,21 @@
#!py
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.
import logging
import salt.client
local = salt.client.LocalClient()
def run():
vm_name = data['name']
logging.error("sominion reactor: start for: %s " % vm_name)
r = local.cmd(vm_name, 'state.apply', ['setup.virt.sominion'])
logging.error("sominion reactor: end for: %s " % vm_name)
return {}

View File

@@ -1,7 +1,7 @@
#!py
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.
@@ -13,7 +13,7 @@ def run():
minionid = data['id']
DATA = data['data']
hv_name = DATA['HYPERVISOR_HOST']
logging.error("setup reactor: %s " % DATA)
logging.error("sominion_setup reactor: %s " % DATA)
vm_out_data = {
'cpu': DATA['CPU'],
@@ -23,13 +23,13 @@ def run():
'sfp': DATA['SFP']
}
logging.error("setup reactor: vm_out_data: %s " % vm_out_data)
logging.error("sominion_setup reactor: vm_out_data: %s " % vm_out_data)
with open("/opt/so/saltstack/local/pillar/hypervisor/" + hv_name + "/" + minionid + ".sls", 'w') as f:
yaml.dump(vm_out_data, f, default_flow_style=False)
rc = call("NODETYPE=" + DATA['NODETYPE'] + " /usr/sbin/so-minion -o=addVirt -m=" + minionid + " -n=" + DATA['MNIC'] + " -i=" + DATA['MAINIP'] + " -a=" + DATA['INTERFACE'] + " -c=" + str(DATA['CORECOUNT']) + " -d='" + DATA['NODE_DESCRIPTION'] + "'", shell=True)
logging.error('setup_reactor: rc: %s' % rc)
logging.error('sominion_setup reactor: rc: %s' % rc)
return {}

View File

@@ -1,7 +1,7 @@
#!py
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.
@@ -9,6 +9,7 @@ import logging
import salt.client
local = salt.client.LocalClient()
import yaml
from time import sleep
def run():
@@ -17,7 +18,6 @@ def run():
compute.update({'free': compute.get('free') - vm_data.get(hw_type)})
logging.error("virtUpdate reactor: claiming %s compute: %s " % (hw_type,compute))
def claim_pci(hw_type):
claimed_hw = hv_data['hypervisor']['hardware'][hw_type]['claimed']
# if a list of devices was defined
@@ -46,8 +46,10 @@ def run():
host_devices.append(hw[1])
logging.error("virtUpdate reactor: claimed_hw: %s " % claimed_hw)
vm_name = data['name']
hv_name = local.cmd(vm_name, 'grains.get', ['hypervisor_host'])
vm_name = data['id']
logging.error("virtUpdate reactor: vm_name: %s " % vm_name)
hv_name = local.cmd(vm_name, 'grains.get', ['hypervisor_host']).get(vm_name)
logging.error("virtUpdate reactor: hv_name: %s " % hv_name)
host_devices = []
@@ -67,7 +69,20 @@ def run():
except yaml.YAMLError as exc:
logging.error(exc)
local.cmd(hv_name, 'virt.stop', ['name=' + vm_name])
r = local.cmd(hv_name, 'virt.shutdown', ['vm_=' + vm_name])
logging.error("virtUpdate reactor: virt.shutdown: %s return: %s " % (vm_name,r))
c = 0
while True:
if c == 60:
logging.error("virtUpdate reactor: vm_name: %s failed to shutdown in time " % vm_name)
return {}
r = local.cmd(hv_name, 'virt.list_inactive_vms')
logging.error("virtUpdate reactor: virt.list_inactive_vms: %s " % r.get(hv_name))
if vm_name in r.get(hv_name):
break
c += 1
sleep(1)
for hw_type in ['disks', 'copper', 'sfp']:
claim_pci(hw_type)
@@ -90,8 +105,9 @@ def run():
mem = vm_data['memory'] * 1024
r = local.cmd(hv_name, 'virt.update', ['name=' + vm_name, 'mem=' + str(mem), 'cpu=' + str(vm_data['cpu']), 'host_devices=' + str(host_devices)])
logging.error("virtUpdate reactor: virt.update: %s" % r)
logging.error("virtUpdate reactor: virt.update: vm_name: %s return: %s" % (vm_name,r))
local.cmd(hv_name, 'virt.start', ['name=' + vm_name])
r = local.cmd(hv_name, 'virt.start', ['name=' + vm_name])
logging.error("virtUpdate reactor: virt.start: vm_name: %s return: %s" % (vm_name,r))
return {}