2019年10月18日金曜日

tf.aws.sh

#!/usr/bin/sh

cmd_filename=${0##*/}

export AWS_ACCESS_KEY_ID="aaaaaaaaaa"
export AWS_SECRET_ACCESS_KEY="ssssssssss"
export AWS_DEFAULT_REGION="ap-northeast-1"

# export TF_LOG="DEBUG"
export CHECKPOINT_DISABLE="true"

TerraformCmd="./terraform"
TerraformCmd_apply_opt="-auto-approve"
TerraformCmd_fmt_opt="-recursive ."

file_resource_list_txt="${cmd_filename}.resource.list.txt"
file_txt="${cmd_filename}.txt"
file_tf="${cmd_filename}.tf"
file_tmp_tf="${cmd_filename}.tmp.tf"


# ================
# main.
# ================
main() {
  if [ "$1" = "" ]; then
    printUsage
  elif [ "$1" = "apply" ]; then
    TerraformCmd_apply
  elif [ "$1" = "fmt" ]; then
    TerraformCmd_fmt
  elif [ "$1" = "import" ]; then
    TerraformCmd_import
  elif [ "$1" = "plan" ]; then
    TerraformCmd_plan
  elif [ "$1" = "init" ]; then
    TerraformCmd_init
  elif [ "$1" = "remove" ]; then
    TerraformCmd_remove
  elif [ "$1" = "showInfo" ]; then
    TerraformCmd_showInfo
  else
    printUsage
  fi
}


# ================
# Print usage.
# ================
printUsage() {
  cat <<__EOF__
Usage:
  ${cmd_filename} import
  ${cmd_filename} apply
  ${cmd_filename} fmt
  ${cmd_filename} plan
  ${cmd_filename} init
  ${cmd_filename} remove
  ${cmd_filename} showInfo
__EOF__
}


# ================
# Filesystem delete files.
# ================
Filesystem_deleteFiles() {
  for filePath in \
    "$@"
  do
    if [ -f "${filePath}" ]; then
      echo "#" \
        rm "${filePath}"
        rm "${filePath}"
    fi
    if [ -f "${filePath}" ]; then
      echo Did not delete file "${filePath}".
      exit 11
    fi
  done
}


# ================
# TerraformFile Make empty file_tmp_tf.
# ================
TerraformFile_makeEmptyTf() {
  if [ ! -f "${file_resource_list_txt}" ]; then
    echo Does not exist file ${file_resource_list_txt}.
    exit 21
  fi
  Filesystem_deleteFiles \
    "${file_tmp_tf}"
  cat "$file_resource_list_txt" | \
  while read aws_resource_type aws_resource_name; do
    TerraformFile_printEmptyTf \
      "${aws_resource_type}" \
      "${aws_resource_name}" \
      >> "${file_tmp_tf}"
  done
}


# ================
# MakeTerraformFile Make one empty resource tf.
# ================
TerraformFile_printEmptyTf() {
  aws_resource_type="$1"
  aws_resource_name="$2"
  tf_resource_name="${aws_resource_name}_tf"
  cat <<__EOF__
resource "${aws_resource_type}" "${tf_resource_name}" {
}
__EOF__
}


# ================
# Terraform cmd.
# ================
TerraformCmd() {
  for tf_commamd in \
    "$@"
  do
    echo "#" \
      "${TerraformCmd}" ${tf_commamd}
      "${TerraformCmd}" ${tf_commamd}
    if [ $? -ne 0 ]; then
      echo Exit code is $?.
      exit 22
    fi
  done
}


# ================
# Terraform Show info.
# ================
TerraformCmd_showInfo() {
  echo "#" \
    cat "${file_resource_list_txt}"
    cat "${file_resource_list_txt}"
  TerraformCmd \
    "version" \
    "providers" \
    "validate -json"
}


# ================
# Terraform apply.
# ================
TerraformCmd_apply() {
  Filesystem_deleteFiles \
    "${file_txt}" \
    "${file_tmp_tf}"
  TerraformCmd_showInfo
  TerraformCmd_init
  TerraformCmd_plan
  TerraformCmd "apply ${TerraformCmd_apply_opt}"
}


# ================
# Terraform fmt.
# ================
TerraformCmd_fmt() {
  TerraformCmd "fmt ${TerraformCmd_fmt_opt}"
}


# ================
# Terraform init.
# ================
TerraformCmd_init() {
  TerraformCmd_fmt
  TerraformCmd "init"
}


# ================
# Terraform plan.
# ================
TerraformCmd_plan() {
  TerraformCmd "plan"
}


# ================
# Terraform remove.
# ================
TerraformCmd_remove() {
  if [ ! -f "${file_tf}" ]; then
    echo Does not exist file "${file_tf}".
    exit 23
  fi
  mv "${file_tf}" "${file_tf}.tmp"
  TerraformCmd_apply
  mv "${file_tf}.tmp" "${file_tf}"
}


# ================
# Terraform import.
# ================
TerraformCmd_import() {
  Filesystem_deleteFiles \
    "terraform.tfstate" \
    "${file_txt}" \
    "${file_tf}" \
    "${file_tmp_tf}"
  TerraformCmd_showInfo

  TerraformFile_makeEmptyTf
  echo "#" \
    cat "${file_tmp_tf}"
    cat "${file_tmp_tf}"
  TerraformCmd_init

  # Make file_txt.
  cat "$file_resource_list_txt" | \
  while read aws_resource_type aws_resource_name; do
    tf_resource_name="${aws_resource_name}_tf"
    TerraformCmd \
      "import ${aws_resource_type}.${tf_resource_name} ${aws_resource_name}"
    TerraformCmd \
      "state show -no-color ${aws_resource_type}.${tf_resource_name}" \
      >> "${file_txt}"
  done
  Filesystem_deleteFiles \
    "${file_tmp_tf}"

  # Make file_tf.
  sed \
    -e "s/\( arn \)/ #\1/g" \
    -e "s/\( id \)/ #\1/g" \
    -e "s/\( bucket_domain_name \)/ #\1/g" \
    -e "s/\( bucket_regional_domain_name \)/ #\1/g" \
    "${file_txt}" \
    >> "${file_tf}"

  TerraformCmd_fmt

  echo "#" \
    cat "${file_tf}"
    cat "${file_tf}"

  TerraformCmd_plan
}


main $@

exit 0

0 件のコメント:

コメントを投稿