# https://docs.python.org/ja/3/library/concurrent.futures.html?highlight=processpoolexecutor
# https://qiita.com/__init__/items/91e5841ed53d55a7895e
import concurrent.futures
import logging
# import math
import math
import os
import time
from logging import getLogger, StreamHandler, FileHandler, Formatter
PRIMES = [
0,
10000,
1,
20000,
30000,
2,
40000,
50000,
60000,
3,
4,
5,
6,
]
def mkLogger(type, n):
# 1.loggerの設定
# loggerオブジェクトの宣言
logger = getLogger(type)
# loggerのログレベル設定(ハンドラに渡すエラーメッセージのレベル)
logger.setLevel(logging.DEBUG)
# 2.handlerの設定
# ログ出力フォーマット設定
handler_format = Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 2-1.標準出力のhandler
# handlerの生成
stream_handler = StreamHandler()
# handlerのログレベル設定(ハンドラが出力するエラーメッセージのレベル)
stream_handler.setLevel(logging.DEBUG)
# ログ出力フォーマット設定
stream_handler.setFormatter(handler_format)
# 2-2.テキスト出力のhandler
# handlerの生成
file_handler = FileHandler('Ex13ProcessPoolExecutor.' + type + '.log', 'a')
# handlerのログレベル設定(ハンドラが出力するエラーメッセージのレベル)
file_handler.setLevel(logging.DEBUG)
# ログ出力フォーマット設定
file_handler.setFormatter(handler_format)
# 3.loggerにhandlerをセット
# 標準出力のhandlerをセット
logger.addHandler(stream_handler)
# テキスト出力のhandlerをセット
logger.addHandler(file_handler)
return logger
def is_prime(n):
print("# %s %s %s\n" % (str(os.getpid()), str(n), "start"))
logger = mkLogger(str(n), n)
logger.debug(str(os.getpid()) + " " + str(n) + " Start Hello World!")
print("# %s %s %s\n" % (str(os.getpid()), str(n), "logger.debug"))
# sqrt_n = int(math.floor(math.sqrt(n))) * 1
print("# %s %s %s\n" % (str(os.getpid()), str(n), "exec " + str(n)))
# time.sleep(n)
for i in range(0, n, 1):
None
logger.debug(str(os.getpid()) + " " + str(n) + " Hello World! " + str(i))
print("# %s %s %s\n" % (str(os.getpid()), str(n), "end"))
logger.debug(str(os.getpid()) + " " + str(n) + " End Hello World!")
return [[True, os.getpid()]]
def main():
logger = mkLogger("Main", 999)
logger.debug(str(os.getpid()) + " " + "Main" + " Hello World!")
for n in PRIMES:
# print("#n=", n, nlf[str(n)])
lf = "Ex13ProcessPoolExecutorA." + str(n) + ".log"
with concurrent.futures.ProcessPoolExecutor(max_workers=15) as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
logger.debug('%d is prime: %s' % (number, prime))
if __name__ == '__main__':
main()
kn1661nk
2019年11月18日月曜日
2019年10月20日日曜日
tterm-aws01.bat
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "PATH=C:\Program Files (x86)\teraterm;%PATH%"
set "host=hhhhhhhhhh.amazonaws.com"
start "" "ttermpro.exe" "ec2-user@!host!" ^
/nosecuritywarning /auth=publickey /user=uuuuuuuuuu ^
/keyfile="%~dp0\\kkkkkkkkkk.pem"
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "PATH=C:\Program Files (x86)\teraterm;%PATH%"
set "host=hhhhhhhhhh.amazonaws.com"
start "" "ttermpro.exe" "ec2-user@!host!" ^
/nosecuritywarning /auth=publickey /user=uuuuuuuuuu ^
/keyfile="%~dp0\\kkkkkkkkkk.pem"
tf.aws.sh.tf
# aws_dynamodb_table.tbl01_tf:
resource "aws_dynamodb_table" "tbl01_tf" {
# arn = "arn:aws:dynamodb:ap-northeast-1:751075880470:table/tbl01"
billing_mode = "PROVISIONED"
hash_key = "fld01"
# id = "tbl01"
name = "tbl01"
read_capacity = 5
stream_enabled = false
tags = {}
write_capacity = 5
attribute {
name = "fld01"
type = "N"
}
point_in_time_recovery {
enabled = false
}
timeouts {}
ttl {
attribute_name = "fld00"
enabled = true
}
}
# aws_dynamodb_table.tbl11_tf:
resource "aws_dynamodb_table" "tbl11_tf" {
# arn = "arn:aws:dynamodb:ap-northeast-1:751075880470:table/tbl11"
billing_mode = "PROVISIONED"
hash_key = "fld11"
# id = "tbl11"
name = "tbl11"
read_capacity = 5
stream_enabled = false
tags = {}
write_capacity = 5
attribute {
name = "fld11"
type = "S"
}
point_in_time_recovery {
enabled = false
}
timeouts {}
ttl {
attribute_name = "fld11"
enabled = true
}
}
# aws_s3_bucket.awss3bkt001_tf:
resource "aws_s3_bucket" "awss3bkt001_tf" {
# arn = "arn:aws:s3:::awss3bkt001"
bucket = "awss3bkt001"
# bucket_domain_name = "awss3bkt001.s3.amazonaws.com"
# bucket_regional_domain_name = "awss3bkt001.s3.ap-northeast-1.amazonaws.com"
hosted_zone_id = "Z2M4EHUR26P7ZW"
# id = "awss3bkt001"
region = "ap-northeast-1"
request_payer = "BucketOwner"
tags = {}
versioning {
enabled = false
mfa_delete = false
}
}
# aws_s3_bucket.awss3bkt002_tf:
resource "aws_s3_bucket" "awss3bkt002_tf" {
# arn = "arn:aws:s3:::awss3bkt002"
bucket = "awss3bkt002"
# bucket_domain_name = "awss3bkt002.s3.amazonaws.com"
# bucket_regional_domain_name = "awss3bkt002.s3.ap-northeast-1.amazonaws.com"
hosted_zone_id = "Z2M4EHUR26P7ZW"
# id = "awss3bkt002"
region = "ap-northeast-1"
request_payer = "BucketOwner"
tags = {}
versioning {
enabled = false
mfa_delete = false
}
}
resource "aws_dynamodb_table" "tbl01_tf" {
# arn = "arn:aws:dynamodb:ap-northeast-1:751075880470:table/tbl01"
billing_mode = "PROVISIONED"
hash_key = "fld01"
# id = "tbl01"
name = "tbl01"
read_capacity = 5
stream_enabled = false
tags = {}
write_capacity = 5
attribute {
name = "fld01"
type = "N"
}
point_in_time_recovery {
enabled = false
}
timeouts {}
ttl {
attribute_name = "fld00"
enabled = true
}
}
# aws_dynamodb_table.tbl11_tf:
resource "aws_dynamodb_table" "tbl11_tf" {
# arn = "arn:aws:dynamodb:ap-northeast-1:751075880470:table/tbl11"
billing_mode = "PROVISIONED"
hash_key = "fld11"
# id = "tbl11"
name = "tbl11"
read_capacity = 5
stream_enabled = false
tags = {}
write_capacity = 5
attribute {
name = "fld11"
type = "S"
}
point_in_time_recovery {
enabled = false
}
timeouts {}
ttl {
attribute_name = "fld11"
enabled = true
}
}
# aws_s3_bucket.awss3bkt001_tf:
resource "aws_s3_bucket" "awss3bkt001_tf" {
# arn = "arn:aws:s3:::awss3bkt001"
bucket = "awss3bkt001"
# bucket_domain_name = "awss3bkt001.s3.amazonaws.com"
# bucket_regional_domain_name = "awss3bkt001.s3.ap-northeast-1.amazonaws.com"
hosted_zone_id = "Z2M4EHUR26P7ZW"
# id = "awss3bkt001"
region = "ap-northeast-1"
request_payer = "BucketOwner"
tags = {}
versioning {
enabled = false
mfa_delete = false
}
}
# aws_s3_bucket.awss3bkt002_tf:
resource "aws_s3_bucket" "awss3bkt002_tf" {
# arn = "arn:aws:s3:::awss3bkt002"
bucket = "awss3bkt002"
# bucket_domain_name = "awss3bkt002.s3.amazonaws.com"
# bucket_regional_domain_name = "awss3bkt002.s3.ap-northeast-1.amazonaws.com"
hosted_zone_id = "Z2M4EHUR26P7ZW"
# id = "awss3bkt002"
region = "ap-northeast-1"
request_payer = "BucketOwner"
tags = {}
versioning {
enabled = false
mfa_delete = false
}
}
2019年10月18日金曜日
tf.provider.tf
provider "aws" {
#access_key = "aaaaaaaaaa"
#secret_key = "ssssssssss"
region = "ap-northeast-1"
version = "2.30"
}
#access_key = "aaaaaaaaaa"
#secret_key = "ssssssssss"
region = "ap-northeast-1"
version = "2.30"
}
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
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
tf.aws.sh.resource.list.txt
aws_dynamodb_table tbl01
aws_dynamodb_table tbl11
aws_s3_bucket awss3bkt001
aws_s3_bucket awss3bkt002
aws_dynamodb_table tbl11
aws_s3_bucket awss3bkt001
aws_s3_bucket awss3bkt002
tf.aws.bat
@rem
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "cmd_filename=%~nx0"
set "AWS_ACCESS_KEY_ID=aaaaaaaaaa"
set "AWS_SECRET_ACCESS_KEY=ssssssssss"
set "AWS_DEFAULT_REGION=ap-northeast-1"
@rem set "TF_LOG=DEBUG"
set "CHECKPOINT_DISABLE=true"
set "TerraformCmd=.\terraform.exe"
set "TerraformCmd_apply_opt=-auto-approve"
set "TerraformCmd_fmt_opt=-recursive ."
set "dot_cmd=uuuuuuuuuu\PlantUML\graphviz\release\bin\dot.exe"
set "file_resource_list_txt=!cmd_filename!.resource.list.txt"
set "file_txt=!cmd_filename!.txt"
set "file_tf=!cmd_filename!.tf"
set "file_tmp_tf=!cmd_filename!.tmp.tf"
set "file_png=!cmd_filename!.png"
@rem ================
@rem main.
@rem ================
:main
if "%1"=="" (
goto :printUsage
) else if "%1"=="apply" (
goto :TerraformCmd_apply
) else if "%1"=="fmt" (
goto :TerraformCmd_fmt
) else if "%1"=="import" (
goto :TerraformCmd_import
) else if "%1"=="plan" (
goto :TerraformCmd_plan
) else if "%1"=="init" (
goto :TerraformCmd_init
) else if "%1"=="remove" (
goto :TerraformCmd_remove
) else if "%1"=="showInfo" (
goto :TerraformCmd_showInfo
) else (
goto :printUsage
)
goto :eof
@rem ================
@rem Print usage.
@rem ================
:printUsage
echo Usage:
echo !cmd_filename! import
echo !cmd_filename! apply
echo !cmd_filename! fmt
echo !cmd_filename! plan
echo !cmd_filename! init
echo !cmd_filename! remove
echo !cmd_filename! showInfo
goto :eof
@rem ================
@rem Filesystem delete files.
@rem ================
:Filesystem_deleteFiles
for %%z in (
%*
) do (
set "filePath=%%z"
if exist "!filePath!" (
echo ^
del "!filePath!"
del "!filePath!"
)
if exist "!filePath!" (
echo Did not delete file "${filePath}".
exit /b 11
)
)
goto :eof
@rem ================
@rem TerraformFile Make empty file_tmp_tf.
@rem ================
:TerraformFile_makeEmptyTf
if not exist "!file_resource_list_txt!" (
echo Does not exist file "!file_resource_list_txt!".
exit /b 21
)
for /f "usebackq tokens=1,2 delims= " %%y in (
`type "!file_resource_list_txt!"`
) do (
set "aws_resource_type=%%y"
set "aws_resource_name=%%z"
call :TerraformFile_printEmptyTf ^
"!aws_resource_type!" ^
"!aws_resource_name!" ^
>> "!file_tmp_tf!"
)
goto :eof
@rem ================
@rem TerraformFile Make one empty resource tf.
@rem ================
:TerraformFile_printEmptyTf
set "aws_resource_type=%~1"
set "aws_resource_name=%~2"
set "tf_resource_name=!aws_resource_name!_tf"
echo resource "!aws_resource_type!" "%!tf_resource_name!" {
echo }
goto :eof
@rem ================
@rem Terraform cmd.
@rem ================
:TerraformCmd
for %%z in (
%*
) do (
set "tf_commamd=%%z"
set "tf_commamd=!tf_commamd:"=!"
@rem "
echo # ^
"!TerraformCmd!" !tf_commamd!
"!TerraformCmd!" !tf_commamd!
if not !errorlevel!==0 (
echo Exit code is !errorlevel!.
exit /b 22
)
)
goto :eof
@rem ================
@rem Terraform Show info.
@rem ================
:TerraformCmd_showInfo
echo # ^
type "!file_resource_list_txt!"
type "!file_resource_list_txt!"
call :TerraformCmd ^
"version" ^
"providers" ^
"validate -json"
goto :eof
@rem ================
@rem Terraform apply.
@rem ================
:TerraformCmd_apply
call :Filesystem_deleteFiles ^
"!file_txt!" ^
"!file_tmp_tf!" ^
"!file_png!"
call :TerraformCmd_showInfo
call :TerraformCmd_init
call :TerraformCmd_plan
call :TerraformCmd "apply !TerraformCmd_apply_opt!"
goto :eof
@rem ================
@rem Terraform fmt.
@rem ================
:TerraformCmd_fmt
call :TerraformCmd "fmt !TerraformCmd_fmt_opt!"
goto :eof
@rem ================
@rem Terraform init.
@rem ================
:TerraformCmd_init
call :TerraformCmd_fmt
call :TerraformCmd "init"
goto :eof
@rem ================
@rem Terraform plan.
@rem ================
:TerraformCmd_plan
call :TerraformCmd "plan"
goto :eof
@rem ================
@rem Terraform remove.
@rem ================
:TerraformCmd_remove
if not exist "!file_tf!" (
echo Does not exist file "!file_tf!".
exit /b 23
)
ren "!file_tf!" "!file_tf!.tmp"
call :TerraformCmd_apply
ren "!file_tf!.tmp" "!file_tf!"
goto :eof
@rem ================
@rem Terraform import.
@rem ================
:TerraformCmd_import
call :Filesystem_deleteFiles ^
"terraform.tfstate" ^
"!file_txt!" ^
"!file_tf!" ^
"!file_tmp_tf!" ^
"!file_png!"
call :TerraformCmd_showInfo
call :TerraformFile_makeEmptyTf
echo # ^
type "!file_tmp_tf!"
type "!file_tmp_tf!"
call :TerraformCmd_init
@rem Make file_txt.
echo # ^
type "!file_resource_list_txt!"
type "!file_resource_list_txt!"
for /f "usebackq tokens=1,2 delims= " %%i in (
`type "!file_resource_list_txt!"`
) do (
set "aws_resource_type=%%i"
set "aws_resource_name=%%j"
set "tf_resource_name=!aws_resource_name!_tf"
echo # ^
aws_resource_type=!aws_resource_type! ^
aws_resource_name=!aws_resource_name! ^
tf_resource_name=!tf_resource_name!
echo # ^
"!TerraformCmd!" import "!aws_resource_type!.!tf_resource_name!" !aws_resource_name!
"!TerraformCmd!" import "!aws_resource_type!.!tf_resource_name!" !aws_resource_name!
if not !errorlevel!==0 exit /b 1
echo # ^
"!TerraformCmd!" state show -no-color "!aws_resource_type!.!tf_resource_name!"
"!TerraformCmd!" state show -no-color "!aws_resource_type!.!tf_resource_name!" >> "!file_txt!"
if not !errorlevel!==0 exit /b 2
)
call :Filesystem_deleteFiles ^
"!file_tmp_tf!"
ren "!file_txt!" "!file_tf!"
call :TerraformCmd_fmt
ren "!file_tf!" "!file_txt!"
@rem Make file_tf.
for /f "usebackq tokens=1* delims=" %%i in (
`type "!file_txt!"`
) do (
set "line=%%i"
set "line=!line: arn = # arn !"
set "line=!line: id = # id !"
set "line=!line: bucket_domain_name = # bucket_domain_name !"
set "line=!line: bucket_regional_domain_name = # bucket_regional_domain_name !"
(echo !line!) >> "!file_tf!"
)
call :TerraformCmd_fmt
echo # ^
type "!file_tf!"
type "!file_tf!"
call :TerraformCmd_plan
@rem Make file_png.
call :L_mkPng
@rem pause
goto :eof
@rem ================
@rem Make file_png.
@rem ================
:L_mkPng
if exist "!dot_cmd!" (
echo # ^
"!TerraformCmd!" graph ^| "!dot_cmd!" -Tpng ^> "!file_png!"
"!TerraformCmd!" graph | "!dot_cmd!" -Tpng > "!file_png!"
)
goto :eof
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "cmd_filename=%~nx0"
set "AWS_ACCESS_KEY_ID=aaaaaaaaaa"
set "AWS_SECRET_ACCESS_KEY=ssssssssss"
set "AWS_DEFAULT_REGION=ap-northeast-1"
@rem set "TF_LOG=DEBUG"
set "CHECKPOINT_DISABLE=true"
set "TerraformCmd=.\terraform.exe"
set "TerraformCmd_apply_opt=-auto-approve"
set "TerraformCmd_fmt_opt=-recursive ."
set "dot_cmd=uuuuuuuuuu\PlantUML\graphviz\release\bin\dot.exe"
set "file_resource_list_txt=!cmd_filename!.resource.list.txt"
set "file_txt=!cmd_filename!.txt"
set "file_tf=!cmd_filename!.tf"
set "file_tmp_tf=!cmd_filename!.tmp.tf"
set "file_png=!cmd_filename!.png"
@rem ================
@rem main.
@rem ================
:main
if "%1"=="" (
goto :printUsage
) else if "%1"=="apply" (
goto :TerraformCmd_apply
) else if "%1"=="fmt" (
goto :TerraformCmd_fmt
) else if "%1"=="import" (
goto :TerraformCmd_import
) else if "%1"=="plan" (
goto :TerraformCmd_plan
) else if "%1"=="init" (
goto :TerraformCmd_init
) else if "%1"=="remove" (
goto :TerraformCmd_remove
) else if "%1"=="showInfo" (
goto :TerraformCmd_showInfo
) else (
goto :printUsage
)
goto :eof
@rem ================
@rem Print usage.
@rem ================
:printUsage
echo Usage:
echo !cmd_filename! import
echo !cmd_filename! apply
echo !cmd_filename! fmt
echo !cmd_filename! plan
echo !cmd_filename! init
echo !cmd_filename! remove
echo !cmd_filename! showInfo
goto :eof
@rem ================
@rem Filesystem delete files.
@rem ================
:Filesystem_deleteFiles
for %%z in (
%*
) do (
set "filePath=%%z"
if exist "!filePath!" (
echo ^
del "!filePath!"
del "!filePath!"
)
if exist "!filePath!" (
echo Did not delete file "${filePath}".
exit /b 11
)
)
goto :eof
@rem ================
@rem TerraformFile Make empty file_tmp_tf.
@rem ================
:TerraformFile_makeEmptyTf
if not exist "!file_resource_list_txt!" (
echo Does not exist file "!file_resource_list_txt!".
exit /b 21
)
for /f "usebackq tokens=1,2 delims= " %%y in (
`type "!file_resource_list_txt!"`
) do (
set "aws_resource_type=%%y"
set "aws_resource_name=%%z"
call :TerraformFile_printEmptyTf ^
"!aws_resource_type!" ^
"!aws_resource_name!" ^
>> "!file_tmp_tf!"
)
goto :eof
@rem ================
@rem TerraformFile Make one empty resource tf.
@rem ================
:TerraformFile_printEmptyTf
set "aws_resource_type=%~1"
set "aws_resource_name=%~2"
set "tf_resource_name=!aws_resource_name!_tf"
echo resource "!aws_resource_type!" "%!tf_resource_name!" {
echo }
goto :eof
@rem ================
@rem Terraform cmd.
@rem ================
:TerraformCmd
for %%z in (
%*
) do (
set "tf_commamd=%%z"
set "tf_commamd=!tf_commamd:"=!"
@rem "
echo # ^
"!TerraformCmd!" !tf_commamd!
"!TerraformCmd!" !tf_commamd!
if not !errorlevel!==0 (
echo Exit code is !errorlevel!.
exit /b 22
)
)
goto :eof
@rem ================
@rem Terraform Show info.
@rem ================
:TerraformCmd_showInfo
echo # ^
type "!file_resource_list_txt!"
type "!file_resource_list_txt!"
call :TerraformCmd ^
"version" ^
"providers" ^
"validate -json"
goto :eof
@rem ================
@rem Terraform apply.
@rem ================
:TerraformCmd_apply
call :Filesystem_deleteFiles ^
"!file_txt!" ^
"!file_tmp_tf!" ^
"!file_png!"
call :TerraformCmd_showInfo
call :TerraformCmd_init
call :TerraformCmd_plan
call :TerraformCmd "apply !TerraformCmd_apply_opt!"
goto :eof
@rem ================
@rem Terraform fmt.
@rem ================
:TerraformCmd_fmt
call :TerraformCmd "fmt !TerraformCmd_fmt_opt!"
goto :eof
@rem ================
@rem Terraform init.
@rem ================
:TerraformCmd_init
call :TerraformCmd_fmt
call :TerraformCmd "init"
goto :eof
@rem ================
@rem Terraform plan.
@rem ================
:TerraformCmd_plan
call :TerraformCmd "plan"
goto :eof
@rem ================
@rem Terraform remove.
@rem ================
:TerraformCmd_remove
if not exist "!file_tf!" (
echo Does not exist file "!file_tf!".
exit /b 23
)
ren "!file_tf!" "!file_tf!.tmp"
call :TerraformCmd_apply
ren "!file_tf!.tmp" "!file_tf!"
goto :eof
@rem ================
@rem Terraform import.
@rem ================
:TerraformCmd_import
call :Filesystem_deleteFiles ^
"terraform.tfstate" ^
"!file_txt!" ^
"!file_tf!" ^
"!file_tmp_tf!" ^
"!file_png!"
call :TerraformCmd_showInfo
call :TerraformFile_makeEmptyTf
echo # ^
type "!file_tmp_tf!"
type "!file_tmp_tf!"
call :TerraformCmd_init
@rem Make file_txt.
echo # ^
type "!file_resource_list_txt!"
type "!file_resource_list_txt!"
for /f "usebackq tokens=1,2 delims= " %%i in (
`type "!file_resource_list_txt!"`
) do (
set "aws_resource_type=%%i"
set "aws_resource_name=%%j"
set "tf_resource_name=!aws_resource_name!_tf"
echo # ^
aws_resource_type=!aws_resource_type! ^
aws_resource_name=!aws_resource_name! ^
tf_resource_name=!tf_resource_name!
echo # ^
"!TerraformCmd!" import "!aws_resource_type!.!tf_resource_name!" !aws_resource_name!
"!TerraformCmd!" import "!aws_resource_type!.!tf_resource_name!" !aws_resource_name!
if not !errorlevel!==0 exit /b 1
echo # ^
"!TerraformCmd!" state show -no-color "!aws_resource_type!.!tf_resource_name!"
"!TerraformCmd!" state show -no-color "!aws_resource_type!.!tf_resource_name!" >> "!file_txt!"
if not !errorlevel!==0 exit /b 2
)
call :Filesystem_deleteFiles ^
"!file_tmp_tf!"
ren "!file_txt!" "!file_tf!"
call :TerraformCmd_fmt
ren "!file_tf!" "!file_txt!"
@rem Make file_tf.
for /f "usebackq tokens=1* delims=" %%i in (
`type "!file_txt!"`
) do (
set "line=%%i"
set "line=!line: arn = # arn !"
set "line=!line: id = # id !"
set "line=!line: bucket_domain_name = # bucket_domain_name !"
set "line=!line: bucket_regional_domain_name = # bucket_regional_domain_name !"
(echo !line!) >> "!file_tf!"
)
call :TerraformCmd_fmt
echo # ^
type "!file_tf!"
type "!file_tf!"
call :TerraformCmd_plan
@rem Make file_png.
call :L_mkPng
@rem pause
goto :eof
@rem ================
@rem Make file_png.
@rem ================
:L_mkPng
if exist "!dot_cmd!" (
echo # ^
"!TerraformCmd!" graph ^| "!dot_cmd!" -Tpng ^> "!file_png!"
"!TerraformCmd!" graph | "!dot_cmd!" -Tpng > "!file_png!"
)
goto :eof
登録:
投稿 (Atom)