summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2020-02-11 10:39:39 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2020-02-11 10:39:39 (GMT)
commit122e8873ebf7e5f21735f2b4d5771dd9ee9be240 (patch)
tree90c9b8ae113e234f0462a2f750c1c1bc9c455c17
parent6a090ded5298be0c481fda671b0318c370e43eff (diff)
downloadmisc-fred-122e8873ebf7e5f21735f2b4d5771dd9ee9be240.zip
misc-fred-122e8873ebf7e5f21735f2b4d5771dd9ee9be240.tar.gz
misc-fred-122e8873ebf7e5f21735f2b4d5771dd9ee9be240.tar.bz2
grand lyon: update management scripts for one to one copy
-rw-r--r--grandlyon/scripts/communes.json12
-rw-r--r--grandlyon/scripts/copy-portal-agent.py29
-rw-r--r--grandlyon/scripts/copy-portal-users.py27
-rw-r--r--grandlyon/scripts/copy-roles.py27
-rwxr-xr-xgrandlyon/scripts/copy-wcs.py61
-rw-r--r--grandlyon/scripts/duplicate-roles.py36
-rw-r--r--grandlyon/scripts/mark-admin-roles.py17
7 files changed, 94 insertions, 115 deletions
diff --git a/grandlyon/scripts/communes.json b/grandlyon/scripts/communes.json
deleted file mode 100644
index 6fab6bf..0000000
--- a/grandlyon/scripts/communes.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
- "orig": "Bron",
- "dests": [
- {"name": "Corbas"},
- {"name": "Dardilly"},
- {"name": "Oullins"},
- {"name": "Vaulx-en-Velin"},
- {"name": "Villeurbanne"}
- ],
- "dests-off": [
- ]
-}
diff --git a/grandlyon/scripts/copy-portal-agent.py b/grandlyon/scripts/copy-portal-agent.py
index e945d35..07a67ff 100644
--- a/grandlyon/scripts/copy-portal-agent.py
+++ b/grandlyon/scripts/copy-portal-agent.py
@@ -1,4 +1,5 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
+# python3 copy-portal-agent.py Oullins Genay
import json
import os
@@ -7,22 +8,22 @@ import sys
from django.utils.text import slugify
-communes_json = os.path.join(os.path.dirname(__file__), 'communes.json')
-
-orig_city = json.load(open(communes_json))['orig']
+orig_city= sys.argv[1]
slug_orig_city = slugify(orig_city)
orig_site = 'portail-agent-%s.guichet-recette.grandlyon.com' % slug_orig_city
-site_export_orig = subprocess.check_output(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'export_site', '-d', orig_site])
+dest_city = sys.argv[2]
+slug_dest_city = slugify(dest_city)
+dest_site = 'portail-agent-%s.guichet-recette.grandlyon.com' % slug_dest_city
-for dest in json.load(open(communes_json))['dests']:
- dest_city = dest['name']
- slug_dest_city = slugify(dest_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'])
+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'])
- subprocess.call(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'runscript', '-d', dest_site, '/data/home/tma/scripts/fsck-combo.py'])
+if os.listdir('/var/lib/combo/tenants/%s/media/*' % orig_site):
+ subprocess.call(['sudo', '-u', 'combo', 'cp', '-ar', '/var/lib/combo/tenants/%s/media' % orig_site, '/var/lib/combo/tenants/%s/' % dest_site])
diff --git a/grandlyon/scripts/copy-portal-users.py b/grandlyon/scripts/copy-portal-users.py
index 0e48f91..d8de98e 100644
--- a/grandlyon/scripts/copy-portal-users.py
+++ b/grandlyon/scripts/copy-portal-users.py
@@ -1,4 +1,5 @@
#! /usr/bin/env python
+# python3 copy-portal-users.py Oullins Genay
import json
import os
@@ -7,22 +8,22 @@ import sys
from django.utils.text import slugify
-communes_json = os.path.join(os.path.dirname(__file__), 'communes.json')
-
-orig_city = json.load(open(communes_json))['orig']
+orig_city= sys.argv[1]
slug_orig_city = slugify(orig_city)
orig_site = 'portail-citoyen-%s.guichet-recette.grandlyon.com' % slug_orig_city
-site_export_orig = subprocess.check_output(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'export_site', '-d', orig_site])
+dest_city = sys.argv[2]
+slug_dest_city = slugify(dest_city)
+dest_site = 'portail-citoyen-%s.guichet-recette.grandlyon.com' % slug_dest_city
-for dest in json.load(open(communes_json))['dests']:
- dest_city = dest['name']
- slug_dest_city = slugify(dest_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'])
+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'])
- subprocess.call(['sudo', '-u', 'combo', 'combo-manage', 'tenant_command', 'runscript', '-d', dest_site, '/data/home/tma/scripts/fsck-combo.py'])
+if os.listdir('/var/lib/combo/tenants/%s/media/' % orig_site):
+ subprocess.call(['sudo', '-u', 'combo', 'cp', '-ar', '/var/lib/combo/tenants/%s/media' % orig_site, '/var/lib/combo/tenants/%s/' % dest_site])
diff --git a/grandlyon/scripts/copy-roles.py b/grandlyon/scripts/copy-roles.py
new file mode 100644
index 0000000..3f9861d
--- /dev/null
+++ b/grandlyon/scripts/copy-roles.py
@@ -0,0 +1,27 @@
+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)
+
+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}
+
+json.dump(mapping, open('/tmp/roles.mapping.json', 'w'), indent=2)
diff --git a/grandlyon/scripts/copy-wcs.py b/grandlyon/scripts/copy-wcs.py
index 3484065..b5d18bd 100755
--- a/grandlyon/scripts/copy-wcs.py
+++ b/grandlyon/scripts/copy-wcs.py
@@ -1,5 +1,8 @@
#! /usr/bin/env python
-# usage: sudo -u wcs-au-quotidien ~tma/scripts/copy-wcs.py
+# 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
@@ -8,44 +11,40 @@ import sys
from django.utils.text import slugify
-communes_json = os.path.join(os.path.dirname(__file__), 'communes.json')
-
-orig_city = json.load(open(communes_json))['orig']
+orig_city = sys.argv[1]
slug_orig_city = slugify(orig_city)
orig_site = 'demarches-%s.guichet-recette.grandlyon.com' % slug_orig_city
-roles_mapping = json.load(open('/tmp/roles.mapping.json'))
+dest_city = sys.argv[2]
+slug_dest_city = slugify(dest_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
- src = open(orig).read()
+ 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, roles_mapping[role_id][dest_city])
- fd = open(dest, 'w')
+ 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 dest in json.load(open(communes_json))['dests']:
- dest_city = dest['name']
- print dest_city
- slug_dest_city = slugify(dest_city)
- dest_site = 'demarches-%s.guichet-recette.grandlyon.com' % slug_dest_city
-
- for object_type in ('formdefs', 'workflows', 'categories', 'datasources', 'models'):
- orig_dir = os.path.join('/var/lib/wcs-au-quotidien/%s/%s/' % (orig_site, object_type))
- dest_dir = os.path.join('/var/lib/wcs-au-quotidien/%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)
-
- subprocess.call(['/usr/bin/python', '/usr/sbin/wcsctl', '-f', '/etc/wcs/wcs-au-quotidien.cfg',
- 'runscript', '--extra', '/usr/lib/pymodules/python2.7/extra-wcs-au-quotidien/',
- '--vhost', dest_site,
- '/data/home/tma/scripts/fsck-wcs.py'])
+for object_type in ('formdefs', 'workflows', 'categories', 'datasources', 'models', 'wcscalls'):
+ 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():
+ formdef.store()
diff --git a/grandlyon/scripts/duplicate-roles.py b/grandlyon/scripts/duplicate-roles.py
deleted file mode 100644
index 0fbba8f..0000000
--- a/grandlyon/scripts/duplicate-roles.py
+++ /dev/null
@@ -1,36 +0,0 @@
-# usage:
-# cmd ou-orig ou-dest
-# ex:
-# authentic2-multitenant-manage tenant_command runscript -d connexion.guichet-recette.grandlyon.com ~tma/scripts/duplicate-roles.py Bron Dardilly
-
-import datetime
-import json
-import sys
-
-from authentic2.models import Attribute
-from authentic2.compat import get_user_model
-from authentic2.utils import datetime_to_utc
-from django_rbac.utils import get_role_model, get_ou_model
-from hobo.agent.authentic2.provisionning import Provisionning
-
-ORIG = json.load(open('communes.json'))['orig']
-DESTS = [x['name'] for x in json.load(open('communes.json'))['dests']]
-
-User = get_user_model()
-Role = get_role_model()
-Ou = get_ou_model()
-
-ou_orig = Ou.objects.get(name=ORIG)
-
-with Provisionning():
- mapping = {}
- for orig_role in Role.objects.filter(ou=ou_orig):
- if orig_role.slug.startswith('_a2'):
- continue
- mapping[orig_role.uuid] = {}
- for dest in DESTS:
- ou_dest = Ou.objects.get(name=dest)
- dest_role, created = Role.objects.get_or_create(ou=ou_dest, name=orig_role.name.replace(ORIG, dest))
- dest_role.save()
- mapping[orig_role.uuid][dest] = dest_role.uuid
- json.dump(mapping, open('/tmp/roles.mapping.json', 'w'), indent=2)
diff --git a/grandlyon/scripts/mark-admin-roles.py b/grandlyon/scripts/mark-admin-roles.py
index 7ef9f7a..66afc58 100644
--- a/grandlyon/scripts/mark-admin-roles.py
+++ b/grandlyon/scripts/mark-admin-roles.py
@@ -1,6 +1,6 @@
# 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
+# 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
@@ -13,12 +13,11 @@ from django_rbac.utils import get_role_model, get_ou_model
Ou = get_ou_model()
Role = get_role_model()
-communes_json = os.path.join(os.path.dirname(__file__), 'communes.json')
+name_dst = sys.argv[1]
+ou = Ou.objects.get(name=name_dst)
-for dest in json.load(open(communes_json))['dests']:
- ou = Ou.objects.get(name=dest['name'])
- role = Role.objects.get(name=u'Administrateur %s' % dest['name'])
- 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)
+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)