Class: Chef::Knife::ClusterProxy

Inherits:
Ironfan::Script show all
Defined in:
lib/chef/knife/cluster_proxy.rb

Overview

Runs the ssh command to open a SOCKS proxy to the given host, and writes a PAC (automatic proxy config) file to /tmp/ironfan_proxy-YOURNAME.pac. Only the first host is used, even if multiple match.

Why not use Net::Ssh directly? The SOCKS proxy support was pretty bad. Though ugly, exec'ing the command works.

Constant Summary collapse

EC2_PROXY_PATTERNS =
[]

Instance Attribute Summary

Attributes included from Ironfan::KnifeCommon

#broker, #problems

Instance Method Summary collapse

Methods inherited from Ironfan::Script

#_run, #aggregates_on_noop?, #prepares_on_noop?

Methods included from Ironfan::KnifeCommon

#all_computers, #bootstrapper, #configure_dry_run, #confirm_execution, #confirm_or_exit, #die, #discover_computers, #display, #exit_if_unhealthy!, #gemfile, #get_relevant_slice, #get_slice, #has_problem, #healthy?, included, load_deps, #load_ironfan, #pick_apart, #predicate_str, #progressbar_for_threads, #run, #run_bootstrap, #section, #sub_command, #wait_for_ssh

Instance Method Details

#aggregates?Boolean

Returns:

  • (Boolean)

124
125
126
# File 'lib/chef/knife/cluster_proxy.rb', line 124

def aggregates?
  false
end

#command_for_target(svr) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/knife/cluster_proxy.rb', line 60

def command_for_target(svr)
   config[:attribute]       ||= Chef::Config[:knife][:ssh_address_attribute] || "fqdn"
   config[:ssh_user]        ||= Chef::Config[:knife][:ssh_user]   #config[:identity_file]   ||= svr.server.selected_cloud.ssh_identity_file

   config[:host_key_verify] ||= Chef::Config[:knife][:host_key_verify] || (not config[:no_host_key_verify]) # pre-vs-post 0.10.4

   address = svr.machine.public_hostname
   if address.blank? && (svr.chef_node)
     address = format_for_display( svr.chef_node )[config[:attribute]]
   end

   cmd  = [ 'ssh', '-N' ]
   cmd += [ '-D', config[:socks_port].to_s ]
   cmd += [ '-p', config[:port].to_s       ]  if  config[:port].present?
   cmd << '-f'                                if  config[:background]
   cmd << "-#{'v' * config[:verbosity].to_i}" if (config[:verbosity].to_i > 0)
   cmd += %w[ -o StrictHostKeyChecking=no  ]  if  config[:host_key_verify]
   cmd += %w[ -o ConnectTimeout=10 -o ServerAliveInterval=60 -o ControlPath=none ]
   cmd += [ '-i', File.expand_path(config[:identity_file]) ] if  config[:identity_file].present?
   cmd << (config[:ssh_user] ? "#{config[:ssh_user]}@#{address}" : address)

   Chef::Log.debug("Cluster proxy config:  #{config.inspect}")
   Chef::Log.debug("Cluster proxy command: #{cmd.inspect}")
   ui.info(["SOCKS Proxy on",
       "local port", ui.color(config[:socks_port], :cyan),
       "for",        ui.color(svr.name,            :cyan),
       "(#{address})"
     ].join(" "))

   cmd
end

#dump_proxy_pacObject

Write a .pac (automatic proxy configuration) file to /etc/ironfan_proxy-YOURNAME.pac


96
97
98
99
100
101
102
# File 'lib/chef/knife/cluster_proxy.rb', line 96

def dump_proxy_pac
  pac_filename = File.expand_path(File.join('/tmp', "ironfan_proxy-#{ENV['USER']}.pac"))
  ui.info("point your browser at PAC (automatic proxy config file) file://#{pac_filename}")
  File.open(pac_filename, 'w') do |f|
    f.print proxy_pac_contents
  end
end

#perform_execution(target) ⇒ Object


52
53
54
55
56
57
58
# File 'lib/chef/knife/cluster_proxy.rb', line 52

def perform_execution(target)
  svr = target.first
  cmd = command_for_target(svr)

  dump_proxy_pac
  exec(*cmd)
end

#prepares?Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/chef/knife/cluster_proxy.rb', line 120

def prepares?
  false
end

#proxy_pac_contentsObject


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/knife/cluster_proxy.rb', line 107

def proxy_pac_contents
  proxy_patterns  = EC2_PROXY_PATTERNS
  proxy_patterns += Array(Chef::Config[:cluster_proxy_patterns])
  rules = proxy_patterns.compact.map{|str| "(shExpMatch(host, %-28s))" % %Q{"#{str}"} }
  %Q{function FindProxyForURL(url, host) {
  if (#{rules.join(" ||\n      ")}
) {
    return "SOCKS5 localhost:#{config[:socks_port]}";
  }
  return "DIRECT";
}\n}
end

#relevant?(server) ⇒ Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/chef/knife/cluster_proxy.rb', line 48

def relevant?(server)
  server.machine.running?
end