add scripts to deploy grandlyon

This commit is contained in:
Nicolas Roche 2021-03-30 09:41:22 +02:00
parent 40b86eb190
commit 8e19111794
6 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,34 @@
#! /usr/bin/env python3
# python3 copy-portal-agent.py Oullins Genay
import json
import os
import subprocess
import sys
from django.utils.text import slugify
orig_city= sys.argv[1]
slug_orig_city = slugify(orig_city)
dest_city = sys.argv[2]
slug_dest_city = slugify(dest_city)
if os.path.exists('/var/lib/combo/tenants/www.toodego.com'):
orig_site = 'agents-%s.toodego.com' % slug_orig_city
dest_site = 'agents-%s.toodego.com' % slug_dest_city
else:
orig_site = 'portail-agent-%s.guichet-recette.grandlyon.com' % slug_orig_city
dest_site = 'portail-agent-%s.guichet-recette.grandlyon.com' % slug_dest_city
site_export_orig = subprocess.check_output(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'export_site', '-d', orig_site])
site_export_orig = site_export_orig.decode('utf-8')
site_export = site_export_orig.replace(orig_city, dest_city)
site_export = site_export.replace(slug_orig_city, slug_dest_city)
open('/tmp/site-export.json', 'w').write(site_export)
subprocess.call(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'import_site', '-d', dest_site, '/tmp/site-export.json'])
subprocess.call(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'runscript', '-d', dest_site, 'fsck-combo.py'])
orig_media_dir = '/var/lib/combo/tenants/%s/media/*' % orig_site
if os.path.exists(orig_media_dir) and os.listdir(orig_media_dir):
subprocess.call(['sudo', '-u', 'combo', 'cp', '-ar', orig_media_dir, '/var/lib/combo/tenants/%s/' % dest_site])

View File

@ -0,0 +1,34 @@
#! /usr/bin/env python
# python3 copy-portal-users.py Oullins Genay
import json
import os
import subprocess
import sys
from django.utils.text import slugify
orig_city= sys.argv[1]
slug_orig_city = slugify(orig_city)
dest_city = sys.argv[2]
slug_dest_city = slugify(dest_city)
if os.path.exists('/var/lib/combo/tenants/www.toodego.com'):
orig_site = '%s.toodego.com' % slug_orig_city
dest_site = '%s.toodego.com' % slug_dest_city
else:
orig_site = 'portail-citoyen-%s.guichet-recette.grandlyon.com' % slug_orig_city
dest_site = 'portail-citoyen-%s.guichet-recette.grandlyon.com' % slug_dest_city
site_export_orig = subprocess.check_output(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'export_site', '-d', orig_site])
site_export_orig = site_export_orig.decode('utf-8')
site_export = site_export_orig.replace(orig_city, dest_city)
site_export = site_export.replace(slug_orig_city, slug_dest_city)
open('/tmp/site-export.json', 'w').write(site_export)
subprocess.call(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'import_site', '-d', dest_site, '/tmp/site-export.json'])
subprocess.call(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'runscript', '-d', dest_site, 'fsck-combo.py'])
orig_media_dir = '/var/lib/combo/tenants/%s/media/*' % orig_site
if os.path.exists(orig_media_dir) and os.listdir(orig_media_dir):
subprocess.call(['sudo', '-u', 'combo', 'cp', '-ar', orig_media_dir, '/var/lib/combo/tenants/%s/' % dest_site])

View File

@ -0,0 +1,37 @@
import os
import json
import sys
from django_rbac.utils import get_role_model, get_ou_model
Ou = get_ou_model()
Role = get_role_model()
mapping = {}
name_src = sys.argv[1]
name_dst = sys.argv[2]
ou_src = Ou.objects.get(name=name_src)
ou = Ou.objects.get(name=name_dst)
agent_role = None
for role in Role.objects.filter(ou=ou_src):
#if not (role.name.startswith('Gestionnaire') or role.name.startswith('Administrateur')):
# continue
if role.slug.startswith('_'):
continue
new_name = role.name.replace(name_src, name_dst).replace(name_src.lower(), name_dst.lower())
new_role, created = Role.objects.get_or_create(ou=ou, name=new_name)
mapping[role.uuid] = {name_dst: new_role.uuid}
if new_name == 'Agent':
agent_role = new_role
if agent_role:
for role in Role.objects.filter(ou=ou):
if ou.uuid == agent_role.uuid:
continue
role.add_parent(agent_role)
json.dump(mapping, open('/tmp/roles.mapping.json', 'w'), indent=2)

56
grandlyon/scripts/copy-wcs.py Executable file
View File

@ -0,0 +1,56 @@
#! /usr/bin/env python
# usage: sudo -u wcs /usr/bin/wcsctl -f /etc/wcs/wcs-au-quotidien.cfg runscript --vhost=demarches-<...>.guichet-recette.grandlyon.com copy-wcs.py Oullins Genay
from quixote import get_publisher
from wcs.formdef import FormDef
import json
import os
import subprocess
import sys
from django.utils.text import slugify
orig_city = sys.argv[1]
slug_orig_city = slugify(orig_city)
dest_city = sys.argv[2]
slug_dest_city = slugify(dest_city)
if os.path.exists('/var/lib/combo/tenants/www.toodego.com'):
orig_site = 'demarches-%s.toodego.com' % slug_orig_city
dest_site = 'demarches-%s.toodego.com' % slug_dest_city
else:
orig_site = 'demarches-%s.guichet-recette.grandlyon.com' % slug_orig_city
dest_site = 'demarches-%s.guichet-recette.grandlyon.com' % slug_dest_city
roles_mapping = json.load(open('/tmp/roles.mapping.json'))
def copy(orig, dest, dest_city):
if os.path.exists(dest):
return # do not overwrite
if not os.path.isfile(orig):
return # ignore non files
src = open(orig, 'rb').read()
for role_id in roles_mapping.keys():
src = src.replace(role_id.encode('ascii'), roles_mapping[role_id][dest_city].encode('ascii'))
fd = open(dest, 'wb')
fd.write(src)
fd.close()
for object_type in ('formdefs', 'carddefs', 'workflows', 'categories', 'datasources', 'wscalls', 'mail-templates'): # , 'models', 'blockdefs', 'apiaccess'):
orig_dir = os.path.join('/var/lib/wcs/%s/%s/' % (orig_site, object_type))
dest_dir = os.path.join('/var/lib/wcs/%s/%s/' % (dest_site, object_type))
if not os.path.exists(orig_dir):
continue
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
for object_id in os.listdir(orig_dir):
copy(os.path.join(orig_dir, object_id),
os.path.join(dest_dir, object_id),
dest_city)
# iterate over all objects and store them again to make sure side-code is run.
for formdef in FormDef.select():
if orig_city in formdef.name:
formdef.name = formdef.name.replace(orig_city, dest_city)
formdef.store()

View File

@ -0,0 +1,7 @@
# run .save() to fill cached properties
from combo.apps.wcs.models import WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell
for obj_type in (WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell):
for obj in obj_type.objects.all():
obj.save()

View File

@ -0,0 +1,23 @@
# add all collectivity technical admin roles to "Administrateur <commune>"
# authentic2-multitenant-manage tenant_command runscript -d connexion.guichet-recette.grandlyon.com ~tma/misc-fred/grandlyon/scripts/mark-admin-roles.py Genais
import os
import json
import sys
from django.db.models.query import Q
from django_rbac.utils import get_role_model, get_ou_model
Ou = get_ou_model()
Role = get_role_model()
name_dst = sys.argv[1]
ou = Ou.objects.get(name=name_dst)
role = Role.objects.get(name=u'Administrateur %s' % name_dst, ou=ou)
for subrole in Role.objects.filter(Q(slug='_a2-hobo-superuser', ou=ou) |
Q(slug__startswith='administrateur-fonc', ou=ou) |
Q(slug__startswith='_a2-administrateur', ou=ou)):
role.add_parent(subrole)