<commit_before>module Singularity
  class Runner
    attr_accessor :request, :ip, :port

    def initialize(commands, uri)
      @commands = commands
      @uri = uri
      @ip = 0
      @port = 0
      @thisTask = ''
      @cpus = 1
      @mem = 512
      @image = "none"
      @mescaljson = []
      @dcosdeploy = []

      if File.exist?('dcos-deploy/config.yml')
        @dcosdeploy = YAML.load_file(File.join(Dir.pwd, 'dcos-deploy/config.yml'))
        @cpus = @dcosdeploy['singularity']['cpu']
        @mem = @dcosdeploy['singularity']['mem']
        @image = "#{@dcosdeploy['repo']}" + ":#{@dcosdeploy['tag']}"
      else
        @mescaljson = JSON.parse(File.read('.mescal.json'))
        @cpus = @mescaljson['cpus']
        @mem = @mescaljson['mem']
        @image = @mescaljson['image']
      end

      @projectName = @image.split('/').last
      user = (ENV['SINGULARITY_USER'] || `whoami`).chomp

      # establish 'id', 'command', and 'args' for filling in the data hash below
      case @commands[0]
        when 'ssh'
          # the 'command' becomes 'run the ssh bootstrap script'
          commandId = 'SSH'
          command = File.exist?('dcos-deploy/config.yml') ? "#{@dcosdeploy['singularity']['sshCmd']}" : "#{@mescaljson['sshCmd']}"
        when 'runx'
          # if 'runx' is passed, skip use of /sbin/my_init
          commandId = @commands.join('-').tr('@/\*?% []#$', '-')
          @commands.shift
          command = @commands.shift
          @args = []
          @setargs = true
          @commands.each { |i| @args.push i }
        else
          # else join /sbin/my_init with your commands
          commandId = @commands.join('-').tr('@/\*?% []#$', '-')
          command = '/sbin/my_init'
          @args = ['--']
          @setargs = true
          @commands.each { |i| @args.push i }
      end

      # create request/deploy json data
      data = {
        'id' => "#{@projectName}_#{commandId}_#{user}_#{Time.now.to_i.to_s}",
        'command' => "#{command}",
        'resources' => {
          'memoryMb' => @mem,
          'cpus' => @cpus,
          'numPorts' => 1
        },
        'env' => {
          'APPLICATION_ENV' => 'production'
        },
        'requestType' => 'RUN_ONCE',
        'containerInfo' => {
          'type' => 'DOCKER',
          'docker' => {
            'image' => @image,
            'network' => 'BRIDGE',
            'portMappings' => [{
              'containerPortType' => 'LITERAL',
              'containerPort' => 22,
              'hostPortType' => 'FROM_OFFER',
              'hostPort' => 0
            }]
          }
        }
      }
      if @setargs
        data['arguments'] = @args
      end
      @request = Request.new(data, @uri, @image.split(':')[1])
    end

    def run
      exit_code = 0
      @request.create
      @request.deploy
      waitForTaskToShowUp()

      if @commands[0] == 'ssh'
        runSsh
      else
        exit_code = runCmd
      end
    rescue Exception
      puts $!.red
      exit_code = 1
    ensure
      @request.delete rescue nil
      return exit_code
    end

    protected

    def waitForTaskToShowUp
      # repeatedly poll API for active tasks until ours shows up so we can get IP/PORT for SSH
      begin
        @tasks = JSON.parse(RestClient.get "#{@uri}/api/tasks/active", :content_type => :json)

        @tasks.each do |entry|<commit_msg>fix for new singularity api constraint on ids<commit_after>module Singularity
  class Runner
    attr_accessor :request, :ip, :port

    def initialize(commands, uri)
      @commands = commands
      @uri = uri
      @ip = 0
      @port = 0
      @thisTask = ''
      @cpus = 1
      @mem = 512
      @image = "none"
      @mescaljson = []
      @dcosdeploy = []

      if File.exist?('dcos-deploy/config.yml')
        @dcosdeploy = YAML.load_file(File.join(Dir.pwd, 'dcos-deploy/config.yml'))
        @cpus = @dcosdeploy['singularity']['cpu']
        @mem = @dcosdeploy['singularity']['mem']
        @image = "#{@dcosdeploy['repo']}" + ":#{@dcosdeploy['tag']}"
      else
        @mescaljson = JSON.parse(File.read('.mescal.json'))
        @cpus = @mescaljson['cpus']
        @mem = @mescaljson['mem']
        @image = @mescaljson['image']
      end

      @projectName = @image.split('/').last.gsub(':', '_')
      user = (ENV['SINGULARITY_USER'] || `whoami`).chomp

      # establish 'id', 'command', and 'args' for filling in the data hash below
      case @commands[0]
        when 'ssh'
          # the 'command' becomes 'run the ssh bootstrap script'
          commandId = 'SSH'
          command = File.exist?('dcos-deploy/config.yml') ? "#{@dcosdeploy['singularity']['sshCmd']}" : "#{@mescaljson['sshCmd']}"
        when 'runx'
          # if 'runx' is passed, skip use of /sbin/my_init
          commandId = @commands.join('-').tr('@/\*?% []#$', '-')
          @commands.shift
          command = @commands.shift
          @args = []
          @setargs = true
          @commands.each { |i| @args.push i }
        else
          # else join /sbin/my_init with your commands
          commandId = @commands.join('-').tr('@/\*?% []#$', '-')
          command = '/sbin/my_init'
          @args = ['--']
          @setargs = true
          @commands.each { |i| @args.push i }
      end

      # create request/deploy json data
      data = {
        'id' => "#{@projectName}_#{commandId}_#{user}_#{Time.now.to_i.to_s}",
        'command' => "#{command}",
        'resources' => {
          'memoryMb' => @mem,
          'cpus' => @cpus,
          'numPorts' => 1
        },
        'env' => {
          'APPLICATION_ENV' => 'production'
        },
        'requestType' => 'RUN_ONCE',
        'containerInfo' => {
          'type' => 'DOCKER',
          'docker' => {
            'image' => @image,
            'network' => 'BRIDGE',
            'portMappings' => [{
              'containerPortType' => 'LITERAL',
              'containerPort' => 22,
              'hostPortType' => 'FROM_OFFER',
              'hostPort' => 0
            }]
          }
        }
      }
      if @setargs
        data['arguments'] = @args
      end
      @request = Request.new(data, @uri, @image.split(':')[1])
    end

    def run
      exit_code = 0
      @request.create
      @request.deploy
      waitForTaskToShowUp()

      if @commands[0] == 'ssh'
        runSsh
      else
        exit_code = runCmd
      end
    rescue 
      p $!
      exit_code = 1
    ensure
      @request.delete rescue nil
      return exit_code
    end

    protected

    def waitForTaskToShowUp
      # repeatedly poll API for active tasks until ours shows up so we can get IP/PORT for SSH
      begin
        @tasks = JSON.parse(RestClient.get "#{@uri}/api/tasks/active", :content_type => :json)

        @tasks.each do |entry|
{'fraction_non_alphanumeric': 0.16158042164234115, 'fraction_numerical': 0.004422821760283061, 'mean_word_length': 1.8349352277475972, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4979015', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2161, 'n_words': 466}