From 7325383dbb1f9aa021086bba4e817add006162cd Mon Sep 17 00:00:00 2001 From: Vladimir Moravec Date: Thu, 24 Mar 2016 16:59:04 +0100 Subject: [PATCH] Use gateway only if cct is run outside of admin node --- lib/cct.rb | 4 ++++ lib/cct/cloud/control_node.rb | 19 ++++++------------- lib/cct/cloud/world.rb | 6 +++++- lib/cct/remote_command.rb | 15 +++++++-------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/cct.rb b/lib/cct.rb index e40a943..c366427 100644 --- a/lib/cct.rb +++ b/lib/cct.rb @@ -58,5 +58,9 @@ def load_tasks! require "cucumber/rake/task" Dir.glob(root.to_s + '/tasks/**/*.rake').each { |task| load(task) } end + + def running_on_admin_node? + File.exist?("/etc/crowbar.install.key") + end end end diff --git a/lib/cct/cloud/control_node.rb b/lib/cct/cloud/control_node.rb index 1d2a070..c7b78fa 100644 --- a/lib/cct/cloud/control_node.rb +++ b/lib/cct/cloud/control_node.rb @@ -23,12 +23,12 @@ def initialize options @environment = {} @crowbar = options[:crowbar] @gateway = options[:gateway] - @command = RemoteCommand.new(gateway: options[:gateway]) + set_command_options + @command = RemoteCommand.new(command_options.merge(skip_validation: [:ip])) end def exec! command_name, *params self.load! - set_command_target params << update_environment(params) command.exec!(command_name, params.compact) end @@ -123,6 +123,7 @@ def load! force: false data = crowbar.node(name) @data = data @ip = data["crowbar"]["network"]["admin"]["address"] + command.options.ip = ip @hostname = data["hostname"] @domain = data["domain"] @@ -134,19 +135,11 @@ def load! force: false private - def set_command_target - return if command.target - - command.target = self - set_command_options - end - def set_command_options - return if command_options - - options = {port: port} - options.merge!(timeout: command.options.extended.timeout) + options = {port: port, user: user} + options.merge!(timeout: config["timeout"]) if config["timeout"] options.merge!(password: password) unless password.to_s.empty? + options.merge!(gateway: gateway) if gateway options.merge!(logger: log) @command_options = options end diff --git a/lib/cct/cloud/world.rb b/lib/cct/cloud/world.rb index e578ddc..3133cce 100644 --- a/lib/cct/cloud/world.rb +++ b/lib/cct/cloud/world.rb @@ -15,7 +15,11 @@ def initialize logger=nil @admin_node = AdminNode.new @crowbar = CrowbarApi.new(admin_node.config) admin_node.crowbar_proxy = Node::CrowbarProxy.new(api: crowbar) - @control_node = ControlNode.new(crowbar: crowbar, gateway: admin_node.attributes) + control_node_options = { crowbar: crowbar } + if !Cct.running_on_admin_node? + control_node_options.merge!(gateway: admin_node.attributes) + end + @control_node = ControlNode.new(control_node_options) @nodes = Nodes.new(crowbar) nodes << control_node << admin_node @command = diff --git a/lib/cct/remote_command.rb b/lib/cct/remote_command.rb index b7276ad..c940dee 100644 --- a/lib/cct/remote_command.rb +++ b/lib/cct/remote_command.rb @@ -9,22 +9,20 @@ class RemoteCommand Result = Struct.new(:success?, :output, :error, :exit_code, :host) attr_reader :session, :options, :log, :gateway, :proxy - attr_accessor :target def initialize opts @gateway = opts[:gateway] - opts.merge!(gateway) if gateway @proxy = opts[:proxy] || set_ssh_proxy @log = BaseLogger.new("SSH") @options = OpenStruct.new construct_options(opts) - validate_options + validate_options(opts[:skip_validation]) end def exec! command, params=[], capture_error: false log.base.level = ::Logger::WARN connect! - host_ip = gateway ? target.ip : options.ip + host_ip = options.ip environment = set_environment(params) full_command = "#{command} #{params.join(" ")}".strip result = Result.new(false, "", "", 1000, host_ip) @@ -63,7 +61,7 @@ def connect! def connected? if gateway - session && session.active? ? true :false + session && session.active? ? true : false else session && !session.closed? ? true : false end @@ -88,7 +86,7 @@ def set_ssh_proxy def open_session_channel &block if gateway - session.ssh(target.ip, target.user, target.command_options) do |session| + session.ssh(options.ip, options.user, options.extended.to_h) do |session| session.open_channel(&block) end else @@ -173,9 +171,10 @@ def detect_timeout opts={} timeout.to_i end - def validate_options + def validate_options *options_to_skip + options_to_skip.flatten! errors = [] - errors.push("missing ip") unless options.ip + errors.push("missing ip") if !options.ip && !options_to_skip.include?(:ip) errors.push("missing user") unless options.user errors.unshift("Invalid options: ") unless errors.empty? raise ValidationError.new(self, errors) unless errors.empty?