#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Isomer - The distributed application framework
# ==============================================
# Copyright (C) 2011-2020 Heiko 'riot' Weinen <riot@c-base.org> and others.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Module: Templates
=================
Internal template handling utilities.
"""
from isomer.tool import log, error, debug
[docs]def write_template_file(source, target, content):
"""Write a new file from a given pystache template file and content"""
# print(formatTemplateFile(source, content))
data = format_template_file(source, content)
with open(target, "w") as f:
for line in data:
if type(line) != str:
line = line.encode("utf-8")
f.write(line)
[docs]def write_template(template, target, content):
"""Write a new file from a given pystache template file and content"""
# print(formatTemplateFile(source, content))
data = format_template(template, content)
with open(target, "w") as f:
for line in data:
if type(line) != str:
line = line.encode("utf-8")
f.write(line)
[docs]def insert_nginx_service(definition): # pragma: no cover
"""Insert a new nginx service definition"""
config_file = "/etc/nginx/sites-available/isomer.conf"
splitter = "### SERVICE DEFINITIONS ###"
with open(config_file, "r") as f:
old_config = "".join(f.readlines())
log(old_config, pretty=True, lvl=debug)
if definition in old_config:
print("Service definition already inserted")
return
parts = old_config.split(splitter)
log("Parts count:", len(parts), lvl=debug)
if len(parts) != 3:
print(
"Nginx configuration seems to be changed and cannot be "
"extended automatically anymore!"
)
log(parts, pretty=True, lvl=debug)
return
try:
with open(config_file, "w") as f:
f.write(parts[0])
f.write(splitter + "\n")
f.write(parts[1])
for line in definition:
f.write(line)
f.write("\n " + splitter)
f.write(parts[2])
except Exception as e:
log(
"Error during Nginx configuration extension:",
type(e),
e,
lvl=error,
exc=True,
)