Projet

Général

Profil

0001-command-add-a-delete_tenant-command-15636.patch

Jean-Baptiste Jaillet, 07 avril 2017 16:05

Télécharger (5,25 ko)

Voir les différences:

Subject: [PATCH] command: add a delete_tenant command (#15636)

 wcs/ctl/check_hobos.py   |  8 +++-
 wcs/ctl/delete_tenant.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 wcs/ctl/delete_tenant.py
wcs/ctl/check_hobos.py
422 422
            cur.execute('''CREATE DATABASE %s''' % database_name)
423 423
        except psycopg2.Error as e:
424 424
            if e.pgcode == psycopg2.errorcodes.DUPLICATE_DATABASE:
425
                new_database = False
425
                cur.execute("SELECT table_name FROM information_schema.tables \
426
                            WHERE table_schema='public' AND \
427
                            table_type='BASE TABLE'")
428
                tables_names = [x[0] for x in cur.fetchall()]
429

  
430
               if 'wcs_meta' in tables_names :
431
                    new_database = False
426 432
            else:
427 433
                print >> sys.stderr, 'failed to create database (%s)' % \
428 434
                        psycopg2.errorcodes.lookup(e.pgcode)
wcs/ctl/delete_tenant.py
1
#w.c.s. -  web application for online forms 
2
# Copyright (C) 2005-2014  Entr'ouvert 
3
# 
4
# This program is free software; you can redistribute it and/or modify 
5
# it under the terms of the GNU General Public License as published by 
6
# the Free Software Foundation; either version 2 of the License, or 
7
# (at your option) any later version. 
8
# 
9
# This program is distributed in the hope that it will be useful, 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12
# GNU General Public License for more details. 
13
# 
14
# You should have received a copy of the GNU General Public License 
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

  
17
import os
18
import sys
19
import psycopg2
20
import psycopg2.errorcodes
21
from datetime import datetime
22
from shutil import rmtree
23

  
24
from qommon.ctl import Command, make_option
25

  
26

  
27
class CmdDeleteTenant(Command):
28
    name = 'delete_tenant'
29

  
30
    def __init__(self):
31
        Command.__init__(self, [
32
                make_option('--force-drop', action='store_true', default=False),
33
                ])
34

  
35
    def execute(self, base_options, sub_options, args):
36
        import publisher
37

  
38
        publisher.WcsPublisher.configure(self.config)
39
        pub = publisher.WcsPublisher.create_publisher(
40
                register_cron=False, register_tld_names=False)
41

  
42
        hostname = args[0]
43
        pub.app_dir = os.path.join(pub.app_dir, hostname)
44
        pub.set_config()
45

  
46
        postgresql_cfg = {}
47
        for k, v in pub.cfg['postgresql'].items():
48
            if v and isinstance(v, basestring):
49
                postgresql_cfg[k] = v
50

  
51
        createdb_cfg = pub.cfg['postgresql'].get('createdb-connection-params')
52
        if not createdb_cfg:
53
            createdb_cfg = postgresql_cfg
54

  
55
        try:
56
            pgconn = psycopg2.connect(**createdb_cfg)
57
        except psycopg2.Error as e:
58
            print >> sys.stderr, 'failed to connect to postgresql (%s)' % \
59
                    psycopg2.errorcodes.lookup(e.pgcode)
60
            return
61

  
62
        pgconn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
63
        cur = pgconn.cursor()
64
        try:
65
            cur.execute("SELECT table_name FROM information_schema.tables \
66
                        WHERE table_schema='public' AND \
67
                        table_type='BASE TABLE'")
68

  
69
            tables_names = [x[0] for x in cur.fetchall()]
70

  
71
            if sub_options.force_drop:
72
                for table_name in tables_names:
73
                    cur.execute('DROP TABLE %s CASCADE' % table_name)
74
                rmtree(pub.app_dir)
75

  
76
            else:
77
                deletion_date = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
78

  
79
                for table_name in tables_names:
80
                    cur.execute('ALTER TABLE %s RENAME TO removed_%s_%s' % \
81
                               (table_name, deletion_date, table_name))
82
                os.rename(pub.app_dir, pub.app_dir + '_removed_%s.invalid' % \
83
                          deletion_date)
84
        except psycopg2.Error as e:
85
            if e.pgcode == psycopg2.errorcodes.DUPLICATE_TABLE:
86
                # when tables names are too long, postgresql truncates, which
87
                # can create some duplicates
88
                temp_date = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
89
                cur.execute('ALTER TABLE %s RENAME TO removed_%s_%s' % \
90
                            (table_name, temp_date, table_name))
91
            else:
92
                print >> sys.stderr, 'failed to alter database %s : (%s)' % \
93
                        (createdb_cfg['database'], psycopg2.errorcodes.lookup(e.pgcode))
94
                return
95

  
96
        cur.close()
97

  
98

  
99
CmdDeleteTenant.register()
0
-