Class: Admiral::LayerBase
- Inherits:
-
Object
- Object
- Admiral::LayerBase
- Defined in:
- lib/admiral/layer.rb
Direct Known Subclasses
Admiral::Layers::AdmiralBuildSh, Admiral::Layers::AdmiralExempleTest, Admiral::Layers::AdmiralPuppetApply, Admiral::Layers::AdmiralSvnProxy, Admiral::Layers::AdmiralSvnPuppetCookbook, Admiral::Layers::AdmiralSvnPuppetManifest, Admiral::Layers::AdmiralTestChefInstall, Admiral::Layers::AdmiralTestServerspecInstall, Admiral::Layers::AdmiralTestServerspecRun, Admiral::Layers::AdmiralTestServerspecUpload
Class Method Summary collapse
Instance Method Summary collapse
- #add_parameter(name, description) ⇒ Object
- #do_action ⇒ Object
-
#initialize(description, config, ipaddress) ⇒ LayerBase
constructor
A new instance of LayerBase.
- #run ⇒ Object
- #run_ssh_command(command, options = {}) ⇒ Object
- #show_information ⇒ Object
- #upload(local, remote) ⇒ Object
- #verify ⇒ Object
Constructor Details
#initialize(description, config, ipaddress) ⇒ LayerBase
Returns a new instance of LayerBase.
33 34 35 36 37 38 |
# File 'lib/admiral/layer.rb', line 33 def initialize(description, config, ipaddress) @description = description @config = config @ipaddress = ipaddress @parameters = [] end |
Class Method Details
.inherited(subclass) ⇒ Object
27 28 29 30 31 |
# File 'lib/admiral/layer.rb', line 27 def self.inherited(subclass) file = caller.first[/^[^:]+/] $uid = File.basename(file,File.extname(file)) $location = File.dirname(file) end |
Instance Method Details
#add_parameter(name, description) ⇒ Object
40 41 42 43 |
# File 'lib/admiral/layer.rb', line 40 def add_parameter(name, description) parameter = { 'name'=>name, 'description'=>description } @parameters << parameter end |
#do_action ⇒ Object
109 110 111 112 |
# File 'lib/admiral/layer.rb', line 109 def do_action() STDERR.puts "do_action must be implemented" return false end |
#run ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/admiral/layer.rb', line 69 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.}" 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 = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/admiral/layer.rb', line 114 def run_ssh_command(command, = {}) username = @config['username'] keyfile = @config['keyfile'] proxy_url = @config['proxy_url'] allow_proxy = .fetch(:allow_proxy, false) env = .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 ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/admiral/layer.rb', line 45 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) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/admiral/layer.rb', line 174 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 ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/admiral/layer.rb', line 59 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 |