<commit_before>#!/bin/bash

# Default key name
key='coreos-cluster'

AWS_PROFILE=${AWS_PROFILE:-coreos-cluster}
AWS_USER=${AWS_USER:-coreos-cluster}
CLUSTER_NAME=${CLUSTER_NAME:-coreos-cluster}

echo "Getting AWS account number..."
AWS_ACCOUNT=$(aws --profile ${AWS_PROFILE} iam get-user --user-name=${AWS_USER} | jq ".User.Arn" | grep -Eo '[[:digit:]]{12}')
echo $AWS_ACCOUNT

TMP_DIR=keypairs

create(){
  if  aws --profile ${AWS_PROFILE} ec2 describe-key-pairs --key-name ${key} > /dev/null 2>&1 ;
  then
    echo "keypair ${key} already exists."
  else
    mkdir -p ${TMP_DIR}
    chmod 700 ${TMP_DIR}
    echo "Creating ${key} and uploading to s3"
    aws --profile ${AWS_PROFILE} ec2 create-key-pair --key-name ${key} --query 'KeyMaterial' --output text > ${TMP_DIR}/${key}.pem
    aws --profile ${AWS_PROFILE} s3 cp ${TMP_DIR}/${key}.pem s3://${AWS_ACCOUNT}-${CLUSTER_NAME}-config/keypairs/${key}.pem
    # copy the key to user's home .ssh
    # cp ${TMP_DIR}/${key}.pem ${HOME}/.ssh; chmod 600 ${HOME}/.ssh/${key}.pem
    chmod 600 ${TMP_DIR}/${key}.pem
    echo "ssh-add ${TMP_DIR}/${key}.pem"
    ssh-add ${TMP_DIR}/${key}.pem
    # Clean up
    # rm -rf ${TMP_DIR}
  fi
}

destroy(){
  if  ! aws --profile ${AWS_PROFILE} ec2 describe-key-pairs --key-name ${key} > /dev/null 2>&1 ;
  then
    echo "keypair ${key} does not exists."
  else
    if [ -f ${TMP_DIR}/${key}.pem ];
    then
      echo "Remove from ssh agent"
      ssh-add -L |grep "${TMP_DIR}/${key}.pem" > ${TMP_DIR}/${key}.pub
      [ -f ${TMP_DIR}/${key}.pub ] && ssh-add -d ${TMP_DIR}/${key}.pub
      echo "Delete s3://${AWS_ACCOUNT}-${CLUSTER_NAME}-config/keypairs/${key}.pem."
      aws --profile ${AWS_PROFILE} s3 rm s3://${AWS_ACCOUNT}-${CLUSTER_NAME}-config/keypairs/${key}.pem
      echo "Delete aws ${key}"
      aws --profile ${AWS_PROFILE} ec2 delete-key-pair --key-name ${key}  
      rm -rf ${TMP_DIR}/${key}.pem
    fi
  fi 
}

while getopts ":c:d:h" OPTION
do
  case $OPTION in
    c)<commit_msg>test pubkey file size before removing.<commit_after>#!/bin/bash

# Default key name
key='coreos-cluster'

AWS_PROFILE=${AWS_PROFILE:-coreos-cluster}
AWS_USER=${AWS_USER:-coreos-cluster}
CLUSTER_NAME=${CLUSTER_NAME:-coreos-cluster}

echo "Getting AWS account number..."
AWS_ACCOUNT=$(aws --profile ${AWS_PROFILE} iam get-user --user-name=${AWS_USER} | jq ".User.Arn" | grep -Eo '[[:digit:]]{12}')
echo $AWS_ACCOUNT

TMP_DIR=keypairs

create(){
  if  aws --profile ${AWS_PROFILE} ec2 describe-key-pairs --key-name ${key} > /dev/null 2>&1 ;
  then
    echo "keypair ${key} already exists."
  else
    mkdir -p ${TMP_DIR}
    chmod 700 ${TMP_DIR}
    echo "Creating keypair ${key} and uploading to s3"
    aws --profile ${AWS_PROFILE} ec2 create-key-pair --key-name ${key} --query 'KeyMaterial' --output text > ${TMP_DIR}/${key}.pem
    aws --profile ${AWS_PROFILE} s3 cp ${TMP_DIR}/${key}.pem s3://${AWS_ACCOUNT}-${CLUSTER_NAME}-config/keypairs/${key}.pem
    # copy the key to user's home .ssh
    # cp ${TMP_DIR}/${key}.pem ${HOME}/.ssh; chmod 600 ${HOME}/.ssh/${key}.pem
    chmod 600 ${TMP_DIR}/${key}.pem
    echo "ssh-add ${TMP_DIR}/${key}.pem"
    ssh-add ${TMP_DIR}/${key}.pem
    # Clean up
    # rm -rf ${TMP_DIR}
  fi
}

destroy(){
  if  ! aws --profile ${AWS_PROFILE} ec2 describe-key-pairs --key-name ${key} > /dev/null 2>&1 ;
  then
    echo "keypair ${key} does not exists."
  else
    if [ -f ${TMP_DIR}/${key}.pem ];
    then
      echo "Remove from ssh agent"
      ssh-add -L |grep "${TMP_DIR}/${key}.pem" > ${TMP_DIR}/${key}.pub
      [ -s ${TMP_DIR}/${key}.pub ] && ssh-add -d ${TMP_DIR}/${key}.pub
      aws --profile ${AWS_PROFILE} s3 rm s3://${AWS_ACCOUNT}-${CLUSTER_NAME}-config/keypairs/${key}.pem
      echo "Delete aws keypair ${key}"
      aws --profile ${AWS_PROFILE} ec2 delete-key-pair --key-name ${key}  
      rm -rf ${TMP_DIR}/${key}.pem
    fi
  fi 
}

while getopts ":c:d:h" OPTION
do
  case $OPTION in
    c)
{'fraction_non_alphanumeric': 0.19877831509289895, 'fraction_numerical': 0.012471366759989819, 'mean_word_length': 3.189765458422175, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 15, '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': '4974256', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1579, 'n_words': 355}