<commit_before>var aws = require('aws-sdk')
var util = require('../util')
var colors = require('colors')
// Todo search by both internal and external dns/ip? private-dns-name, private-ip-address
// Todo figure out how to do tags
var filterNames = {
	'id': 'instance-id',
	'name': 'tag:Name',
	'ip': 'association.public-ip',
	'ipv6': 'association.public-ip',
	'hostname': 'dns-name',
	'type': 'instance-type',
	'tag': 'tag:{TAG}'
}

var amazon = module.exports = {
	search: function (account, filters, callback) {
		// Remove me
		//filters = [{ name: 'name', value: 'MojangStatus' }]
		//filters = [{ name: 'name', value: 'MojangStatus' }, { name: 'hostname', value: 'ec2-54-204-36-51.compute-1.amazonaws.com'}, { name: 'hostname', value: 'ec2-107-22-228-99.compute-1.amazonaws.com'}]
		var params = {
		  Filters: [
		    {
			  Name: 'instance-state-name',
		      Values: [
		        'running',
		      ]
		    }
		  ]
		}
		if (filters && filters.length > 0) {
			var filtersToAdd = {}
			filters.forEach(function (filter) {
				var name = filterNames[filter.name]
				if (name != null) {
					if (filtersToAdd[name] == null) {
						filtersToAdd[name] = { 
							Name: name, 
							Values: [ filter.value ] 
						}
					} else {
						filtersToAdd[name].Values.push(filter.value)
					}
				} else {
					console.log('Ignoring invalid filter %s'.red, filter.name)
				}
			})
			Object.keys(filtersToAdd).forEach(function (key) {
				var filter = filtersToAdd[key]
				params.Filters.push(filter)
			})
		}
		var result = []
		aws.config.update({ accessKeyId: account.publicToken, secretAccessKey: account.token })
		var todo = account.regions.length;
		account.regions.forEach(function (region) {
			amazon.searchRegion(region, params, function (servers) {
				servers.forEach(function (server) {
					result.push({
						'id': server.Instances[0].InstanceId,
						'name': amazon.findName(server.Instances[0].Tags),
						'region': region,
						// Todo show ipv6? command line argument?
						'hostname': server.Instances[0].PublicDnsName,
						'account': account
					})
					server.Instances[0].Tags[0].Value
				})
				todo--;
				if (todo == 0) {
					callback(result);
				}
			})
		})
	},
	searchRegion: function (region, params, callback) {
		var ec2 = new aws.EC2({ region: region })
		// Todo, check for NextToken and use for pagination, in case of more than 1k servers
		// Todo, error handling
		ec2.describeInstances(params, function(err, data) {
		  if (err) {
		  	return console.log(err, err.stack)
		  }
		  if (data.NextToken) {
		  	console.log('NextToken found, more servers available')
		  }
		  callback(data.Reservations)
		  //console.log(data.Reservations[0].Instances[0].Placement.AvailabilityZone)
		})
	},
	findName: function (tags) {
		var result = tags.filter(function (element) {
			return element.Key == 'Name'
		})
		return result[0].Value
	},
	ssh: function (server, user, port, keyfile, options) {
		util.ssh(server, user, port, keyfile, options)
	},

	display: function (server, index) {
		util.display(server, index)
	},

	regions: ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2']
}<commit_msg>Show public ip if dns doesn't exist for amazon<commit_after>var aws = require('aws-sdk')
var util = require('../util')
var colors = require('colors')
// Todo search by both internal and external dns/ip? private-dns-name, private-ip-address
// Todo figure out how to do tags
var filterNames = {
	'id': 'instance-id',
	'name': 'tag:Name',
	'ip': 'association.public-ip',
	'ipv6': 'association.public-ip',
	'hostname': 'dns-name',
	'type': 'instance-type',
	'tag': 'tag:{TAG}'
}

var amazon = module.exports = {
	search: function (account, filters, callback) {
		// Remove me
		//filters = [{ name: 'name', value: 'MojangStatus' }]
		//filters = [{ name: 'name', value: 'MojangStatus' }, { name: 'hostname', value: 'ec2-54-204-36-51.compute-1.amazonaws.com'}, { name: 'hostname', value: 'ec2-107-22-228-99.compute-1.amazonaws.com'}]
		var params = {
		  Filters: [
		    {
			  Name: 'instance-state-name',
		      Values: [
		        'running',
		      ]
		    }
		  ]
		}
		if (filters && filters.length > 0) {
			var filtersToAdd = {}
			filters.forEach(function (filter) {
				var name = filterNames[filter.name]
				if (name != null) {
					if (filtersToAdd[name] == null) {
						filtersToAdd[name] = { 
							Name: name, 
							Values: [ filter.value ] 
						}
					} else {
						filtersToAdd[name].Values.push(filter.value)
					}
				} else {
					console.log('Ignoring invalid filter %s'.red, filter.name)
				}
			})
			Object.keys(filtersToAdd).forEach(function (key) {
				var filter = filtersToAdd[key]
				params.Filters.push(filter)
			})
		}
		var result = []
		aws.config.update({ accessKeyId: account.publicToken, secretAccessKey: account.token })
		var todo = account.regions.length;
		account.regions.forEach(function (region) {
			amazon.searchRegion(region, params, function (servers) {
				servers.forEach(function (server) {
					result.push({
						'id': server.Instances[0].InstanceId,
						'name': amazon.findName(server.Instances[0].Tags),
						'region': region,
						// Todo show ipv6? command line argument?
						'hostname': server.Instances[0].PublicDnsName ? server.Instances[0].PublicDnsName : server.Instances[0].PublicIpAddress,
						'account': account
					})
				})
				todo--;
				if (todo == 0) {
					callback(result);
				}
			})
		})
	},
	searchRegion: function (region, params, callback) {
		var ec2 = new aws.EC2({ region: region })
		// Todo, check for NextToken and use for pagination, in case of more than 1k servers
		// Todo, error handling
		ec2.describeInstances(params, function(err, data) {
		  if (err) {
		  	return console.log(err, err.stack)
		  }
		  if (data.NextToken) {
		  	console.log('NextToken found, more servers available')
		  }
		  callback(data.Reservations)
		  //console.log(data.Reservations[0].Instances[0].Placement.AvailabilityZone)
		})
	},
	findName: function (tags) {
		var result = tags.filter(function (element) {
			return element.Key == 'Name'
		})
		return result[0].Value
	},
	ssh: function (server, user, port, keyfile, options) {
		util.ssh(server, user, port, keyfile, options)
	},

	display: function (server, index) {
		util.display(server, index)
	},

	regions: ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2']
}
{'fraction_non_alphanumeric': 0.16507545426547582, 'fraction_numerical': 0.014474899907607022, 'mean_word_length': 3.1107594936708862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4466171', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2241, 'n_words': 487}