diff options
author | Julian Blake Kongslie <jblake@omgwallhack.org> | 2010-07-07 15:44:46 -0700 |
---|---|---|
committer | Julian Blake Kongslie <jblake@omgwallhack.org> | 2010-07-07 15:44:46 -0700 |
commit | d809044c2f00d7f8358dc8013c4e6639760aba67 (patch) | |
tree | d15343ee934bc6b23a395046d35f7fb4e6131459 /usr/src/libpiny/lib/Piny/Repo.pm | |
parent | f5674a8b57fe48d470fae283ac3d3c259f8aa9c7 (diff) | |
download | piny-code-d809044c2f00d7f8358dc8013c4e6639760aba67.tar.gz piny-code-d809044c2f00d7f8358dc8013c4e6639760aba67.zip |
Revising newrepo to use libpiny.
Diffstat (limited to 'usr/src/libpiny/lib/Piny/Repo.pm')
-rw-r--r-- | usr/src/libpiny/lib/Piny/Repo.pm | 127 |
1 files changed, 106 insertions, 21 deletions
diff --git a/usr/src/libpiny/lib/Piny/Repo.pm b/usr/src/libpiny/lib/Piny/Repo.pm index 5ad7007..48690c4 100644 --- a/usr/src/libpiny/lib/Piny/Repo.pm +++ b/usr/src/libpiny/lib/Piny/Repo.pm @@ -7,11 +7,15 @@ use Moose; use Moose::Util::TypeConstraints; use File::Find qw( find ); +use File::Temp qw( ); +use IO::Dir qw( ); use IkiWiki::FakeSetup qw( readSetup writeSetup ); +use Piny::Environment; use Piny::Group; use Piny::User; +use Piny::User::IkiWiki; # Types @@ -27,6 +31,8 @@ subtype 'SimpleText' => message { 'That description is not in the correct format for a piny repo.' } ; +Moose::Util::TypeConstraints::export_type_constraints_as_functions; + # Attributes has 'name' => @@ -239,6 +245,103 @@ sub all_repos { return @ret; }; +sub create { + my ( $class, $name, $description ) = @_; + + my $user = Piny::Environment->new->user; + + Reponame->assert_valid( $name ); + SimpleText->assert_valid( $description ); + + my $repo = $class->new( "name" => $name ); + + mkdir( $repo->path ) or die "The repo $name appears to already exist! ($!)"; + + system( "/usr/sbin/adduser", "--quiet", "--system", "--group", "--gecos", $name, "ikiwiki-$name" ) and die "Could not create ikiwiki user!"; + + my $ikiuser = Piny::User::IkiWiki->new( "name" => "ikiwiki-$name" ); + + system( "/usr/sbin/addgroup", "--quiet", "git-$name" ) and die "Could not create repo group!"; + + my $group = Piny::Group->new( "name" => "git-$name" ); + + system( "/usr/sbin/adduser", "--quiet", $user->name, $group->name ) and die "Could not add you to the repo group!"; + system( "/usr/sbin/adduser", "--quiet", $ikiuser->name, $group->name ) and die "Could not add ikiwiki user to the repo group!"; + + $ENV{"GIT_DIR"} = $repo->path; + system( "/usr/bin/git", "init", "--template=/srv/git-template.git", "--quiet", "--shared" ) and die "Could not initialize git repo!"; + delete $ENV{"GIT_DIR"}; + + $repo->set_description( $description ); + + open( TOUCH, ">", $repo->path . "/git-daemon-export-ok" ) or die "Could not touch git-daemon-export-ok for repo: $!"; + close( TOUCH ); + + system( "/bin/chown", "-R", $user->name . "." . $group->name, $repo->path ) and die "Could not change ownership of git repo!"; + system( "/bin/chown", "-R", $ikiuser->name . "." . $ikiuser->name, $repo->path . "/hooks" ) and die "Could not change ownership of git hooks!"; + + open( SETUP, ">", "/etc/ikiwiki/piny/" . $repo->name . ".setup" ) or die "Could not open new ikiwiki setup file: $!"; + print SETUP $repo->ikiwiki_setup; + close( SETUP ) or die "Could not close new ikiwiki setup file: $!"; + + open( APACHE, ">", "/etc/apache2/piny-available/" . $repo->name ) or die "Could not open new apache config: $!"; + print APACHE $repo->apache_config; + close( APACHE ) or die "Could not close new apache config: $!"; + + symlink( "/etc/apache2/piny-available/" . $repo->name, "/etc/apache2/piny-enabled/" . $repo->name ) or die "Could not symlink apache config: $!"; + + system( "/etc/init.d/apache2", "reload" ) and die "Could not reload apache config!"; + + system( "/usr/bin/git", "clone", "--quiet", $repo->path, $repo->ikiwiki_srcdir ) and die "Could not clone repo to ikiwiki srcdir!"; + + mkdir( $repo->ikiwiki_destdir ) or die "Could not create ikiwiki destdir: $!"; + mkdir( $repo->secure_path ) or die "Could not create secure dir: $!"; + + system( "/bin/chown", "-R", $ikiuser->name . "." . $ikiuser->name, $repo->ikiwiki_srcdir, $repo->ikiwiki_destdir, $repo->secure_path ) and die "Could not change ownership of ikiwiki directories!"; + + open( WIKILIST, ">", "/etc/ikiwiki/wikilist.d/" . $repo->name ) or die "Could not create wikilist.d file: $!"; + print WIKILIST $ikiuser->name . " /etc/ikiwiki/piny/" . $repo->name . ".setup\n"; + close( WIKILIST ) or die "Could not close wikilist.d file: $!"; + + my $temp = File::Temp->new( ) or die "Could not create temporary file: $!"; + $temp->unlink_on_destroy( 0 ); + + my $dh = IO::Dir->new( "/etc/ikiwiki/wikilist.d" ) or die "Could not open wikilist.d directory: $!"; + while ( defined ( my $entry = $dh->read ) ) { + next if ( $entry =~ /^\./ ); + open( FILE, "<", "/etc/ikiwiki/wikilist.d/" . $entry ) or die "Could not open wikilist.d entry $entry: $!"; + print $temp <FILE>; + close( FILE ) or die "Could not close wikilist.d entry $entry: $!"; + }; + + $temp->close or die "Could not close new wikilist: $!"; + + rename( $temp->filename, "/etc/ikiwiki/wikilist" ) or die "Could not rename over old wikilist: $!"; + + open( CGITLIST, ">", "/etc/cgitrc.d/" . $repo->name ) or die "Could not create cgitrc.d file: $!"; + print CGITLIST "repo.url=" . $repo->name . "\nrepo.path=" . $repo->path . "\nrepo.desc=" . $repo->description . "\nrepo.owner=" . $repo->owner->email . "\n\n"; + close( CGITLIST ) or die "Could not close cgitrc.d file: $!"; + + $temp = File::Temp->new( ) or die "Could not create temporary file: $!"; + $temp->unlink_on_destroy( 0 ); + + $dh = IO::Dir->new( "/etc/cgitrc.d" ) or die "Could not open cgitrc.d directory: $!"; + while ( defined ( my $entry = $dh->read ) ) { + next if ( $entry =! /^\./ ); + open( FILE, "<", "/etc/cgitrc.d/" . $entry ) or die "Could not open cgitrc.d entry $entry: $!"; + print $temp <FILE>; + close( FILE ) or die "Could not close cgitrc.d entry $entry: $!"; + }; + + $temp->close or die "Could not close new cgitrc: $!"; + + rename( $temp->filename, "/etc/cgitrepos" ) or die "Could not rename over old cgitrc: $!"; + + system( "/usr/bin/sudo", "-u", $ikiuser->name, "/usr/bin/ikiwiki", "--setup", "/etc/ikiwiki/piny/" . $repo->name . ".setup" ) and die "Could not do initial compile of ikiwiki!"; + + return $repo; +}; + # Builder methods # If constructed with just one argument, then treat it as a repo name. @@ -261,13 +364,7 @@ sub _build_group { sub _build_path { my ( $s ) = @_; - my $dir = "/srv/git/" . $s->name . ".git"; - - if ( -d $dir ) { - return $dir; - } else { - die "Expected repo $dir does not exist!"; - }; + return "/srv/git/" . $s->name . ".git"; }; sub _build_description { @@ -345,25 +442,13 @@ sub _build_ikiwiki_setup { sub _build_ikiwiki_destdir { my ( $s ) = @_; - my $dir = "/srv/www/piny.be/" . $s->name; - - if ( -d $dir ) { - return $dir; - } else { - die "Expected destdir $dir does not exist!"; - }; + return "/srv/www/piny.be/" . $s->name; }; sub _build_ikiwiki_srcdir { my ( $s ) = @_; - my $dir = "/srv/ikiwiki/" . $s->name; - - if ( -d $dir ) { - return $dir; - } else { - die "Expected srcdir $dir does not exist!"; - }; + return "/srv/ikiwiki/" . $s->name; }; sub _build_ikiwiki_url { |