<commit_before>module Mobilize
  class Box
    module Action
      def view_ssh_cmd
        _box                      = self
        _ssh_cmd                  = "ssh -i #{Box.private_key_path} " +
                                    "-o 'UserKnownHostsFile=/dev/null' -o 'StrictHostKeyChecking=no' " +
                                    "#{_box.user_name}@#{_box.dns}"
        puts                        _ssh_cmd
      end

      def sh( _command,  _except = true, _streams = :stdout )
        _box                                  = self
        _ssh_args                             = { keys: [ Box.private_key_path ],
                                                  paranoid: false }
        _command_file_path                    = "/tmp/" + "#{ _command }#{ Time.now.utc.to_f.to_s }".to_md5
        _box.write                              _command, _command_file_path, false, false
        _send_args                            = [ "start", _box.dns, _box.user_name, _ssh_args ]
        _result                               = Net::SSH.send_w_retries( *_send_args ) do |_ssh|
                                                  _ssh.run _box.name, "bash -l -c 'sh #{ _command_file_path }'",
                                                           _except, [ _streams ].flatten
                                                end
        Net::SSH.send_w_retries( *_send_args ) { |_ssh| _ssh.run _box.name, "rm #{_command_file_path}" }

        if _streams == :stdout;_result[:stdout];else;_result;end
      end

      def cp( _loc_path, _rem_path, _mkdir = true, _log = true )
        _box                       = self
        _box.sh(                     "mkdir -p " + _rem_path.dirname ) if _mkdir
        _ssh_args                  = { keys: [ Box.private_key_path ],
                                       paranoid: false }
        _send_args                 = [ "start", _box.dns, _box.user_name, _ssh_args ]
        _result                    = Net::SCP.send_w_retries( *_send_args ) do |_scp|
                                       _scp.upload!( _loc_path, _rem_path, recursive: true ) do |_ch, _name, _sent, _total|
                                       Log.write( "#{ _name }: #{ _sent }/#{ _total }" ) if _log
                                       end
                                     end
        _result
      end

      def write( _string, _rem_path, _mkdir = true, _log = true )
        _box                      = self
        _temp_file_path           = "/tmp/" + "#{ _string }#{ Time.now.utc.to_f.to_s }".to_md5
        begin
          File.write                _temp_file_path, _string
          _box.cp                   _temp_file_path, _rem_path, _mkdir, false
        ensure
          FileUtils.rm              _temp_file_path, force: true
        end
        Log.write                   "Wrote: #{ _string.ellipsize 25 } to #{ _box.id }:#{ _rem_path }" if _log
      end

      def write_mobrc
        _box                  = self
        _mobrc_path           = _box.mobilize_config_dir + "/mobrc"

        _mob_envs             = ENV.select { |_key, _value|
                                              _key.starts_with? "MOB" }

        _mobrc_string         = _mob_envs.map { |_key, _value|
                                              %{export #{ _key }=#{ _value }}
                                              }.join "\n"

        _box.write              _mobrc_string, _mobrc_path
        true
      end

      def write_keys
        _box    = self
        _box.cp   Config.key_dir, _box.key_dir
      end

      def terminate( _session = Box.session )
        #terminates the remote then
        #deletes the local database version
        _box                          = self

        if _box.remote_id
          _session.terminate_instances  _box.remote_id
          Log.write                     "Terminated remote #{_box.remote_id} for #{_box.id}"
        end

        _box.delete
        Log.write                       "Deleted #{_box.id} from DB"

        true
      end

      def launch( _session = Box.session )

        _box                         = self

        _remote_params               = {key_name:      _box.keypair_name,
                                        group_ids:     _box.security_groups,
                                        instance_type:   _box.size}

        _remotes                     = _session.launch_instances(_box.ami, _remote_params)
        _remote                      = _remotes.first

        _box.update_attributes         remote_id: _remote[:aws_instance_id]
        _session.create_tag            _box.remote_id, "Name", _box.name
        _remote                      = _box.wait_for_running _session
        _box.sync                      _remote
      end

      def wait_for_running (_session  = Box.session )
        _box                          = self
        _remote                       = _box.remote _session
        while                           _remote[:aws_state] != "running"
          Log.write                     "remote #{_box.remote_id} still at #{_remote[:aws_state]} -- waiting 10 sec"
          sleep                         10
          _remote                     = _box.remote _session
        end
        _remote
      end

      def apt_install( _name, _version )
        _box                   = self
        Log.write                "Installing apt #{ _name } #{ _version }..."
        _box.sh                  "sudo apt-get install -y #{ _name }=#{ _version }"
      end

      def install_ruby
        _box            = self
        _box.sh           '\curl -L https://get.rvm.io | bash -s stable --ruby=1.9.3'
      end

      def install_mobilize_gem( _path = "c4ssio/mobilize" )
        _box                          = self
        _box.sh                         "rm -rf mobilize && " +
                                        "git clone http://u:p@github.com/#{ _path }.git --depth=1"
        _repo_revision                = _box.sh "cd mobilize && git log -1 --pretty=format:%H"
        _installed_revision           = begin; _box.sh "mob revision";rescue;nil;end
        if _installed_revision       != _repo_revision
          Log.write                     "Installing Mobilize on #{ _box.id }\n" +
                                        "installed revision: #{ _installed_revision.to_s }\n" +
                                        "repo revision: #{ _repo_revision }"
          _box.sh                       "cd mobilize && bundle install && rake install"
        else
           Log.write                    "mobilize revision #{ _installed_revision } already installed on #{ _box.id }"
        end
        _box.sh                         "rm -rf mobilize"
      end
<commit_msg>back to regular<commit_after>module Mobilize
  class Box
    module Action
      def view_ssh_cmd
        _box                      = self
        _ssh_cmd                  = "ssh -i #{Box.private_key_path} " +
                                    "-o 'UserKnownHostsFile=/dev/null' -o 'StrictHostKeyChecking=no' " +
                                    "#{_box.user_name}@#{_box.dns}"
        puts                        _ssh_cmd
      end

      def sh( _command,  _except = true, _streams = :stdout )
        _box                                  = self
        _ssh_args                             = { keys: [ Box.private_key_path ],
                                                  paranoid: false }
        _command_file_path                    = "/tmp/" + "#{ _command }#{ Time.now.utc.to_f.to_s }".to_md5
        _box.write                              _command, _command_file_path, false, false
        _send_args                            = [ "start", _box.dns, _box.user_name, _ssh_args ]
        _result                               = Net::SSH.send_w_retries( *_send_args ) do |_ssh|
                                                  _ssh.run _box.name, "bash -l -c 'sh #{ _command_file_path }'",
                                                           _except, [ _streams ].flatten
                                                end
        Net::SSH.send_w_retries( *_send_args ) { |_ssh| _ssh.run _box.name, "rm #{_command_file_path}" }

        if _streams == :stdout;_result[:stdout];else;_result;end
      end

      def cp( _loc_path, _rem_path, _mkdir = true, _log = true )
        _box                       = self
        _box.sh(                     "mkdir -p " + _rem_path.dirname ) if _mkdir
        _ssh_args                  = { keys: [ Box.private_key_path ],
                                       paranoid: false }
        _send_args                 = [ "start", _box.dns, _box.user_name, _ssh_args ]
        _result                    = Net::SCP.send_w_retries( *_send_args ) do |_scp|
                                       _scp.upload!( _loc_path, _rem_path, recursive: true ) do |_ch, _name, _sent, _total|
                                       Log.write( "#{ _name }: #{ _sent }/#{ _total }" ) if _log
                                       end
                                     end
        _result
      end

      def write( _string, _rem_path, _mkdir = true, _log = true )
        _box                      = self
        _temp_file_path           = "/tmp/" + "#{ _string }#{ Time.now.utc.to_f.to_s }".to_md5
        begin
          File.write                _temp_file_path, _string
          _box.cp                   _temp_file_path, _rem_path, _mkdir, false
        ensure
          FileUtils.rm              _temp_file_path, force: true
        end
        Log.write                   "Wrote: #{ _string.ellipsize 25 } to #{ _box.id }:#{ _rem_path }" if _log
      end

      def write_mobrc
        _box                  = self
        _mobrc_path           = _box.mobilize_config_dir + "/mobrc"

        _mob_envs             = ENV.select { |_key, _value|
                                              _key.starts_with? "MOB" }

        _mobrc_string         = _mob_envs.map { |_key, _value|
                                              %{export #{ _key }=#{ _value }}
                                              }.join "\n"

        _box.write              _mobrc_string, _mobrc_path
        true
      end

      def write_keys
        _box    = self
        _box.cp   Config.key_dir, _box.key_dir
      end

      def terminate( _session = Box.session )
        #terminates the remote then
        #deletes the local database version
        _box                          = self

        if _box.remote_id
          _session.terminate_instances  _box.remote_id
          Log.write                     "Terminated remote #{_box.remote_id} for #{_box.id}"
        end

        _box.delete
        Log.write                       "Deleted #{_box.id} from DB"

        true
      end

      def launch( _session = Box.session )

        _box                         = self

        _remote_params               = {key_name:      _box.keypair_name,
                                        group_ids:     _box.security_groups,
                                        instance_type:   _box.size}

        _remotes                     = _session.launch_instances(_box.ami, _remote_params)
        _remote                      = _remotes.first

        _box.update_attributes         remote_id: _remote[:aws_instance_id]
        _session.create_tag            _box.remote_id, "Name", _box.name
        _remote                      = _box.wait_for_running _session
        _box.sync                      _remote
      end

      def wait_for_running (_session  = Box.session )
        _box                          = self
        _remote                       = _box.remote _session
        while                           _remote[:aws_state] != "running"
          Log.write                     "remote #{_box.remote_id} still at #{_remote[:aws_state]} -- waiting 10 sec"
          sleep                         10
          _remote                     = _box.remote _session
        end
        _remote
      end

      def apt_install( _name, _version )
        _box                   = self
        Log.write                "Installing apt #{ _name } #{ _version }..."
        _box.sh                  "sudo apt-get install -y #{ _name }=#{ _version }"
      end

      def install_ruby
        _box            = self
        _box.sh           '\curl -L https://get.rvm.io | bash -s stable --ruby=1.9.3'
      end

      def install_mobilize_gem( _path = "mobilize/mobilize" )
        _box                          = self
        _box.sh                         "rm -rf mobilize && " +
                                        "git clone http://u:p@github.com/#{ _path }.git --depth=1"
        _repo_revision                = _box.sh "cd mobilize && git log -1 --pretty=format:%H"
        _installed_revision           = begin; _box.sh "mob revision";rescue;nil;end
        if _installed_revision       != _repo_revision
          Log.write                     "Installing Mobilize on #{ _box.id }\n" +
                                        "installed revision: #{ _installed_revision.to_s }\n" +
                                        "repo revision: #{ _repo_revision }"
          _box.sh                       "cd mobilize && bundle install && rake install"
        else
           Log.write                    "mobilize revision #{ _installed_revision } already installed on #{ _box.id }"
        end
        _box.sh                         "rm -rf mobilize"
      end

{'fraction_non_alphanumeric': 0.08220603537981269, 'fraction_numerical': 0.0020068381150587186, 'mean_word_length': 1.0067114093959733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1998994', 'n_tokens_mistral': 3748, 'n_tokens_neox': 3537, 'n_words': 796}