<commit_before>require "ood_core/refinements/hash_extensions"

module OodCore
  module BatchConnect
    class Factory
      using Refinements::HashExtensions

      # Build the VNC template from a configuration
      # @param config [#to_h] the configuration for the batch connect template
      def self.build_vnc(config)
        context = config.to_h.symbolize_keys.reject { |k, _| k == :template }
        Templates::VNC.new(context)
      end
    end

    module Templates
      # A batch connect template that starts up a VNC server within a batch job
      class VNC < Template
        # @param context [#to_h] the context used to render the template
        # @option context [#to_sym, Array<#to_sym>] :conn_params ([]) A list of
        #   connection parameters added to the connection file (`:host`,
        #   `:port`, `:password`, `:<PASSWORD>`, `:display` and `:websocket`
        #   will always exist)
        # @option context [#to_s] :websockify_cmd
        #   ("${WEBSOCKIFY_CMD:-/opt/websockify/run}") the path to the
        #   websockify script (assumes you don't modify `:after_script`)
        # @option context [#to_s] :vnc_log ("vnc.log") path to vnc server log
        #   file (assumes you don't modify `:before_script` or `:after_script`)
        # @option context [#to_s] :vnc_passwd ("<PASSWORD>") path to the file
        #   generated that contains the encrypted vnc password (assumes you
        #   don't modify `:before_script`)
        # @option context [#to_s] :vnc_args arguments used when starting up the
        #   vnc server (overrides any specific vnc argument) (assumes you don't
        #   modify `:before_script`)
        # @option context [#to_s] :name ("") name of the vnc server session
        #   (not set if blank or `:vnc_args` is set) (assumes you don't modify
        #   `:before_script`)
        # @option context [#to_s] :geometry ("") resolution of vnc display (not
        #   set if blank or `:vnc_args` is set) (assumes you don't modify
        #   `:before_script`)
        # @option context [#to_s] :dpi ("") dpi of vnc display (not set if
        #   blank or `:vnc_args` is set) (assumes you don't modify
        #   `:before_script`)
        # @option context [#to_s] :fonts ("") command delimited list of fonts
        #   available in vnc display (not set if blank or `:vnc_args` is set)
        #   (assumes you don't modify `:before_script`)
        # @option context [#to_s] :idle ("") timeout vnc server if no
        #   connection in this amount of time in seconds (not set if blank or
        #   `:vnc_args` is set) (assumes you don't modify `:before_script`)
        # @option context [#to_s] :extra_args ("") any extra arguments used
        #   when initializing the vnc server process (not set if blank or
        #   `:vnc_args` is set) (assumes you don't modify `:before_script`)
        # @option context [#to_s] :vnc_clean ("...") script used to clean up
        #   any active vnc sessions (assumes you don't modify `:before_script`
        #   or `:clean_script`)
        # @see Template
        def initialize(context = {})
          super
        end

        private
          # We need to know the VNC and websockify connection information
          def conn_params
            (super + [:display, :websocket, :spassword]).uniq
          end

          # Before running the main script, start up a VNC server and record
          # the connection information
          def before_script
            <<-EOT.gsub(/^ {14}/, "")
              # Setup one-time use passwords and initialize the VNC password
              function change_passwd () {
                echo "Setting VNC password..."
                password=$(create_passwd "#{password_size}")
                spassword=${spassword:-$(create_passwd "#{password_size}")}
                (
                  umask 077
                  echo -ne "${password}\\n${spassword}" | vncpasswd -f > "#{vnc_passwd}"
                )
              }
              change_passwd

              # Start up vnc server (if at first you don't succeed, try, try again)
              echo "Starting VNC server..."
              for i in $(seq 1 10); do
                # Clean up any old VNC sessions that weren't cleaned before
                #{vnc_clean}

                # Attempt to start VNC server
                VNC_OUT=$(vncserver -log "#{vnc_log}" -rfbauth "#{vnc_passwd}" -nohttpd -noxstartup #{vnc_args} 2>&1)
                VNC_PID=$(pgrep -s 0 Xvnc) # the script above will daemonize the Xvnc process
                echo "${VNC_OUT}"<commit_msg>check for nohttpd option before using it with vncserver (#767)<commit_after>require "ood_core/refinements/hash_extensions"

module OodCore
  module BatchConnect
    class Factory
      using Refinements::HashExtensions

      # Build the VNC template from a configuration
      # @param config [#to_h] the configuration for the batch connect template
      def self.build_vnc(config)
        context = config.to_h.symbolize_keys.reject { |k, _| k == :template }
        Templates::VNC.new(context)
      end
    end

    module Templates
      # A batch connect template that starts up a VNC server within a batch job
      class VNC < Template
        # @param context [#to_h] the context used to render the template
        # @option context [#to_sym, Array<#to_sym>] :conn_params ([]) A list of
        #   connection parameters added to the connection file (`:host`,
        #   `:port`, `:password`, `:spassword`, `:display` and `:websocket`
        #   will always exist)
        # @option context [#to_s] :websockify_cmd
        #   ("${WEBSOCKIFY_CMD:-/opt/websockify/run}") the path to the
        #   websockify script (assumes you don't modify `:after_script`)
        # @option context [#to_s] :vnc_log ("vnc.log") path to vnc server log
        #   file (assumes you don't modify `:before_script` or `:after_script`)
        # @option context [#to_s] :vnc_passwd ("vnc.passwd") path to the file
        #   generated that contains the encrypted vnc password (assumes you
        #   don't modify `:before_script`)
        # @option context [#to_s] :vnc_args arguments used when starting up the
        #   vnc server (overrides any specific vnc argument) (assumes you don't
        #   modify `:before_script`)
        # @option context [#to_s] :name ("") name of the vnc server session
        #   (not set if blank or `:vnc_args` is set) (assumes you don't modify
        #   `:before_script`)
        # @option context [#to_s] :geometry ("") resolution of vnc display (not
        #   set if blank or `:vnc_args` is set) (assumes you don't modify
        #   `:before_script`)
        # @option context [#to_s] :dpi ("") dpi of vnc display (not set if
        #   blank or `:vnc_args` is set) (assumes you don't modify
        #   `:before_script`)
        # @option context [#to_s] :fonts ("") command delimited list of fonts
        #   available in vnc display (not set if blank or `:vnc_args` is set)
        #   (assumes you don't modify `:before_script`)
        # @option context [#to_s] :idle ("") timeout vnc server if no
        #   connection in this amount of time in seconds (not set if blank or
        #   `:vnc_args` is set) (assumes you don't modify `:before_script`)
        # @option context [#to_s] :extra_args ("") any extra arguments used
        #   when initializing the vnc server process (not set if blank or
        #   `:vnc_args` is set) (assumes you don't modify `:before_script`)
        # @option context [#to_s] :vnc_clean ("...") script used to clean up
        #   any active vnc sessions (assumes you don't modify `:before_script`
        #   or `:clean_script`)
        # @see Template
        def initialize(context = {})
          super
        end

        private
          # We need to know the VNC and websockify connection information
          def conn_params
            (super + [:display, :websocket, :spassword]).uniq
          end

          # Before running the main script, start up a VNC server and record
          # the connection information
          def before_script
            <<-EOT.gsub(/^ {14}/, "")
              # Setup one-time use passwords and initialize the VNC password
              function change_passwd () {
                echo "Setting VNC password..."
                password=$(create_passwd "#{password_size}")
                spassword=${spassword:-$(create_passwd "#{password_size}")}
                (
                  umask 077
                  echo -ne "${password}\\n${spassword}" | vncpasswd -f > "#{vnc_passwd}"
                )
              }
              change_passwd

              # Start up vnc server (if at first you don't succeed, try, try again)
              echo "Starting VNC server..."
              for i in $(seq 1 10); do
                # Clean up any old VNC sessions that weren't cleaned before
                #{vnc_clean}

                # for turbovnc 3.0 compatability.
                if timeout 2 vncserver --help 2>&1 | grep 'nohttpd' >/dev/null 2>&1; then
                  HTTPD_OPT='-nohttpd'
                fi

                # Attempt to start VNC server
                VNC_OUT=$(vncserver -log "#{vnc_log}" -rfbauth "#{vnc_passwd}" $HTTPD_OPT -noxstartup #{vnc_args} 2>&1)
                VNC_PID=$(pgrep -s 0 Xvnc) # the script above will daemonize the Xvnc process
                echo "${VNC_OUT}"
{'fraction_non_alphanumeric': 0.10293338981255956, 'fraction_numerical': 0.003388753574076035, 'mean_word_length': 2.1680644079168063, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '3423061', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2697, 'n_words': 965}