source: branches/stable/vega-import.pl@ 793

Last change on this file since 793 was 548, checked in by Kris Deugau, 11 years ago

/branches/stable

Merge reverse DNS work and object conversion from /trunk, part 5

Includes changes through r543 with a few more minor conflicts.

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