aboutsummaryrefslogtreecommitdiffstats
path: root/lib/MGA/Git.pm
blob: 6f49833b7a18f9e3dd70f44948f38be1794de616 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package MGA::Git;

use strict;
use YAML qw(LoadFile);
use Template;
use File::Slurp;
use Net::LDAP;
use feature 'state';
use Data::Dump qw/dd/;

our $config_file = '/usr/share/mgagit/config';
our $config = LoadFile($ENV{MGAGIT_CONF} || $config_file);
our $etc_config_file = '/etc/mgagit.conf';
my $etc_config = LoadFile($etc_config_file);
@{$config}{keys %$etc_config} = values %$etc_config;

sub load_gitrepos_dir {
    my ($repos, $infos) = @_;
    opendir(my $dh, $infos->{include_dir})
        || die "Error opening $infos->{include_dir}: $!";
    while (my $file = readdir($dh)) {
        if (-d "$infos->{include_dir}/$file") {
            next if $file =~ m/^\./;
            my %i = %$infos;
            $i{prefix} .= '/' . $file;
            $i{include_dir} .= '/' . $file;
            load_gitrepos_dir($repos, \%i);
        } elsif ($file =~ m/(.+)\.repo$/) {
            my $bname = $1;
            my $name = "$infos->{prefix}/$bname";
            $repos->{$name} = LoadFile("$infos->{include_dir}/$file");
            $repos->{$name}{name} = $bname;
            @{$repos->{$name}}{keys %$infos} = values %$infos;
        }
    }
    closedir $dh;
}

sub load_gitrepos {
    my ($r) = @_;
    $r->{repos} = {};
    foreach my $include (@{$config->{repos_config}}) {
        load_gitrepos_dir($r->{repos}, $include);
    }
}

sub get_ldap {
    state $ldap;
    return $ldap if $ldap;
    my $bindpw = read_file($config->{bindpwfile}) 
        or die "Error reading $config->{bindpwfile}";
    chomp $bindpw;
    $ldap = Net::LDAP->new($config->{ldapserver}) or die "$@";
    my $m = $ldap->start_tls(verify => 'none');
    die $m->error if $m->is_error;
    $m = $ldap->bind($config->{binddn}, password => $bindpw);
    die $m->error if $m->is_error;
    return $ldap;
}

sub re {
    my ($re, $txt) = @_;
    my $rr = qr/$config->{$re}/;
    $txt =~ s/$rr/$1/;
    return $txt;
}

sub load_groups {
    my ($r) = @_;
    my $ldap = get_ldap;
    my $m = $ldap->search(
        base => $config->{groupbase},
        filter => $config->{groupfilter},
    );
    my $res = $m->as_struct;
    @{$r->{groups}}{map { re('group_re', $_) } keys %$res} =
        map { [ map { re('uid_username_re', $_) } @{$_->{member}} ] }
        values %$res;
}

sub load_users {
    my ($r) = @_;
    my $ldap = get_ldap;
    my $m = $ldap->search(
        base => $config->{userbase},
        filter => $config->{userfilter},
    );
    my @users = grep { $_->{sshpublickey} } values %{$m->as_struct};
    @{$r->{users}}{map { $_->{uid}[0] } @users} = map { $_->{sshpublickey} } @users;
}

sub get_tmpl {
    my ($name, $ext) = @_;
    state %tmpl;
    return $tmpl{"$name.$ext"} if $tmpl{"$name.$ext"};
    my $tmpl_file = "$config->{tmpl_dir}/$name.$ext";
    $tmpl{"$name.$ext"} = read_file($tmpl_file);
    die "Error reading $tmpl_file" unless $tmpl{"$name.$ext"};
    return $tmpl{"$name.$ext"};
}

sub process_tmpl {
    my ($tmplname, $ext, $vars) = @_;
    my $tt = Template->new;
    my $tmpl = get_tmpl($tmplname, $ext);
    my $c;
    $tt->process(\$tmpl, $vars, \$c);
    return $c;
}

sub gitolite_repo_config {
    my ($r, $repo) = @_;
    my $vars = {
        r      => $r,
        repo   => $repo,
        config => $config,
    };
    return process_tmpl($r->{repos}{$repo}{gl_template}, 'gl', $vars);
}

sub gitolite_group_config {
    my ($r, $group) = @_;
    my $vars = {
        r      => $r,
        group  => $group,
        config => $config,
    };
    return process_tmpl('group', 'gl', $vars);
}

sub gitolite_config {
    my ($r) = @_;
    my (@repos, @groups);
    @repos = map { gitolite_repo_config($r, $_) } sort keys %{$r->{repos}};
    @groups = map { gitolite_group_config($r, $_) } sort keys %{$r->{groups}};
    return join("\n", @groups, @repos);
}

sub update_gitolite_keydir {
    my ($r) = @_;
    opendir(my $dh, $config->{pubkey_dir})
        || die "Error opening $config->{include_dir}: $!";
    my @files = grep { ! m/^\./ } readdir($dh);
    closedir $dh;
    my %users_old;
    @users_old{@files} = map { read_file("$config->{pubkey_dir}/$_") } @files;
    my %users_new;
    foreach my $u (keys %{$r->{users}}) {
        my $i = 0;
        foreach my $key (@{$r->{users}{$u}}) {
            next unless $key;
            $users_new{"$u\@$i.pub"} = $key;
            $i++;
        }
    }
    foreach my $file (keys %users_old) {
        if (!$users_new{$file}) {
            print "Removing $file\n";
            unlink "$config->{pubkey_dir}/$file";
            $r->{keydir_changed} = 1;
        }
    }
    foreach my $file (keys %users_new) {
        if (!$users_old{$file} || chomp $users_old{$file} ne chomp $users_new{$file}) {
            print "Writing $file\n";
            write_file("$config->{pubkey_dir}/$file", $users_new{$file});
            $r->{keydir_changed} = 1;
        }
    }
}

sub update_gitolite_config {
    my ($r) = @_;
    my $oldconf = -f $config->{gitolite_config} 
                ? read_file($config->{gitolite_config}) : '';
    my $newconf = gitolite_config($r);
    if ($oldconf ne $newconf) {
        write_file($config->{gitolite_config}, $newconf);
        $r->{glconf_changed} = 1;
    }
}

sub run_gitolite {
    my ($r) = @_;
    if ($config->{run_gitolite} && $config->{run_gitolite} eq 'yes'
        && ($r->{keydir_changed} || $r->{glconf_changed})) {
        system('gitolite', 'compile');
        system('gitolite', 'setup', '--hooks-only');
        system('gitolite', 'trigger', 'POST_COMPILE');
    }
}

1;