Guest User

Untitled

a guest
Jun 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # Creates a .repositoryrc if there isn't one already in the current working directory and
  4. # updates Repositories table with the already existing or created .repositoryrc info.
  5.  
  6. use DBI;
  7. use Cwd;
  8. use File::Basename;
  9. use File::Spec;
  10. use Sys::Hostname;
  11. use List::MoreUtils qw(:all);
  12. use File::Grep qw( fgrep fmap fdo );
  13.  
  14. sub identify_hostname;
  15. sub identify_admin;
  16. sub find_rc_file;
  17. sub get_id_from_DB;
  18. sub create_new_rc_file;
  19.  
  20. my $DB = 'DBI:mysql:Repository:localhost';
  21. my $USER = 'wwwrun';
  22. my $PASSWORD = 'SeNh4Z';
  23.  
  24. my $RC_FILE = '.repositoryrc';
  25. my $HOSTNAME = 'luist';
  26. my $ADMIN_GROUP = 'grouptest';
  27.  
  28. my $dbh = DBI->connect($DB, $USER, $PASSWORD)
  29. or die "Couldn't connect to $DB\n";
  30.  
  31. my $id_number = 0;
  32.  
  33. identify_hostname();
  34. identify_admin();
  35.  
  36. unless (find_rc_file($dbh)) {
  37. # Se ainda não existe arquivo .repositoryrc
  38.  
  39. #Pega um Id do banco de dados
  40. $id_number = get_id_from_DB($dbh);
  41.  
  42. # Cria um novo .repositoryrc com o Id escolhido
  43. create_new_rc_file($id_number);
  44. }
  45.  
  46. $dbh->disconnect;
  47. exit;
  48.  
  49. sub identify_hostname {
  50. my $this_host = hostname;
  51.  
  52. if ($this_host ne $HOSTNAME) {
  53. die "${this_host}: Invalid hostname.\n";
  54. }
  55. return 1;
  56. }
  57.  
  58. sub identify_admin {
  59. my $login = getlogin();
  60. my ($name, $passwd, $gid, $members) = getgrnam($ADMIN_GROUP);
  61. #print "Name: ${name}\nPasswd: ${passwd}\nGID: ${gid}\nMembers ${members}\n";
  62.  
  63. unless (defined($name)) {
  64. die "${ADMIN_GROUP}: Inexistent group.\n";
  65. }
  66.  
  67. my @member_list = split(/ /,$members);
  68. if(grep { $_ eq $login } @member_list) {
  69. #print "Login: ${login}\n";
  70. return 1;
  71. }
  72. else {
  73. die "${login}: Permission denied for this user.\n";
  74. }
  75. }
  76.  
  77. sub find_rc_file {
  78. my ($my_dbh, $my_id_number) = @_;
  79.  
  80. my ($root_dir) = getcwd; #'/work/repo/smeserver/8/updates';
  81. my $file = File::Spec->catfile( $root_dir, $RC_FILE );
  82.  
  83. open my $rc_in, '<', $file or do {
  84. return 0;
  85. };
  86. return 1;
  87. }
  88.  
  89. sub get_id_from_DB {
  90. my ($my_dbh) = @_;
  91. my ($sth, $id, @id_list, $name, @name_list);
  92.  
  93. my $statement = "SELECT `Id`,`Name` FROM Repositories";
  94.  
  95. unless ($sth = $my_dbh->prepare($statement)) {
  96. print STDERR __LINE__.": Can't prepare \"$statement\": ".$my_dbh->errstr."\n";
  97. $my_dbh->disconnect;
  98. exit 2;
  99. }
  100. unless ($sth->execute()) {
  101. print STDERR __LINE__.": Can't execute \"$statement\": ".$my_dbh->errstr."\n";
  102. $dbh->disconnect;
  103. exit 2;
  104. }
  105. while (($id ,$name) = $sth->fetchrow()) {
  106. push(@id_list, $id);
  107. push(@name_list, $name);
  108. }
  109. unless (@id_list) {
  110. die "Repositories: Table is empty. Can't create .repositoryrc.\n";
  111. }
  112.  
  113. print "Creating .repositoryrc for: $ARGV[0]\n";
  114. print "Listing supported repositories...\n";
  115. print "|-----------------------------|\n";
  116. print "| Id Name |\n";
  117. print "|-----------------------------|\n";
  118.  
  119. #Print arrays as columns
  120. my $list = each_array(@id_list, @name_list);
  121. while (my ($id_column, $name_column) = $list->()) {
  122. printf "| %d % -23s|\n", ${id_column}, ${name_column};
  123. }
  124. print "|-----------------------------|\n";
  125.  
  126. while(1) {
  127. print "Choose one by Id, or enter 0 to cancel: ";
  128.  
  129. my $input_id = <STDIN>;
  130. chomp($input_id);
  131.  
  132. if ($input_id eq 0) {
  133. die "Canceled by user. Exiting...\n";
  134. }
  135.  
  136. if (grep { $_ eq $input_id } @id_list) {
  137. return $input_id;
  138. }
  139. else {
  140. print "Invalid Id. Try again...\n\n";
  141. }
  142. }
  143. }
  144.  
  145. sub create_new_rc_file {
  146. my ($my_id_number) = @_;
  147.  
  148. my ($root_dir) = getcwd; #'/work/repo/smeserver/8/updates';
  149. my $file = File::Spec->catfile( $root_dir, $RC_FILE );
  150.  
  151. open my $rc_out, '>', $file or do {
  152. $dbh->disconnect;
  153. die "$0: $file: $!\n"
  154. };
  155.  
  156. print $rc_out "repository=${my_id_number}\n";
  157. #print $rc_out "local=1\n";
  158. #print $rc_out "exclude=\\.(patch|delta)\\.rpm\n";
  159. #print $rc_out "ignore_mismatch_name=\\.nosrc\\.rpm\$\n";
  160. close($rc_out);
  161.  
  162. print "${file} created.\n";
  163. }
Add Comment
Please, Sign In to add comment