Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- #
- # Creates a .repositoryrc if there isn't one already in the current working directory and
- # updates Repositories table with the already existing or created .repositoryrc info.
- use DBI;
- use Cwd;
- use File::Basename;
- use File::Spec;
- use Sys::Hostname;
- use List::MoreUtils qw(:all);
- use File::Grep qw( fgrep fmap fdo );
- sub identify_hostname;
- sub identify_admin;
- sub find_rc_file;
- sub get_id_from_DB;
- sub create_new_rc_file;
- my $DB = 'DBI:mysql:Repository:localhost';
- my $USER = 'wwwrun';
- my $PASSWORD = 'SeNh4Z';
- my $RC_FILE = '.repositoryrc';
- my $HOSTNAME = 'luist';
- my $ADMIN_GROUP = 'grouptest';
- my $dbh = DBI->connect($DB, $USER, $PASSWORD)
- or die "Couldn't connect to $DB\n";
- my $id_number = 0;
- identify_hostname();
- identify_admin();
- unless (find_rc_file($dbh)) {
- # Se ainda não existe arquivo .repositoryrc
- #Pega um Id do banco de dados
- $id_number = get_id_from_DB($dbh);
- # Cria um novo .repositoryrc com o Id escolhido
- create_new_rc_file($id_number);
- }
- $dbh->disconnect;
- exit;
- sub identify_hostname {
- my $this_host = hostname;
- if ($this_host ne $HOSTNAME) {
- die "${this_host}: Invalid hostname.\n";
- }
- return 1;
- }
- sub identify_admin {
- my $login = getlogin();
- my ($name, $passwd, $gid, $members) = getgrnam($ADMIN_GROUP);
- #print "Name: ${name}\nPasswd: ${passwd}\nGID: ${gid}\nMembers ${members}\n";
- unless (defined($name)) {
- die "${ADMIN_GROUP}: Inexistent group.\n";
- }
- my @member_list = split(/ /,$members);
- if(grep { $_ eq $login } @member_list) {
- #print "Login: ${login}\n";
- return 1;
- }
- else {
- die "${login}: Permission denied for this user.\n";
- }
- }
- sub find_rc_file {
- my ($my_dbh, $my_id_number) = @_;
- my ($root_dir) = getcwd; #'/work/repo/smeserver/8/updates';
- my $file = File::Spec->catfile( $root_dir, $RC_FILE );
- open my $rc_in, '<', $file or do {
- return 0;
- };
- return 1;
- }
- sub get_id_from_DB {
- my ($my_dbh) = @_;
- my ($sth, $id, @id_list, $name, @name_list);
- my $statement = "SELECT `Id`,`Name` FROM Repositories";
- unless ($sth = $my_dbh->prepare($statement)) {
- print STDERR __LINE__.": Can't prepare \"$statement\": ".$my_dbh->errstr."\n";
- $my_dbh->disconnect;
- exit 2;
- }
- unless ($sth->execute()) {
- print STDERR __LINE__.": Can't execute \"$statement\": ".$my_dbh->errstr."\n";
- $dbh->disconnect;
- exit 2;
- }
- while (($id ,$name) = $sth->fetchrow()) {
- push(@id_list, $id);
- push(@name_list, $name);
- }
- unless (@id_list) {
- die "Repositories: Table is empty. Can't create .repositoryrc.\n";
- }
- print "Creating .repositoryrc for: $ARGV[0]\n";
- print "Listing supported repositories...\n";
- print "|-----------------------------|\n";
- print "| Id Name |\n";
- print "|-----------------------------|\n";
- #Print arrays as columns
- my $list = each_array(@id_list, @name_list);
- while (my ($id_column, $name_column) = $list->()) {
- printf "| %d % -23s|\n", ${id_column}, ${name_column};
- }
- print "|-----------------------------|\n";
- while(1) {
- print "Choose one by Id, or enter 0 to cancel: ";
- my $input_id = <STDIN>;
- chomp($input_id);
- if ($input_id eq 0) {
- die "Canceled by user. Exiting...\n";
- }
- if (grep { $_ eq $input_id } @id_list) {
- return $input_id;
- }
- else {
- print "Invalid Id. Try again...\n\n";
- }
- }
- }
- sub create_new_rc_file {
- my ($my_id_number) = @_;
- my ($root_dir) = getcwd; #'/work/repo/smeserver/8/updates';
- my $file = File::Spec->catfile( $root_dir, $RC_FILE );
- open my $rc_out, '>', $file or do {
- $dbh->disconnect;
- die "$0: $file: $!\n"
- };
- print $rc_out "repository=${my_id_number}\n";
- #print $rc_out "local=1\n";
- #print $rc_out "exclude=\\.(patch|delta)\\.rpm\n";
- #print $rc_out "ignore_mismatch_name=\\.nosrc\\.rpm\$\n";
- close($rc_out);
- print "${file} created.\n";
- }
Add Comment
Please, Sign In to add comment