pass slice group name to new
[cloudstore.git] / lib / CloudStore / API.pm
1 package CloudStore::API;
2 use warnings;
3 use strict;
4 use autodie;
5
6 use lib 'lib';
7 use base qw(CloudStore::Gearman);
8
9 use File::Path qw(make_path remove_tree);
10 use File::Find;
11 use Data::Dump qw(dump);
12
13 sub new {
14         my ($class,$group) = @_;
15
16         my ( undef, $dir, $port, undef ) = getgrnam($group) || die "can't find group $group: $!";
17         my $self = {
18                 passwd => '/var/lib/extrausers/passwd',
19                 PORT => $port,
20                 SLICE => $dir,
21         };
22         bless $self, $class;
23
24         $self->{md5} = $self->user_info('md5');
25
26         return $self;
27 }
28
29 sub user_info {
30         my ($self,$login) = @_;
31
32         my @n = qw/ login passwd uid gid quota comment gecos dir shell expire /;
33         my @p = $login =~ m/^\d+$/ ? getpwuid $login : getpwnam $login;
34         my $user;
35         $user->{$_} = shift @p foreach @n;
36         return $user;
37 }
38
39 sub create_user {
40         my ( $self, $new_email, $new_passwd, $new_quota ) = @_;
41
42         my $max_uid = 0;
43         my $found = 0;
44
45         open(my $fh, '<', $self->{passwd});
46         while(<$fh>) {
47                 my ( $login, $passwd, $uid, $gid, $email, $dir, $shell ) = split(/:/,$_);
48                 $max_uid = $uid if $uid > $max_uid;
49                 $found = $uid if $email eq $new_email;
50         }
51         close($fh);
52
53         if ( ! $found ) {
54                 $max_uid++;
55                 my $dir = "$self->{SLICE}/$max_uid";
56                 warn "# create_user $new_email $new_quota = $max_uid $dir";
57                 open(my $fh, '>>', $self->{passwd});
58                 print $fh "u$max_uid:$new_passwd:$max_uid:$self->{PORT}:$new_email:$dir:/bin/true\n";
59                 close($fh);
60                 $found = $max_uid;
61
62                 mkdir $dir;
63                 chown $max_uid, $self->{PORT}, $dir;
64
65         }
66
67         # FIXME update quota only on create?
68         $self->gearman_do( 'narada_s1_quota_set' => "$found $new_quota" );
69
70         return $found;
71 }
72
73 sub mkbasepath {
74         my ($path,$opts) = @_;
75         $path =~ s{/[^/]+$}{};
76         make_path $path unless -d $path;
77 }
78
79 sub user_dir {
80         my ( $self,$user, $dir ) = @_;
81         $user = $self->user_info($user) unless ref $user eq 'HASH';
82         my $path;
83         if ( exists $user->{dir} ) {
84                 $path = $user->{dir} . '/' . $dir;
85         } else {
86                 die "no dir in ", dump $user;
87         }
88         $path =~ s{//+}{/}g;
89 #       warn "## user_dir $path";
90         return $path;
91 }
92
93 sub append {
94         my $self = shift @_;
95         my $user = shift @_;
96         my $path = $self->user_dir( $user => '.log');
97         my $line = join('#',@_);
98         open(my $fh, '>>', $path);
99         print $fh "$line\n";
100         close $fh;
101         warn "## $path $line\n";
102         $user = $self->user_info($user) unless ref $user eq 'HASH';
103 }
104
105 sub usage {
106         my ( $self, $user ) = @_;
107         $user = $self->user_info($user) unless ref $user eq 'HASH';
108         my $path = $self->user_dir( $user => '.log');
109         my $sum;
110         open(my $fh, '<', $path);
111         while(<$fh>) {
112                 chomp;
113                 my @v = split(/#/,$_);
114                 $sum->{ $v[0] } += $v[1];
115                 $sum->{_usage}  += $v[1];
116         }
117         my ( $usage, $quota ) = split(/ /,
118                 $self->gearman_do( 'narada_s1_quota_get' => $user->{uid} )
119         );
120         $sum->{_usage} += $usage;
121         $sum->{_quota} = $quota;
122         warn "## usage ",dump($user, $sum), $/;
123         return $sum;
124 }
125
126 sub send_file {
127         my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_;
128
129         my $f = $self->user_info($f_uid);
130         my $t = $self->user_info($t_uid);
131
132         my $f_full = "$f->{dir}/$f_path";
133         my $t_full = "$t->{dir}/$t_path";
134
135         mkbasepath $t_full, { uid => $t->{uid} };
136
137         my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $f_full;
138         if ( $uid == $f->{uid} ) {
139                 warn "# send_file - move $f_uid $f_path to pool\n";
140                 chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
141                 chmod oct("0444"), $f_full;
142                 $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
143         } elsif ( $uid == $self->{md5}->{uid} ) {
144                 warn "# send_file - shared $f_full\n";
145         }
146
147         $self->delete( $t, $t_path ) if -e $t_full;
148
149         link $f_full, $t_full; 
150         $self->append( $t, 'recv', -s $t_full, $f->{uid}, $t_path );
151 }
152
153 sub rename_file {
154         my ( $self, $user, $from, $to ) = @_;
155         $user = $self->user_info($user) unless ref $user eq 'HASH';
156
157         $self->append( $user, 'rename', $from, $to );
158 }
159
160
161 sub delete {
162         my ( $self, $user, $path ) = @_;
163         $user = $self->user_info($user) unless ref $user eq 'HASH';
164
165         my $deleted_size = 0;
166         my $full_path = "$user->{dir}/$path";
167
168         if ( -d $full_path ) {
169
170                 find({ 
171                 no_chdir => 1,
172                 wanted => sub {
173                         return if -d $_;
174                         my ($uid,$size) = (stat($_))[4,7];
175                         warn "## find $uid $size $_\n";
176                         if ( $uid == $self->{md5}->{uid} ) {
177                                 $deleted_size += $size;
178                         }
179                 }}, $full_path);
180
181                 remove_tree $full_path;
182         } else {
183                 $deleted_size += -s $full_path;
184                 unlink $full_path;
185         }
186
187         warn "delete $deleted_size bytes shared\n";
188
189         $self->append( $user, 'delete', -$deleted_size, $user->{uid}, $path );
190 }
191
192 1;