<commit_before>require 'json'
require 'timeout'

PUPPET_TIMEOUT = 60*60

module Astute
  class DeploymentEngine
    def initialize(context)
      @ctx = context
      @pattern_spec = {'type' => 'count-lines',
        'endlog_patterns' => [{'pattern' => /Finished catalog run in [0-9]+\.[0-9]* seconds\n/, 'progress' => 1.0}],
        'expected_line_number' => 500}
      @deploy_log_parser = Astute::LogParser::ParseNodeLogs.new('puppet-agent.log', @pattern_spec)
    end

    def deploy(nodes, attrs)
      attrs['deployment_mode'] ||= 'multinode_compute'  # simple multinode deployment is the default
      Astute.logger.info "Deployment mode #{attrs['deployment_mode']}, using #{self.class} for deployment."
      attrs_for_mode = self.send("attrs_#{attrs['deployment_mode']}", nodes, attrs)
      result = self.send("deploy_#{attrs['deployment_mode']}", nodes, attrs_for_mode)
    end

    def method_missing(method, *args)
      Astute.logger.error "Method #{method} is not implemented for #{self.class}, raising exception."
      raise "Method #{method} is not implemented for #{self.class}"
    end

    def attrs_singlenode_compute(nodes, attrs)
      attrs
    end

    def deploy_singlenode_compute(nodes, attrs)
      # TODO(mihgen) some real stuff is needed
      Astute.logger.info "Starting deployment of single node OpenStack"
      deploy_piece(nodes, attrs)
    end

    # we mix all attrs and prepare them for Puppet
    # Works for multinode_compute deployment mode
    def attrs_multinode_compute(nodes, attrs)
      ctrl_nodes = nodes.select {|n| n['role'] == 'controller'}
      # TODO(mihgen): we should report error back if there are not enough metadata passed
      ctrl_management_ips = []
      ctrl_public_ips = []
      ctrl_nodes.each do |n|
        ctrl_management_ips << n['network_data'].select {|nd| nd['name'] == 'management'}[0]['ip']
        ctrl_public_ips << n['network_data'].select {|nd| nd['name'] == 'public'}[0]['ip']
      end

      attrs['controller_node_address'] = ctrl_management_ips[0].split('/')[0]
      attrs['controller_node_public'] = ctrl_public_ips[0].split('/')[0]
      attrs
    end

    # This method is called by Ruby metaprogramming magic from deploy method
    # It should not contain any magic with attributes, and should not directly run any type of MC plugins<commit_msg>[astute] Fix for singlenode deployment<commit_after>require 'json'
require 'timeout'

PUPPET_TIMEOUT = 60*60

module Astute
  class DeploymentEngine
    def initialize(context)
      @ctx = context
      @pattern_spec = {'type' => 'count-lines',
        'endlog_patterns' => [{'pattern' => /Finished catalog run in [0-9]+\.[0-9]* seconds\n/, 'progress' => 1.0}],
        'expected_line_number' => 500}
      @deploy_log_parser = Astute::LogParser::ParseNodeLogs.new('puppet-agent.log', @pattern_spec)
    end

    def deploy(nodes, attrs)
      attrs['deployment_mode'] ||= 'multinode_compute'  # simple multinode deployment is the default
      Astute.logger.info "Deployment mode #{attrs['deployment_mode']}, using #{self.class} for deployment."
      attrs_for_mode = self.send("attrs_#{attrs['deployment_mode']}", nodes, attrs)
      result = self.send("deploy_#{attrs['deployment_mode']}", nodes, attrs_for_mode)
    end

    def method_missing(method, *args)
      Astute.logger.error "Method #{method} is not implemented for #{self.class}, raising exception."
      raise "Method #{method} is not implemented for #{self.class}"
    end

    def attrs_singlenode_compute(nodes, attrs)
      ctrl_management_ips = nodes[0]['network_data'].select {|nd| nd['name'] == 'management'}[0]['ip']
      ctrl_public_ips = nodes[0]['network_data'].select {|nd| nd['name'] == 'public'}[0]['ip']
      attrs['controller_node_address'] = ctrl_management_ips[0].split('/')[0]
      attrs['controller_node_public'] = ctrl_public_ips[0].split('/')[0]
      attrs
    end

    def deploy_singlenode_compute(nodes, attrs)
      # TODO(mihgen) some real stuff is needed
      Astute.logger.info "Starting deployment of single node OpenStack"
      deploy_piece(nodes, attrs)
    end

    # we mix all attrs and prepare them for Puppet
    # Works for multinode_compute deployment mode
    def attrs_multinode_compute(nodes, attrs)
      ctrl_nodes = nodes.select {|n| n['role'] == 'controller'}
      # TODO(mihgen): we should report error back if there are not enough metadata passed
      ctrl_management_ips = []
      ctrl_public_ips = []
      ctrl_nodes.each do |n|
        ctrl_management_ips << n['network_data'].select {|nd| nd['name'] == 'management'}[0]['ip']
        ctrl_public_ips << n['network_data'].select {|nd| nd['name'] == 'public'}[0]['ip']
      end

      attrs['controller_node_address'] = ctrl_management_ips[0].split('/')[0]
      attrs['controller_node_public'] = ctrl_public_ips[0].split('/')[0]
      attrs
    end

    # This method is called by Ruby metaprogramming magic from deploy method
    # It should not contain any magic with attributes, and should not directly run any type of MC plugins
{'fraction_non_alphanumeric': 0.13262074425969914, 'fraction_numerical': 0.009105304829770388, 'mean_word_length': 3.8261700095510984, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4996438', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1540, 'n_words': 403}