source: trunk/vega-import.pl@ 191

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

/trunk

Complete VegaDNS import script

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