Class: Libriciel::GetConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/libriciel.rb

Overview

 Class GetConfig for chef deploy

Class Method Summary collapse

Class Method Details

.compare_files_iparapheur(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/libriciel.rb', line 491

def self.compare_files_iparapheur(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file)
  ## Schema pour le Xmx
  ## default['iparapheur']['ctl']['xms'] pour le iParapheur
  ## ["-Xmx": "xxxxm", "-Xms": "xxxxm"] pour le parse_spec_conf
  if !default_config_file_parse[libriciel]
    default_config_file_parse[libriciel] = {}

  end

  case

  ## Xmx et Xms
  ## Lancement du parse pour voir les bonnes valeurs des Xmx et xms
  ## Puis mise en place dans le config.json au cas ou pas existant
  when File.readlines(conf_file).grep(/Xms|Xmx/).size > 0
    tableau = {}
    tabs = parse_spec_conf(conf_file, caracs_to_extract_from, tableau, nil)

    if !default_config_file_parse[libriciel]["ctl"]
      default_config_file_parse[libriciel]["ctl"] = {}

    end

    if default_config_file_parse[libriciel]["ctl"].empty?
     default_config_file_parse[libriciel]["ctl"] = default_config_file_parse[libriciel]["ctl"].merge(tabs)

    else
     default_config_file_parse[libriciel]["ctl"] = tabs



    end

  ## database vérification et surcharge des
  ## entrée dans le config.json pour régénération
  when File.readlines(conf_file).grep(/db./).size > 0
    conf_file_parse = JSON.parse(File.read(conf_file))
    if !default_config_file_parse[libriciel]["database"]
      default_config_file_parse[libriciel]["database"] = {}

    end

    conf_file_parse.keys.each do |valeur_tableau_configuration|

      if valeur_tableau_configuration.include? "db."
        case valeur_tableau_configuration
        when "db.username"
          default_config_file_parse[libriciel]["database"]["user"] = conf_file_parse[valeur_tableau_configuration]

        when "db.password"
          default_config_file_parse[libriciel]["database"]["password"] = conf_file_parse[valeur_tableau_configuration]

        when "db.name"
          default_config_file_parse[libriciel]["database"]["name"] = conf_file_parse[valeur_tableau_configuration]

        when "db.url"
          default_config_file_parse[libriciel]["database"]["host_conf"] = conf_file_parse[valeur_tableau_configuration].gsub("jdbc:mysql://", "").gsub(/\/#{conf_file_parse["db.name"]}\z/, "")

        end
      elsif valeur_tableau_configuration == "hostname"
        default_config_file_parse[libriciel]["fullname"] = conf_file_parse[valeur_tableau_configuration]

      elsif valeur_tableau_configuration == "mail.from"
        default_config_file_parse[libriciel]["domain"] = conf_file_parse[valeur_tableau_configuration].gsub(/\S*@/, "")

      end
    end
  else
    puts "autres parametres"
  end

  ## Ici pour surcharge de mes valeurs par défaut du config .JSON
  ## Qui est généré par défaut
  File.open("#{default_config_file}", "w") {|f| f.write(JSON.pretty_generate(default_config_file_parse))}
end

.compare_files_pastell(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file) ⇒ Object

 Partie pour Pastell à contnuer



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/libriciel.rb', line 447

def self.compare_files_pastell(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file)
  if !default_config_file_parse[libriciel]
    default_config_file_parse[libriciel] = {}

  end

  if File.readlines(conf_file).grep(/BD_/).size > 0
    conf_file_parse = JSON.parse(File.read(conf_file))
    if !default_config_file_parse[libriciel]["database"]
      default_config_file_parse[libriciel]["database"] = {}

    end

    conf_file_parse.keys.each do |valeur_tableau_configuration|

      if valeur_tableau_configuration.include? "BD_"
        case valeur_tableau_configuration
        when "BD_USER"
          default_config_file_parse[libriciel]["database"]["user"] = conf_file_parse[valeur_tableau_configuration]

        when "BD_PASS"
          default_config_file_parse[libriciel]["database"]["password"] = conf_file_parse[valeur_tableau_configuration]

        when "BD_DSN"

          default_config_file_parse[libriciel]["database"]["host_conf"] = conf_file_parse[valeur_tableau_configuration]

        end
      elsif valeur_tableau_configuration == "hostname"
        default_config_file_parse[libriciel]["fullname"] = conf_file_parse[valeur_tableau_configuration]

      elsif valeur_tableau_configuration == "mail.from"
        default_config_file_parse[libriciel]["domain"] = conf_file_parse[valeur_tableau_configuration].gsub(/\S*@/, "")

      end
    end
  end
end

.compare_files_webdelib(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file) ⇒ Object

 Partie pour Webdelib à continuer



487
488
489
# File 'lib/libriciel.rb', line 487

def self.compare_files_webdelib(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file)

end

.compare_json(template_json, conf_json, table_valeur_diff) ⇒ Object

faire une seconde fonction qui va prendre le tableau généré par celle ci pour le lire et inscrire dans le conf.json parsé au bon endroit “iparapheur”: {} par exemple les valeurs pour etre ready pour la mise à jour



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/libriciel.rb', line 637

def self.compare_json(template_json, conf_json, table_valeur_diff)
  if !FileUtils.compare_file(template_json, conf_json)

    template_file_json = JSON.parse(File.read(template_json))
    conf_file_json = JSON.parse(File.read(conf_json))

    template_file_json.keys.each do |keys_template_file_lvl_1|

      if !conf_file_json.keys.include? keys_template_file_lvl_1
        puts "Attention pas bonne syntaxe de la configuration"
        exit 222
      else
        if conf_file_json[keys_template_file_lvl_1] == template_file_json[keys_template_file_lvl_1]

        else

          ## Les if suivants sur le conf_json sont exclusif pour le iParapheur et
          ## la mise en place de l'aborescence du config.json
          if File.basename(conf_json) == "iparapheur-global.properties.json"
            keys_template_file_ip = "ihm"

            if !table_valeur_diff[keys_template_file_ip]
              table_valeur_diff[keys_template_file_ip] = {}

            end
            table_valeur_diff["ihm"][keys_template_file_lvl_1] = conf_file_json[keys_template_file_lvl_1]

          elsif File.basename(conf_json) == "alfresco-global.properties.json"
            keys_template_file_ip = "alf"

            if !table_valeur_diff[keys_template_file_ip]
              table_valeur_diff[keys_template_file_ip] = {}

            end
            table_valeur_diff["alf"][keys_template_file_lvl_1] = conf_file_json[keys_template_file_lvl_1]

          else
            table_valeur_diff[keys_template_file_lvl_1] = conf_file_json[keys_template_file_lvl_1]

          end
        end
      end
    end

  else
    # Mettre en place gestion des erreurs et raises
  end

  # Return du tableau avec les infos à ajouter au tableau config.json parsé
  return table_valeur_diff
end

.concat_config_json(config_json_file, tableau, libriciel) ⇒ Object

 Fonction qui va prendre le tableau généré par celle ci pour le lire et inscrire dans le conf.json parsé au bon endroit “iparpaheur”: {} par exemple les valeurs pour etre ready pour la mise à jour



625
626
627
628
629
630
631
632
633
# File 'lib/libriciel.rb', line 625

def self.concat_config_json(config_json_file, tableau, libriciel)
  config_json_file_parse = JSON.parse(File.read(config_json_file))
  config_json_file_parse[libriciel] = config_json_file_parse[libriciel].merge(tableau)

  ## Ici pour surcharge de mes valeurs par défaut du config .JSON
  ## Qui est généré par défaut
  File.open("#{config_json_file}", "w") {|f| f.write(JSON.pretty_generate(config_json_file_parse))}

end

.config_json_generator(template_json_file, config_json_file, libriciel, default_config_json) ⇒ Object

 Générateur du config.json avec les infos modifiées du fichier de configuration  par rapport au template



691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/libriciel.rb', line 691

def self.config_json_generator(template_json_file, config_json_file, libriciel, default_config_json)
  tableau = {}
  template_json_file.each do |each_template|
    config_json_file.each do |each_conf_files|
      if File.basename(each_template) == File.basename(each_conf_files)
        compare_json(each_template, each_conf_files, tableau)
        concat_config_json(default_config_json, tableau, libriciel)
      #else
        # Le test donne deux fichier différents qui n'ont pas à etre comparés
      end
    end
  end
end

.cp_json(files, directory) ⇒ Object



792
793
794
# File 'lib/libriciel.rb', line 792

def self.cp_json(files, directory)
    FileUtils.cp(files, directory)
end

.ip_conf_to_json(line, tableau) ⇒ Object

 Fonction permettant de parser les fichiers de properties iParapheur en JSON



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/libriciel.rb', line 706

def self.ip_conf_to_json(line, tableau)
  array_properties_value = line.split(/[=\n]+/)
  array_properties_dot_split = array_properties_value[0].split(/[\.\n]+/)

  if array_properties_dot_split[0].include? "parapheur"
     array_properties_parapheur_split = array_properties_value[0].split(/parapheur\./)
     tableau[array_properties_parapheur_split[1]] = array_properties_value[1]

  elsif array_properties_value[0].include? "authentication.chain" or array_properties_value[0].include? "alfresco.authentication.allowGuestLogin" or array_properties_value[0].include? "synchronization.autoCreatePeopleOnLogin"
      if array_properties_value[0].include? "#"
        array_properties_value[0] = array_properties_value[0].gsub("#", "")
        tableau[array_properties_value[0]] = array_properties_value[1]
      else
        tableau[array_properties_value[0]] = array_properties_value[1]
      end
  else
      tableau[array_properties_value[0]] = array_properties_value[1]

  end
  return tableau
end

.mv_json(files, directory) ⇒ Object



788
789
790
# File 'lib/libriciel.rb', line 788

def self.mv_json(files, directory)
    FileUtils.mv(files, directory)
end

.pa_conf_to_json(line, tableau) ⇒ Object

 Fonction permettant de parser les fichiers de conf pastell en json



729
730
731
732
733
734
735
736
# File 'lib/libriciel.rb', line 729

def self.pa_conf_to_json(line, tableau)
  tmp_clean = line.gsub(/define\(/, "").gsub(/\);/, "")
  array_properties_value = tmp_clean.split(/[, \n]+/)

  tableau[array_properties_value[0]] = array_properties_value[1].gsub(/\\;/, "")

  return tableau
end

.parse_spec_conf(conf_file, caracs_to_extract_from, carac_tableau, separateur) ⇒ Object

 Fonction pour l’extraction d’une chaine de caractère  (par exemple le xmx et le xms dans le ctl.sh)  Fichier en entrée, la ou les chaines de caractères à parser  Petit tableau en retours pour pouvoir le faire corrspondre au tableau



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/libriciel.rb', line 596

def self.parse_spec_conf(conf_file, caracs_to_extract_from, carac_tableau, separateur)
  conf_file_open = File.open(conf_file)

  conf_file_open.each_line do |line|
    line_split = line.split

    caracs_to_extract_from.each do |carac_to_split|
      if line.include? carac_to_split
        line_grep = line_split.grep(/#{carac_to_split}/)
        carac_mis_en_forme = carac_to_split.gsub(/[^0-9A-Za-z]/, '').to_s.downcase

        line_grep.each do |line_to_gsub|
          if !carac_mis_en_forme.empty?
            carac_tableau[carac_mis_en_forme] = line_to_gsub.gsub("#{carac_to_split}", '').gsub(/[^0-9A-Za-z]/, '')

          else
            # puts "entrée vide"
          end

        end
      end
    end
  end
  return carac_tableau

end

.parsing_conf(files, directory_json) ⇒ Object

 Fonction qui fait le parsing des fichiers en confs en data_bag



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/libriciel.rb', line 749

def self.parsing_conf(files, directory_json)
  Array(files).each do |file|

    # Récupération des nom de fichier dans la chemin absolu
    array_absolute_path_to_file = file.split('/')
    config_file_to_write = "/tmp/#{array_absolute_path_to_file.last}.json"

    config_files = File.open(file).read

    # Si existant on surcharge
    File.open("#{config_file_to_write}", "w") {|f| f.write("") }

    tableau = {}
    ## Passage de chaque ligne en json pour le fichier de conf en cours
    config_files.each_line do |line|
      if line.include? "define("
        pa_conf_to_json(line, tableau)

      elsif line.include? "Configure::write("
        wd_conf_to_json(line, tableau)

      elsif line.include? "="
        if !line[0,1].include? "#" or line.include? "ldap"
          ip_conf_to_json(line, tableau)
        end

      end
    end

    ## Ecriture du parsing en json du tableau généré par les fonctions dans le fichier json
    File.open("#{config_file_to_write}", "a") {|f| f.write(JSON.pretty_generate(tableau)) }

    ## Copie des fichiers dans le répertoire commun
    mv_json("#{config_file_to_write}", directory_json.to_s)
  end
end

.table_comparaison(all_conf_files, caracs_to_extract_from, libriciel, default_config_file) ⇒ Object

Fonction pour la table de correspondance qui va permettre  D’ajouter au config.json existant les bonnes valeurs  Par exemple le db.host avec le “database” : “hostname” = “” pour



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/libriciel.rb', line 570

def self.table_comparaison(all_conf_files, caracs_to_extract_from, libriciel, default_config_file)
  default_config_file_parse = JSON.parse(File.read(default_config_file))
  all_conf_files.each do |conf_file|
    conf_file_open = File.open(conf_file)

    case libriciel
    when "iparapheur"
      compare_files_iparapheur(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file)

    when "webdelib"
      compare_files_webdelib(default_config_file_parse, conf_file_open,conf_file, libriciel, caracs_to_extract_from, default_config_file)

    when "pastell"
      compare_files_pastell(default_config_file_parse, conf_file_open, conf_file, libriciel, caracs_to_extract_from, default_config_file)

    else
      puts "Outils pas dans la liste"
    end
  end
end

.wd_conf_to_json(line, tableau) ⇒ Object

 Fonction permettant de parser les fichiers de conf webdelib en json



739
740
741
742
743
744
745
746
# File 'lib/libriciel.rb', line 739

def self.wd_conf_to_json(line, tableau)
  tmp_clean = line.gsub(/Configure::write\(/, "").gsub(/\);/, "")
  array_properties_value = tmp_clean.split(/[, \n]+/)

  tableau[array_properties_value[0]] = array_properties_value[1]

  return tableau
end