Skip to content

Commit

Permalink
Merge pull request #94 from chef/SHACK-145/multitarget/rename-remote-…
Browse files Browse the repository at this point in the history
…connection-to-target-host

[SHACK-145] rename remote connection to target host
  • Loading branch information
marcparadise authored Apr 26, 2018
2 parents 69dea94 + 3ea05e3 commit 8d4beff
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 109 deletions.
11 changes: 5 additions & 6 deletions components/chef-workstation/lib/chef-workstation/action/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
module ChefWorkstation
module Action
# Derive new Actions from Action::Base
# "connection" is a train connection that may be active and available
# based on
# "target_host" is a TargetHost that the action is being applied to. May be nil
# if the action does not require a target.
# "config" is hash containing any options that your command may need
#
# Implement perform_action to perform whatever action your class is intended to do.
# Run time will be captured via telemetry and categorized under ":action" with the
# unqualified class name of your Action.
class Base
attr_reader :connection, :config
attr_reader :target_host, :config

def initialize(config = {})
c = config.dup
@connection = c.delete :connection
@target_host = c.delete :target_host
# Remaining options are for child classes to make use of.
@config = c
end
Expand Down Expand Up @@ -90,15 +90,14 @@ def notify(action, *args)

def family
@family ||= begin
f = @connection.platform.family
f = target_host.platform.family
if f == "windows"
:windows
else
:other
end
end
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module ChefWorkstation::Action
class ConvergeTarget < Base

def perform_action
remote_tmp = connection.run_command!(mktemp)
remote_tmp = target_host.run_command!(mktemp)
remote_dir_path = escape_windows_path(remote_tmp.stdout.strip)
remote_recipe_path = create_remote_recipe(@config, remote_dir_path)
remote_config_path = create_remote_config(remote_dir_path)

c = connection.run_command("#{chef_client} #{remote_recipe_path} --config #{remote_config_path}")
c = target_host.run_command("#{chef_client} #{remote_recipe_path} --config #{remote_config_path}")

connection.run_command!("#{delete_folder} #{remote_dir_path}")
target_host.run_command!("#{delete_folder} #{remote_dir_path}")
if c.exit_status == 0
ChefWorkstation::Log.debug(c.stdout)
notify(:success)
Expand All @@ -30,7 +30,7 @@ def create_remote_recipe(config, dir)
if config.has_key?(:recipe_path)
recipe_path = config.delete :recipe_path
begin
connection.upload_file(recipe_path, remote_recipe_path)
target_host.upload_file(recipe_path, remote_recipe_path)
rescue RuntimeError
raise RecipeUploadFailed.new(recipe_path)
end
Expand All @@ -42,7 +42,7 @@ def create_remote_recipe(config, dir)
recipe_file = Tempfile.new
recipe_file.write(create_resource(resource_type, resource_name, properties))
recipe_file.close
connection.upload_file(recipe_file.path, remote_recipe_path)
target_host.upload_file(recipe_file.path, remote_recipe_path)
rescue RuntimeError
raise ResourceUploadFailed.new()
ensure
Expand All @@ -66,7 +66,7 @@ def create_remote_config(dir)
config_file = Tempfile.new
config_file.write(workstation_rb)
config_file.close
connection.upload_file(config_file.path, remote_config_path)
target_host.upload_file(config_file.path, remote_config_path)
rescue RuntimeError
raise ConfigUploadFailed.new()
ensure
Expand All @@ -78,13 +78,13 @@ def create_remote_config(dir)
def handle_ccr_error
require "chef-workstation/errors/ccr_failure_mapper"
mapper_opts = {}
c = connection.run_command(read_chef_stacktrace)
c = target_host.run_command(read_chef_stacktrace)
if c.exit_status == 0
lines = c.stdout.split("\n")
# We need to delete the stacktrace after copying it over. Otherwise if we get a
# remote failure that does not write a chef stacktrace its possible to get an old
# stale stacktrace.
connection.run_command!(delete_chef_stacktrace)
target_host.run_command!(delete_chef_stacktrace)
ChefWorkstation::Log.error("Remote chef-client error follows:")
ChefWorkstation::Log.error("\n " + lines.join("\n "))
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require "chef-workstation/action/install_chef/linux"

module ChefWorkstation::Action::InstallChef
def self.instance_for_target(conn, opts = {})
opts[:connection] = conn
p = conn.platform
def self.instance_for_target(target_host, opts = {})
opts[:target_host] = target_host
p = target_host.platform
if p.family == "windows" # Family is reliable even when mocking; `windows?` is not.
Windows.new(opts)
elsif p.linux?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def perform_remote_install

def lookup_artifact
require "mixlib/install"
platform = connection.platform
platform = target_host.platform
platform_name = platform.family == "windows" ? "windows" : platform.name
c = {
platform_version: platform.release,
Expand All @@ -53,7 +53,7 @@ def download_to_workstation(url_path)
def upload_to_target(local_path)
installer_dir = setup_remote_temp_path()
remote_path = File.join(installer_dir, File.basename(local_path))
connection.upload_file(local_path, remote_path)
target_host.upload_file(local_path, remote_path)
remote_path
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ChefWorkstation::Action::InstallChef
class Linux < ChefWorkstation::Action::InstallChef::Base
def already_installed_on_target?
r = connection.run_command("test -f /opt/chef/bin/chef-client")
r = target_host.run_command("test -f /opt/chef/bin/chef-client")
r.exit_status == 0
end

Expand All @@ -12,14 +12,14 @@ def install_chef_to_target(remote_path)
when ".deb"
"dpkg -i #{remote_path}"
end
connection.run_command!(install_cmd)
target_host.run_command!(install_cmd)
nil
end

def setup_remote_temp_path
installer_dir = "/tmp/chef-installer"
connection.run_command!("mkdir -p #{installer_dir}")
connection.run_command!("chmod 777 #{installer_dir}")
target_host.run_command!("mkdir -p #{installer_dir}")
target_host.run_command!("chmod 777 #{installer_dir}")
installer_dir
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def already_installed_on_target?
Write-Host -NoNewline 'true'
}
EOM
r = connection.run_command!(cmd)
r = target_host.run_command!(cmd)
r.stdout == "true"
end

Expand All @@ -23,7 +23,7 @@ def perform_remote_install
channel: :stable,
shell_type: :ps1,
})
connection.run_command! installer.install_command
target_host.run_command! installer.install_command
end

# TODO: These methods are implemented, but are currently
Expand All @@ -33,17 +33,17 @@ def install_chef_to_target(remote_path)
# 'cmd.exe' definitely does - so we'll make the path cmd-friendly
# before running the command
cmd = "cmd /c msiexec /package #{remote_path.tr("/", "\\")} /quiet"
connection.run_command!(cmd)
target_host.run_command!(cmd)
end

def setup_remote_temp_path
return @temppath if @temppath

r = connection.run_command!("Write-Host -NoNewline $env:TEMP")
r = target_host.run_command!("Write-Host -NoNewline $env:TEMP")
temppath = "#{r.stdout}\\chef-installer"

# Failure here is acceptable - the dir could already exist
connection.run_command("New-Item -ItemType Directory -Force -Path #{temppath}")
target_host.run_command("New-Item -ItemType Directory -Force -Path #{temppath}")
@temppath = temppath
end
end
Expand Down
12 changes: 6 additions & 6 deletions components/chef-workstation/lib/chef-workstation/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ def handle_perform_error(e)
id = e.respond_to?(:id) ? e.id : e.class.to_s
message = e.respond_to?(:message) ? e.message : e.to_s
Telemetry.capture(:error, exception: { id: id, message: message })
# TODO: connection assignment below won't work, because the connection is internal the
# action that failed. We can work around this for CW::Error-derived errors by accepting connection
# TODO: connection assignment below won't work, because the target host is internal the
# action that failed. We can work around this for CW::Error-derived errors by accepting target host
# in the constructor; but we still need to find a happy path for third-party errors
# (train, runtime) - perhaps moving connection tracking and lookup to its own component
# (train, runtime) - perhaps moving target host tracking and lookup to its own component
#
# #conn = @cmd.nil? ? nil : @cmd.connection
conn = nil
wrapper = ChefWorkstation::WrappedError.new(e, conn)
# #target_host = @cmd.nil? ? nil : @cmd.target_host
target_host = nil
wrapper = ChefWorkstation::WrappedError.new(e, target_host)
capture_exception_backtrace(wrapper)
# Now that our housekeeping is done, allow user-facing handling/formatting
# in `run` to execute by re-raising
Expand Down
10 changes: 5 additions & 5 deletions components/chef-workstation/lib/chef-workstation/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ def run(params)
# If reporter is nil a Terminal spinner will be used; otherwise
# the provided reporter will be used.
def connect(target, settings, reporter = nil)
conn = RemoteConnection.new(target, settings)
target_host = TargetHost.new(target, settings)
if reporter.nil?
UI::Terminal.spinner(T.status.connecting, prefix: "[#{conn.config[:host]}]") do |rep|
conn.connect!
UI::Terminal.spinner(T.status.connecting, prefix: "[#{target_host.config[:host]}]") do |rep|
target_host.connect!
rep.success(T.status.connected)
end
else
reporter.update(T.status.connecting)
conn = conn.connect!
target_host.connect!
reporter.success(T.status.connected)
end
conn
target_host
rescue RuntimeError => e
if reporter.nil?
UI::Terminal.output(e.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

require "chef-workstation/command/base"
require "chef-workstation/command/target"
require "chef-workstation/remote_connection"
require "chef-workstation/target_host"
require "chef-workstation/action/install_chef"
require "chef-workstation/action/converge_target"
require "chef-workstation/ui/terminal"
Expand Down Expand Up @@ -78,14 +78,14 @@ def run(params)

target = cli_arguments.shift

@conn = connect(target, config)
UI::Terminal.spinner(TS.install_chef.verifying, prefix: "[#{@conn.hostname}]") do |r|
@target_host = connect(target, config)
UI::Terminal.spinner(TS.install_chef.verifying, prefix: "[#{@target_host.hostname}]") do |r|
install(r)
end

converge_args = { connection: @conn }
converge_args = { target_host: @target_host }
converge_args, spinner_msg = parse_converge_args(converge_args, cli_arguments)
UI::Terminal.spinner(spinner_msg, prefix: "[#{@conn.hostname}]") do |r|
UI::Terminal.spinner(spinner_msg, prefix: "[#{@target_host.hostname}]") do |r|
converge(r, converge_args)
end
end
Expand Down Expand Up @@ -196,7 +196,7 @@ def recipe_strategy?(cli_arguments)
# Runs the InstallChef action and renders UI updates as
# the action reports back
def install(r)
installer = Action::InstallChef.instance_for_target(@conn)
installer = Action::InstallChef.instance_for_target(@target_host)
context = Text.status.install_chef
installer.run do |event, data|
case event
Expand Down
6 changes: 3 additions & 3 deletions components/chef-workstation/lib/chef-workstation/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def initialize(id, *params)
end

class WrappedError < StandardError
attr_accessor :conn, :contained_exception
def initialize(e, connection)
attr_accessor :target_host, :contained_exception
def initialize(e, target_host)
super(e.message)
@contained_exception = e
@conn = connection
@target_host = target_host
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

require "chef-workstation/log"
require "train"
class ChefWorkstation::RemoteConnection
class ChefWorkstation::TargetHost
attr_reader :config, :reporter, :backend

def self.make_connection(target, opts = {})
conn = new(target, opts)
conn.connect!
conn
def self.instance_for_url(target, opts = {})
target_host = new(target, opts)
target_host.connect!
target_host
end

def initialize(host_url, opts = {}, logger = nil)
Expand All @@ -47,7 +47,7 @@ def connect!
@backend = @train_connection.connection
@backend.wait_until_ready
end
@backend
nil
end

def hostname
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "chef-workstation/remote_connection"
require "chef-workstation/target_host"

module ChefWorkstation
class TargetResolver
Expand All @@ -9,7 +9,7 @@ def initialize(unparsed_target, conn_options)

def targets
@targets ||= @unparsed_target.split(",").map do |target|
RemoteConnection.new(target, @conn_options)
TargetHost.new(target, @conn_options)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

module ChefWorkstation::UI
class ErrorPrinter
attr_reader :pastel, :show_log, :show_stack, :exception, :conn
attr_reader :pastel, :show_log, :show_stack, :exception, :target_host
# TODO define 't' as a method is a temporary workaround
# to ensure that text key lookups are testable.
def t
Expand Down Expand Up @@ -62,9 +62,9 @@ def self.dump_unexpected_error(e)
Terminal.output "=-" * 30
end

def initialize(wrapper, unwrapped = nil, conn = nil)
def initialize(wrapper, unwrapped = nil, target_host = nil)
@exception = unwrapped || wrapper.contained_exception
@conn = wrapper.conn
@target_host = wrapper.target_host || target_host
@pastel = Pastel.new
@show_log = exception.respond_to?(:show_log) ? exception.show_log : true
@show_stack = exception.respond_to?(:show_stack) ? exception.show_stack : true
Expand Down Expand Up @@ -175,8 +175,8 @@ def format_other_exception
end

def formatted_host
return nil if conn.nil?
cfg = conn.config
return nil if target_host.nil?
cfg = target_host.config
port = cfg[:port].nil? ? "" : ":#{cfg[:port]}"
if cfg[:user].nil?
user = ""
Expand Down
2 changes: 1 addition & 1 deletion components/chef-workstation/spec/target_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

context "when a comma-separated list of targets is provided" do
let(:target_string) { "ssh://node1.example.com,winrm://node2.example.com" }
it "returns an array with correct RemoteConnection instances" do
it "returns an array with correct TargetHost instances" do
actual_targets = subject.targets
expect(actual_targets[0].config[:host]).to eq "node1.example.com"
expect(actual_targets[1].config[:host]).to eq "node2.example.com"
Expand Down
Loading

0 comments on commit 8d4beff

Please sign in to comment.