Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- #
- # Creates a new repository in the database with input data
- use DBI;
- use Sys::Hostname;
- sub identify_hostname;
- sub identify_admin;
- sub read_path_parameter;
- sub create_repo;
- my $DB = 'DBI:mysql:Repository:localhost';
- my $USER = 'wwwrun';
- my $PASSWORD = 'SeNh4Z';
- my $HOSTNAME = 'luist';
- my $ADMIN_GROUP = 'grouptest';
- identify_hostname();
- identify_admin();
- my $dbh = DBI->connect($DB, $USER, $PASSWORD)
- or die "Couldn't connect to $DB\n";
- my $name = $ARGV[0];
- my $path = $ARGV[1];
- my $url = $ARGV[2];
- read_path_parameter($path);
- create_repo($dbh, $name, $path, $url);
- 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 read_path_parameter {
- my ($my_path) = @_;
- unless (-d $my_path) {
- die "${my_path}: Invalid directory\n"
- }
- }
- sub create_repo {
- my ($my_dbh, $my_name, $my_path, $my_url) = @_;
- my ($sth);
- my $statement = 'INSERT INTO Repositories (`Name`, `InsertBy`, `UpdateBy`, `InsertTime`, `UpdateTime`, `Path`, `Url`) VALUES ';
- $statement .= "(\"${my_name}\", \"${USER}\", \"${USER}\", now(), now(), \"${my_path}\", \"${my_url}\")";
- 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;
- }
- #print "Name: ${my_name}\nPath: ${my_path}\nUrl: ${my_url}\n";
- }
Add Comment
Please, Sign In to add comment