Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 6 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Showing with 211 additions and 35 deletions.
  1. +37 −23 lib/Gup.pm
  2. +113 −0 lib/Gup/Plugin/Sync/Mysql.pm
  3. +11 −3 lib/Gup/Plugin/Sync/Rsync.pm
  4. +17 −0 lib/Gup/Role/AfterSync.pm
  5. +17 −0 lib/Gup/Role/BeforeSync.pm
  6. +14 −1 lib/Gup/Role/Sync.pm
  7. +2 −8 t/base.t
View
60 lib/Gup.pm
@@ -39,8 +39,10 @@ has repo => (
ref $_[0] and ref $_[0] eq 'Git::Repository'
or die 'repo must be a Git::Repository object'
} ),
+ lazy => 1,
writer => 'set_repo',
predicate => 'has_repo',
+ builder => '_build_repo',
);
has repo_dir => (
@@ -53,7 +55,6 @@ has repo_dir => (
has source_dir => (
is => 'ro',
isa => Str,
- required => 1,
predicate => 'has_source_dir',
);
@@ -76,9 +77,16 @@ has plugin_objs => (
builder => '_build_plugin_objs',
);
+sub _build_repo {
+ my $self = shift;
+
+ Git::Repository->new( work_tree => $self->repo_dir )
+}
+
sub _build_repo_dir {
my $self = shift;
- return File::Spec->catdir( $self->repos_dir, $self->name );
+
+ File::Spec->catdir( $self->repos_dir, $self->name );
};
sub _build_plugin_objs {
@@ -92,13 +100,13 @@ sub _build_plugin_objs {
eval "use $class";
$@ and die "Failed loading plugin $class: $@\n";
- my @args = $self->plugin_args->{$plugin} ?
- @{ $self->plugin_args->{$plugin} } :
+ my %args = $self->plugin_args->{$plugin} ?
+ %{ $self->plugin_args->{$plugin} } :
();
push @plugins, $class->new(
gup => $self,
- @args,
+ %args,
);
}
@@ -119,12 +127,19 @@ sub sync_repo {
$self->has_source_dir or croak 'Must provide a source_dir';
- # find all plugins that use a role Sync
- # then run it
- # TODO: add BeforeSync, AfterSync
+ # Run method before_sync on all plunigs with BeforeSync role
+ $_->before_sync( $self->source_dir, $self->repo_dir )
+ foreach ( $self->find_plugins('-BeforeSync' ) );
+
+ # find all plugins that use a role Sync then run it
foreach my $plugin ( $self->find_plugins('-Sync' ) ) {
$plugin->sync( $self->source_dir, $self->repo_dir );
}
+
+ # Run method before_sync on all plunigs with AfterSync role
+ $_->after_sync() foreach ( $self->find_plugins('-AfterSync' ) );
+
+ $self;
}
# TODO: allow to control the git user and email for this
@@ -141,28 +156,27 @@ sub create_repo {
# init new repo
Git::Repository->run( init => $repo_dir );
- my $repo = Git::Repository->new( work_tree => $repo_dir );
- $repo->run( 'config', '--local', 'user.email', 'you@example.com' );
- $repo->run( 'config', '--local', 'user.name', 'Your Name' );
+ $self->repo->run( 'config', '--local', 'user.email', 'you@example.com' );
+ $self->repo->run( 'config', '--local', 'user.name', 'Your Name' );
# create HEAD and first commit
- $repo->run( 'symbolic-ref', 'HEAD', 'refs/heads/master' );
- $repo->run( commit => '--allow-empty', '-m', 'Initial commit' );
-
- $self->set_repo($repo);
+ $self->repo->run( 'symbolic-ref', 'HEAD', 'refs/heads/master' );
+ $self->repo->run( commit => '--allow-empty', '-m', 'Initial commit' );
- return $repo;
+ $self;
}
sub update_repo {
my $self = shift;
# Sync repo before
- $self->sync_repo or croak 'sync_repo failed';
-
+ $self->sync_repo;
+
# Commit updates
- return $self->commit_updates(@_);
+ $self->commit_updates(@_);
+
+ $self;
}
sub commit_updates {
@@ -175,13 +189,13 @@ sub commit_updates {
$opts{'message'} :
'Gup commit: ' . strftime "%Y/%m/%d - %H:%M", localtime;
- my $repo = $self->repo;
-
# add all
- $repo->run( 'add', '-A' );
+ $self->repo->run( 'add', '-A' );
# commit update
- return $self->repo->run( 'commit', '-a', '-m', $message );
+ $self->repo->run( 'commit', '-a', '-m', $message );
+
+ $self;
}
1;
View
113 lib/Gup/Plugin/Sync/Mysql.pm
@@ -0,0 +1,113 @@
+use strict;
+use warnings;
+package Gup::Plugin::Sync::Mysql;
+# ABSTRACT: Mysql sync plugin for Gup
+
+use Moo;
+use Carp;
+use Sub::Quote;
+use System::Command;
+use MooX::Types::MooseLike::Base qw/Str/;
+
+with 'Gup::Role::Sync';
+with 'Gup::Role::BeforeSync';
+
+has mysqldump_path => (
+ is => 'ro',
+ isa => Str,
+ default => quote_sub(q{'/usr/bin/mysqldump'}),
+);
+
+has mysqldump_user => (
+ is => 'ro',
+ isa => Str,
+ predicate => 'has_mysqldump_user',
+);
+
+has mysqldump_password => (
+ is => 'ro',
+ isa => Str,
+ predicate => 'has_mysqldump_password',
+);
+
+has mysqldump_databases => (
+ is => 'ro',
+ isa => Str,
+ predicate => 'has_mysqldump_databases',
+);
+
+has mysqldump_args => (
+ is => 'ro',
+ isa => Str,
+ predicate => 'has_mysqldump_args',
+);
+
+has remote_dump_path => (
+ is => 'ro',
+ isa => Str,
+ default => quote_sub(q{'dump.sql'}),
+);
+
+has user_at_host => (
+ is => 'ro',
+ isa => Str,
+ lazy => 1,
+ builder => '_build_user_at_host',
+);
+
+sub _build_user_at_host {
+ my $self = shift;
+ my $host = $self->host;
+ my $user = $self->username;
+
+ $self->has_host ? "$user\@$host" : '';
+}
+
+sub before_sync {
+ my $self = shift;
+ my $from = shift;
+ my $to = shift;
+ my $scp = $self->scp_path;
+ my $ssh = $self->ssh_path;
+ my $host = $self->user_at_host;
+ my $rpath = $from.'/'.$self->remote_dump_path;
+ my $mysqldump = $self->mysqldump_path;
+ my $args = $self->has_mysqldump_args ?
+ $self->mysqldump_args : '';
+ my $user = $self->has_mysqldump_user ?
+ '-u '.$self->mysqldump_user : '';
+ my $password = $self->has_mysqldump_password ?
+ '-p'.$self->mysqldump_password : '';
+ my $databases = $self->has_mysqldump_databases ?
+ '--databases '.$self->mysqldump_databases : '';
+
+ my $myslqdump_cmd = sprintf("'%s %s %s %s %s > %s'",
+ $mysqldump, $user, $password, $databases, $args, $rpath );
+
+ my $cmd = System::Command->new( $self->ssh_path, $host, $myslqdump_cmd );
+ $cmd->close;
+ $cmd->exit;
+}
+
+sub sync {
+ my $self = shift;
+ my $from = shift;
+ my $to = shift;
+ my $rpath = $self->remote_dump_path;
+ my $path = $self->user_at_host ne '' ?
+ $self->user_at_host.":$from/$rpath" : "$from/$rpath";
+
+ my $cmd = System::Command->new( $self->scp_path, $path, $to );
+ $cmd->close;
+
+ # return 1 for good, undef for rest
+ # TODO: we don't really document the errors
+ # should we call die/croak? should we return arrayref with it?
+ # what about stdout vs. stderr? tricky stuff...
+ return $cmd->exit == 0 ? 1 : undef;
+}
+
+1;
+
+__END__
+
View
14 lib/Gup/Plugin/Sync/Rsync.pm
@@ -7,13 +7,21 @@ use Moo;
use Carp;
use Sub::Quote;
use System::Command;
+use MooX::Types::MooseLike::Base qw/Str/;
# requires us to add sync() method which will be run
# XXX: for now provides username and host, which should be integrated here
with 'Gup::Role::Sync';
-has args => (
+has rsync_path => (
is => 'ro',
+ isa => Str,
+ default => quote_sub( q{'/usr/bin/rsync'} ),
+);
+
+has rsync_args => (
+ is => 'ro',
+ isa => Str,
default => quote_sub(q{'-acz'}),
);
@@ -32,8 +40,8 @@ sub sync {
my $path = $host ? "$user\@$host:$from/" : "$from/";
my $cmd = System::Command->new(
- 'rsync',
- $self->args,
+ $self->rsync_path,
+ $self->rsync_args,
$path,
$to,
'--quiet',
View
17 lib/Gup/Role/AfterSync.pm
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+package Gup::Role::AfterSync;
+# ABSTRACT: After sync role
+
+use English '-no_match_vars';
+use Moo::Role;
+use Sub::Quote;
+
+with 'Gup::Role::Plugin';
+
+requires 'after_sync';
+
+1;
+
+__END__
+
View
17 lib/Gup/Role/BeforeSync.pm
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+package Gup::Role::BeforeSync;
+# ABSTRACT: Before sync role
+
+use English '-no_match_vars';
+use Moo::Role;
+use Sub::Quote;
+
+with 'Gup::Role::Plugin';
+
+requires 'before_sync';
+
+1;
+
+__END__
+
View
15 lib/Gup/Role/Sync.pm
@@ -6,16 +6,30 @@ package Gup::Role::Sync;
use English '-no_match_vars';
use Moo::Role;
use Sub::Quote;
+use MooX::Types::MooseLike::Base qw/Str/;
requires 'sync';
with 'Gup::Role::Plugin';
+has ssh_path => (
+ is => 'ro',
+ isa => Str,
+ default => quote_sub( q{'/usr/bin/ssh'} ),
+);
+
+has scp_path => (
+ is => 'ro',
+ isa => Str,
+ default => quote_sub( q{'/usr/bin/scp'} ),
+);
+
has host => (
is => 'ro',
isa => quote_sub( q{
$_[0] =~ /^(?:[A-Za-z0-9_-]|\.)+$/ or die "Improper host: '$_[0]'\n";
} ),
+ predicate => 'has_host',
);
# these two could be separated at some point to Gup::Role::SyncUser
@@ -35,4 +49,3 @@ sub _build_username { getpwuid $REAL_USER_ID }
1;
__END__
-
View
10 t/base.t
@@ -4,7 +4,7 @@ use strict;
use warnings;
use Gup;
-use Test::More tests => 3;
+use Test::More tests => 2;
use Test::Fatal;
like(
@@ -13,14 +13,8 @@ like(
'Gup->new requires a name',
);
-like(
- exception { Gup->new( name => 'test' ) },
- qr/^Missing required arguments: source_dir/,
- 'Gup->new with name works',
-);
-
is(
- exception { Gup->new( name => 'test', source_dir => 'test' ) },
+ exception { Gup->new( name => 'test' ) },
undef,
'Gup->new with name works',
);

No commit comments for this range

Something went wrong with that request. Please try again.