blib/script/moby-s-caching.pl untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 151 CC 7 SCAR 19.1
CC
7
Cov
0%
check_root
20.0
BEGIN
12.0
chown_by_name
6.0
CRAP 6.8
1 #!/usr/local/bin/perl -w
2 #
3 # Prepare the stage...
4 #
5 # $Id: moby-s-caching.pl,v 1.2 2008/07/07 17:59:41 kawas Exp $
6 # Contact: Edward Kawas <edward.kawas@gmail.com>
7 # -----------------------------------------------------------
8  
9 BEGIN {
10 use Getopt::Std;
11 use vars qw/ $opt_h $opt_d /;
12 getopts('hd:');
13  
14 use constant MSWIN => $^O =~ /MSWin32|Windows_NT/i ? 1 : 0;
15  
16 # usage
17 if ($opt_h or @ARGV == 0) {
18 print STDOUT <<'END_OF_USAGE';
19 Help on creating your registry RDF cache.
20 Usage: moby-s-caching.pl [-h] [-d cache-dir] cache-owner
21  
22 -d ..... the directory that you would like
23 to write the cache.
24  
25 This value is extracted from
26 MOBY_CENTRAL_CONFIG when it is
27 not specified.
28  
29 cache-owner ..... the owner of the cache; once the
30 cache is created, ownership of the
31 cache dir and all its contained
32 files are given to this cache-owner.
33
34 NOTE: If running this to update the
35 cache for your registry, make sure
36 to put here the value of the username
37 for your web server!
38  
39 -h ..... shows this message
40  
41 If you are running this as root and the user environment is not being set
42 appropriately (for one reason or another), try running the script like the
43 following:
44 sudo -E moby-s-caching.pl [rest of arguments]
45  
46 Good luck!
47  
48  
49 END_OF_USAGE
50 exit(0);
51 }
52  
53 sub say { print @_; }
54  
55 }
56  
57 use English qw( -no_match_vars );
58 use strict;
59  
60 # performs a chown on a list of files and then returns the number of changed files
61 sub chown_by_name {
62 return 0 if (MSWIN);
63 my ( $uid, $guid, @files ) = @_;
64 return chown($uid, $guid, @files );
65 }
66  
67 # performs a chmod on a list of files and returns the number of changed files
68 # UGO == UG{rw}, O{r}
69 sub chmod_by_name {
70 my (@files) = @_;
71 my $mode = 0664;
72 return chmod $mode, @files;
73 }
74  
75 sub check_root {
76  
77 # assume that windows has no security preventing the copying of files
78 return if (MSWIN);
79  
80 unless ( getpwuid($<) eq 'root' ) {
81 print STDOUT <<EOT;
82  
83 Hmmm - you are not running this as root. If you indicate any
84 system directories for the installation, such as '/usr/local/apache/', then
85 you may not have permission to install files there. If so, you should
86 cancel now with ^C, su to root, and restart.
87  
88 EOT
89  
90 print STDOUT "Should I proceed? [n] ";
91 my $tmp = <STDIN>;
92 $tmp =~ s/\s//g;
93 exit() unless $tmp =~ /y/i;
94 } else {
95 print STDOUT <<EOT;
96  
97 Take care! you are running this as ** root **. Please take the normal
98 precautions that you would ordinarily take when running software as root.
99 In particular, be careful with ownership, paths, and environment variables.
100  
101 EOT
102  
103 }
104 }
105  
106 # --- main ---
107 use MOBY::Config;
108 use MOBY::Client::Central;
109 use MOBY::RDF::Ontologies::Cache::NamespaceCache;
110 use MOBY::RDF::Ontologies::Cache::ObjectCache;
111 use MOBY::RDF::Ontologies::Cache::ServiceCache;
112 use MOBY::RDF::Ontologies::Cache::ServiceTypeCache;
113  
114 no warnings 'once';
115  
116 # give a waring if we are not running with su privelege
117 check_root();
118  
119 say "Preparing for cache creation ...\n\n";
120  
121 my $cache_dir = undef;
122 my $cache_owner = shift; #ARGV checked in BEGIN
123 my ($login,$pass,$uid,$gid) = getpwnam($cache_owner) or die "User '$cache_owner' doesn't seem to exist!\n\tPlease check the username and try again.\n\n$!";
124 my $registry_url = undef;
125 $registry_url = $ENV{MOBY_SERVER} if $ENV{MOBY_SERVER};
126  
127 if ($opt_d) {
128 $cache_dir = $opt_d;
129 $cache_dir =~ s/^ //;
130 $cache_dir =~ s/ $//;
131  
132 # dont check for existence
133 } else {
134 my $c = MOBY::Config->new();
135 $cache_dir = $c->{mobycentral}->{rdf_cache}
136 || die
137 "Unfortunately, I could not determine where it is that you store your RDF cache ...\n"
138 . "Please run this script again using the -d option!\n";
139  
140 $cache_dir = $cache_dir . "/" unless $cache_dir =~ m/.*\/$/;
141 }
142  
143 # set up registry_url
144 do {
145 my $c = MOBY::Client::Central->new();
146 $registry_url = $c->{default_MOBY_server};
147 } unless $registry_url;
148  
149 say "Creating cache ...\n";
150  
151 say "\tCreating datatype cache ... ";
152 my $x = MOBY::RDF::Ontologies::Cache::ObjectCache->new(
153 endpoint => $registry_url,
154 cache => $cache_dir,
155 );
156 $x->create_object_cache();
157 say"Done!\n";
158 say "\tCreating service type cache ... ";
159 $x = MOBY::RDF::Ontologies::Cache::ServiceTypeCache->new(
160 endpoint => $registry_url,
161 cache => $cache_dir,
162 );
163 $x->create_service_type_cache();
164 say"Done!\n";
165 say "\tCreating namespace cache ... ";
166 $x = MOBY::RDF::Ontologies::Cache::NamespaceCache->new(
167 endpoint => $registry_url,
168 cache => $cache_dir,
169 );
170 $x->create_namespace_cache();
171 say"Done!\n";
172 say "\tCreating services cache ... ";
173 $x = MOBY::RDF::Ontologies::Cache::ServiceCache->new(
174 endpoint => $registry_url,
175 cache => $cache_dir,
176 );
177 $x->create_service_cache();
178 say"done!\n\n";
179 say "Setting permissions for the cache ...\n";
180 my $dirs = $x->{utils}->get_cache_dirs();
181 while ( my ( $key, $value ) = each( %{$dirs} ) ) {
182 say "\tUpdating permissions for '$value' ...\n";
183 say "\t\tchown of $value: " . chown_by_name($uid, $gid, $value) . " files updated.\n";
184 say "\t\tchown of all files in $value: " . chown_by_name($uid, $gid, <$value/*>) . " files updated.\n";
185 }
186  
187 say "Done!\n";
188  
189 __END__