source: trunk/vega-import.pl@ 179

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

/trunk

VegaDNS import tool should be complete.

  • User import tweaked to set permissions correctly
  • Domains and domain records imported
  • Default records imported. Note Vega 1.1.5/6 does not seem to keep multiple sets of default records; subgroups will not inherit these records
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 9.8 KB
RevLine 
[171]1#!/usr/bin/perl
2# Import script for VegaDNS data
3# $Id: vega-import.pl 172 2011-11-29 21:52:25Z 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;
[172]35initGlobals($newdbh);
[171]36
[172]37my %vegatypes = ('S' => 'SOA', 'N' => 'NS', 'A' => 'A', 'T' => 'TXT',
38 'V' => 'SRV', 'P' => 'PTR', 'M' => 'MX', 'C' => 'CNAME' );
39
[171]40if ($mode eq 'replace') {
41 print "WARNING: Using 'replace' will **DELETE** all existing group,\n".
[172]42 "user, domain, and record data in DeepNet DNS Administrator.\n".
43 "Once started this cannot be reversed. Enter 'yes' to proceed: ";
[171]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 }
62}
63
64my $vegadbh = DBI->connect('DBI:mysql:database=vegadns;host=localhost', 'vegadns', 'secret', {
65 PrintError => 1,
66 PrintWarn => 1,
67 AutoCommit => 1
68 });
69
70my $grpsubs = $vegadbh->prepare("SELECT group_id FROM groups ORDER BY group_id");
71my $grpdata = $vegadbh->prepare("SELECT group_id, parent_group_id, name FROM groups WHERE group_id=?");
72my $grppget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
73 "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
74 "record_create,record_delete FROM group_permissions WHERE group_id=?");
75my $userget = $vegadbh->prepare("SELECT user_id, group_id, email, password, first_name, last_name, phone, ".
76 "account_type, status FROM accounts WHERE group_id=?");
77my $userpget = $vegadbh->prepare("SELECT perm_id,inherit_group_perms,accouedit,accoucreate,accoudelete,".
78 "self_edit,group_edit,group_create,group_delete,domain_edit,domain_create,domain_delete,record_edit,".
79 "record_create,record_delete FROM user_permissions WHERE user_id=?");
[172]80my $domget = $vegadbh->prepare("SELECT domain_id,domain,description,status FROM domains WHERE group_id=?");
81my $recget = $vegadbh->prepare("SELECT record_id,host,type,val,distance,weight,port,ttl,description ".
82 "FROM records WHERE domain_id=?");
83my $defrecget = $vegadbh->prepare("SELECT record_id, group_id, host, type, val, distance, weight, port, ttl, ".
84 "description FROM default_records WHERE group_id=?");
[171]85
86my $newgrp = $newdbh->prepare("INSERT INTO groups (group_id,parent_group_id,group_name,permission_id) ".
87 "VALUES (?,?,?,?)");
88my $newgrppset = $newdbh->prepare("INSERT INTO permissions (self_edit, group_create, ".
89 "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
90 "domain_delete, record_create, record_edit, record_delete, group_id) ".
91 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
92my $newpid = $newdbh->prepare("SELECT currval('permissions_permission_id_seq')");
93my $newuser = $newdbh->prepare("INSERT INTO users (user_id, group_id, username, password, firstname, ".
94 "lastname, phone, type, status, permission_id, inherit_perm) VALUES (?,?,?,?,?,?,?,?,?,?,'f')");
[172]95my $newuserpset = $newdbh->prepare("INSERT INTO permissions (admin, self_edit, group_create, ".
[171]96 "group_edit, group_delete, user_create, user_edit, user_delete, domain_create, domain_edit, ".
97 "domain_delete, record_create, record_edit, record_delete, user_id) ".
[172]98 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
99my $newdom = $newdbh->prepare("INSERT INTO domains (domain_id, domain, group_id, description, status) VALUES ".
100 "(?,?,?,?,?)");
101my $newrec = $newdbh->prepare("INSERT INTO records (domain_id,record_id,host,type,val,distance,weight,port,".
102 "ttl,description) VALUES (?,?,?,?,?,?,?,?,?,?)");
103my $newdefrec = $newdbh->prepare("INSERT INTO default_records (record_id,group_id,host,type,val,distance,".
104 "weight,port,ttl,description) VALUES (?,?,?,?,?,?,?,?,?,?)");
[171]105
106my $foo = qq(
107
108perm_id | group_id | inherit_group_perms | accouedit | accoucreate | accoudelete | self_edit | group_edit |
109group_create | group_delete | domain_edit | domain_create | domain_delegate | domain_delete | record_edit |
110record_create | record_delete | record_delegate | default_record_edit | default_record_create |
111default_record_delete | rrtype_allow_n | rrtype_allow_a | rrtype_allow_3 | rrtype_allow_6 | rrtype_allow_m |
112rrtype_allow_p | rrtype_allow_t | rrtype_allow_v | rrtype_allow_all
113
114permission_id | admin | self_edit | group_create | group_edit | group_delete | user_create | user_edit |
115user_delete | domain_create | domain_edit | domain_delete | record_create | record_edit | record_delete |
116user_id | group_id
117
118);
119
120my $stage = 'start';
121$grpsubs->execute;
122my $grp = 0;
123while (($grp) = $grpsubs->fetchrow_array()) {
124 dogroup($grp);
125}
126# set group seq to $grp+1
127exit;
128
129sub dogroup {
130 my $grpid = shift;
131 $grpdata->execute($grpid) or warn $grpdata->errstr;
132 my $oldgrp = $grpdata->fetchrow_hashref() or warn $grpdata->errstr;
133 print "group id $grpid, name $oldgrp->{name}\n";
134
135 $grppget->execute($grpid) or warn $grppget->errstr;
136 my $oldp = $grppget->fetchrow_hashref() or warn $grppget->errstr;
137
138 # de-nullify
139 foreach my $permtype ('self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
140 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
141 'record_edit', 'record_delete') {
142 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
143 }
144 $newgrppset->execute($oldp->{self_edit}, $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
145 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
146 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
147 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
148 $grpid) or warn $newgrppset->errstr;
149 $newpid->execute;
150 my ($pid) = $newpid->fetchrow_array;
151 $newgrp->execute($grpid, $oldgrp->{parent_group_id}, $oldgrp->{name}, $pid);
152
153 ## Users
154 print " users:\n";
155 $userget->execute($grpid);
156 while (my $user = $userget->fetchrow_hashref) {
[172]157 # fiddle user data
158 $user->{account_type} = 'S' if $user->{account_type} eq 'senior_admin';
159 $user->{account_type} = 'u' if $user->{account_type} eq 'user';
160 $user->{status} = ($user->{status} eq 'active' ? 1 : 0);
[171]161 $userpget->execute($user->{user_id}) or warn "failed permission get on ".$user->{user_id}."\n";
162 my $oldp = $userpget->fetchrow_hashref;
163 # de-nullify
[172]164 foreach my $permtype ('admin', 'self_edit', 'group_create', 'group_edit', 'group_delete', 'user_create',
[171]165 'user_edit', 'user_delete', 'domain_create', 'domain_edit', 'domain_delete', 'record_create',
166 'record_edit', 'record_delete') {
167 $oldp->{$permtype} = 'f' if !defined($oldp->{$permtype});
168 }
[172]169 $newuserpset->execute(($user->{account_type} eq 'S' ? 't' : 'f'), $oldp->{self_edit},
170 $oldp->{group_create}, $oldp->{group_edit}, $oldp->{group_delete},
[171]171 $oldp->{user_create}, $oldp->{user_edit}, $oldp->{user_delete},
172 $oldp->{domain_create}, $oldp->{domain_edit}, $oldp->{domain_delete},
173 $oldp->{record_create}, $oldp->{record_edit}, $oldp->{record_delete},
174 $user->{user_id}) or warn $newgrppset->errstr;
175 $newpid->execute;
176 my ($pid) = $newpid->fetchrow_array;
177 $newuser->execute($user->{user_id}, $user->{group_id}, $user->{email}, $user->{password},
178 $user->{first_name}, $user->{last_name}, $user->{phone}, $user->{account_type}, $user->{status}, $pid)
179 or warn " new user ".$user->{email}." (".$user->{user_id}.") failed: ".$newuser->errstr."\n";
[172]180 print " user ".$user->{email}." (".$user->{user_id}.")\n";
[171]181 }
[172]182 print " done\n";
[171]183
[172]184 ## Domains
185 print " domains: \n";
186 $domget->execute($grpid);
187 while (my ($id,$dom,$desc,$status) = $domget->fetchrow_array) {
188 $status = ($status eq 'active' ? 1 : 0);
189 $newdom->execute($id, $dom, $grpid, $desc, $status);
190 print " domain $dom ($id)\n";
191 $recget->execute($id);
192 while (my @rec = $recget->fetchrow_array) {
193 $rec[2] = $reverse_typemap{$vegatypes{$rec[2]}};
194 $rec[4] = 0 if !$rec[4];
195 $rec[5] = 0 if !$rec[5];
196 $rec[6] = 0 if !$rec[6];
197 $newrec->execute($id,@rec);
198 }
199 }
200 print " done\n";
[171]201
[172]202 ## Default records
203 print " default records: ";
204 $defrecget->execute($grpid);
205 while (my @rec = $defrecget->fetchrow_array) {
206 $rec[3] = $reverse_typemap{$vegatypes{$rec[3]}};
207 $rec[5] = 0 if !$rec[5];
208 $rec[6] = 0 if !$rec[6];
209 $rec[7] = 0 if !$rec[7];
210 $newdefrec->execute(@rec);
211 }
212 print " done\n";
[171]213}
Note: See TracBrowser for help on using the repository browser.