Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ssh gateway only if cct is run outside of admin node #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/cct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 6 additions & 13 deletions lib/cct/cloud/control_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]

Expand All @@ -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}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SpaceInsideHashLiteralBraces: Space inside { missing. (https://github.com/bbatsov/ruby-style-guide#spaces-operators)
Style/SpaceInsideHashLiteralBraces: Space inside } missing. (https://github.com/bbatsov/ruby-style-guide#spaces-operators)

options.merge!(timeout: config["timeout"]) if config["timeout"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance/RedundantMerge: Use options[:timeout] = config["timeout"] instead of options.merge!(timeout: config["timeout"]). (https://github.com/JuanitoFatas/fast-ruby#hashmerge-vs-hash-code)

options.merge!(password: password) unless password.to_s.empty?
options.merge!(gateway: gateway) if gateway

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance/RedundantMerge: Use options[:gateway] = gateway instead of options.merge!(gateway: gateway). (https://github.com/JuanitoFatas/fast-ruby#hashmerge-vs-hash-code)

options.merge!(logger: log)
@command_options = options
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cct/cloud/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/NegatedIf: Favor unless over if for negative conditions. (https://github.com/bbatsov/ruby-style-guide#unless-for-negatives)

control_node_options.merge!(gateway: admin_node.attributes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance/RedundantMerge: Use control_node_options[:gateway] = admin_node.attributes instead of control_node_options.merge!(gateway: admin_node.attributes). (https://github.com/JuanitoFatas/fast-ruby#hashmerge-vs-hash-code)

end
@control_node = ControlNode.new(control_node_options)
@nodes = Nodes.new(crowbar)
nodes << control_node << admin_node
@command =
Expand Down
15 changes: 7 additions & 8 deletions lib/cct/remote_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -173,9 +171,10 @@ def detect_timeout opts={}
timeout.to_i
end

def validate_options
def validate_options *options_to_skip

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MethodDefParentheses: Use def with parentheses when there are parameters. (https://github.com/bbatsov/ruby-style-guide#method-parens)

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?
Expand Down