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 41     41   6445 use strict;
  41         91  
  41         1247  
3 41     41   232 use warnings;
  41         93  
  41         1442  
4 41     41   56946 use AnyEvent;
  41         212694  
  41         1312  
5 41     41   19779 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_6';
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              
50             $|++;
51             my $status = $client->bootstrap_identity;
52              
53             unless ( $status eq 'IdentityImported' ) {
54             $dbw->rollback;
55             $error =
56             "unexpected IdentityImported status received: $status";
57             return;
58             }
59              
60             my ( $iid, $uuid, $name, $sn ) = $dbw->xlist(
61             select => [
62             'bifkv.identity_id',
63             '"u" || substr(n.uuid,1,8) AS uuid',
64             'e.name', 'i.shortname',
65             ],
66             from => 'bifkv',
67             inner_join => 'nodes n',
68             on => 'n.id = bifkv.identity_id',
69             inner_join => 'identities i',
70             on => 'i.id = n.id',
71             inner_join => 'entities e',
72             on => 'e.id = i.id',
73             where => { key => 'self' },
74             );
75              
76             $self->start_work(
77             node_id => $iid,
78             start => $start,
79             start_comment => 'pull identity',
80             stop => time,
81             billable => 0,
82             );
83              
84             print "\n";
85             }
86             );
87             };
88              
89             if ($@) {
90             $error = $@;
91             print "\n";
92             }
93              
94             return $cv->send;
95             };
96              
97             $cv->recv;
98             $client->disconnect;
99             return $self->err( 'Unknown', $error ) if $error;
100             return $self->ok('PullIdentity');
101             }
102              
103             1;
104             __END__