source: trunk/LATMOS-Accounts/bin/la-sync

Last change on this file was 2501, checked in by nanardon, 3 years ago

add --attr option to la-sync

  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use LATMOS::Accounts::Synchro;
7use Getopt::Long;
8use Pod::Usage;
9
10=head1 NAME
11
12    la-sync - Tools to synchronize base
13
14=head1 SYNOPSIS
15
16Show configured bases:
17
18    la-sync [options]
19    la-sync --from source --to dest -o type [--all|obj1 obj2]
20
21=cut
22
23GetOptions(
24    'c|config=s'   => \my $config,
25    'help'         => sub { pod2usage(0) },
26    'from=s'       => \my $from,
27    'to=s'         => \my @to,
28    'nocreate'     => \my $nocreate,
29    'nodelete'     => \my $nodelete,
30    'test'         => \my $test,
31    'o|object=s'   => \my $otype,
32    's|syncname=s' => \my $syncname,
33    'b|batch'      => \my $batch,
34    'unexp'        => \my $unexp,
35    'all'          => \my $all,
36    'a|attr=s'     => \my @attrs, 
37) or pod2usage();
38
39=head1 OPTIONS
40
41=over 4
42
43=item -c|--config configdir
44
45Use this configuration directory instead of the default one.
46
47=item --from basename
48
49Specify source base to use
50
51=item --to basename
52
53Specify destination base to use, this option can be used
54severals times
55
56=item --nodelete
57
58Don't delete object on destination
59
60=item --nocreate
61
62Don't create object, synchronize existing
63
64=item --test
65
66Don't change anything, just test
67
68=item -s|--syncname name
69
70Use synchronisation named C<name> in configuration instead default
71
72=item -o|object object_type
73
74Query will be performed on this object. Default is the 'User' object.
75
76=item -b|--batch
77
78Don't really perform any synchronisation but try to call daemon process
79to perform immediate synchronisation.
80
81=item --unexp
82
83Sync unexported objects in source base
84
85=item -a|--attr
86
87Can be given multiple time, works only when oject type is given
88
89Sync only this attributes.
90
91If the destination attributes must be differents it can be given using a semi colon:
92
93
94To copy sn attribute sn from source to dest objects
95
96    --attr sn
97
98To copy sn attribute to uid on dest:
99
100   --aatr sn:uid
101
102=back
103
104=cut
105
106my $LA = LATMOS::Accounts->new($config, noacl => 1);
107
108if ($batch) {
109    warn "Using la-sync -b is deprecated, use la-sync-ctl -s instead\n";
110    exit ($LA->call_batch_sync ? 0 : 1);
111}
112
113my $sync = $LA->create_synchro(
114    $syncname,
115    from => $from,
116    to => (@to ? [ @to ] : undef),
117    test     => $test,
118    unexported => $unexp,
119) or die "cannot create sync object\n";
120$sync->load_dest and do {
121    die "Cannot load dest base\n";
122};
123
124if ($otype) {
125    my @list;
126
127    if ($all) {
128        @list =  $sync->from->list_objects($otype)
129    } elsif (@ARGV) {
130        @list = @ARGV;
131    } else {
132        die "No object to synchronize given, -a missing ?\n";
133    }
134
135    if ( @attrs ) {
136
137        foreach my $id ( @list ) {
138            my $srcOjb = $sync->from->get_object($otype, $id) or next;
139            foreach my $dest ( $sync->to ) {
140                my $destObj = $dest->get_object($otype, $id) or next;
141                my %destvalue;
142                foreach ( sort @attrs ) {
143                    m/([^:]+)(?::(.*))?/;
144                    $destvalue{ $1 } = $srcOjb->get_c_field( $2 || $1 );
145                }
146                $destObj->set_c_fields( %destvalue );
147                $dest->commit;
148            }
149        }
150    } else {
151        foreach (@list) {
152            warn "Try to sync $otype: $_\n";
153            $sync->sync_object($otype, $_, onepass => 1) or do {
154            warn "Error while synching $_ ($otype)\n";
155            exit 1;
156            }
157        }
158    }
159} else {
160    $sync->process(
161        test     => $test,
162        nodelete => $nodelete,
163        nocreate => $nocreate,
164    ) or warn "Sync has failed\n";
165}
166
167exit 0;
Note: See TracBrowser for help on using the repository browser.