Class: Chef::Knife::ZeroConverge

Inherits:
BootstrapSsh show all
Includes:
ZeroBase
Defined in:
lib/chef/knife/zero_converge.rb

Direct Known Subclasses

ZeroChefClient

Instance Method Summary collapse

Methods included from ZeroBase

included

Methods inherited from BootstrapSsh

#build_client_json, #ssh_command

Constructor Details

#initialize(argv = []) ⇒ ZeroConverge

Returns a new instance of ZeroConverge.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/knife/zero_converge.rb', line 94

def initialize(argv = [])
  super
  self.configure_chef

  ## Command hook before_converge (Before launched Chef-Zero)
  if Chef::Config[:knife][:before_converge]
    ::Knife::Zero::Helper.hook_shell_out!('before_converge', ui, Chef::Config[:knife][:before_converge])
  end

  if @config[:json_attribs]
    @config[:chef_client_json] = fetch_json_from_url
  end

  validate_options!

  @name_args = [@name_args[0], start_chef_client]
end

Instance Method Details

#fetch_json_from_urlObject



175
176
177
178
# File 'lib/chef/knife/zero_converge.rb', line 175

def fetch_json_from_url
  config_fetcher = Chef::ConfigFetcher.new(@config[:json_attribs])
  config_fetcher.fetch_json
end

#json_attribs_given?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/chef/knife/zero_converge.rb', line 171

def json_attribs_given?
  !config[:json_attribs].nil? && !config[:json_attribs].empty?
end

#json_attribs_without_override_given?Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
# File 'lib/chef/knife/zero_converge.rb', line 162

def json_attribs_without_override_given?
  if json_attribs_given?
    return true unless override_runlist_given?
  else
    false
  end
  false
end

#named_run_list_given?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/chef/knife/zero_converge.rb', line 158

def named_run_list_given?
  !config[:named_run_list].nil? && !config[:named_run_list].empty?
end

#override_and_named_given?Boolean

True if policy_name and run_list are both given

Returns:

  • (Boolean)


150
151
152
# File 'lib/chef/knife/zero_converge.rb', line 150

def override_and_named_given?
  override_runlist_given? && named_run_list_given?
end

#override_runlist_given?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/chef/knife/zero_converge.rb', line 154

def override_runlist_given?
  !config[:override_runlist].nil? && !config[:override_runlist].empty? || @config[:chef_client_json]&.key?('run_list')
end

#start_chef_clientObject

rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/CyclomaticComplexity



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

def start_chef_client # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/CyclomaticComplexity
  client_path = @config[:use_sudo] || Chef::Config[:knife][:use_sudo] ? 'sudo ' : ''
  client_path = @config[:chef_client_path] ? "#{client_path}#{@config[:chef_client_path]}" : "#{client_path}#{ChefUtils::Dist::Infra::CLIENT}"
  s = String.new(client_path)
  s << ' -l debug' if @config[:verbosity] && @config[:verbosity] >= 2
  s << " -S http://127.0.0.1:#{::Knife::Zero::Helper.zero_remote_port}"
  s << " -o #{@config[:override_runlist]}" if @config[:override_runlist]
  s << ' -j /etc/' + ChefUtils::Dist::Infra::DIR_SUFFIX + '/chef_client_json.json' if @config[:json_attribs]
  s << " --splay #{@config[:splay]}" if @config[:splay]
  s << " -n #{@config[:named_run_list]}" if @config[:named_run_list]
  s << " --config #{@config[:node_config_file]}"
  s << ' --skip-cookbook-sync' if @config[:skip_cookbook_sync]
  s << ' --no-color' unless @config[:color]
  s << " -E #{@config[:environment]}" if @config[:environment]
  s << " --chef-license #{@config[:chef_license]}" if @config[:chef_license]
  s << ' -W' if @config[:why_run]
  Chef::Log.info 'Remote command: ' + s
  s
end

#validate_options!Object

For support policy_document_databag(old style)



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

def validate_options!
  if override_and_named_given?
    ui.error('--override_runlist and --named_run_list are exclusive')
    exit 1
  end
  if json_attribs_without_override_given?
    ui.error(
      '--json-attributes must be used with --override-runlist ' \
      'or passed json should includes key `run-list` ' \
      'to avoid unexpected updating local node object.'
    )
    exit 1
  end
  true
end