Update version according to OSCI-883
[packages/precise/mcollective.git] / install.rb
1 #! /usr/bin/env ruby
2 #--
3 # Copyright 2004 Austin Ziegler <ruby-install@halostatue.ca>
4 #   Install utility. Based on the original installation script for rdoc by the
5 #   Pragmatic Programmers.
6 #
7 # This program is free software. It may be redistributed and/or modified under
8 # the terms of the GPL version 2 (or later) or the Ruby licence.
9 #
10 # Usage
11 # -----
12 # In most cases, if you have a typical project layout, you will need to do
13 # absolutely nothing to make this work for you. This layout is:
14 #
15 #   bin/    # executable files -- "commands"
16 #   lib/    # the source of the library
17 #
18 # The default behaviour:
19 # 1) Build Rdoc documentation from all files in bin/ (excluding .bat and .cmd),
20 #    all .rb files in lib/, ./README, ./ChangeLog, and ./Install.
21 #    and all .rb files in lib/.
22 # 2) Install configuration files in etc/.
23 # 3) Install commands from bin/ into the Ruby bin directory.
24 # 4) Install system commands from bin/ into the Ruby sbin directory.
25 # 5) Install all library files from lib/ into Ruby's site_lib/version
26 #    directory.
27 # 6) Install all plugins from plugins/ into the plugins directory
28 #    (usually $libexecdir/mcollective).
29 #
30 #++
31
32 require 'rbconfig'
33 require 'find'
34 require 'fileutils'
35 require 'tempfile'
36 require 'optparse'
37 require 'ostruct'
38 include FileUtils
39
40 begin
41   require 'rdoc/rdoc'
42   $haverdoc = true
43 rescue LoadError
44   puts "Missing rdoc; skipping documentation"
45   $haverdoc = false
46 end
47
48 if (defined?(RbConfig) ? RbConfig : Config)::CONFIG['host_os'] =~ /mswin|win32|dos|mingw|cygwin/i
49     $stderr.puts "install.rb does not support Microsoft Windows. See ext/windows/README.md for information on installing on Microsoft Windows."
50     exit(-1)
51 end
52
53 PREREQS = %w{}
54
55 InstallOptions = OpenStruct.new
56
57 def glob(list)
58   g = list.map { |i| Dir.glob(i) }
59   g.flatten!
60   g.compact!
61   g.uniq!
62   g
63 end
64
65 def check_prereqs
66   PREREQS.each do |pre|
67     begin
68       require pre
69     rescue LoadError
70       puts "Could not load #{pre} Ruby library; cannot install"
71       exit(-1)
72     end
73   end
74 end
75
76 def do_configs(configs, target, strip = 'etc/')
77   Dir.mkdir(target) unless File.directory? target
78   configs.each do |cf|
79     ocf = File.join(target, cf.gsub(Regexp.new(strip), ''))
80     oc = File.dirname(ocf)
81     makedirs(oc, {:mode => 0755, :verbose => true})
82     install(cf, ocf, {:mode => 0644, :preserve => true, :verbose => true})
83   end
84 end
85
86 def do_bins(bins, target, strip = 's?bin/')
87   Dir.mkdir(target) unless File.directory? target
88   bins.each do |bf|
89     obf = bf.gsub(/#{strip}/, '')
90     install_binfile(bf, obf, target)
91   end
92 end
93
94 def do_libs(libs, target, strip = 'lib/')
95   libs.each do |lf|
96     olf = File.join(target, lf.sub(/^#{strip}/, ''))
97     op = File.dirname(olf)
98     if File.directory?(lf)
99       makedirs(olf, {:mode => 0755, :verbose => true})
100     else
101       makedirs(op, {:mode => 0755, :verbose => true})
102       install(lf, olf, {:mode => 0644, :preserve => true, :verbose => true})
103     end
104   end
105 end
106
107 ##
108 # Prepare the file installation.
109 #
110 def prepare_installation
111   InstallOptions.configs = true
112
113   # Only try to do docs if we're sure they have rdoc
114   if $haverdoc
115     InstallOptions.rdoc = true
116   else
117     InstallOptions.rdoc = false
118   end
119
120
121   ARGV.options do |opts|
122     opts.banner = "Usage: #{File.basename($0)} [options]"
123     opts.separator ""
124     opts.on('--[no-]rdoc', 'Creation of RDoc output.', 'Default is create rdoc.') do |onrdoc|
125       InstallOptions.rdoc = onrdoc
126     end
127     opts.on('--[no-]configs', 'Installation of config files', 'Default is install configs.') do |onconfigs|
128       InstallOptions.configs = onconfigs
129     end
130     opts.on('--destdir[=OPTIONAL]', 'Installation prefix for all targets', 'Default essentially /') do |destdir|
131       InstallOptions.destdir = destdir
132     end
133     opts.on('--configdir[=OPTIONAL]', 'Installation directory for config files', 'Default /etc/mcollective') do |configdir|
134       InstallOptions.configdir = configdir
135     end
136     opts.on('--bindir[=OPTIONAL]', 'Installation directory for binaries', 'overrides RbConfig::CONFIG["bindir"]') do |bindir|
137       InstallOptions.bindir = bindir
138     end
139     opts.on('--sbindir[=OPTIONAL]', 'Installation directory for system binaries', 'overrides RbConfig::CONFIG["sbindir"]') do |sbindir|
140       InstallOptions.sbindir = sbindir
141     end
142     opts.on('--ruby[=OPTIONAL]', 'Ruby interpreter to use with installation', 'overrides ruby used to call install.rb') do |ruby|
143       InstallOptions.ruby = ruby
144     end
145     opts.on('--sitelibdir[=OPTIONAL]', 'Installation directory for libraries', 'overrides RbConfig::CONFIG["sitelibdir"]') do |sitelibdir|
146       InstallOptions.sitelibdir = sitelibdir
147     end
148     opts.on('--plugindir[=OPTIONAL]', 'Installation directory for plugins', 'Default /usr/libexec/mcollective') do |plugindir|
149       InstallOptions.plugindir = plugindir
150     end
151     opts.on('--quick', 'Performs a quick installation. Only the', 'installation is done.') do |quick|
152       InstallOptions.rdoc    = false
153       InstallOptions.ri      = false
154       InstallOptions.configs = true
155     end
156     opts.on('--full', 'Performs a full installation. All', 'optional installation steps are run.') do |full|
157       InstallOptions.rdoc    = true
158       InstallOptions.ri      = true
159       InstallOptions.configs = true
160     end
161     opts.separator("")
162     opts.on_tail('--help', "Shows this help text.") do
163       $stderr.puts opts
164       exit
165     end
166
167     opts.parse!
168   end
169
170   version = [RbConfig::CONFIG["MAJOR"], RbConfig::CONFIG["MINOR"]].join(".")
171   libdir = File.join(RbConfig::CONFIG["libdir"], "ruby", version)
172
173   # Mac OS X 10.5 and higher declare bindir
174   # /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin
175   # which is not generally where people expect executables to be installed
176   # These settings are appropriate defaults for all OS X versions.
177   if RUBY_PLATFORM =~ /^universal-darwin[\d\.]+$/
178     RbConfig::CONFIG['bindir'] = "/usr/bin"
179     RbConfig::CONFIG['sbindir'] = "/usr/sbin"
180   end
181
182   if InstallOptions.configdir
183     configdir = InstallOptions.configdir
184   else
185     configdir = "/etc/mcollective"
186   end
187
188   if InstallOptions.bindir
189     bindir = InstallOptions.bindir
190   else
191     bindir = RbConfig::CONFIG['bindir']
192   end
193
194   if InstallOptions.sbindir
195     sbindir = InstallOptions.sbindir
196   else
197     sbindir = RbConfig::CONFIG['sbindir']
198   end
199
200   if InstallOptions.sitelibdir
201     sitelibdir = InstallOptions.sitelibdir
202   else
203     sitelibdir = RbConfig::CONFIG["sitelibdir"]
204     if sitelibdir.nil?
205       sitelibdir = $LOAD_PATH.find { |x| x =~ /site_ruby/ }
206       if sitelibdir.nil?
207         sitelibdir = File.join(libdir, "site_ruby")
208       elsif sitelibdir !~ Regexp.quote(version)
209         sitelibdir = File.join(sitelibdir, version)
210       end
211     end
212   end
213
214   if InstallOptions.plugindir
215     plugindir = InstallOptions.plugindir
216   else
217     plugindir = "/usr/libexec/mcollective"
218   end
219
220   if InstallOptions.destdir
221     destdir = InstallOptions.destdir
222   else
223     destdir = ''
224   end
225
226   configdir   = File.join(destdir, configdir)
227   bindir      = File.join(destdir, bindir)
228   sbindir     = File.join(destdir, sbindir)
229   sitelibdir  = File.join(destdir, sitelibdir)
230   plugindir   = File.join(destdir, plugindir)
231
232   makedirs(configdir) if InstallOptions.configs
233   makedirs(bindir)
234   makedirs(sbindir)
235   makedirs(sitelibdir)
236   makedirs(plugindir)
237
238   InstallOptions.sitelibdir = sitelibdir
239   InstallOptions.configdir = configdir
240   InstallOptions.bindir  = bindir
241   InstallOptions.sbindir  = sbindir
242   InstallOptions.plugindir  = plugindir
243 end
244
245 ##
246 # Build the rdoc documentation.
247 #
248 def build_rdoc(files)
249   return unless $haverdoc
250   begin
251     r = RDoc::RDoc.new
252     r.document(["--main", "MCollective", "--line-numbers"] + files)
253   rescue RDoc::RDocError => e
254     $stderr.puts e.message
255   rescue Exception => e
256     $stderr.puts "Couldn't build RDoc documentation\n#{e.message}"
257   end
258 end
259
260 ##
261 # Install file(s) from ./bin to RbConfig::CONFIG['bindir']. Patch it on the way
262 # to insert a #! line; on a Unix install, the command is named as expected
263 def install_binfile(from, op_file, target)
264   tmp_file = Tempfile.new('mcollective-binfile')
265
266   if InstallOptions.ruby
267     ruby = InstallOptions.ruby
268   else
269     ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
270   end
271
272   File.open(from) do |ip|
273     File.open(tmp_file.path, "w") do |op|
274       op.puts "#!#{ruby}"
275       contents = ip.readlines
276       contents.shift if contents[0] =~ /^#!/
277       op.write contents.join
278     end
279   end
280
281   install(tmp_file.path, File.join(target, op_file), :mode => 0755, :preserve => true, :verbose => true)
282   tmp_file.unlink
283 end
284
285 # Change directory into the mcollective root so we don't get the wrong files for install.
286 cd File.dirname(__FILE__) do
287   # Set these values to what you want installed.
288   configs = glob(%w{etc/*.dist})
289   erbs = glob(%w{etc/*.erb})
290   bins = glob(%w{bin/mco})
291   sbins = glob(%w{bin/mcollectived bin/mc-call-agent})
292   rdoc = glob(%w{bin/* lib/**/*.rb README* })
293   libs = glob(%w{lib/**/*})
294   plugins = glob(%w{plugins/**/*})
295
296   check_prereqs
297   prepare_installation
298
299   build_rdoc(rdoc) if InstallOptions.rdoc
300   do_configs(configs, InstallOptions.configdir, 'etc/|\.dist') if InstallOptions.configs
301   do_configs(erbs, InstallOptions.configdir) if InstallOptions.configs
302   do_bins(bins, InstallOptions.bindir)
303   do_bins(sbins, InstallOptions.sbindir)
304   do_libs(libs, InstallOptions.sitelibdir)
305   do_libs(plugins, InstallOptions.plugindir, 'plugins/')
306 end