<commit_before># Author:: <NAME> (<<EMAIL>>) & <NAME> (<<EMAIL>>)
# Cookbook Name:: aix
# Provider:: suma
#
# Copyright:: 2016, Atos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

property :desc, [String,nil], name_property: true
property :oslevel, String
property :location, String, default: "/usr/sys/inst.images"
property :targets, String
property :timeout, Integer, default: 1200

load_current_value do
end

action :download do

  Chef::Log.info("desc=#{desc}")
  Chef::Log.info("oslevel=#{oslevel}")
  Chef::Log.info("location=#{location}")
  Chef::Log.info("targets=#{targets}")

  # compute suma request type based on oslevel property
  if property_is_set?(:oslevel)
    if oslevel =~ /^([0-9]{4}-[0-9]{2})(|-00|-00-[0-9]{4})$/
      rq_type="TL"
      rq_name=$1
    else
      if oslevel =~ /^([0-9]{4}-[0-9]{2}-[0-9]{2})(|-[0-9]{4})$/
        rq_type="SP"
        rq_name=$1
      else
        raise "SUMA-SUMA-SUMA oslevel is not recognized!"
      end
    end
  else
    rq_type="Latest"
	rq_name='7200-00-01-1527'    # TODO: GET LAST SP LEVEL (WITH METADATA ?)
	rq_name=rq_name.match(/^([0-9]{4}-[0-9]{2}-[0-9]{2})(|-[0-9]{4})$/)[1]
  end
  Chef::Log.info("rq_type=#{rq_type}")
  Chef::Log.info("rq_name=#{rq_name}")

  # find lowest ML level by comparing each machine's oslevel from ohai
  filter_ml=rq_name.match(/^([0-9]{4}-[0-9]{2})(|-[0-9]{2}|-[0-9]{2}-[0-9]{4})$/)[1]
  Chef::Log.info("Base ML level is #{filter_ml}")
  filter_ml.delete!('-')
  if property_is_set?(:targets)
    targets.split(',').each do |machine|

      begin
        new_filter_ml=String.new(node.fetch('nim', {}).fetch('clients', {}).fetch(machine, {}).fetch('mllevel'))
        Chef::Log.info("Obtained ML level for machine #{machine}: #{new_filter_ml}")
        new_filter_ml.delete!('-')
        if new_filter_ml.to_i <= filter_ml.to_i
          filter_ml=new_filter_ml
        end
      rescue Exception => e
        Chef::Log.info("No ML level for machine #{machine}")
      end
    end
  else
    raise "SUMA-SUMA-SUMA no client targets specified!"
  end
  filter_ml.insert(4, '-')
  Chef::Log.info("Lowest ML level is: #{filter_ml}")

  # create location if it does not exist
  res_name="#{rq_name}-lpp_source"
  dl_target="#{location}/#{res_name}"
  Chef::Log.info("Checking location #{dl_target}...")
  shell_out!("mkdir -p #{dl_target}")

  # suma preview
  suma_s="suma -x -a DisplayName=\"#{desc}\" -a RqType=#{rq_type} -a DLTarget=#{dl_target} -a FilterML=#{filter_ml}"
  unless rq_type.eql?("Latest")
    suma_s << " -a RqName=#{rq_name}"
  end
  dl=0
  Chef::Log.info("SUMA preview operation: #{suma_s}")
  so=shell_out("#{suma_s} -a Action=Preview 2>&1", :timeout => timeout)
  if so.error?
    Chef::Log.info("suma returns an error...")
    if so.stdout =~ /0500-035 No fixes match your query./
      Chef::Log.info("Suma error: No fixes match your query")
    else
      Chef::Log.info("Other suma error:\n#{so.stdout}")
    end
  else
    Chef::Log.info("#{so.stdout}")
    if so.stdout =~ /Total bytes of updates downloaded: ([0-9]+)/
      dl=$1.to_f/1024/1024/1024
      Chef::Log.info("#{dl.to_f.round(2)} GB to download")
    else
      Chef::Log.info("Nothing to download")
    end
    if so.stdout =~ /([0-9]+) failed/
	  failed=$1
      Chef::Log.info("#{failed} failed fixes")
    else
      Chef::Log.info("No failed fixes")
    end
  end

  unless dl.to_f == 0 or failed.to_i > 0
    # suma download
    converge_by("suma download operation: \"#{suma_s}\"") do
      Chef::Log.info("Download fixes...")
      so=shell_out!("#{suma_s} -a Action=Download 2>&1", :timeout => timeout)
    end
  end

  toto=node.fetch('nim', {}).fetch('lpp_sources', {}).fetch(res_name, nil)
  Chef::Log.info("toto=#{toto}")
  if toto.nil?
    # nim define
	nim_s="nim -o define -t lpp_source -a server=master -a location=#{dl_target} #{res_name}"
    converge_by("nim define lpp_source: \"#{nim_s}\"") do
      Chef::Log.info("Define #{res_name} ...")
      so=shell_out!("#{nim_s}")
    end
  end

end<commit_msg>Fix no clients raise exception<commit_after># Author:: <NAME> (<<EMAIL>>) & <NAME> (<<EMAIL>>)
# Cookbook Name:: aix
# Provider:: suma
#
# Copyright:: 2016, Atos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

property :desc, [String,nil], name_property: true
property :oslevel, String
property :location, String, default: "/usr/sys/inst.images"
property :targets, String
property :timeout, Integer, default: 3600

load_current_value do
end

action :download do

  Chef::Log.info("desc=#{desc}")
  Chef::Log.info("oslevel=#{oslevel}")
  Chef::Log.info("location=#{location}")
  Chef::Log.info("targets=#{targets}")

  # compute suma request type based on oslevel property
  if property_is_set?(:oslevel)
    if oslevel =~ /^([0-9]{4}-[0-9]{2})(|-00|-00-[0-9]{4})$/
      rq_type="TL"
      rq_name=$1
    else
      if oslevel =~ /^([0-9]{4}-[0-9]{2}-[0-9]{2})(|-[0-9]{4})$/
        rq_type="SP"
        rq_name=$1
      else
        raise "SUMA-SUMA-SUMA oslevel is not recognized!"
      end
    end
  else
    rq_type="Latest"
	rq_name='9999-99-99-9999'
	rq_name=rq_name.match(/^([0-9]{4}-[0-9]{2}-[0-9]{2})(|-[0-9]{4})$/)[1]
  end
  Chef::Log.info("rq_type=#{rq_type}")
  Chef::Log.info("rq_name=#{rq_name}")

  # find lowest ML level by comparing each machine's oslevel from ohai
  filter_ml=nil
  if property_is_set?(:targets)
    targets.split(',').each do |machine|

      begin
        new_filter_ml=String.new(node.fetch('nim', {}).fetch('clients', {}).fetch(machine, {}).fetch('mllevel'))
        Chef::Log.info("Obtained ML level for machine #{machine}: #{new_filter_ml}")
        new_filter_ml.delete!('-')
        if filter_ml.nil? or new_filter_ml.to_i <= filter_ml.to_i
          filter_ml=new_filter_ml
        end
      rescue Exception => e
        Chef::Log.info("No ML level for machine #{machine}")
      end
    end
  end
  if filter_ml.nil?
    raise "SUMA-SUMA-SUMA no client targets specified!"
  end
  filter_ml.insert(4, '-')
  Chef::Log.info("Lowest ML level is: #{filter_ml}")

  # create location if it does not exist
  res_name="#{rq_name}-lpp_source"
  dl_target="#{location}/#{res_name}"
  Chef::Log.info("Checking location #{dl_target}...")
  shell_out!("mkdir -p #{dl_target}")

  # suma preview
  suma_s="suma -x -a DisplayName=\"#{desc}\" -a RqType=#{rq_type} -a DLTarget=#{dl_target} -a FilterML=#{filter_ml}"
  unless rq_type.eql?("Latest")
    suma_s << " -a RqName=#{rq_name}"
  end
  dl=0
  Chef::Log.info("SUMA preview operation: #{suma_s}")
  so=shell_out("#{suma_s} -a Action=Preview 2>&1", :timeout => timeout)
  if so.error?
    Chef::Log.info("suma returns an error...")
    if so.stdout =~ /0500-035 No fixes match your query./
      Chef::Log.info("Suma error: No fixes match your query")
    else
      Chef::Log.info("Other suma error:\n#{so.stdout}")
    end
  else
    Chef::Log.info("#{so.stdout}")
    if so.stdout =~ /Total bytes of updates downloaded: ([0-9]+)/
      dl=$1.to_f/1024/1024/1024
      Chef::Log.info("#{dl.to_f.round(2)} GB to download")
    else
      Chef::Log.info("Nothing to download")
    end
    if so.stdout =~ /([0-9]+) failed/
	  failed=$1
      Chef::Log.info("#{failed} failed fixes")
    else
      Chef::Log.info("No failed fixes")
    end
  end

  unless dl.to_f == 0 or failed.to_i > 0
    # suma download
    converge_by("suma download operation: \"#{suma_s}\"") do
      Chef::Log.info("Download fixes...")
      so=shell_out!("#{suma_s} -a Action=Download 2>&1", :timeout => timeout)
    end
	
    toto=node.fetch('nim', {}).fetch('lpp_sources', {}).fetch(res_name, nil)
    Chef::Log.info("toto=#{toto}")
    if toto.nil?
      # nim define
	  nim_s="nim -o define -t lpp_source -a server=master -a location=#{dl_target} #{res_name}"
      converge_by("nim define lpp_source: \"#{nim_s}\"") do
        Chef::Log.info("Define #{res_name} ...")
        so=shell_out!("#{nim_s}")
      end
    end

  end

end
{'fraction_non_alphanumeric': 0.17906428811847863, 'fraction_numerical': 0.023785481880399417, 'mean_word_length': 3.488418932527694, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6297919', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3273, 'n_words': 774}