2023-01-15 09:18:15 +01:00
#!/usr/bin/env python3
2023-01-21 18:38:41 +01:00
# Prepares a minion for local testing of Salt code in this repository. Requires roles to be written as grains, as development and pipeline containers generally do not have access to our roles API. Consider nbroles_to_grains.sh first if this is the case.
2023-01-15 09:18:15 +01:00
import roles
import os
import yaml
import socket
2023-01-26 18:58:09 +01:00
import yaml
2023-01-15 09:18:15 +01:00
roles = roles . get ( )
grainsfile = ' /etc/salt/grains '
2023-01-26 18:58:09 +01:00
idfile = os . path . join ( ' pillar/id/ ' , socket . gethostname ( ) + ' .sls ' )
2023-01-21 19:51:39 +01:00
configfile = ' /etc/salt/minion.d/local.conf '
2023-01-15 09:18:15 +01:00
mypwd = os . getcwd ( )
2023-01-26 18:58:09 +01:00
formulasfile = ' formulas.yaml '
formulasdir = ' /srv/formulas '
2023-01-15 09:18:15 +01:00
2023-01-26 18:58:09 +01:00
configdata = { ' file_roots ' : { ' production ' : [ ' /srv/salt ' ] } }
2023-01-15 09:18:15 +01:00
grainsdata = { ' roles ' : roles }
with open ( grainsfile , mode = ' w ' ) as grainsfh :
yaml . dump ( grainsdata , grainsfh )
with open ( idfile , mode = ' w ' ) as idfh :
idfh . write ( ' # empty ' )
2023-01-26 18:58:09 +01:00
with open ( formulasfile , mode = ' r ' ) as formulasfh :
formulas = yaml . load ( formulasfh , Loader = yaml . FullLoader )
for formula in formulas :
formula = os . path . join ( ' /srv/formulas/ ' , formula + ' -formula ' )
configdata [ ' file_roots ' ] [ ' production ' ] . append ( formula )
2023-01-21 19:51:39 +01:00
with open ( configfile , mode = ' w ' ) as configfh :
2023-01-26 18:58:09 +01:00
configfh . write ( ' # written by prepare_minion.py \n ' )
yaml . dump ( configdata , configfh )
2023-01-21 19:51:39 +01:00
2023-01-15 09:18:15 +01:00
os . symlink ( mypwd + ' /salt ' , ' /srv/salt ' )
os . symlink ( mypwd + ' /pillar ' , ' /srv/pillar ' )