Class: Chef::Knife::ClusterLaunch

Inherits:
Chef::Knife show all
Includes:
ClusterChef::KnifeCommon
Defined in:
lib/chef/knife/cluster_launch.rb

Instance Method Summary collapse

Methods included from ClusterChef::KnifeCommon

#bootstrapper, #configure_dry_run, #confirm_execution, #confirm_or_exit, #die, #get_relevant_slice, #get_slice, included, #load_cluster_chef, load_deps, #predicate_str, #progressbar_for_threads, #relevant?, #run_bootstrap, #section, #sub_command

Instance Method Details

#display(target) ⇒ Object



105
106
107
108
109
# File 'lib/chef/knife/cluster_launch.rb', line 105

def display(target)
  super(target, ["Name", "InstanceID", "State", "Flavor", "Image", "AZ", "Public IP", "Private IP", "Created At", 'Volumes', 'Elastic IP']) do |svr|
    { 'launchable?' => (svr.launchable? ? "[blue]#{svr.launchable?}[reset]" : '-' ), }
  end
end

#perform_after_launch_tasks(server) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chef/knife/cluster_launch.rb', line 111

def perform_after_launch_tasks(server)
  # Wait for node creation on amazon side
  server.fog_server.wait_for{ ready? }

  # Try SSH
  unless config[:dry_run]
    nil until tcp_test_ssh(server.fog_server.dns_name){ sleep @initial_sleep_delay ||= 10  }
  end

  # Make sure our list of volumes is accurate
  ClusterChef.fetch_fog_volumes
  server.discover_volumes!

  # Attach volumes, etc
  server.sync_to_cloud

  # Run Bootstrap
  if config[:bootstrap]
    run_bootstrap(server, server.fog_server.dns_name)
  end
end

#runObject



67
68
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
# File 'lib/chef/knife/cluster_launch.rb', line 67

def run
  load_cluster_chef
  die(banner) if @name_args.empty?
  configure_dry_run

  #
  # Load the facet
  #
  full_target = get_slice(*@name_args)
  display(full_target)
  target = full_target.select(&:launchable?)

  warn_or_die_on_bogus_servers(full_target) unless full_target.bogus_servers.empty?

  die("", "#{ui.color("All servers are running -- not launching any.",:blue)}", "", 1) if target.empty?

  # Pre-populate information in chef
  section("Sync'ing to chef and cloud")
  target.sync_to_cloud
  target.sync_to_chef

  # Launch servers
  section("Launching machines", :green)
  target.create_servers

  ui.info("")
  display(target)

  # As each server finishes, configure it
  watcher_threads = target.parallelize do |svr|
    perform_after_launch_tasks(svr)
  end

  progressbar_for_threads(watcher_threads)

  display(target)
end

#tcp_test_ssh(hostname) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/knife/cluster_launch.rb', line 133

def tcp_test_ssh(hostname)
  tcp_socket = TCPSocket.new(hostname, 22)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
    yield
    true
  else
    false
  end
rescue Errno::ETIMEDOUT
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end

#warn_or_die_on_bogus_servers(target) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chef/knife/cluster_launch.rb', line 152

def warn_or_die_on_bogus_servers(target)
  ui.info("")
  ui.info "Cluster has servers in a transitional or undefined state (shown as 'bogus'):"
  ui.info("")
  display(target)
  ui.info("")
  unless config[:force]
    die(
      "Launch operations may be unpredictable under these circumstances.",
      "You should wait for the cluster to stabilize, fix the undefined server problems",
      "(run \"knife cluster show CLUSTER\" to see what the problems are), or launch",
      "the cluster anyway using the --force option.", "", -2)
  end
  ui.info("")
  ui.info "--force specified"
  ui.info "Proceeding to launch anyway. This may produce undesired results."
  ui.info("")
end