Skip to content

Commit

Permalink
Improve genesis and Add terraform helper
Browse files Browse the repository at this point in the history
  • Loading branch information
JINWOO-J committed Dec 24, 2024
1 parent 8e10955 commit d0ca345
Show file tree
Hide file tree
Showing 15 changed files with 607 additions and 310 deletions.
4 changes: 2 additions & 2 deletions BUILD_ARGS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

--build-arg BASE_IMAGE=python:3.9.13-slim-buster
--build-arg BUILD_DATE=2024-12-17T05:17:29UTC
--build-arg BUILD_DATE=2024-12-24T01:58:39UTC
--build-arg DOCKER_BUILD_OPTION=--no-cache --rm=true
--build-arg ECHO_OPTION=
--build-arg GIT_USER=JINWOO-J
Expand All @@ -11,4 +11,4 @@
--build-arg REPO_HUB=jinwoo
--build-arg SED_OPTION=''
--build-arg UNAME_S=Darwin
--build-arg VERSION=2.0.89
--build-arg VERSION=2.0.93
58 changes: 0 additions & 58 deletions genesis.json

This file was deleted.

Binary file removed icon_genesis.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion pawnlib/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'pawnlib'
__description__ = 'pawnlib is a collection of libraries for IaC.'
__url__ = 'https://github.com/jinwoo-j/pawnlib'
__version__ = '2.0.89'
__version__ = '2.0.93'
__author__ = 'Jinwoo Jeong'
__author_email__ = 'jinwoo@parametacorp.com'
__license__ = 'MIT'
Expand Down
1 change: 0 additions & 1 deletion pawnlib/cli/goloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,6 @@ def guess_network(network_info):
return {}



main.__doc__ = (
f"{__description__} \n"
f"{__epilog__}"
Expand Down
27 changes: 14 additions & 13 deletions pawnlib/cli/gs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
from pawnlib.config import pawn
from pawnlib.output import color_print
from pawnlib.output import color_print, print_grid, is_file, open_json

try:
import eth_keys
Expand All @@ -9,10 +9,8 @@

from pawnlib.builder.generator import generate_banner
from pawnlib.__version__ import __version__ as _version

from pawnlib.utils.genesis import GenesisGenerator, create_cid, genesis_generator, create_cid, validate_genesis_json
from pawnlib.utils.in_memory_zip import read_genesis_dict_from_zip
from pawnlib.output import is_file, print_json, open_json
from pawnlib.typing import get_size, sys_exit, FlatDict, hex_to_number
from pawnlib.input.prompt import CustomArgumentParser, ColoredHelpFormatter
from pawnlib.typing.check import error_and_exit
Expand Down Expand Up @@ -95,16 +93,19 @@ def main():
json_dict = open_json(genesis_file)

validate_genesis_json(json_dict)
genesis_gen = GenesisGenerator(genesis_json_or_dict=json_dict, base_dir=args.base_dir, genesis_filename=args.output_file)
genesis_gen.initialize()
genesis_gen.log_initialization_info()
genesis_gen.parse_and_write_genesis_json()
file_info = genesis_gen.write_genesis_zip()
cid = genesis_gen.create_cid()
pawn.console.log(f"CID={cid}, NID={genesis_gen.nid}({hex_to_number(genesis_gen.nid)}), {genesis_gen.genesis_filename} {file_info}")

# cid2 = genesis_generator(genesis_json_or_dict=json_dict, base_dir=args.base_dir, genesis_filename=args.output_file)
# pawn.console.log(f"CID = {cid2}")
genesis_gen = GenesisGenerator(
genesis_json_or_dict=json_dict,
base_dir=args.base_dir,
genesis_filename=args.output_file
)
genesis_gen.run()
file_info = genesis_gen.genesis_zip_info
cid = genesis_gen.cid

pawn.console.log(f"CID={cid}, NID={genesis_gen.nid}({hex_to_number(genesis_gen.nid)}), "
f"{genesis_gen.genesis_filename}")

print_grid(file_info, title="Genesis file info", key_ratio=3)

elif args.command == "info":
if not is_file(args.genesis_zip_file):
Expand Down
70 changes: 50 additions & 20 deletions pawnlib/cli/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pawnlib.config import pawnlib_config as pawn
from pawnlib.output import write_json, syntax_highlight, PrintRichTable
from pawnlib.typing.converter import FlatDict, FlatterDict, flatten_dict

from pawnlib.resource import server

__description__ = 'Get meta information from Cloud Providers (AWS, GCP, OCI).'
Expand Down Expand Up @@ -48,6 +47,11 @@
'headers': {'Authorization': 'Bearer Oracle'},
# OCI는 루트 URL로는 감지가 불가능하므로 특정 경로를 사용
'detect_url': 'http://169.254.169.254/opc/v1/instance/id',
},
'KAKAO': {
'meta_url': 'http://169.254.169.254/latest/meta-data/',
'headers': {'X-aws-ec2-metadata-token': 'required'},
'detect_url': 'http://169.254.169.254/latest/meta-data/',
}
}

Expand All @@ -57,6 +61,7 @@ def get_parser():
parser = get_arguments(parser)
return parser


def get_arguments(parser):
parser.add_argument(
"--metadata-ip", "-i", type=str,
Expand Down Expand Up @@ -85,16 +90,16 @@ def get_arguments(parser):
)
return parser

def detect_cloud_provider(meta_ip, timeout):

def detect_cloud_provider(timeout=2.0):
"""
클라우드 제공업체를 자동으로 감지합니다.
Automatically detects the cloud provider based on metadata service.
Parameters:
meta_ip (str): 메타데이터 서비스의 IP 주소.
timeout (float): HTTP 요청의 타임아웃 (초).
timeout (float): Timeout for HTTP requests in seconds
Returns:
str: 감지된 클라우드 제공업체의 이름(AWS, GCP, OCI). 감지되지 않으면 'Unknown'.
str: Name of the detected cloud provider (AWS, GCP, OCI, KAKAO)
"""
for provider, info in CLOUD_PROVIDERS.items():
try:
Expand All @@ -104,36 +109,61 @@ def detect_cloud_provider(meta_ip, timeout):
timeout=timeout
)
if response.status_code == 200:
# Distinguish between AWS and Kakao Cloud using server header
if provider in ['AWS', 'KAKAO']:
server_header = response.headers.get('Server', '')
detected_provider = 'AWS' if 'EC2ws' in server_header else 'KAKAO'
pawn.console.log(f"Detected cloud provider: {detected_provider}")
return detected_provider

pawn.console.log(f"Detected cloud provider: {provider}")
return provider
except requests.exceptions.RequestException as e:
pawn.console.debug(f"Could not detect {provider}: {e}")
pawn.console.debug(f"Failed to detect {provider}: {e}")
continue

pawn.console.log("Could not detect cloud provider. Exiting.")
sys.exit(1)


def get_metadata(provider, meta_ip, timeout):
"""
클라우드 제공업체별로 메타데이터를 가져옵니다.
Retrieves metadata specific to the detected cloud provider.
Parameters:
provider (str): 클라우드 제공업체의 이름.
meta_ip (str): 메타데이터 서비스의 IP 주소.
timeout (float): HTTP 요청의 타임아웃 (초).
provider (str): The name of the cloud provider.
meta_ip (str): The IP address of the metadata service.
timeout (float): Timeout for the HTTP request in seconds.
Returns:
dict: 수집된 메타데이터.
dict: The collected metadata from the cloud provider.
Example:
.. code-block:: python
provider = "AWS"
meta_ip = "169.254.169.254"
timeout = 2.0
metadata = get_metadata(provider, meta_ip, timeout)
print(metadata) # Output: Metadata dictionary for AWS, GCP, or OCI
"""
if provider == 'AWS':
return server.get_aws_metadata(meta_ip=meta_ip, timeout=timeout)
elif provider == 'GCP':
return server.get_gcp_metadata(meta_ip=meta_ip, timeout=timeout)
elif provider == 'OCI':
return server.get_oci_metadata(meta_ip=meta_ip, timeout=timeout)
else:
metadata_handlers = {
'AWS': server.get_aws_metadata,
'GCP': server.get_gcp_metadata,
'OCI': server.get_oci_metadata,
'KAKAO': server.get_kakao_metadata
}

handler = metadata_handlers.get(provider)
if not handler:
pawn.console.log(f"Unsupported cloud provider. - {provider}")
sys.exit(1)

return handler(meta_ip=meta_ip, timeout=timeout)


def main():
banner = generate_banner(
app_name="Cloud Metadata",
Expand All @@ -152,7 +182,7 @@ def main():
if args.provider:
provider = args.provider.upper()
else:
provider = detect_cloud_provider(meta_ip=args.metadata_ip, timeout=args.timeout)
provider = detect_cloud_provider(timeout=args.timeout)

res = get_metadata(provider=provider, meta_ip=args.metadata_ip, timeout=args.timeout)

Expand Down
28 changes: 18 additions & 10 deletions pawnlib/cli/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import argparse
from pawnlib.builder.generator import generate_banner
from pawnlib.__version__ import __version__ as _version
from pawnlib.config import pawnlib_config as pawn, pconf
from pawnlib.config import pawnlib_config as pawn, pconf, setup_app_logger
from pawnlib.utils import disable_ssl_warnings
from pawnlib.resource import net, wait_for_port_open
from pawnlib.typing import is_valid_ipv4, sys_exit

from pawnlib.input.prompt import CustomArgumentParser, ColoredHelpFormatter

logger = setup_app_logger()

NONE_STRING = "__NOT_DEFINED_VALUE__"
__description__ = "This is a tool to measure your server's resources."

Expand Down Expand Up @@ -147,15 +148,22 @@ def format_range(range_tuple):
return f"'{range_tuple[0]}' ⏩'{range_tuple[1]}'"


def wait_for_host(args):
"""
Wait for a specific host and port to become available and provide user feedback.
"""
logger.info(f"🔄 Waiting for host: {args.host}, port: {args.port} to be available... (Timeout: {args.timeout}s)")
result = wait_for_port_open(args.host, args.port, timeout=args.timeout)
if result:
logger.info(f"✅ Host {args.host} on port {args.port} is available.")
else:
logger.error(f"❌ Failed to connect to {args.host} on port {args.port} within the timeout period ({args.timeout}s).")


def main():
args, parser = initialize_arguments()

disable_ssl_warnings()
pawn.console.log(args)

# if not args.command:
# parser.error("ssss")

logger.info(args)
if not args.command:
parser.print_help()
parser.error("command not found")
Expand All @@ -164,15 +172,15 @@ def main():
find_fastest_region(args)

elif args.command == "wait":
wait_for_port_open(args.host, args.port, timeout=args.timeout)
wait_for_host(args)

elif args.command == "scan":
if not args.host_range and args.host:
args.host_range = f"{args.host}-{args.host}"

host_range = validate_host_range(args.host_range)
port_range = validate_port_range(args.port_range)
pawn.console.log(f"🔎 Start scanning worker={args.worker}, view_type={args.view_type} 🔎\n Host range:{format_range(host_range)} , Port range:{format_range(port_range)} ")
logger.info(f"🔎 Start scanning worker={args.worker}, view_type={args.view_type} 🔎\n Host range:{format_range(host_range)} , Port range:{format_range(port_range)} ")

scanner = net.AsyncPortScanner(
ip_range=host_range,
Expand Down
Loading

0 comments on commit d0ca345

Please sign in to comment.