<commit_before>var aws = require('aws-sdk');
var bunyan = require('bunyan');

module.exports = function (config, logger) {
  'use strict';

  logger = logger || bunyan.createLogger({name:'autoscaling-module'});

  var autoscaling = new aws.AutoScaling(config);

  var createLaunchConfiguration = function (launchConfigurationName, imageId, securityGroups, instanceType, keyName, cb) {
    var params = {
      LaunchConfigurationName: launchConfigurationName,
      ImageId: imageId,
      InstanceType: instanceType, // this need to be configurable
      SecurityGroups: securityGroups,
      KeyName: keyName
    };
    logger.info('createLaunchConfiguration', params);
    return autoscaling.createLaunchConfiguration(params, cb);
  };


  var createAutoScalingGroup = function (
    autoScalingGroupName, healthCheckType, launchConfigurationName,
    loadBalancerNames, vpcZoneIdentifier, tags, availabilityZones,
    minSize, maxSize, cb) {
    // tags is an array of object like
    // {
    //   Key: 'Name',
    //   PropagateAtLaunch: false,
    //   Value: 'Value'
    // }

    var params = {
      AutoScalingGroupName: autoScalingGroupName,
      MaxSize: maxSize, /* required */
      MinSize: minSize,
      HealthCheckGracePeriod: 10 * 60, // 10 minutes, might be 5?
      HealthCheckType: healthCheckType, // EC2 if a parent ELB could not be found
      LaunchConfigurationName: launchConfigurationName,
      LoadBalancerNames: loadBalancerNames,
      VPCZoneIdentifier: vpcZoneIdentifier,
      Tags: tags
    };

    if (availabilityZones !== null) {
      params.AvailabilityZones = availabilityZones;
      params.VPCZoneIdentifier = null;
    }

    logger.info(params, 'createAutoScalingGroup');
    autoscaling.createAutoScalingGroup(params, cb);
  };


  var describeLaunchConfigurations = function (launchConfigurationNames, cb) {
    var params = {
      LaunchConfigurationNames: launchConfigurationNames,
      MaxRecords: 1
    };
    logger.info(params, 'describeLaunchConfigurations');
    autoscaling.describeLaunchConfigurations(params, cb);
  };

  var describeAutoScalingGroups = function (autoScalingGroupNames, cb) {
    var params = {
      AutoScalingGroupNames: autoScalingGroupNames,
      MaxRecords: 1
    };

    logger.info(params, 'describeAutoScalingGroups');
    autoscaling.describeAutoScalingGroups(params, cb);
  };

  var autoScalingParams = function (autoScalingGroupName, cb) {
    describeAutoScalingGroups([autoScalingGroupName], function (err, result) {
      if (err) {
        return cb(err);
      }
      if (result.AutoScalingGroups.length > 0) {
        return cb(null, result.AutoScalingGroups[0]);
      } else {
        return cb(null, null);
      }
    });
  };

  var launchConfigurationParams = function (launchConfigurationName, cb) {
    describeLaunchConfigurations([launchConfigurationName], function (err, result) {
      if (err) {
        return cb(err);
      }
      if (result.LaunchConfigurations.length > 0) {
        return cb(null, result.LaunchConfigurations[0]);
      } else {
        return cb(null, null);
      }
    });
  };

  var createLaunchConfigurationAndGetParams = function (launchConfigurationName, imageId, securityGroups, instanceType, keyName, cb) {
    launchConfigurationParams(launchConfigurationName, function (err, params) {
      if (err) {
        return cb(err);
      }
      if (params !== null) {
        return cb(null, params);
      }

      createLaunchConfiguration(launchConfigurationName, imageId, securityGroups, instanceType, keyName, function (err, params) {
        if (err) {
          return cb(err);
        }

        launchConfigurationParams(launchConfigurationName, cb);
      });
    });
  };

  var createAutoScalingGroupAndGetParams = function (
    autoScalingGroupName, healthCheckType, launchConfigurationName,
    loadBalancerNames, vpcZoneIdentifier, tags, availabilityZones,
    minSize, maxSize, cb) {

    return autoScalingParams(autoScalingGroupName, function (err, params) {
      if (err) {
        return cb(err);
      }
      if (params !== null) {
        return cb(null, params);
      }

      createAutoScalingGroup(autoScalingGroupName, healthCheckType,
        launchConfigurationName, loadBalancerNames, vpcZoneIdentifier,
        tags, availabilityZones, minSize, maxSize, function (err, params) {

        if (err) {
          return cb(err);
        }

        autoScalingParams(autoScalingGroupName, cb);
      });
    });
  };


  return {
    createLaunchConfiguration: createLaunchConfiguration,
    createAutoScalingGroup: createAutoScalingGroup,
    describeLaunchConfigurations: describeLaunchConfigurations,
    describeAutoScalingGroups: describeAutoScalingGroups,
    createLaunchConfigurationAndGetParams: createLaunchConfigurationAndGetParams,
    createAutoScalingGroupAndGetParams: createAutoScalingGroupAndGetParams
  };
};
<commit_msg>Enable public ip on autoscaling instances.<commit_after>var aws = require('aws-sdk');
var bunyan = require('bunyan');

module.exports = function (config, logger) {
  'use strict';

  logger = logger || bunyan.createLogger({name:'autoscaling-module'});

  var autoscaling = new aws.AutoScaling(config);

  var createLaunchConfiguration = function (launchConfigurationName, imageId, securityGroups, instanceType, keyName, cb) {
    var params = {
      LaunchConfigurationName: launchConfigurationName,
      AssociatePublicIpAddress: true,
      ImageId: imageId,
      InstanceType: instanceType, // this need to be configurable
      SecurityGroups: securityGroups,
      KeyName: keyName
    };
    logger.info('createLaunchConfiguration', params);
    return autoscaling.createLaunchConfiguration(params, cb);
  };


  var createAutoScalingGroup = function (
    autoScalingGroupName, healthCheckType, launchConfigurationName,
    loadBalancerNames, vpcZoneIdentifier, tags, availabilityZones,
    minSize, maxSize, cb) {
    // tags is an array of object like
    // {
    //   Key: 'Name',
    //   PropagateAtLaunch: false,
    //   Value: 'Value'
    // }

    var params = {
      AutoScalingGroupName: autoScalingGroupName,
      MaxSize: maxSize, /* required */
      MinSize: minSize,
      HealthCheckGracePeriod: 10 * 60, // 10 minutes, might be 5?
      HealthCheckType: healthCheckType, // EC2 if a parent ELB could not be found
      LaunchConfigurationName: launchConfigurationName,
      LoadBalancerNames: loadBalancerNames,
      VPCZoneIdentifier: vpcZoneIdentifier,
      Tags: tags
    };

    if (availabilityZones !== null) {
      params.AvailabilityZones = availabilityZones;
      params.VPCZoneIdentifier = null;
    }

    logger.info(params, 'createAutoScalingGroup');
    autoscaling.createAutoScalingGroup(params, cb);
  };


  var describeLaunchConfigurations = function (launchConfigurationNames, cb) {
    var params = {
      LaunchConfigurationNames: launchConfigurationNames,
      MaxRecords: 1
    };
    logger.info(params, 'describeLaunchConfigurations');
    autoscaling.describeLaunchConfigurations(params, cb);
  };

  var describeAutoScalingGroups = function (autoScalingGroupNames, cb) {
    var params = {
      AutoScalingGroupNames: autoScalingGroupNames,
      MaxRecords: 1
    };

    logger.info(params, 'describeAutoScalingGroups');
    autoscaling.describeAutoScalingGroups(params, cb);
  };

  var autoScalingParams = function (autoScalingGroupName, cb) {
    describeAutoScalingGroups([autoScalingGroupName], function (err, result) {
      if (err) {
        return cb(err);
      }
      if (result.AutoScalingGroups.length > 0) {
        return cb(null, result.AutoScalingGroups[0]);
      } else {
        return cb(null, null);
      }
    });
  };

  var launchConfigurationParams = function (launchConfigurationName, cb) {
    describeLaunchConfigurations([launchConfigurationName], function (err, result) {
      if (err) {
        return cb(err);
      }
      if (result.LaunchConfigurations.length > 0) {
        return cb(null, result.LaunchConfigurations[0]);
      } else {
        return cb(null, null);
      }
    });
  };

  var createLaunchConfigurationAndGetParams = function (launchConfigurationName, imageId, securityGroups, instanceType, keyName, cb) {
    launchConfigurationParams(launchConfigurationName, function (err, params) {
      if (err) {
        return cb(err);
      }
      if (params !== null) {
        return cb(null, params);
      }

      createLaunchConfiguration(launchConfigurationName, imageId, securityGroups, instanceType, keyName, function (err, params) {
        if (err) {
          return cb(err);
        }

        launchConfigurationParams(launchConfigurationName, cb);
      });
    });
  };

  var createAutoScalingGroupAndGetParams = function (
    autoScalingGroupName, healthCheckType, launchConfigurationName,
    loadBalancerNames, vpcZoneIdentifier, tags, availabilityZones,
    minSize, maxSize, cb) {

    return autoScalingParams(autoScalingGroupName, function (err, params) {
      if (err) {
        return cb(err);
      }
      if (params !== null) {
        return cb(null, params);
      }

      createAutoScalingGroup(autoScalingGroupName, healthCheckType,
        launchConfigurationName, loadBalancerNames, vpcZoneIdentifier,
        tags, availabilityZones, minSize, maxSize, function (err, params) {

        if (err) {
          return cb(err);
        }

        autoScalingParams(autoScalingGroupName, cb);
      });
    });
  };


  return {
    createLaunchConfiguration: createLaunchConfiguration,
    createAutoScalingGroup: createAutoScalingGroup,
    describeLaunchConfigurations: describeLaunchConfigurations,
    describeAutoScalingGroups: describeAutoScalingGroups,
    createLaunchConfigurationAndGetParams: createLaunchConfigurationAndGetParams,
    createAutoScalingGroupAndGetParams: createAutoScalingGroupAndGetParams
  };
};

{'fraction_non_alphanumeric': 0.09451127059537047, 'fraction_numerical': 0.0028302840392196503, 'mean_word_length': 3.4347826086956523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2179478', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2667, 'n_words': 597}