1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use LATMOS::Accounts::Synchro; |
---|
7 | use Getopt::Long; |
---|
8 | use Pod::Usage; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | la-sync - Tools to synchronize base |
---|
13 | |
---|
14 | =head1 SYNOPSIS |
---|
15 | |
---|
16 | Show configured bases: |
---|
17 | |
---|
18 | la-sync [options] |
---|
19 | la-sync --from source --to dest -o type [--all|obj1 obj2] |
---|
20 | |
---|
21 | =cut |
---|
22 | |
---|
23 | GetOptions( |
---|
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 | |
---|
45 | Use this configuration directory instead of the default one. |
---|
46 | |
---|
47 | =item --from basename |
---|
48 | |
---|
49 | Specify source base to use |
---|
50 | |
---|
51 | =item --to basename |
---|
52 | |
---|
53 | Specify destination base to use, this option can be used |
---|
54 | severals times |
---|
55 | |
---|
56 | =item --nodelete |
---|
57 | |
---|
58 | Don't delete object on destination |
---|
59 | |
---|
60 | =item --nocreate |
---|
61 | |
---|
62 | Don't create object, synchronize existing |
---|
63 | |
---|
64 | =item --test |
---|
65 | |
---|
66 | Don't change anything, just test |
---|
67 | |
---|
68 | =item -s|--syncname name |
---|
69 | |
---|
70 | Use synchronisation named C<name> in configuration instead default |
---|
71 | |
---|
72 | =item -o|object object_type |
---|
73 | |
---|
74 | Query will be performed on this object. Default is the 'User' object. |
---|
75 | |
---|
76 | =item -b|--batch |
---|
77 | |
---|
78 | Don't really perform any synchronisation but try to call daemon process |
---|
79 | to perform immediate synchronisation. |
---|
80 | |
---|
81 | =item --unexp |
---|
82 | |
---|
83 | Sync unexported objects in source base |
---|
84 | |
---|
85 | =item -a|--attr |
---|
86 | |
---|
87 | Can be given multiple time, works only when oject type is given |
---|
88 | |
---|
89 | Sync only this attributes. |
---|
90 | |
---|
91 | If the destination attributes must be differents it can be given using a semi colon: |
---|
92 | |
---|
93 | |
---|
94 | To copy sn attribute sn from source to dest objects |
---|
95 | |
---|
96 | --attr sn |
---|
97 | |
---|
98 | To copy sn attribute to uid on dest: |
---|
99 | |
---|
100 | --aatr sn:uid |
---|
101 | |
---|
102 | =back |
---|
103 | |
---|
104 | =cut |
---|
105 | |
---|
106 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
107 | |
---|
108 | if ($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 | |
---|
113 | my $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 | |
---|
124 | if ($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 | |
---|
167 | exit 0; |
---|