File Coverage

blib/lib/App/bif/pull/identity.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package App::bif::pull::identity;
2 50     50   4049 use strict;
  50         89  
  50         1825  
3 50     50   236 use warnings;
  50         82  
  50         1725  
4 50     50   50342 use AnyEvent;
  50         227480  
  50         1920  
5 50     50   21126 use Bif::Sync::Client;
  0            
  0            
6             use Bif::Mo;
7             use Coro;
8             use DBIx::ThinSQL qw/qv/;
9             use Log::Any '$log';
10              
11             our $VERSION = '0.1.5_5';
12             extends 'App::bif';
13              
14             sub run {
15             my $self = shift;
16             my $opts = $self->opts;
17             my $cv = AE::cv;
18             my $dbw = $self->dbw;
19              
20             $opts->{message} ||= "Importing identity from $opts->{location}";
21              
22             $|++;
23              
24             my $error;
25             my $client = Bif::Sync::Client->new(
26             name => $opts->{location},
27             db => $dbw,
28             location => $opts->{location},
29             debug => $opts->{debug},
30             debug_bifsync => $opts->{debug_bifsync},
31             on_update => sub {
32             my $client = shift;
33             $self->lprint( $client->name . ': ' . $_[0] );
34             },
35             on_error => sub {
36             $error = shift;
37             $cv->send;
38             },
39             );
40              
41             my $fh = select;
42             my $coro = async {
43             select $fh;
44              
45             eval {
46             $dbw->txn(
47             sub {
48             my $start = time;
49             $self->stop_work(
50             stop => $start,
51             save => 1,
52             );
53              
54             $|++;
55             my $status = $client->bootstrap_identity;
56              
57             unless ( $status eq 'IdentityImported' ) {
58             $dbw->rollback;
59             $error =
60             "unexpected IdentityImported status received: $status";
61             return;
62             }
63              
64             my ( $iid, $uuid, $name, $sn ) = $dbw->xlist(
65             select => [
66             'bifkv.identity_id',
67             '"u" || substr(n.uuid,1,8) AS uuid',
68             'e.name', 'i.shortname',
69             ],
70             from => 'bifkv',
71             inner_join => 'nodes n',
72             on => 'n.id = bifkv.identity_id',
73             inner_join => 'identities i',
74             on => 'i.id = n.id',
75             inner_join => 'entities e',
76             on => 'e.id = i.id',
77             where => { key => 'self' },
78             );
79              
80             $self->start_work(
81             node_id => $iid,
82             start => $start,
83             start_comment => "pull identity",
84             billable => 1,
85             );
86              
87             $self->stop_work(
88             stop => time,
89             restore => 1,
90             );
91             print "\n";
92             }
93             );
94             };
95              
96             if ($@) {
97             $error = $@;
98             print "\n";
99             }
100              
101             return $cv->send;
102             };
103              
104             $cv->recv;
105             $client->disconnect;
106             return $self->err( 'Unknown', $error ) if $error;
107             return $self->ok('PullIdentity');
108             }
109              
110             1;
111             __END__