<commit_before>
module CORL
module Action
class Seed < Plugin::CloudAction
 
  #-----------------------------------------------------------------------------
  # Settings
  
  def configure
    super do
      codes :key_store_failure,
            :project_failure,
            :network_init_failure,
            :network_load_failure,
            :node_load_failure,
            :node_save_failure
      #---
      
      register :project_branch, :str, 'master'
      register :project_reference, :str, nil do |value|
        value           = value.to_sym
        project_plugins = CORL.loaded_plugins(:project)
        
        if @project_info = CORL.plugin_class(:project).translate_reference(value, true)
          provider = @project_info[:provider]
        else
          provider = value
        end
        
        unless project_plugins.keys.include?(provider.to_sym)
          warn('corl.actions.seed.errors.project_reference', { :value => value, :provider => provider, :choices => project_plugins.keys.join(', ') })
          next false
        end
        true
      end      
    end
  end
  
  #---
  
  def arguments
    [ :project_reference ]
  end

  #-----------------------------------------------------------------------------
  # Operations
   
  def execute
    super do |node, network|
      info('corl.actions.seed.start')
      
      if node && network
        admin_exec do
          network_path = lookup(:corl_network)
          backup_path  = File.join(Dir.tmpdir(), 'corl')
          
          render("Generating network SSH deploy keys")
          
          if keys = Util::SSH.generate.store
            if @project_info
              project_info = Config.new(@project_info)
            else
              project_info = Config.new({ :provider => :git })
            end
            
            render("Backing up current network configuration")
            FileUtils.rm_rf(backup_path)
            FileUtils.mv(network_path, backup_path)
            
            render("Seeding network configuration from #{settings[:project_reference]}")
            project = CORL.project(extended_config(:project, {
              :directory => network_path,
              :reference => project_info.get(:reference, nil),
              :url       => project_info.get(:url, settings[:project_reference]),
              :revision  => project_info.get(:revision, settings[:project_branch]),
              :create    => true,
              :pull      => true,
              :keys      => keys
            }), project_info[:provider])
        
            if project
              render("Finalizing network path and removing temporary backup")
              FileUtils.chmod_R(0600, network_path)
              FileUtils.rm_rf(backup_path)
              
              render("Reinitializing network")
              if network = init_network
                if network.load
                  if node = network.local_node
                    render("Updating node network configurations")
                    myself.status = code.node_save_failure unless node.save  
                  else
                    myself.status = code.node_load_failure
                  end                  
                else
                  myself.status = code.network_load_failure    
                end
              else
                myself.status = code.network_init_failure
              end     
            else
              myself.status = code.project_failure  
            end            
          else
            myself.status = code.key_store_failure
          end
        end
      else<commit_msg>Adding a require new requirement on the local_node call in the seed action provider.<commit_after>
module CORL
module Action
class Seed < Plugin::CloudAction
 
  #-----------------------------------------------------------------------------
  # Settings
  
  def configure
    super do
      codes :key_store_failure,
            :project_failure,
            :network_init_failure,
            :network_load_failure,
            :node_load_failure,
            :node_save_failure
      #---
      
      register :project_branch, :str, 'master'
      register :project_reference, :str, nil do |value|
        value           = value.to_sym
        project_plugins = CORL.loaded_plugins(:project)
        
        if @project_info = CORL.plugin_class(:project).translate_reference(value, true)
          provider = @project_info[:provider]
        else
          provider = value
        end
        
        unless project_plugins.keys.include?(provider.to_sym)
          warn('corl.actions.seed.errors.project_reference', { :value => value, :provider => provider, :choices => project_plugins.keys.join(', ') })
          next false
        end
        true
      end      
    end
  end
  
  #---
  
  def arguments
    [ :project_reference ]
  end

  #-----------------------------------------------------------------------------
  # Operations
   
  def execute
    super do |node, network|
      info('corl.actions.seed.start')
      
      if node && network
        admin_exec do
          network_path = lookup(:corl_network)
          backup_path  = File.join(Dir.tmpdir(), 'corl')
          
          render("Generating network SSH deploy keys")
          
          if keys = Util::SSH.generate.store
            if @project_info
              project_info = Config.new(@project_info)
            else
              project_info = Config.new({ :provider => :git })
            end
            
            render("Backing up current network configuration")
            FileUtils.rm_rf(backup_path)
            FileUtils.mv(network_path, backup_path)
            
            render("Seeding network configuration from #{settings[:project_reference]}")
            project = CORL.project(extended_config(:project, {
              :directory => network_path,
              :reference => project_info.get(:reference, nil),
              :url       => project_info.get(:url, settings[:project_reference]),
              :revision  => project_info.get(:revision, settings[:project_branch]),
              :create    => true,
              :pull      => true,
              :keys      => keys
            }), project_info[:provider])
        
            if project
              render("Finalizing network path and removing temporary backup")
              FileUtils.chmod_R(0600, network_path)
              FileUtils.rm_rf(backup_path)
              
              render("Reinitializing network")
              if network = init_network
                if network.load
                  if node = network.local_node(true)
                    render("Updating node network configurations")
                    myself.status = code.node_save_failure unless node.save  
                  else
                    myself.status = code.node_load_failure
                  end                  
                else
                  myself.status = code.network_load_failure    
                end
              else
                myself.status = code.network_init_failure
              end     
            else
              myself.status = code.project_failure  
            end            
          else
            myself.status = code.key_store_failure
          end
        end
      else
{'fraction_non_alphanumeric': 0.1182649024689674, 'fraction_numerical': 0.0010912563088255353, 'mean_word_length': 1.3552842916800514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '776704', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1736, 'n_words': 382}