~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to database/replication/initialize.py

  • Committer: Stuart Bishop
  • Date: 2008-09-29 11:46:39 UTC
  • mto: This revision was merged to the branch mainline in revision 7142.
  • Revision ID: stuart.bishop@canonical.com-20080929114639-r3vxcmpf02pxk8qy
Improve initialize, less magic dev setup

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
import helpers
17
17
 
 
18
from canonical.config import config
18
19
from canonical.database.sqlbase import connect
19
20
from canonical.database.postgresql import (
20
 
        all_sequences_in_schema, all_tables_in_schema
 
21
        all_sequences_in_schema, all_tables_in_schema, ConnectionString
21
22
        )
22
23
from canonical.launchpad.scripts import (
23
24
        logger, logger_options, db_options
37
38
def duplicate_schema():
38
39
    """Duplicate the master schema into the slaves."""
39
40
    log.info('Duplicating database schema')
 
41
 
 
42
    master_cs = ConnectionString(config.database.main_master)
 
43
    master_cs.user = options.dbuser
 
44
    slave1_cs = ConnectionString(config.database.main_slave)
 
45
    slave1_cs.user = options.dbuser
 
46
 
40
47
    # We can't use pg_dump to replicate security as not all of the roles
41
48
    # may exist in the slave databases' clusters yet.
42
 
    rv = subprocess.call(
43
 
        "pg_dump -x -s -U slony lpmain_demo "
44
 
        "| psql -q -U slony authdb_demo", shell=True)
 
49
    cmd = "pg_dump -x -s %s | psql -q %s" % (
 
50
        master_cs.asPGCommandLineArgs(), slave1_cs.asPGCommandLineArgs())
 
51
    log.debug('Running %s' % cmd)
 
52
    rv = subprocess.call(cmd, shell=True)
45
53
    if rv != 0:
46
54
        log.fatal("Schema duplication failed, pg_dump returned %d" % rv)
47
55
        sys.exit(rv)
48
56
 
49
57
    # Now setup security on the slaves and create any needed roles,
50
58
    log.info('Setting up security on slave')
51
 
    rv = subprocess.call([
52
 
        "../schema/security.py",  "-d", "authdb_demo"])
 
59
    cmd = "../schema/security.py %s" % slave1_cs.asLPCommandLineArgs()
 
60
    log.debug("Running %s" % cmd)
 
61
    rv = subprocess.call(cmd.split())
53
62
    if rv != 0:
54
63
        print >> sys.stderr, "ERR: security setup failed, returning %d" % rv
55
64
        sys.exit(rv)
61
70
    helpers.execute_slonik("""
62
71
        try {
63
72
            echo 'Initializing cluster and Master node.';
64
 
            init cluster (id=@master_id, comment='Master Node');
 
73
            init cluster (id=@master_node, comment='Master Node');
65
74
            }
66
75
        on success { echo 'Cluster initialized.'; }
67
76
        on error { echo 'Cluster initialization failed.'; exit 1; }
69
78
    helpers.execute_slonik("""
70
79
        try {
71
80
            echo 'Initializing Slave#1 node.';
72
 
            store node (id=@slave1_id, comment='Slave Node #1');
 
81
            store node (id=@slave1_node, comment='Slave Node #1');
73
82
 
74
83
            echo 'Storing Master -> Slave#1 path.';
75
84
            store path (
76
 
                server=@master_id, client=@slave1_id,
 
85
                server=@master_node, client=@slave1_node,
77
86
                conninfo=@master_conninfo);
78
87
 
79
88
            echo 'Storing Slave#1 -> Master path.';
80
89
            store path (
81
 
                server=@slave1_id, client=@master_id,
 
90
                server=@slave1_node, client=@master_node,
82
91
                conninfo=@slave1_conninfo);
83
92
            }
84
93
        on success { echo 'Slave#1 initialized.'; }
97
106
    log.info('Creating Slony-I replication sets.')
98
107
    script = ["""
99
108
        try {
100
 
        echo 'Creating AuthDB replication set (@authdb_set_id)';
 
109
        echo 'Creating AuthDB replication set (@authdb_set)';
101
110
        create set (
102
 
            id=@authdb_set_id, origin=@master_id,
 
111
            id=@authdb_set, origin=@master_node,
103
112
            comment='AuthDB tables and sequences');
104
113
        """]
105
114
 
106
115
    entry_id = 1
107
116
    for table in sorted(authdb_tables):
108
117
        script.append("""
109
 
            echo 'Adding %(table)s to replication set @authdb_set_id';
 
118
            echo 'Adding %(table)s to replication set @authdb_set';
110
119
            set add table (
111
 
                set id=@authdb_set_id,
112
 
                origin=@master_id,
 
120
                set id=@authdb_set,
 
121
                origin=@master_node,
113
122
                id=%(entry_id)d,
114
123
                fully qualified name='%(table)s');
115
124
            """ % vars())
117
126
    entry_id = 1
118
127
    for sequence in sorted(authdb_sequences):
119
128
        script.append("""
120
 
            echo 'Adding %(sequence)s to replication set @authdb_set_id';
 
129
            echo 'Adding %(sequence)s to replication set @authdb_set';
121
130
            set add sequence (
122
 
                set id=@authdb_set_id,
123
 
                origin=@master_id,
 
131
                set id=@authdb_set,
 
132
                origin=@master_node,
124
133
                id=%(entry_id)d,
125
134
                fully qualified name='%(sequence)s');
126
135
            """ % vars())
127
136
        entry_id += 1
128
137
 
129
138
    script.append("""
130
 
        echo 'Creating LPMain replication set (@lpmain_set_id)';
 
139
        echo 'Creating LPMain replication set (@lpmain_set)';
131
140
        create set (
132
 
            id=@lpmain_set_id, origin=@master_id,
 
141
            id=@lpmain_set, origin=@master_node,
133
142
            comment='Launchpad tables and sequences');
134
143
        """)
135
144
 
137
146
    entry_id = 200
138
147
    for table in sorted(lpmain_tables):
139
148
        script.append("""
140
 
            echo 'Adding %(table)s to replication set @lpmain_set_id';
 
149
            echo 'Adding %(table)s to replication set @lpmain_set';
141
150
            set add table (
142
 
                set id=@lpmain_set_id,
143
 
                origin=@master_id,
 
151
                set id=@lpmain_set,
 
152
                origin=@master_node,
144
153
                id=%(entry_id)d,
145
154
                fully qualified name='%(table)s');
146
155
            """ % vars())
149
158
    entry_id = 200
150
159
    for sequence in sorted(lpmain_sequences):
151
160
        script.append("""
152
 
            echo 'Adding %(sequence)s to replication set @lpmain_set_id';
 
161
            echo 'Adding %(sequence)s to replication set @lpmain_set';
153
162
            set add sequence (
154
 
                set id=@lpmain_set_id,
155
 
                origin=@master_id,
 
163
                set id=@lpmain_set,
 
164
                origin=@master_node,
156
165
                id=%(entry_id)d,
157
166
                fully qualified name='%(sequence)s');
158
167
            """ % vars())
176
185
    # always need forward=yes as per Slony-I docs.
177
186
    helpers.execute_slonik("""
178
187
        subscribe set (
179
 
            id=@authdb_set_id,
180
 
            provider=@master_id, receiver=@slave1_id,
 
188
            id=@authdb_set,
 
189
            provider=@master_node, receiver=@slave1_node,
181
190
            forward=yes);
182
191
        subscribe set (
183
 
            id=@lpmain_set_id,
184
 
            provider=@master_id, receiver=@slave1_id,
 
192
            id=@lpmain_set,
 
193
            provider=@master_node, receiver=@slave1_node,
185
194
            forward=yes);
186
195
        """)
187
196
    helpers.validate_replication(cur) # Explode now if we have messed up.