class Admiral::LayerBase
Public Class Methods
inherited(subclass)
click to toggle source
# File lib/admiral/layer.rb, line 26 def self.inherited(subclass) file = caller.first[/^[^:]+/] $uid = File.basename(file,File.extname(file)) $location = File.dirname(file) end
new(description, config, ipaddress)
click to toggle source
# File lib/admiral/layer.rb, line 32 def initialize(description, config, ipaddress) @description = description @config = config @ipaddress = ipaddress @parameters = [] end
Public Instance Methods
add_parameter(name, description)
click to toggle source
# File lib/admiral/layer.rb, line 39 def add_parameter(name, description) parameter = { 'name'=>name, 'description'=>description } @parameters << parameter end
do_action()
click to toggle source
# File lib/admiral/layer.rb, line 108 def do_action() STDERR.puts "do_action must be implemented" return false end
run()
click to toggle source
# File lib/admiral/layer.rb, line 68 def run() puts "--- #{@description} ---" username = @config['username'] layer_location = $location layer_uid = $uid layer_folder = "#{layer_location}/#{layer_uid}.d" layer_shell = "#{layer_location}/#{layer_uid}.sh" layer_perl = "#{layer_location}/#{layer_uid}.pl" layer_remote_dir = "/tmp/#{username}/" if File.exists?(layer_folder) upload(layer_folder, layer_remote_dir) end if File.exists?(layer_shell) upload(layer_shell, layer_remote_dir) end if File.exists?(layer_perl) upload(layer_perl, layer_remote_dir) end begin success = do_action() rescue Interrupt STDERR.puts "Layer interrupted" return false rescue Errno::EACCES, Errno::ENOENT, Errno::ECONNREFUSED, IOError => e STDERR.puts "Layer has error : #{e.message}" return false rescue Net::SSH::AuthenticationFailed STDERR.puts "Layer has error : SSH - Authentication failed" return false end return success end
run_ssh_command(command, options = {})
click to toggle source
# File lib/admiral/layer.rb, line 113 def run_ssh_command(command, options = {}) username = @config['username'] keyfile = @config['keyfile'] proxy_url = @config['proxy_url'] allow_proxy = options.fetch(:allow_proxy, false) env = options.fetch(:env, nil) env_array = [] cmd = "" if allow_proxy and proxy_url cmd << %Q[export http_proxy="#{proxy_url}";] cmd << %Q[export https_proxy=$http_proxy;] end cmd << command ssh_cmd = "" if not env.nil? env.each do |key, value| ENV[key] = value env_array << key end end ssh_cmd << "sudo -E sh -c '#{cmd}'" Net::SSH.start(@ipaddress, username, :host_key => "ssh-rsa", :keys => [ keyfile ], :user_known_hosts_file => '/dev/null', :send_env => env_array) do |ssh| ssh.open_channel do |channel| channel.exec(ssh_cmd) do |ch, success| unless success STDERR.puts "FAILED: couldn't execute command (#{command})" return false end channel.on_data do |ch, data| puts data end channel.on_extended_data do |ch, type, data| STDERR.puts data end channel.on_request("exit-status") do |ch,data| exit_code = data.read_long if exit_code > 0 STDERR.puts "FAILED: command (#{command}) has failed" return exit_code end end end end ssh.loop return 0 end end
show_information()
click to toggle source
# File lib/admiral/layer.rb, line 44 def show_information puts "Layer UID: #{$uid}" puts "Description: #{@description}" if @parameters.count > 0 puts "Paramaters:" @parameters.each do | parameter | puts " #{parameter['name']} / #{parameter['description']}" end else puts "No paramater" end end
upload(local, remote)
click to toggle source
# File lib/admiral/layer.rb, line 173 def upload(local, remote) username = @config['username'] keyfile = @config['keyfile'] Net::SCP.upload!(@ipaddress, username, local, remote, :recursive => true, :ssh => { :host_key => "ssh-rsa", :keys => [ keyfile ], :user_known_hosts_file => '/dev/null' }) end
verify()
click to toggle source
# File lib/admiral/layer.rb, line 58 def verify () @parameters.each do | parameter | if not @config.key?(parameter['name']) STDERR.puts "Layer #{$uid} requires the parameter #{parameter['name']}, but it is not found" return false end end return true end