<commit_before>#!/usr/bin/env node

var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));

var path = require('path');
var express = require('express');
var app = express();

var env = {
  baseUrl : process.env.COREOS_IPXE_SERVER_BASE_URL ? process.env.COREOS_IPXE_SERVER_BASE_URL : '',
  dataDirectory : process.env.COREOS_IPXE_SERVER_DATA_DIR ? process.env.COREOS_IPXE_SERVER_DATA_DIR : "/opt/coreos-ipxe-server",
  listenPort : process.env.COREOS_IPXE_SERVER_LISTEN_ADDR ? process.env.COREOS_IPXE_SERVER_LISTEN_ADDR : "4777"
}

var script = '#!ipxe\r\n' +
'set coreos-version {{.Version}}\r\n' +
'set base-url http://{{.BaseUrl}}/images/amd64-usr/${coreos-version}\r\n' +
'kernel ${base-url}/coreos_production_pxe.vmlinuz{{.Options}}\r\n' +
'initrd ${base-url}/coreos_production_pxe_image.cpio.gz\r\n' +
'boot\r\n';

var cloudConfigBase = path.join(env.dataDirectory,'/configs');

app.get('/', getScript);

app.use('/configs', express.static(path.join(env.dataDirectory,'/configs')));
app.use('/images', express.static(path.join(env.dataDirectory,'/images')));
app.use('/sshKeys', express.static(path.join(env.dataDirectory,'/sshKeys')));

app.listen(env.listenPort.split(':')[1]);

console.log('Listening on ' + env.listenPort)

function getScript(req, res){
  var profileId = req.query['profile'] || '';
  if(env.baseUrl === ''){
    env.baseUrl = req.hostname;
  }
  console.log('Request for profile ' + profileId)
  getProfile(profileId)
    .then(JSON.parse)
    .then(getOptions)
    .then(processScript)
    .then(function(genScript){
      res.header('Content-Type', 'text/plain');
      res.status(200).send(genScript);
    }).catch(function(err){
      res.status(500).send(err);
    })
}

function getProfile(profileId){
  if(profileId === ''){
    return Promise.resolve('{}');
  }
  var profilePath = path.join(env.dataDirectory, 'profiles', profileId + '.json');
  return fs.readFileAsync(profilePath, 'utf8');
}

function getOptions(profile){
  var options = {
    'cloud_config' : '',
    'console' : [],
    'coreos_autologin' : '',
    'root' : '',
    'rootfstype' : '',
    'sshkey' : '',
    'version' : '',
    toString : function(){
      var optString = "";
      optString += this.rootfstype ? (' rootfstype=' + this.rootfstype) : '';
      for(var i in this.console){
        optString += ' console=' + this.console[i];
      }
      optString += this.cloud_config ? (' cloud-config-url=http://' + env.baseUrl + '/configs/' + this.cloud_config + '.yml ') : '';
      optString += this.coreos_autologin ? (' coreos.autologin=' + this.coreos_autologin): '';
      optString += this.sshkey ? (' sshkey="' + fs.readFileSync(path.join(env.dataDirectory, 'sshKeys', this.sshkey + '.pub'), 'utf8')).trim() + '"' : '';
      optString += this.root ? (' root=' + this.root) : '';

      return optString;
    }
  };
  options.toString = options.toString.bind(options)

  for(var i in options){
    for(var j in profile){
      if(i === j){
        options[i] = profile[j];
      }
    }
  }<commit_msg>Dont ask<commit_after>#!/usr/bin/env node

var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));

var path = require('path');
var express = require('express');
var app = express();

var env = {
  baseUrl : process.env.COREOS_IPXE_SERVER_BASE_URL ? process.env.COREOS_IPXE_SERVER_BASE_URL : '',
  dataDirectory : process.env.COREOS_IPXE_SERVER_DATA_DIR ? process.env.COREOS_IPXE_SERVER_DATA_DIR : "/opt/coreos-ipxe-server",
  listenPort : process.env.COREOS_IPXE_SERVER_LISTEN_ADDR ? process.env.COREOS_IPXE_SERVER_LISTEN_ADDR : "4777"
}

var script = '#!ipxe\r\n' +
'set coreos-version {{.Version}}\r\n' +
'set base-url http://{{.BaseUrl}}/images/amd64-usr/${coreos-version}\r\n' +
'kernel ${base-url}/coreos_production_pxe.vmlinuz{{.Options}}\r\n' +
'initrd ${base-url}/coreos_production_pxe_image.cpio.gz\r\n' +
'boot\r\n';

var cloudConfigBase = path.join(env.dataDirectory,'/configs');

app.get('/', getScript);

app.use('/configs', express.static(path.join(env.dataDirectory,'/configs')));
app.use('/images', express.static(path.join(env.dataDirectory,'/images')));
app.use('/sshKeys', express.static(path.join(env.dataDirectory,'/sshKeys')));

app.listen(env.listenPort.split(':')[1]);

console.log('Listening on ' + env.listenPort)

function getScript(req, res){
  var profileId = req.query['profile'] || '';
  if(env.baseUrl === ''){
    env.baseUrl = req.hostname;
  }
  console.log('Request for profile ' + profileId)
  getProfile(profileId)
    .then(JSON.parse)
    .then(getOptions)
    .then(processScript)
    .then(function(genScript){
      res.header('Content-Type', 'text/plain');
      res.status(200).send(genScript);
    }).catch(function(err){
      res.status(500).send(err);
    })
}

function getProfile(profileId){
  if(profileId === ''){
    return Promise.resolve('{}');
  }
  var profilePath = path.join(env.dataDirectory, 'profiles', profileId + '.json');
  return fs.readFileAsync(profilePath, 'utf8');
}

function getOptions(profile){
  var options = {
    'cloud_config' : '',
    'console' : [],
    'coreos_autologin' : '',
    'root' : '',
    'rootfstype' : '',
    'sshkey' : '',
    'version' : '',
    toString : function(){
      var optString = "";
      optString += this.rootfstype ? ('  rootfstype=' + this.rootfstype) : '';
      for(var i in this.console){
        optString += ' console=' + this.console[i];
      }
      optString += this.cloud_config ? ('  cloud-config-url=http://' + env.baseUrl + '/configs/' + this.cloud_config + '.yml ') : '';
      optString += this.coreos_autologin ? (' coreos.autologin=' + this.coreos_autologin): '';
      optString += this.sshkey ? (' sshkey="' + fs.readFileSync(path.join(env.dataDirectory, 'sshKeys', this.sshkey + '.pub'), 'utf8')).trim() + '"' : '';
      optString += this.root ? (' root=' + this.root) : '';

      return optString;
    }
  };
  options.toString = options.toString.bind(options)

  for(var i in options){
    for(var j in profile){
      if(i === j){
        options[i] = profile[j];
      }
    }
  }
{'fraction_non_alphanumeric': 0.19425758818703856, 'fraction_numerical': 0.004922067268252666, 'mean_word_length': 3.8535031847133756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '384446', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2153, 'n_words': 323}