Page MenuHomeDevCentral

No OneTemporary

diff --git a/_tests/pillar/paas/test_docker.py b/_tests/pillar/paas/test_docker.py
index 1300edd..be19857 100644
--- a/_tests/pillar/paas/test_docker.py
+++ b/_tests/pillar/paas/test_docker.py
@@ -1,25 +1,25 @@
import unittest
import yaml
PILLAR_FILE = '../pillar/paas/docker.sls'
class Testinstance(unittest.TestCase):
def setUp(self):
with open(PILLAR_FILE, 'r') as fd:
- self.pillar = yaml.load(fd)
+ self.pillar = yaml.safe_load(fd)
# nginx needs a host/app_port pair to spawn a configuration
def test_host_is_paired_with_app_port_option(self):
for node, services in self.pillar['docker_containers'].items():
for service, containers in services.items():
for instance, container in containers.items():
if 'host' not in container:
continue
entry = ':'.join(['docker_containers', node,
service, instance])
self.assertIn('app_port', container,
entry + ": app_port missing")
diff --git a/_tests/salt_test_case.py b/_tests/salt_test_case.py
index 9f2c7c2..3f8dacc 100644
--- a/_tests/salt_test_case.py
+++ b/_tests/salt_test_case.py
@@ -1,30 +1,30 @@
from importlib.machinery import SourceFileLoader
import yaml
class SaltTestCase:
def initialize_mocks(self):
source = SourceFileLoader("dunder", "mocks/dunder.py").load_module()
self.pillar = source.dunder()
self.grains = source.dunder()
@staticmethod
def import_data_from_yaml(filename):
with open(filename, 'r') as fd:
- return yaml.load(fd.read())
+ return yaml.safe_load(fd.read())
def mock_pillar(self, filename=None, target=None):
if not target:
target = self.instance
if filename:
self.pillar.data = self.import_data_from_yaml(filename)
target.__pillar__ = self.pillar
def mock_grains(self, target=None):
if not target:
target = self.instance
target.__grains__ = self.grains
diff --git a/utils/dump-py-state.py b/utils/dump-py-state.py
index 079a30b..f8b5ea0 100755
--- a/utils/dump-py-state.py
+++ b/utils/dump-py-state.py
@@ -1,99 +1,99 @@
#!/usr/bin/env python3
# -------------------------------------------------------------
# rOPS — compile a #!py .sls file and dump result in YAML
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2018-10-17
# Description: Read the web_content_sls pillar entry
# and regenerate the webserver-content include.
# License: BSD-2-Clause
# -------------------------------------------------------------
import os
import subprocess
import sys
import yaml
# -------------------------------------------------------------
# Pillar helper
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_pillar_files(pillar_directory):
pillar_files = []
for dir_path, dir_names, file_names in os.walk(pillar_directory):
files = [os.path.join(dir_path, file_name)
for file_name in file_names
if file_name.endswith(".sls")]
pillar_files.extend(files)
return pillar_files
def load_pillar(pillar_directory):
pillar = {}
for pillar_file in get_pillar_files(pillar_directory):
- data = yaml.load(open(pillar_file, "r"))
+ data = yaml.safe_load(open(pillar_file, "r"))
pillar.update(data)
return pillar
# -------------------------------------------------------------
# Grains helper
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def system(args):
result = subprocess.run(args, stdout=subprocess.PIPE)
return result.stdout.decode('utf-8').strip()
# -------------------------------------------------------------
# Source code helper
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def run_shim():
return "\n\nif __name__ == '__main__':\n\tprint(yaml.dump(run(), default_flow_style=False))"
def assemble_source_code(filename):
with open(filename, 'r') as fd:
source_code = fd.read()
return source_code + run_shim()
# -------------------------------------------------------------
# Run task
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if __name__ == "__main__":
argc = len(sys.argv)
if argc < 2:
print("Usage: dump-py-state.py <sls file>", file=sys.stderr)
exit(1)
sls_file = sys.argv[1]
try:
source_code = assemble_source_code(sls_file)
except OSError as ex:
print(ex, file=sys.stderr)
exit(ex.errno)
__pillar__ = load_pillar("pillar")
__grains__ = {
'os': system(["uname", "-o"])
}
exec(source_code)
diff --git a/utils/generate-webcontent-index.py b/utils/generate-webcontent-index.py
index 195b7d0..2ce223d 100755
--- a/utils/generate-webcontent-index.py
+++ b/utils/generate-webcontent-index.py
@@ -1,75 +1,75 @@
#!/usr/bin/env python3
# -------------------------------------------------------------
# rOPS — regenerate roles/webserver-content/init.sls
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2017-11-24
# Description: Read the web_content_sls pillar entry
# and regenerate the webserver-content include.
# License: BSD-2-Clause
# -------------------------------------------------------------
import yaml
# -------------------------------------------------------------
# Table of contents
# -------------------------------------------------------------
#
# :: Configuration
# :: Update code
# :: Run task
#
# -------------------------------------------------------------
# -------------------------------------------------------------
# Configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pillar_file = "pillar/webserver/sites.sls"
file_to_update = "roles/webserver-content/init.sls"
# -------------------------------------------------------------
# Update code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def do_update(pillar_file, file_to_update):
print_header(file_to_update)
print("\ninclude:")
for site in get_sites(pillar_file):
print(" - {}".format(site))
def get_pillar_entry(pillar_file, key):
with open(pillar_file) as fd:
- pillar = yaml.load(fd.read())
+ pillar = yaml.safe_load(fd.read())
return pillar[key]
def get_sites(pillar_file):
sites = get_pillar_entry(pillar_file, 'web_content_sls')
return sorted([site for sublist in
[sites[role] for role in sites]
for site in sublist])
def print_header(file_to_update):
with open(file_to_update) as fd:
for line in fd:
if not line.startswith("#"):
break
print(line, end="")
# -------------------------------------------------------------
# Run task
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if __name__ == "__main__":
do_update(pillar_file, file_to_update)
diff --git a/utils/migrate-ssh-keys.py b/utils/migrate-ssh-keys.py
index 0cf65d5..584bd10 100755
--- a/utils/migrate-ssh-keys.py
+++ b/utils/migrate-ssh-keys.py
@@ -1,114 +1,114 @@
#!/usr/bin/env python3
# -------------------------------------------------------------
# rOPS — migrate SSH keys from file to Salt state
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2017-11-09
# Description: Read a dictionary, and for each key, find in
# a specified folder a data file. Add data from
# this file to the dictionary. Output in YAML.
# License: BSD-2-Clause
# -------------------------------------------------------------
# -------------------------------------------------------------
# Table of contents
# -------------------------------------------------------------
#
# :: Configuration
# :: YAML style
# :: Update code
# :: Run task
#
# -------------------------------------------------------------
import os
import yaml
# -------------------------------------------------------------
# Configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Where is located the dictionary to update?
state_file = 'pillar/core/users.sls'
state_key = 'shellusers'
# Where are located the data fileS?
data_path = 'roles/shellserver/users/files/ssh_keys/'
# What property should get the data and be added if missing in the dict?
state_data_property = 'ssh_keys'
# -------------------------------------------------------------
# YAML style
#
# Allows to dump with indented lists
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class SaltStyleDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(SaltStyleDumper, self).increase_indent(flow, False)
# -------------------------------------------------------------
# Update code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def do_update():
state = read_state()
update_state(state)
print(dump_state(state))
def read_state():
fd = open(state_file, "r")
- states = yaml.load(fd.read())
+ states = yaml.safe_load(fd.read())
fd.close()
return states[state_key]
def update_state(state):
for key in state:
if state_data_property not in state[key]:
state[key][state_data_property] = read_data(key)
def read_data(key):
path = data_path + key
if not os.path.exists(path):
return []
return [line.strip() for line in open(path, "r") if is_value_line(line)]
def is_value_line(line):
if line.startswith("#"):
return False
if line.strip() == '':
return False
return True
def dump_state(state):
return yaml.dump({state_key: state},
default_flow_style=False,
Dumper=SaltStyleDumper, width=1000)
# -------------------------------------------------------------
# Run task
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
do_update()
diff --git a/utils/next-uid.py b/utils/next-uid.py
index 4273d18..befd3dd 100755
--- a/utils/next-uid.py
+++ b/utils/next-uid.py
@@ -1,32 +1,32 @@
#!/usr/bin/env python3
#
# Guesses a free sequential user ID for a new account.
#
# To do so, reads from pillar/core/users.sls (USERS_DATASOURCE) the last users
# uid # and offers the next one available.
#
# ID > 5000 (USERS_CUT) are ignored.
import yaml
USERS_DATASOURCE = 'pillar/core/users.sls'
USERS_DATASOURCE_KEY = 'shellusers'
USERS_CUT = 5000
def get_shellusers(filename, key):
with open(filename) as stream:
- data = yaml.load(stream)
+ data = yaml.safe_load(stream)
return data[key]
def get_uids(users, threshold):
return [users[username]['uid']
for username in users
if users[username]['uid'] < threshold]
users = get_shellusers(USERS_DATASOURCE, USERS_DATASOURCE_KEY)
uids = get_uids(users, USERS_CUT)
print(max(uids) + 1)

File Metadata

Mime Type
text/x-diff
Expires
Wed, Jul 8, 15:01 (56 m, 3 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3903882
Default Alt Text
(11 KB)

Event Timeline