Class: Chef::Knife::UpdateRun

Inherits:
Chef::Knife show all
Includes:
Update
Defined in:
lib/chef/knife/update_run.rb

Constant Summary collapse

BATCH_SIZE =
50

Instance Method Summary collapse

Methods included from Update

#except, included, #node_list, #only, #recipes, #target_version

Instance Method Details

#runObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chef/knife/update_run.rb', line 87

def run
  list = node_list
  compatible = recipes(only(list, target_version))
  unknown = recipes(except(list, target_version))
  compatible.each { |recipe, _| unknown.delete(recipe.to_s) }

  nodes = list.inject([]) do |memo, (version, nodes)|
    if version != target_version
      nodes.each do |node|
        if config[:force] || node.recipes.all? { |recipe| compatible.keys.include?(recipe) }
          memo << node
        end
      end
    end

    memo
  end

  if config[:force] && unknown.any?
    ui.warn "It's unknown if the following recipes are Chef #{target_version} compatible: #{unknown.keys.sort.join(', ')}"
  end

  if nodes.size > 0
    if !config[:dry_run]
      ui.confirm "Are you sure you want to update #{nodes.size} nodes to chef #{target_version}"
      nodes.each_slice(BATCH_SIZE) { |batch| update_ssh(batch, target_version).run }
    else
      ui.msg "Dry run. The following #{nodes.size} nodes would get updated:"
      nodes.each { |node| ui.msg "  #{node.fqdn}" }
    end
  elsif list.size > 0
    ui.msg "None of the nodes can be updated. Try option --force"
  else
    ui.msg "No nodes found."
  end
end

#ssh_command(version) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/chef/knife/update_run.rb', line 142

def ssh_command(version)
  command = config[:command] % { :version => version }

  if config[:use_sudo]
    command = "sudo #{command}"
  end

  command
end

#update_ssh(nodes, version) ⇒ Object

Update given nodes to specified version



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/knife/update_run.rb', line 125

def update_ssh(nodes, version)
  fqdns = nodes.map { |node| node.fqdn }.join(' ')

  ssh = Chef::Knife::Ssh.new
  ssh.ui = ui
  ssh.name_args = [ fqdns, ssh_command(version) ]
  ssh.config[:manual] = true
  ssh.config[:on_error] = :skip
  ssh.config[:ssh_user] = Chef::Config[:knife][:ssh_user] || config[:ssh_user]
  ssh.config[:ssh_password] = config[:ssh_password]
  ssh.config[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
  ssh.config[:ssh_gateway] = Chef::Config[:knife][:ssh_gateway] || config[:ssh_gateway]
  ssh.config[:identity_file] = Chef::Config[:knife][:identity_file] || config[:identity_file]
  ssh.config[:host_key_verify] = Chef::Config[:knife][:host_key_verify] || config[:host_key_verify]
  ssh
end