<commit_before>#!/usr/bin/env python
import sys, os, logging, argparse, socket, textwrap, boto3

__version__='0.1'
try:
  import coloredlogs
  coloredlogs.install(
    isatty = True,
    show_name = False,
    show_severity = False,
    level = logging.NOTSET,
    severity_to_style = { 'DEBUG': {'color': 'blue'}},
  )
except:
  logging.basicConfig(
    stream = sys.stdout,
    format = '%(asctime)s ' + socket.gethostname() + ' %(levelname)-8s %(message)s',
    datefmt = "%Y-%m-%d %H:%M:%S",
    level = logging.NOTSET,
  )
log = logging.getLogger(__name__)

def find_autoscaling_group(client, component, stack_prefix):
  groups = client.describe_auto_scaling_groups()['AutoScalingGroups']
  for group in groups:
    count_found = 0
    for tag in group['Tags']:
      if (tag['Key'] == 'StackPrefix' and tag['Value'] == stack_prefix) or \
          (tag['Key'] == 'Component' and tag['Value'] == component):
        if count_found == 1:
          log.debug('Autoscaling Group: %r', group)
          return group
        else:
          count_found += 1
  raise ValueError('No Autoscaling Group found for stack_prefix: {} and component: {}.'.format(stack_prefix, component))

def update_snapshot_id(launch_config, device_name, snapshot_id):
  launch_config.pop('LaunchConfigurationARN')
  launch_config.pop('CreatedTime')
  launch_config.pop('KernelId')
  launch_config.pop('RamdiskId')
  devices = launch_config['BlockDeviceMappings']
  for device in devices:
    if device['DeviceName'] == device_name:
      ebs = device['Ebs']
      ebs['SnapshotId'] = snapshot_id

def find_launch_conf(client, launch_conf_name):
  launch_conf = \
  client.describe_launch_configurations(LaunchConfigurationNames=[launch_conf_name])['LaunchConfigurations'][0]
  log.debug('Launch Configuration to update: %r', launch_conf)
  return launch_conf

def delete_launch_conf(client, launch_conf_name):
  client.delete_launch_configuration(LaunchConfigurationName=launch_conf_name)

def update_autoscaling_group(client, group_name, launch_conf_name):
  client.update_auto_scaling_group(
    AutoScalingGroupName=group_name,
    LaunchConfigurationName=launch_conf_name
  )

def create_launch_conf(client, launch_config, new_launch_config_name):
  launch_config['LaunchConfigurationName'] = new_launch_config_name
  client.create_launch_configuration(**launch_config)

def repoint_autoscaling_group(client, group_name, launch_config, new_launch_config_name):
  create_launch_conf(client, launch_config, new_launch_config_name)
  update_autoscaling_group(client, group_name, new_launch_config_name)

def update_snapshot_id_to_launch_conf(snapshot_id, component, stack_prefix, device_name):
  client = boto3.client('autoscaling')
  group = find_autoscaling_group(client, component, stack_prefix)
  group_name = group['AutoScalingGroupName']<commit_msg>bugfix: UserData is retrieved in Base64, so we need to decode it before creating a copy of the LC<commit_after>#!/usr/bin/env python
import sys, os, logging, argparse, socket, textwrap, boto3
import base64

__version__='0.1'
try:
  import coloredlogs
  coloredlogs.install(
    isatty = True,
    show_name = False,
    show_severity = False,
    level = logging.NOTSET,
    severity_to_style = { 'DEBUG': {'color': 'blue'}},
  )
except:
  logging.basicConfig(
    stream = sys.stdout,
    format = '%(asctime)s ' + socket.gethostname() + ' %(levelname)-8s %(message)s',
    datefmt = "%Y-%m-%d %H:%M:%S",
    level = logging.NOTSET,
  )
log = logging.getLogger(__name__)

def find_autoscaling_group(client, component, stack_prefix):
  groups = client.describe_auto_scaling_groups()['AutoScalingGroups']
  for group in groups:
    count_found = 0
    for tag in group['Tags']:
      if (tag['Key'] == 'StackPrefix' and tag['Value'] == stack_prefix) or \
          (tag['Key'] == 'Component' and tag['Value'] == component):
        if count_found == 1:
          log.debug('Autoscaling Group: %r', group)
          return group
        else:
          count_found += 1
  raise ValueError('No Autoscaling Group found for stack_prefix: \'{}\' and component: \'{}\'.'.format(stack_prefix, component))

def update_snapshot_id(launch_config, device_name, snapshot_id):
  launch_config.pop('LaunchConfigurationARN')
  launch_config.pop('CreatedTime')
  launch_config.pop('KernelId')
  launch_config.pop('RamdiskId')
  devices = launch_config['BlockDeviceMappings']
  for device in devices:
    if device['DeviceName'] == device_name:
      ebs = device['Ebs']
      ebs['SnapshotId'] = snapshot_id
      return
  raise ValueError('No Device found: \'{}\' in launch configuration \'{}\''.format(device_name, launch_config['LaunchConfigurationName']))

def find_launch_conf(client, launch_conf_name):
  launch_conf = \
  client.describe_launch_configurations(LaunchConfigurationNames=[launch_conf_name])['LaunchConfigurations'][0]
  launch_conf['UserData'] = base64.b64decode(launch_conf['UserData'])
  log.debug('Launch Configuration to update: %r', launch_conf)
  return launch_conf

def delete_launch_conf(client, launch_conf_name):
  client.delete_launch_configuration(LaunchConfigurationName=launch_conf_name)

def update_autoscaling_group(client, group_name, launch_conf_name):
  client.update_auto_scaling_group(
    AutoScalingGroupName=group_name,
    LaunchConfigurationName=launch_conf_name
  )

def create_launch_conf(client, launch_config, new_launch_config_name):
  launch_config['LaunchConfigurationName'] = new_launch_config_name
  client.create_launch_configuration(**launch_config)

def repoint_autoscaling_group(client, group_name, launch_config, new_launch_config_name):
  create_launch_conf(client, launch_config, new_launch_config_name)
  update_autoscaling_group(client, group_name, new_launch_config_name)

def update_snapshot_id_to_launch_conf(snapshot_id, component, stack_prefix, device_name):
  client = boto3.client('autoscaling')
  group = find_autoscaling_group(client, component, stack_prefix)
  group_name = group['AutoScalingGroupName']
{'fraction_non_alphanumeric': 0.11494828161494829, 'fraction_numerical': 0.004337671004337671, 'mean_word_length': 5.523394994559304, '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': '2392078', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1882, 'n_words': 371}