source: trunk/vega-import.pl@ 241

Last change on this file since 241 was 211, checked in by Kris Deugau, 13 years ago

/trunk

Fix niggling little bug in vega-import.pl that caused the permissions
and default_records IDs to start at 2 instead of 1.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 17.4 KB
RevLine 
[171]1#!/usr/bin/perl
[200]2# $Id: vega-import.pl 211 2011-12-21 22:48:17Z kdeugau $
[171]3# Import script for VegaDNS data
[200]4# Copyright (C) 2011 - Kris Deugau <kdeugau@deepnet.cx>
[171]5
6# Runs in one of two modes:
7# - Wipe the contents and import the VegaDNS data with (almost) all entity-ID info as-is
8# - Add the contents to the current DB, as a subgroup of the root parent group 1
9
10use strict;
11use warnings;
12
13use DBI;
14use Data::Dumper;
15
16use DNSDB qw(:ALL);
17
18if (!loadConfig()) {
19 warn "Using default configuration; unable to load custom settings: $DNSDB::errstr";
20}
21
22my $mode = 'add';
23
24if ($ARGV[0] && $ARGV[0] !~ /^(add|replace)$/) {
25 die "Usage: vega-import.pl [add|replace]\n".
[199]26 " add Import VegaDNS data as a subgroup of the default root group (default)\n".
[171]27 " replace Wipe all existing data and import VegaDNS data into the root group\n";
28}
29
30$mode = $ARGV[0] if $ARGV[0];
31
32my ($newdbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
33$newdbh->{PrintError} = 1;
34$newdbh->{PrintWarn} = 1;
[172]35initGlobals($newdbh);
[171]36
[172]37my %vegatypes = ('S' => 'SOA', 'N' => 'NS', 'A' => 'A', 'T' => 'TXT',
[204]38 'V' => 'SRV', 'P' => 'PTR', 'M' => 'MX', 'C' => 'CNAME',
39 '3' => 'AAAA' );
[172]40
[171]41if ($mode eq 'replace') {
42 print "WARNING: Using 'replace' will **DELETE** all existing group,\n".
[172]43 "user, domain, and record data in DeepNet DNS Administrator.\n".
44 "Once started this cannot be reversed. Enter 'yes' to proceed: ";
[171]45 my $resp = <STDIN>;
46 chomp $resp;
47 if ($resp ne 'yes') {
48 print "Aborting; no changes made.\n";
49 exit;
50 } else {
51 print "OK, continuing.\n";
52 $newdbh->do("DELETE FROM users") or warn "error deleting users: ".$newdbh->errstr."\n";
53 $newdbh->do("DELETE FROM log") or warn "error deleting log: ".$newdbh->errstr."\n";
54 $newdbh->do("DELETE FROM default_records") or warn "error deleting defrecs: ".$newdbh->errstr."\n";
55 $newdbh->do("DELETE FROM records") or warn "error deleting records: ".$newdbh->errstr."\n";
56 $newdbh->do("DELETE FROM domains") or warn "error deleting domains: ".$newdbh->errstr."\n";
57 $newdbh->do("DELETE FROM groups") or warn "error deleting groups: ".$newdbh->errstr."\n";
58 $newdbh->do("DELETE FROM permissions") or warn "error deleting permissions: ".$newdbh->errstr."\n";
59 # set seq id on permissions, since we merged the two tables, and the new permission IDs will not match the old
60 $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('permissions', 'permission_id'),".
[211]61 "1, false)") or warn "couldn't set permission sequence: ".$newdbh->errstr."\n";
[189]62 # set seq id on default_records since we're inserting more than we retrieved
63 $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('default_records', 'record_id'),".
[211]64 "1, false)") or warn "couldn't set defrec sequence: ".$newdbh->errstr."\n";
[171]65 }
66}
67
68my $vegadbh = DBI->connect('DBI:mysql:database=vegadns;host=localhost', 'vegadns', 'secret', {
69 PrintError => 1,
70 PrintWarn => 1,
[189]71 AutoCommit => 0
[171]72 });
73
74my $grpsubs = $vegadbh->prepare("SELECT group_id FROM groups ORDER BY group_id");
75my $grpdata = $vegadbh->prepare("SELECT group_id, parent_group_id, name FROM groups WHERE group_id=?");
76my $grppget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
77 "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
78 "record_create,record_delete FROM group_permissions WHERE group_id=?");
79my $userget = $vegadbh->prepare("SELECT user_id, group_id, email, password, first_name, last_name, phone, ".
80 "account_type, status FROM accounts WHERE group_id=?");
81my $userpget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
82 "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
83 "record_create,record_delete FROM user_permissions WHERE user_id=?");
[172]84my $domget = $vegadbh->prepare("SELECT domain_id,domain,description,status FROM domains WHERE group_id=?");
85my $recget = $vegadbh->prepare("SELECT record_id,host,type,val,distance,weight,port,ttl,description ".
86 "FROM records WHERE domain_id=?");
[184]87my $defrecget = $vegadbh->prepare("SELECT group_id, host, type, val, distance, weight, port, ttl, ".
[172]88 "description FROM default_records WHERE group_id=?");
[189]89my $logget = $vegadbh->prepare("SELECT domain_id, user_id, group_id, email, name, entry, time FROM log ".
90 "WHERE group_id=?");
[171]91
92my $newgrp = $newdbh->prepare("INSERT INTO groups (group_id,parent_group_id,group_name,permission_id) ".
93 "VALUES (?,?,?,?)");
94my $newgrppset = $newdbh->prepare("INSERT INTO permissions (self_edit, group_create, ".
95 "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
96 "domain_delete, record_create, record_edit, record_delete, group_id) ".
97 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
98my $newpid = $newdbh->prepare("SELECT currval('permissions_permission_id_seq')");
99my $newuser = $newdbh->prepare("INSERT INTO users (user_id, group_id, username, password, firstname, ".
100 "lastname, phone, type, status, permission_id, inherit_perm) VALUES (?,?,?,?,?,?,?,?,?,?,'f')");
[172]101my $newuserpset = $newdbh->prepare("INSERT INTO permissions (admin, self_edit, group_create, ".
[171]102 "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
103 "domain_delete, record_create, record_edit, record_delete, user_id) ".
[172]104 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
105my $newdom = $newdbh->prepare("INSERT INTO domains (domain_id, domain, group_id, description, status) VALUES ".
106 "(?,?,?,?,?)");
107my $newrec = $newdbh->prepare("INSERT INTO records (domain_id,record_id,host,type,val,distance,weight,port,".
108 "ttl,description) VALUES (?,?,?,?,?,?,?,?,?,?)");
[184]109my $newdefrec = $newdbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,".
110 "weight,port,ttl,description) VALUES (?,?,?,?,?,?,?,?,?)");
[189]111my $logput = $newdbh->prepare("INSERT INTO log (domain_id, user_id, group_id, email, name, entry, stamp) ".
112 "VALUES (?,?,?,?,?,?,?)");
[171]113
[189]114my $newgrpid = $newdbh->prepare("SELECT currval('groups_group_id_seq')");
115my $newpgidup = $newdbh->prepare("UPDATE groups SET permission_id=? WHERE group_id=?");
116my $newuserid = $newdbh->prepare("SELECT currval('users_user_id_seq')");
117my $newpuidup = $newdbh->prepare("UPDATE users SET permission_id=? WHERE user_id=?");
118my $newdomid = $newdbh->prepare("SELECT currval('domains_domain_id_seq')");
119my $newrecid = $newdbh->prepare("SELECT currval('records_record_id_seq')");
120
121my $newgrp_add = $newdbh->prepare("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)");
122my $newuser_add = $newdbh->prepare("INSERT INTO users (group_id, username, password, firstname, ".
123 "lastname, phone, type, status, inherit_perm) VALUES (?,?,?,?,?,?,?,?,'f')");
124my $newdom_add = $newdbh->prepare("INSERT INTO domains (domain, group_id, description, status) VALUES ".
125 "(?,?,?,?)");
126my $newrec_add = $newdbh->prepare("INSERT INTO records (domain_id,host,type,val,distance,weight,port,".
127 "ttl,description) VALUES (?,?,?,?,?,?,?,?,?)");
128
[171]129my $foo = qq(
130
131perm_id | group_id | inherit_group_perms | accouedit | accoucreate | accoudelete | self_edit | group_edit |
132group_create | group_delete | domain_edit | domain_create | domain_delegate | domain_delete | record_edit |
133record_create | record_delete | record_delegate | default_record_edit | default_record_create |
134default_record_delete | rrtype_allow_n | rrtype_allow_a | rrtype_allow_3 | rrtype_allow_6 | rrtype_allow_m |
135rrtype_allow_p | rrtype_allow_t | rrtype_allow_v | rrtype_allow_all
136
137permission_id | admin | self_edit | group_create | group_edit | group_delete | user_create | user_edit |
138user_delete | domain_create | domain_edit | domain_delete | record_create | record_edit | record_delete |
139user_id | group_id
140
141);
142
[189]143my $maxgrp = 0;
144my %pcmap; # used to map parent/child relations
145
146if ($mode eq 'replace') {
147
148 my $stage = 'start';
149 $grpsubs->execute;
150 my $grp = 0;
151 while (($grp) = $grpsubs->fetchrow_array()) {
152 $maxgrp = $grp if $grp > $maxgrp;
153 dogroup_replace($grp);
154 }
155
156 # Fix up sequence numbers to prevent insert problems
157 my %idmap = ('groups' => 'group_id', 'users' => 'user_id', 'domains' => 'domain_id',
158 'records' => 'record_id');
159 for my $table ('groups','users','domains','records') {
160 $newdbh->do("SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('$table','$idmap{$table}'),".
161 "(SELECT max($idmap{$table}) FROM $table),true)");
162 }
163
164} else {
165
166 $grpsubs->execute;
167 my $grp = 0;
168 while (($grp) = $grpsubs->fetchrow_array()) {
169 $maxgrp = $grp if $grp > $maxgrp;
170 dogroup_add($grp);
171 }
172
[171]173}
[189]174
175# and done.
[171]176exit;
177
[189]178sub dogroup_replace {
[171]179 my $grpid = shift;
180 $grpdata->execute($grpid) or warn $grpdata->errstr;
181 my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
182 print "group id $grpid, name $oldgrp->{name}\n";
183
184 $grppget->execute($grpid) or warn $grppget->errstr;
[203]185 my $oldp;
186 unless ($oldp = $grppget->fetchrow_hashref()) {
187 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
188 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
189 'record_edit', 'record_delete') { $oldp->{$permtype} = 't'; }
190 }
[171]191
192 # de-nullify
193 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
194 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
195 'record_edit', 'record_delete') {
196 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
197 }
198 $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
199 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
200 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
201 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
202 $grpid) or warn $newgrppset->errstr;
203 $newpid->execute;
204 my ($pid) = $newpid->fetchrow_array;
205 $newgrp->execute($grpid, $oldgrp->{parent_group_id}, $oldgrp->{name}, $pid);
206
207 ## Users
[189]208 print " users: ";
[171]209 $userget->execute($grpid);
210 while (my $user = $userget->fetchrow_hashref) {
[172]211 # fiddle user data
212 $user->{account_type} = 'S' if $user->{account_type} eq 'senior_admin';
213 $user->{account_type} = 'u' if $user->{account_type} eq 'user';
214 $user->{status} = ($user->{status} eq 'active' ? 1 : 0);
[171]215 $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
216 my $oldp = $userpget->fetchrow_hashref;
217 # de-nullify
[172]218 foreach my $permtype ('admin', 'self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
[171]219 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
220 'record_edit', 'record_delete') {
221 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
222 }
[172]223 $newuserpset->execute(($user->{account_type} eq 'S' ? 't' : 'f'), $oldp->{self_edit},
224 $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
[171]225 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
226 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
227 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
228 $user->{user_id}) or warn $newgrppset->errstr;
229 $newpid->execute;
230 my ($pid) = $newpid->fetchrow_array;
231 $newuser->execute($user->{user_id}, $user->{group_id}, $user->{email}, $user->{password},
232 $user->{first_name}, $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status}, $pid)
233 or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
[189]234 print $user->{email}." (".$user->{user_id}."), ";
[171]235 }
236
[172]237 ## Domains
[189]238 print "\n domains: ";
[172]239 $domget->execute($grpid);
240 while (my ($id,$dom,$desc,$status) = $domget->fetchrow_array) {
241 $status = ($status eq 'active' ? 1 : 0);
242 $newdom->execute($id, $dom, $grpid, $desc, $status);
[189]243 print "$dom ($id), ";
[172]244 $recget->execute($id);
245 while (my @rec = $recget->fetchrow_array) {
246 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
247 $rec[4] = 0 if !$rec[4];
248 $rec[5] = 0 if !$rec[5];
249 $rec[6] = 0 if !$rec[6];
250 $newrec->execute($id,@rec);
251 }
252 }
[171]253
[172]254 ## Default records
[189]255 print "\n default records: ";
[184]256 $defrecget->execute(1); # Vega 1.1.5/1.1.6 do not have default records for all groups;
257 # there is only support for one set of default records coded.
[172]258 while (my @rec = $defrecget->fetchrow_array) {
[184]259 $rec[0] = $grpid;
260 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
261 $rec[4] = 0 if !$rec[4];
[172]262 $rec[5] = 0 if !$rec[5];
263 $rec[6] = 0 if !$rec[6];
264 $newdefrec->execute(@rec);
265 }
[189]266
267 ## Log entries
268 print "\n log entries: ";
269 $logget->execute($grpid);
270 while (my ($did,$uid,$gid,$email,$name,$entry,$stamp) = $logget->fetchrow_array) {
271 $stamp = localtime($stamp).'';
[194]272 $logput->execute($did,$uid,$gid,$email,$name,'[Vega] '.$entry,$stamp);
[189]273 }
274
275 print "\n done\n";
[171]276}
[189]277
278sub dogroup_add {
279 my $oldgrpid = shift;
280
281 $grpdata->execute($oldgrpid) or warn $grpdata->errstr;
282 my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
283 print "group id $oldgrpid, name $oldgrp->{name}\n";
284
285 my $newgrpparent = $pcmap{g}{$oldgrp->{parent_group_id}};
286 $newgrpparent = 1 if $oldgrpid == 1;
287
288 # do in the same order as dnsdb
289 $newgrp_add->execute($newgrpparent, $oldgrp->{name});
290 $newgrpid->execute;
291 my ($newgid) = $newgrpid->fetchrow_array;
292 $pcmap{g}{$oldgrpid} = $newgid;
293
294 $grppget->execute($oldgrpid) or warn $grppget->errstr;
[203]295 my $oldp;
296 unless ($oldp = $grppget->fetchrow_hashref()) {
297 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
298 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
299 'record_edit', 'record_delete') { $oldp->{$permtype} = 't'; }
300 }
[189]301
302 # de-nullify
303 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
304 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
305 'record_edit', 'record_delete') {
306 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
307 }
308 $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
309 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
310 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
311 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
312 $newgid) or warn $newgrppset->errstr;
313 $newpid->execute;
314 my ($pid) = $newpid->fetchrow_array;
315 $newpgidup->execute($pid,$newgid);
316
317 ## Users
318 print " users: ";
319 $userget->execute($oldgrpid);
320 while (my $user = $userget->fetchrow_hashref) {
321 # fiddle user data
322 $user->{account_type} = 'S' if $user->{account_type} eq 'senior_admin';
323 $user->{account_type} = 'u' if $user->{account_type} eq 'user';
324 $user->{status} = ($user->{status} eq 'active' ? 1 : 0);
325 $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
326
327 $newuser_add->execute($newgid, $user->{email}, $user->{password}, $user->{first_name},
328 $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status})
329 or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
330 print " user ".$user->{email}." (".$user->{user_id}."), ";
331 $newuserid->execute;
332 my ($newuid) = $newuserid->fetchrow_array;
333 $pcmap{u}{$user->{user_id}} = $newuid;
334
335 my $oldp = $userpget->fetchrow_hashref;
336 # de-nullify
337 foreach my $permtype ('admin', 'self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
338 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
339 'record_edit', 'record_delete') {
340 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
341 }
342 $newuserpset->execute(($user->{account_type} eq 'S' ? 't' : 'f'), $oldp->{self_edit},
343 $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
344 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
345 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
346 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
347 $newuid) or warn $newgrppset->errstr;
348 $newpid->execute;
349 my ($pid) = $newpid->fetchrow_array;
350 $newpuidup->execute($pid,$newuid);
351 }
352
353 ## Domains
354 print "\n domains: ";
355 $domget->execute($oldgrpid);
356 while (my ($id,$dom,$desc,$status) = $domget->fetchrow_array) {
357 $status = ($status eq 'active' ? 1 : 0);
358 $newdom_add->execute($dom, $newgid, $desc, $status);
359 print "$dom ($id), ";
360 $newdomid->execute;
361 my ($newdid) = $newdomid->fetchrow_array;
362 $pcmap{d}{$id} = $newdid;
363 $recget->execute($id);
364 while (my @rec = $recget->fetchrow_array) {
365 $rec[0] = $newdid;
366 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
367 $rec[4] = 0 if !$rec[4];
368 $rec[5] = 0 if !$rec[5];
369 $rec[6] = 0 if !$rec[6];
370 $newrec_add->execute(@rec);
371 }
372 }
373
374 ## Default records
375 print "\n default records: ";
376 $defrecget->execute(1); # Vega 1.1.5/1.1.6 do not have default records for all groups;
377 # there is only support for one set of default records coded.
378 while (my @rec = $defrecget->fetchrow_array) {
379 $rec[0] = $newgid;
380 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
381 $rec[4] = 0 if !$rec[4];
382 $rec[5] = 0 if !$rec[5];
383 $rec[6] = 0 if !$rec[6];
384 $newdefrec->execute(@rec);
385 }
386
387 ## Log entries
388 print "\n log entries: ";
389 $logget->execute($oldgrpid);
390 while (my ($did,$uid,$gid,$email,$name,$entry,$stamp) = $logget->fetchrow_array) {
391 $did = $pcmap{d}{$did};
392 $uid = $pcmap{u}{$uid};
393 $gid = $pcmap{g}{$gid};
394 $stamp = localtime($stamp).'';
395 $logput->execute($did,$uid,$gid,$email,$name,'[V] '.$entry,$stamp);
396 }
397
398 print "\n done\n";
399}
Note: See TracBrowser for help on using the repository browser.