diff options
-rwxr-xr-x | git-ro-daemon.csh | 3 | ||||
-rwxr-xr-x | gitjoe-do.pl | 65 | ||||
-rwxr-xr-x | src/addsshkey.pl | 31 | ||||
-rwxr-xr-x | src/adduser.pl | 55 | ||||
-rwxr-xr-x | src/chdesc.pl | 37 | ||||
-rwxr-xr-x | src/chowner.pl | 37 | ||||
-rwxr-xr-x | src/chstate.pl | 44 | ||||
-rwxr-xr-x | src/mvrepo.pl | 38 | ||||
-rwxr-xr-x | src/newrepo.pl | 76 | ||||
-rwxr-xr-x | src/rmrepo.pl | 36 | ||||
-rwxr-xr-x | src/rmuser.pl | 31 |
11 files changed, 453 insertions, 0 deletions
diff --git a/git-ro-daemon.csh b/git-ro-daemon.csh new file mode 100755 index 0000000..162fb84 --- /dev/null +++ b/git-ro-daemon.csh @@ -0,0 +1,3 @@ +#!/bin/csh + +exec git daemon --reuseaddr --base-path=/usr/local/git /usr/local/git & diff --git a/gitjoe-do.pl b/gitjoe-do.pl new file mode 100755 index 0000000..7c17617 --- /dev/null +++ b/gitjoe-do.pl @@ -0,0 +1,65 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; + +use constant SCRIPTS_DIR => '/root/bin/gitjoe/'; +use constant SSH_BOY => 'root@jozanofastora.xyz'; + +sub main { + my $argc = $#ARGV + 1; + my $called_script = ''; + my $argv_line = ''; + if ( + $ARGV[0] eq 'addsshkey' || + $ARGV[0] eq 'newuser' || + $ARGV[0] eq 'rmuser' || + $ARGV[0] eq 'chdesc' || + $ARGV[0] eq 'chowner' || + $ARGV[0] eq 'chstate' || + $ARGV[0] eq 'newrepo' || + $ARGV[0] eq 'rmrepo' || + $ARGV[0] eq 'mvrepo' + ) { + $called_script = SCRIPTS_DIR . 'src/' . $ARGV[0] . '.pl'; + } + else { + print colored("Failed!\n", 'bold red') + . colored($ARGV[0], 'bold yellow') + . ": unknown script. Known scripts are:\n" + . colored("addsshkey\n", 'bold green') + . colored("newuser\n", 'bold green') + . colored("rmuser\n", 'bold green') + . colored("chdesc\n", 'bold green') + . colored("chowner\n", 'bold green') + . colored("chstate\n", 'bold green') + . colored("newrepo\n", 'bold green') + . colored("rmrepo\n", 'bold green') + . colored("mvrepo\n", 'bold green'); + exit 2; + } + print "Calling " . colored($called_script, 'bold green') . " via " . colored(SSH_BOY, 'bold magenta') . ".\n"; + if ($argc > 1) { + print "Arguments:\n"; + my $i = 1; + while ($i < $argc) { + $argv_line = $argv_line . ' "' . $ARGV[$i] . '"'; + print colored($ARGV[$i], 'bold yellow') . "\n"; + $i += 1; + } + } + system( + 'ssh ' . SSH_BOY . " << EOF +" . $called_script . $argv_line . " +exit +EOF +" + ); + print "Done.\n"; + exit; +} + +main(); + +__END__ diff --git a/src/addsshkey.pl b/src/addsshkey.pl new file mode 100755 index 0000000..431ce7d --- /dev/null +++ b/src/addsshkey.pl @@ -0,0 +1,31 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 2) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 2 needed " + . colored("[user - ssh public key]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $sshkey = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty "; + $sshkey = $sshkey . $ARGV[1]; + my $home_dir = HOME_DIR . $usr . '/'; + open(my $fh, '>>:encoding(UTF-8)', $home_dir . '.ssh/authorized_keys'); + print $fh "$sshkey\n"; + close($fh); + print "Added new ssh key for user " . colored($usr, 'bold green') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/adduser.pl b/src/adduser.pl new file mode 100755 index 0000000..4e0746d --- /dev/null +++ b/src/adduser.pl @@ -0,0 +1,55 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 2) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 2 needed " + . colored("[user - ssh public key]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $sshkey = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty "; + $sshkey = $sshkey . $ARGV[1]; + my $home_dir = HOME_DIR . $usr . '/'; + system( + "adduser << EOF +" . $usr . " + + + + + +git-shell +" . HOME_DIR . $usr . " + + + +yes + +yes +no +EOF" + ); + my (undef, undef, $uid, $gid) = getpwnam($usr); + mkdir $home_dir . '.ssh/', 0700; + chown $uid, $gid, $home_dir . '.ssh/'; + open(my $fh, '>:encoding(UTF-8)', $home_dir . '.ssh/authorized_keys'); + print $fh $sshkey . "\n"; + close($fh); + chown $uid, $gid, $home_dir . '.ssh/authorized_keys'; + chmod 0600, $home_dir . '.ssh/authorized_keys'; + print "Created new git user " . colored($usr, 'bold green') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/chdesc.pl b/src/chdesc.pl new file mode 100755 index 0000000..b0c25b7 --- /dev/null +++ b/src/chdesc.pl @@ -0,0 +1,37 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 3) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 3 needed " + . colored("[user - reponame - new description]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $repo = $ARGV[1]; + my $desc = $ARGV[2]; + my $home_dir = HOME_DIR . $usr . '/'; + if (substr($repo, -4) ne '.git') { + $repo = $repo . '.git'; + } + $repo = $repo . '/'; + open(my $desc_fh, '>:encoding(UTF-8)', $home_dir . $repo . 'description'); + print $desc_fh $desc; + close($desc_fh); + substr($repo, -1) = ""; + print "Changed git repository " . colored($repo, 'bold green') . " description for user " . colored($usr, 'bold') . ".\n" + . "New description: ". colored($desc, 'bold green') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/chowner.pl b/src/chowner.pl new file mode 100755 index 0000000..f3b1218 --- /dev/null +++ b/src/chowner.pl @@ -0,0 +1,37 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 3) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 3 needed " + . colored("[user - reponame - new owner]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $repo = $ARGV[1]; + my $owner = $ARGV[2]; + my $home_dir = HOME_DIR . $usr . '/'; + if (substr($repo, -4) ne '.git') { + $repo = $repo . '.git'; + } + $repo = $repo . '/'; + open(my $owner_fh, '>:encoding(utf-8)', $home_dir . $repo . 'owner'); + print $owner_fh $owner; + close($owner_fh); + substr($repo, -1) = ""; + print "Changed git repository " . colored($repo, 'bold green') . colored(" owner", 'bold') . " for user " . colored($usr, 'bold green') . ".\n" + . "New owner: ". colored($owner, 'bold green') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/chstate.pl b/src/chstate.pl new file mode 100755 index 0000000..1411e99 --- /dev/null +++ b/src/chstate.pl @@ -0,0 +1,44 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 2) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 2 needed " + . colored("[user - reponame]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $repo = $ARGV[1]; + my $home_dir = HOME_DIR . $usr . '/'; + if (substr($repo, -4) ne '.git') { + $repo = $repo . '.git'; + } + my $state; + if (-e $home_dir . $repo . '/git-daemon-export-ok') { + unlink($home_dir . $repo . '/git-daemon-export-ok'); + $state = 'private'; + } + else { + open(my $fh, '>', $home_dir . $repo . '/git-daemon-export-ok'); + close($fh); + $state = 'public'; + my (undef, undef, $uid, $gid) = getpwnam($usr); + chown $uid, $gid, $home_dir . $repo . '/git-daemon-export-ok'; + } + print "Changed git repository " . colored($repo, 'bold green') + . " for user " . colored($usr, 'bold green') + . colored(' visibility state', 'bold') . ' to '. colored($state, 'bold green') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/mvrepo.pl b/src/mvrepo.pl new file mode 100755 index 0000000..968fdbc --- /dev/null +++ b/src/mvrepo.pl @@ -0,0 +1,38 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use File::Copy; +use Term::ANSIColor; + +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 3) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 3 needed " + . colored("[user - reponame - new name]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $repo = $ARGV[1]; + my $newname = $ARGV[2]; + my $home_dir = HOME_DIR . $usr . '/'; + if (substr($repo, -4) ne '.git') { + $repo .= '.git'; + } + if (substr($newname, -4) ne '.git') { + $newname .= '.git'; + } + move($home_dir . $repo, $home_dir . $newname); + print "Changed git repository " . colored($repo, 'bold green') + . " name to " . colored($newname, 'bold green') . " for user " . colored($usr, 'bold') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/newrepo.pl b/src/newrepo.pl new file mode 100755 index 0000000..dc8041b --- /dev/null +++ b/src/newrepo.pl @@ -0,0 +1,76 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use File::Find; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 2) { + print colored("Failed!\n", 'bold red') + . "Missing argument, at least 2 needed " + . colored("[user - reponame - (description)]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $repo = $ARGV[1]; + my $desc = ""; + if ($argc >= 3) { + $desc = $ARGV[2]; + } + my $home_dir = HOME_DIR . $usr . '/'; + if (substr($repo, -4) ne '.git') { + $repo = $repo . '.git'; + } + $repo = $repo . '/'; + mkdir $home_dir . $repo, 0755; + system( + '/usr/local/bin/git', + '-C', + $home_dir . $repo, + 'init', + '--bare' + ); + my (undef, undef, $uid, $gid) = getpwnam($usr); + find( + sub { + chown $uid, $gid, $_; + }, + $home_dir . $repo + ); + system( + '/usr/bin/touch', + $home_dir . $repo . 'git-daemon-export-ok' + ); + chown $uid, $gid, $home_dir . $repo . 'git-daemon-export-ok'; + open(my $owner_fh, '>:encoding(UTF-8)', $home_dir . $repo . 'owner'); + print $owner_fh $usr; + close($owner_fh); + open(my $url_fh, '>:encoding(UTF-8)', $home_dir . $repo . 'url'); + substr($repo, -1) = ""; + print $url_fh 'git://gitjoe.xyz/' . $usr . '/' . $repo; + close($url_fh); + $repo = $repo . '/'; + open(my $desc_fh, '>:encoding(UTF-8)', $home_dir . $repo . 'description'); + if ($argc >= 3) { + print $desc_fh $desc; + } + else { + print $desc_fh 'No description yet'; + } + close($desc_fh); + chown $uid, $gid, $home_dir . $repo . 'description'; + substr($repo, -1) = ""; + print "Created git repository " . colored($repo, 'bold green') . " for user " . colored($usr, 'bold') . ".\n"; + print "Remote url: " . colored($usr . '@gitjoe.xyz:' . $repo, 'bold green') . "\n" + . "Public clone url: " . colored('git://gitjoe.xyz/' . $usr . '/' . $repo, 'bold green') . "\n"; + exit; +} + +main(); + +__END__ diff --git a/src/rmrepo.pl b/src/rmrepo.pl new file mode 100755 index 0000000..b028bb4 --- /dev/null +++ b/src/rmrepo.pl @@ -0,0 +1,36 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; +use constant HOME_DIR => '/usr/local/git/'; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 2) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 2 needed " + . colored("[user - reponame]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + my $repo = $ARGV[1]; + my $home_dir = HOME_DIR . $usr . '/'; + if (substr($repo, -4) ne '.git') { + $repo = $repo . '.git'; + } + $repo = $repo . '/'; + system( + '/bin/rm', + '-rfv', + $home_dir . $repo + ); + print "Deleted git repository " . colored($repo, 'bold yellow') . " for user " . colored($usr, 'bold') . ".\n"; + exit; +} + +main(); + +__END__ diff --git a/src/rmuser.pl b/src/rmuser.pl new file mode 100755 index 0000000..9753950 --- /dev/null +++ b/src/rmuser.pl @@ -0,0 +1,31 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Term::ANSIColor; + +sub main +{ + my $argc = $#ARGV + 1; + if ($argc < 1) { + print colored("Failed!\n", 'bold red') + . "Missing argument, 1 needed " + . colored("[user]", 'bold') + . "\n"; + exit 1; + } + my $usr = $ARGV[0]; + system( + "rmuser << EOF +" . $usr . " +y +y +EOF" + ); + print "Removed git user " . colored($usr, 'bold yellow') . ".\n"; + exit; +} + +main(); + +__END__ |