# -*- coding: utf-8 -*-
from setuphelpers import *
import time
import jinja2
from waptcrypto import SSLPrivateKey, SSLCertificate
import datetime
bin_name = "nginx.exe"
service_name = "WAPTRepoNginx"
def install():
global force
global service_name
def make_nginx_config(local_repo):
global force
ap_conf_dir = os.path.join(WAPT.wapt_base_dir, "waptservice", "nginx", "conf")
ap_file_name = "nginx.conf"
ap_conf_file = os.path.join(ap_conf_dir, ap_file_name)
ap_ssl_dir = os.path.join(WAPT.wapt_base_dir, "waptservice", "nginx", "ssl")
if os.path.isfile(ap_conf_file) and (not force):
if "waptservice" in open(ap_conf_file, "r").read():
return ap_conf_file
mkdirs(ap_ssl_dir)
key_fn = os.path.join(ap_ssl_dir, "key.pem")
key = SSLPrivateKey(key_fn)
if not os.path.isfile(key_fn):
print("Create SSL RSA Key %s" % key_fn)
key.create()
key.save_as_pem()
cert_fn = os.path.join(ap_ssl_dir, "cert.pem")
if os.path.isfile(cert_fn):
crt = SSLCertificate(cert_fn)
if crt.cn != get_fqdn():
os.rename(cert_fn, "%s-%s.old" % (cert_fn, "{:%Y%m%d-%Hh%Mm%Ss}".format(datetime.datetime.now())))
crt = key.build_sign_certificate(cn=get_fqdn(), dnsname=get_fqdn(), is_code_signing=False)
print("Create X509 cert %s" % cert_fn)
crt.save_as_pem(cert_fn)
else:
crt = key.build_sign_certificate(cn=get_fqdn(), dnsname=get_fqdn(), is_code_signing=False)
print("Create X509 cert %s" % cert_fn)
crt.save_as_pem(cert_fn)
# write config file
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader("scripts"))
template = jinja_env.get_template("waptwindows.nginxconfig.j2")
template_variables = {
"wapt_repository_path": local_repo.replace("\\", "/"),
"ssl": True,
"force_https": False,
"wapt_ssl_key_file": key_fn.replace("\\", "/"),
"wapt_ssl_cert_file": cert_fn.replace("\\", "/"),
"log_dir": os.path.join(WAPT.wapt_base_dir, "waptservice", "nginx", "logs").replace("\\", "/"),
"wapt_root_dir": WAPT.wapt_base_dir.replace("\\", "/"),
"nginx_http": 80,
"nginx_https": 443,
"server_tokens": "off",
}
config_string = template.render(template_variables)
print("Create nginx conf file %s" % ap_conf_file)
with open(ap_conf_file, "wt") as dst_file:
dst_file.write(config_string)
return ap_conf_file
def install_nginx_service(local_repo):
print("Register nginx frontend")
# rights
mkdirs(os.path.join(WAPT.wapt_base_dir, "waptservice", "nginx", "temp"))
for repo_path in ("wapt", "wapt-host", "waptwua"):
p = makepath(local_repo, repo_path)
mkdirs(p)
run(r'icacls "%s" /grant "*S-1-5-20":(OI)(CI)(M)' % (p))
run(r'icacls "%s" /grant "*S-1-5-20":(OI)(CI)(M)' % (os.path.join(WAPT.wapt_base_dir, "waptservice", "nginx", "temp")))
run(r'icacls "%s" /grant "*S-1-5-20":(OI)(CI)(M)' % os.path.join(WAPT.wapt_base_dir, "waptservice", "nginx", "logs"))
run(r'icacls "%s" /grant "*S-1-5-20":(OI)(CI)(M)' % os.path.join(WAPT.wapt_base_dir, "log"))
make_nginx_config(local_repo)
filecopyto('waptnginx.service',os.path.join(WAPT.wapt_base_dir, "waptservice", "services",'waptnginx.service'))
#install_windows_nssm_service(service_binary, service_parameters, service_logfile)
print("installing %s" % control.asrequirement())
print("Install nginx to permit WAPTAgent to become a repository")
local_repo = inifile_readstring(WAPT.config_filename, "repo-sync", "local_repo_path") or makepath(WAPT.wapt_base_dir, "repository")
print("Create WAPTService directories for nginx")
for dirname in ("nginx", "scripts"):
mkdirs(makepath(WAPT.wapt_base_dir, "waptservice", dirname))
for dirname in ("logs", "conf"):
mkdirs(makepath(WAPT.wapt_base_dir, "waptservice", "nginx", dirname))
print("Copy conf file for nginx")
if not isfile(makepath(WAPT.wapt_base_dir, "waptservice", "nginx", bin_name)):
filecopyto(bin_name, makepath(WAPT.wapt_base_dir, "waptservice", "nginx"))
if not isfile(makepath(WAPT.wapt_base_dir, "waptservice", "nginx", "conf", "mime.types")):
filecopyto("scripts/mime.types", makepath(WAPT.wapt_base_dir, "waptservice", "nginx", "conf"))
print("Install nginx")
install_nginx_service(local_repo)
print("Add firewall rule for nginx")
run(
'netsh advfirewall firewall add rule name="%s" dir=in action=allow program="%s" enable=yes'
% (service_name, makepath(WAPT.wapt_base_dir, "waptservice", "nginx", bin_name))
)
service_name = "WAPTRepoNginx"
if service_installed(service_name):
if service_is_running(service_name):
service_stop(service_name)
service_delete(service_name)
create_onetime_task('restart_waptservice','cmd.exe','/c net stop waptservice & net start waptservice')
def uninstall():
remove_file(os.path.join(WAPT.wapt_base_dir, "waptservice", "services",'waptnginx.service'))
print("Remove firewall rule")
run(
'netsh advfirewall firewall delete rule name="%s" program="%s"'
% (service_name, makepath(WAPT.wapt_base_dir, "waptservice", "nginx", bin_name))
)
print("Remove nginx files")
remove_tree(makepath(WAPT.wapt_base_dir, "waptservice", "nginx"))
remove_file(makepath(WAPT.wapt_base_dir, "waptservice", "scripts", "mime.types"))