Browse Source

Added creation of yaml metadata file based on the scripts documentation file.

master
Franco Masotti 3 years ago
parent
commit
8852e6109b
Signed by: frnmst
GPG Key ID: 24116ED85666780A
  1. 3
      docs/scripts.rst
  2. 4
      utils/prepare_environment.conf
  3. 140
      utils/prepare_environment.py

3
docs/scripts.rst

@ -66,8 +66,7 @@ YAML data
::
<--YAML-->
entry:
name: archive_documents_simple.sh
archive_documents_simple.sh:
type: archiving
configuration files:
paths:

4
utils/prepare_environment.conf

@ -23,6 +23,10 @@ jobs user = jobs
scripts directory = scripts
services directory = services
documentation scripts file = ../docs/scripts.rst
documentation yaml section start marker = <--YAML-->
documentation yaml section end marker = <!--YAML-->
metadata file = ./metadata.yaml
deploy script = ./deploy.py

140
utils/prepare_environment.py

@ -128,8 +128,48 @@ def gen_multiple_copy_file_commands_by_unit_type(files: dict, scripts_directory,
return commands
def extract_text_between_delimiters(file: str, start_marker: str, end_marker: str) -> str:
"""Get all the text included in two delimiters."""
keep = False
out = str()
with open (file, 'r') as f:
for line in f:
delimiter=False
if line.lstrip().startswith(start_marker):
keep = True
delimiter=True
elif line.lstrip().startswith(end_marker):
keep = False
delimiter=True
if keep and not delimiter:
out += line
return out
def generate_yaml_struct(input_string: str) -> str:
"""Generate a YAML string which conforms to the expected inputs for some functions of this script."""
elements = yaml.load(input_string, Loader=yaml.BaseLoader)
out = dict()
for element in elements:
type = elements[element]['type']
if type not in out:
out[type] = dict()
out[type][element] = dict()
out[type][element]['enabled'] = False
if 'configuration files' in elements[element]:
out[type][element]['configuration files'] = elements[element]['configuration files']
if 'systemd unit files' in elements[element]:
out[type][element]['systemd unit files'] = elements[element]['systemd unit files']
return yaml.dump(out)
if __name__ == '__main__':
configuration_file = shlex.quote(sys.argv[1])
generate_yaml = False
if len(sys.argv) > 2:
if sys.argv[2] == '--generate-yaml':
generate_yaml = True
config = configparser.ConfigParser()
config.read(configuration_file)
jobs_user = config['DEFAULT']['jobs user']
@ -138,49 +178,57 @@ if __name__ == '__main__':
metadata_file = config['DEFAULT']['metadata file']
deploy_script = config['DEFAULT']['deploy script']
source_files_directory = config['DEFAULT']['source files directory']
files, users = get_files_to_copy(metadata_file, str(pathlib.Path.cwd()), source_files_directory)
d_scripts_by_user = get_base_objects_directory_name_by_user(jobs_user, scripts_directory, scripts_directory, services_directory)
d_services_by_user = get_base_objects_directory_name_by_user(jobs_user, services_directory, scripts_directory, services_directory)
file_copy_commands = gen_multiple_copy_file_commands_by_unit_type(files, d_scripts_by_user, d_services_by_user)
home_jobs = get_home_directory(jobs_user)
c = list()
c.append('#!/usr/bin/env bash')
c.append('# autogenerated by automated-tasks/prepare_environment.py')
c.append('')
c.append('set -euo pipefail')
c.append(gen_create_user_command(jobs_user))
c.append(gen_create_directory_command(d_scripts_by_user))
c.append(gen_create_directory_command(d_services_by_user))
# Copy the deploy script before changing the permissions.
c.append(gen_copy_file_command(deploy_script, d_services_by_user))
c.append(gen_change_owners_command(home_jobs, jobs_user, jobs_user))
c.append(gen_change_permissions_command(home_jobs, permissions='070'))
# Create directories.
# User names are gathered from the YAML file.
for u in users:
c.append(gen_add_users_to_group_command(u, jobs_user))
d_scripts = get_objects_directory_name(jobs_user, scripts_directory, u, scripts_directory, services_directory)
d_services = get_objects_directory_name(jobs_user, services_directory, u, scripts_directory, services_directory)
c.append(gen_create_directory_command(d_scripts))
c.append(gen_create_directory_command(d_services))
# We need to run the copy command before chown and chmod.
c = c + file_copy_commands
# Change owners and permissions.
for u in users:
d_scripts = get_objects_directory_name(jobs_user, scripts_directory, u, scripts_directory, services_directory)
d_services = get_objects_directory_name(jobs_user, services_directory, u, scripts_directory, services_directory)
c.append(gen_change_owners_command(d_scripts,u,u))
c.append(gen_change_owners_command(d_services,u,u))
c.append(gen_change_permissions_command(d_scripts))
c.append(gen_change_permissions_command(d_services))
print_commands(c)
scripts_file = config['DEFAULT']['documentation scripts file']
yaml_section_start_marker = config['DEFAULT']['documentation yaml section start marker']
yaml_section_end_marker = config['DEFAULT']['documentation yaml section end marker']
if generate_yaml:
yaml_input = extract_text_between_delimiters(scripts_file, yaml_section_start_marker, yaml_section_end_marker)
yaml_struct = generate_yaml_struct(yaml_input)
print(yaml_struct, end='')
else:
files, users = get_files_to_copy(metadata_file, str(pathlib.Path.cwd()), source_files_directory)
d_scripts_by_user = get_base_objects_directory_name_by_user(jobs_user, scripts_directory, scripts_directory, services_directory)
d_services_by_user = get_base_objects_directory_name_by_user(jobs_user, services_directory, scripts_directory, services_directory)
file_copy_commands = gen_multiple_copy_file_commands_by_unit_type(files, d_scripts_by_user, d_services_by_user)
home_jobs = get_home_directory(jobs_user)
c = list()
c.append('#!/usr/bin/env bash')
c.append('# autogenerated by automated-tasks/prepare_environment.py')
c.append('')
c.append('set -euo pipefail')
c.append(gen_create_user_command(jobs_user))
c.append(gen_create_directory_command(d_scripts_by_user))
c.append(gen_create_directory_command(d_services_by_user))
# Copy the deploy script before changing the permissions.
c.append(gen_copy_file_command(deploy_script, d_services_by_user))
c.append(gen_change_owners_command(home_jobs, jobs_user, jobs_user))
c.append(gen_change_permissions_command(home_jobs, permissions='070'))
# Create directories.
# User names are gathered from the YAML file.
for u in users:
c.append(gen_add_users_to_group_command(u, jobs_user))
d_scripts = get_objects_directory_name(jobs_user, scripts_directory, u, scripts_directory, services_directory)
d_services = get_objects_directory_name(jobs_user, services_directory, u, scripts_directory, services_directory)
c.append(gen_create_directory_command(d_scripts))
c.append(gen_create_directory_command(d_services))
# We need to run the copy command before chown and chmod.
c = c + file_copy_commands
# Change owners and permissions.
for u in users:
d_scripts = get_objects_directory_name(jobs_user, scripts_directory, u, scripts_directory, services_directory)
d_services = get_objects_directory_name(jobs_user, services_directory, u, scripts_directory, services_directory)
c.append(gen_change_owners_command(d_scripts,u,u))
c.append(gen_change_owners_command(d_services,u,u))
c.append(gen_change_permissions_command(d_scripts))
c.append(gen_change_permissions_command(d_services))
print_commands(c)

Loading…
Cancel
Save