App-Puppet-Environment-Updater

 view release on metacpan or  search on metacpan

lib/App/Puppet/Environment/Updater.pm  view on Meta::CPAN

			}
			$self->update_branch($branch);
		}

		$self->merge($self->get_from(), $self->get_environment());

		$self->update_submodules();

		$self->get_logger()->log(
			BOLD.GREEN.'Done. Please check the changes and '
				. '"git push '.$self->get_remote().' '.$self->get_environment().'"'
					.' them.'.RESET
		);
	}
	catch {
		my $error = $_;
		$error =~ s{^(?:\[.*\]\s)*}{}x; # Remove prefixes
		$self->get_logger()->log(BOLD.RED.'Failed to update. Error was: '.RESET.$error);
	};

	return;
}



sub get_local_branches {
	my ($self) = @_;

	my @branches;
	for my $branch ($self->get_git()->branch()) {
		$branch =~ s{^[*\s]*}{}x;
		push @branches, $branch;
	}

	return @branches;
}



sub remote_branch_for {
	my ($self, $branch) = @_;

	return $self->get_remote().'/'.$branch;
}



sub create_and_switch_to_branch {
	my ($self, $branch) = @_;

	$self->get_proxy_logger(BLUE.'[create-branch] '.RESET)->log(
		'Creating local branch '.$branch.' from '.$self->remote_branch_for($branch).'...'
	);
	$self->get_git()->checkout('-b', $branch, $self->remote_branch_for($branch));

	return;
}



sub update_branch {
	my ($self, $branch) = @_;

	my $logger = $self->get_proxy_logger(YELLOW.'[update-branch] '.RESET);

	my $remote_branch = $self->remote_branch_for($branch);
	$logger->log(
		'Updating local branch '.$branch.' from '.$remote_branch.'...'
	);
	$self->get_git()->checkout($branch);
	try {
		$logger->log($self->get_git()->merge('--ff-only', $remote_branch));
	}
	catch {
		chomp;
		$logger->log_fatal(
			"$_ - does ".$remote_branch.' exist and is local branch '
				.$branch.' not diverged from it?'
		);
	};

	return;
}



sub merge {
	my ($self, $from, $to) = @_;

	my $logger = $self->get_proxy_logger(MAGENTA.'[merge] '.RESET);
	$logger->log('Merging '.$from.' into '.$to.'...');
	$self->get_git()->checkout($to);
	$logger->log($self->get_git()->merge('--no-ff', $from));
	return;
}



sub update_submodules {
	my ($self) = @_;

	my $workdir = pushd($self->get_workdir());
	my $logger = $self->get_proxy_logger(YELLOW.'[update-submodules] '.RESET);
	$logger->log('Updating submodules...');
	if (my @updated = $self->get_git()->submodule('update', '--init')) {
		$logger->log($_) for @updated;
	}
	else {
		$logger->log('No submodules to update.');
	}

	return;
}

__PACKAGE__->meta()->make_immutable();

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Puppet::Environment::Updater - Update a Puppet environment in a Git branch

=head1 VERSION

version 0.001002

=head1 SYNOPSIS

	use App::Puppet::Environment::Updater;

	App::Puppet::Environment::Updater->new_with_options()->run();

=head1 DESCRIPTION

App::Puppet::Environment::Updater is intended to update Puppet environments which
are in Git branches. There are many ways to organize a Puppet setup and Puppet
environments, and this application supports the following approach:

=over

=item *

There is one Git repository with four branches, each of which represents a
Puppet environment:

=over

=item *

C<development>

=item *



( run in 1.478 second using v1.01-cache-2.11-cpan-9581c071862 )