summaryrefslogtreecommitdiff
path: root/bin/clients.conf.import
blob: 16aac4b2774c80276a3313fce793a8f53c55ffc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/perl -w

use strict;
use DBI;
use FS::UID qw(adminsuidsetup); #datasrc
use FS::Record qw(qsearch qsearchs dbh);
use FS::nas;
use FS::export_nas;
use FS::part_export;

my $user = shift or die &usage;
my $filename = shift or die &usage;
my $all_nas = [];

my $client;
my $in;
open ($in, '<', $filename) or die "can't open $filename for reading\n";
my $i = 0;
while (my $line = <$in>) {
  $i++;
  $line =~ s/#.*//;
  my @t = grep $_, split(/\s+/, $line);
  next if !@t;
  if ( $client ) {
    if ( $t[0] eq 'ipaddr' ) {
      $client->{nasname} = $t[2];
    }
    elsif ( $t[0] eq 'secret' ) {
      $client->{secret} = $t[2];
    }
    elsif( $t[0] eq 'shortname' ) {
      $client->{shortname} = $t[2];
    }
    elsif( $t[0] eq 'nastype' ) {
      $client->{type} = $t[2];
    }
    elsif( $t[0] eq 'virtual_server' ) {
      $client->{server} = $t[2];
    }
    elsif( $t[0] eq '}' ) {
      $client->{description} = $client->{shortname};
      push @$all_nas, $client;
      undef $client;
    }
    else {
      warn "unknown parameter '$t[0]' (line $i), skipped\n";
      next;
    }
  }
  else { # not in a client section
    die "parse error (line $i)\n" if $t[0] ne 'client' or $t[2] ne '{';
    $client = { nasname => $t[1],
                shortname => $t[1] }; # hostname
  }
}
close $in;

warn scalar(@$all_nas)." records found.\n";

adminsuidsetup $user;

$FS::UID::AutoCommit = 0;
my $dbh = dbh;

# cache NAS names we already know about, and don't import them
my %existing_names = map { $_->nasname , $_->nasnum } qsearch('nas', {});

my $inserted = 0;
foreach my $row (@$all_nas) {
  my %hash = %$row;
  if (my $num = $existing_names{ $hash{nasname} }) {
    warn "NAS $hash{nasname} already exists as #$num (skipped)\n";
  }
  else {
    my $nas = FS::nas->new(\%hash);
    my $error = $nas->insert;
    if ( $error ) {
      $dbh->rollback;
      die "error inserting $hash{nasname}: $error (changes reverted)\n";
    }
    $inserted++;
  }
} #foreach $row

warn "Inserted $inserted NAS records.\n\n";
$dbh->commit;

sub usage {
  die "Usage:\n\n  clients.conf.import user filename\n\n";
}