#!/usr/bin/env ruby require 'json' require 'uri' require 'net/http' require File.dirname(__FILE__) + '/../lib/nuri/targz' module Nuri module Util TarGzip = Object.new.extend(Util::Tar) def self.install_modules(address, port, modules, protocol="http", open_timeout=2, read_timeout=10) data = {} modules.each do |module_name| raise Exception, "Module #{module_name} is not exist!" if not ::File.directory?(module_name) data[module_name] = TarGzip.targzip(module_name, module_name).read end begin url = "#{protocol}://#{address}:#{port}/modules" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = open_timeout http.read_timeout = read_timeout request = Net::HTTP::Put.new(uri.request_uri) request.set_form_data(data) response = http.request(request) (response.code == '200') rescue Exception => e $stderr.puts "Cannot install module with URL: #{url}" $stderr.puts e false end end end end if $0 == __FILE__ if ARGV.length < 2 puts "Usage: #{$0.split('/').last}
:[port] + " exit(1) end DefaultPort = 1314 address, port = ARGV.shift.split(':', 2) port = port.to_s.to_i port = DefaultPort unless port > 0 modules = ARGV missing = modules.select { |mod| not ::File.directory?(mod) } modules = modules - missing success = Nuri::Util.install_modules(address, port, modules) output = { :status => success, :installed_modules => modules, :missing_modules => missing } puts JSON.generate(output) exit (success ? 0 : 1) end