#!/usr/bin/env python3
"""
Oracle Cloud Always Free ARM VPS Auto-Provisioner
=================================================
Keeps asking Oracle Cloud for a free Ampere A1 server (VM.Standard.A1.Flex) until one is free, for regions
such as Mumbai (ap-mumbai-1) where the console says "Out of capacity".

Full guide (setup, every option, troubleshooting): https://www.codewing.app/oracle

What it does to stay safe:
1. Free limit: never asks for more than Oracle's Always Free ARM limit (4 OCPUs and 24 GB RAM across all your
   ARM servers). It checks what you already run first.
2. Polite retries: about every 15 minutes by default (13-17 minutes, random), never faster than once a minute.
3. No duplicates: before every try it checks whether a server with the same name already exists.
4. No double server on a network drop: each try sends a one-time retry token.
5. Clear errors: "out of capacity" (normal, it keeps trying) is told apart from a wrong key, a full quota or a
   rate limit (it stops or waits and tells you why).
6. Finds your public subnet by itself (or uses --subnet-id), and checks it has a route to the internet.
7. SSH key: uses your own public key; if you have none it makes one and saves the private key with 0600 permissions.

Nothing in this file is tied to anyone's account. It reads YOUR Oracle API key from YOUR config file
(~/.oci/config by default). Never share that file or the .pem key it points to.

Quick start:
    pip install oci
    python3 oci_free_vps.py --ocpus 2 --memory 12

Free to use and change. No warranty: you run it on your own Oracle account.
"""

import os
import sys
import time
import uuid
import random
import argparse
import datetime
from pathlib import Path

try:
    import oci
except ImportError as e:
    print(f"Error: OCI Python SDK is not installed ({e}). Run: pip install oci")
    sys.exit(1)


# --- Defaults (change them with the command-line options, see --help) ---
DEFAULT_SHAPE = "VM.Standard.A1.Flex"
DEFAULT_OCPUS = 1.0                      # 1 OCPU
DEFAULT_MEMORY_IN_GBS = 6.0              # 6 GB RAM
DEFAULT_BOOT_VOLUME_GB = 50              # 50 GB (Always Free includes 200 GB of block storage in total)
DEFAULT_DISPLAY_NAME = "oracle-arm-1"    # the server's name in the console
DEFAULT_CHECK_INTERVAL_SECONDS = 900     # 15 minutes between tries
DEFAULT_JITTER_SECONDS = 120             # +/- 2 minutes random (13-17 minutes in all)
MIN_INTERVAL_SECONDS = 60                # never ask faster than once a minute, or Oracle answers "too many requests"
DEFAULT_LOG_FILE = "oci_vps_hunter.log"

FREE_ARM_OCPUS = 4.0
FREE_ARM_MEMORY_GB = 24.0


def log(message: str, log_file: str = DEFAULT_LOG_FILE):
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    formatted = f"[{timestamp}] {message}"
    print(formatted, flush=True)
    try:
        with open(log_file, "a", encoding="utf-8") as f:
            f.write(formatted + "\n")
    except Exception:
        pass


def resolve_config_path(custom_path: str = None) -> str:
    """Finds the OCI config file: --config -> ~/.oci/config -> ./oci_config.ini -> ~/oci_hunter/oci_config.ini."""
    candidates = []
    if custom_path:
        candidates.append(Path(custom_path).expanduser())
    candidates.append(Path.home() / ".oci" / "config")
    candidates.append(Path("oci_config.ini"))
    candidates.append(Path.home() / "oci_hunter" / "oci_config.ini")

    for candidate in candidates:
        if candidate.resolve().exists():
            return str(candidate.resolve())

    raise FileNotFoundError(
        "Could not find your OCI config file. Create an API key in the Oracle Cloud console (Profile > My profile > "
        "API keys > Add API key) and save the config it shows to ~/.oci/config. Checked: " +
        ", ".join(str(c) for c in candidates)
    )


def load_oci_config(config_file: str) -> dict:
    config = oci.config.from_file(config_file, "DEFAULT")
    oci.config.validate_config(config)
    return config


def check_existing_instance(compute_client, compartment_id: str, display_name: str):
    """
    Duplicate protection: returns a running or starting server with this display_name, if there is one.
    """
    instances = oci.pagination.list_call_get_all_results(
        compute_client.list_instances, compartment_id=compartment_id
    ).data
    for inst in instances:
        if inst.lifecycle_state in ["TERMINATED", "TERMINATING"]:
            continue
        if inst.display_name == display_name:
            return inst

    return None


def check_arm_resource_limits(compute_client, compartment_id: str, requested_ocpus: float, requested_ram: float):
    """
    Makes sure your ARM (A1.Flex) servers together stay within 4 OCPUs and 24 GB RAM after this one.
    """
    instances = oci.pagination.list_call_get_all_results(
        compute_client.list_instances, compartment_id=compartment_id
    ).data
    used_ocpus = 0.0
    used_ram = 0.0
    for inst in instances:
        if inst.lifecycle_state not in ["TERMINATED", "TERMINATING"] and "A1.Flex" in inst.shape:
            if inst.shape_config:
                used_ocpus += (inst.shape_config.ocpus or 0.0)
                used_ram += (inst.shape_config.memory_in_gbs or 0.0)

    log(f"Current ARM A1 Usage: {used_ocpus:.1f}/{FREE_ARM_OCPUS:.1f} OCPUs, {used_ram:.1f}/{FREE_ARM_MEMORY_GB:.1f} GB RAM")
    if (used_ocpus + requested_ocpus) > FREE_ARM_OCPUS or (used_ram + requested_ram) > FREE_ARM_MEMORY_GB:
        raise RuntimeError(
            f"Requested {requested_ocpus} OCPUs and {requested_ram} GB RAM would exceed "
            f"Always Free limits (Current: {used_ocpus} OCPUs, {used_ram} GB RAM, Max: 4 OCPUs, 24 GB RAM)."
        )


def get_or_create_ssh_key(custom_key: str = None) -> str:
    """
    The SSH public key the new server will accept: --ssh-key (the key itself or a .pub file), else your own key
    in ~/.ssh, else a new keypair made here (oci_instance_key.pem / .pub, the private key with 0600 permissions).
    """
    if custom_key and custom_key.strip().startswith(("ssh-ed25519", "ssh-rsa", "ecdsa-")):
        log("Using the SSH public key given with --ssh-key")
        return custom_key.strip()

    candidates = []
    if custom_key:
        candidates.append(Path(custom_key).expanduser())

    home_ssh = Path.home() / ".ssh"
    candidates.extend([
        home_ssh / "id_ed25519.pub",
        home_ssh / "id_rsa.pub",
        home_ssh / "authorized_keys",
        Path("oci_instance_key.pub")
    ])

    for key_file in candidates:
        if key_file.exists():
            try:
                with open(key_file, "r", encoding="utf-8") as f:
                    for line in f:
                        line = line.strip()
                        if line.startswith("ssh-ed25519") or line.startswith("ssh-rsa") or line.startswith("ecdsa-"):
                            log(f"Using SSH public key from: {key_file}")
                            return line
            except Exception:
                continue

    log("No SSH public key found. Generating dedicated RSA keypair ('oci_instance_key')...")
    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography.hazmat.primitives import serialization

    private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
    private_pem = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption()
    )

    priv_path = Path("oci_instance_key.pem")
    pub_path = Path("oci_instance_key.pub")

    with open(priv_path, "wb") as f:
        f.write(private_pem)

    try:
        os.chmod(priv_path, 0o600)
    except Exception:
        pass

    public_ssh = private_key.public_key().public_bytes(
        encoding=serialization.Encoding.OpenSSH,
        format=serialization.PublicFormat.OpenSSH
    ).decode("utf-8").strip()

    with open(pub_path, "w", encoding="utf-8") as f:
        f.write(public_ssh + "\n")

    log(f"Generated SSH keypair: {priv_path.resolve()} (keep it private, you log in with it) and {pub_path.resolve()}")
    return public_ssh


def find_availability_domain(identity_client, compartment_id: str, custom_ad: str = None) -> str:
    ads = identity_client.list_availability_domains(compartment_id).data
    if not ads:
        raise RuntimeError("No Availability Domains found in region.")

    if custom_ad:
        for ad in ads:
            if custom_ad.lower() in ad.name.lower():
                return ad.name
    return ads[0].name


def find_public_subnet(core_client, compartment_id: str) -> str:
    """Finds a subnet in your account that gives servers a public IP (the one the VCN wizard makes)."""
    vcns = oci.pagination.list_call_get_all_results(core_client.list_vcns, compartment_id=compartment_id).data
    for vcn in vcns:
        if vcn.lifecycle_state != "AVAILABLE":
            continue
        subnets = oci.pagination.list_call_get_all_results(
            core_client.list_subnets, compartment_id=compartment_id, vcn_id=vcn.id
        ).data
        for subnet in subnets:
            if subnet.lifecycle_state == "AVAILABLE" and not subnet.prohibit_public_ip_on_vnic:
                log(f"Found public subnet '{subnet.display_name}' in VCN '{vcn.display_name}'")
                return subnet.id
    raise RuntimeError(
        "No public subnet found in your account. In the Oracle Cloud console open Networking > Virtual cloud networks, "
        "choose Actions > Start VCN Wizard > 'Create VCN with Internet Connectivity', create it, then run this "
        "script again (or pass --subnet-id with your subnet's OCID)."
    )


def verify_public_subnet(core_client, compartment_id: str, subnet_id: str) -> str:
    """Checks that the subnet exists, allows public IPs and has a route to an internet gateway."""
    try:
        subnet = core_client.get_subnet(subnet_id).data
    except Exception as e:
        log(f"Subnet verification failed for {subnet_id}: {e}")
        raise

    if subnet.prohibit_public_ip_on_vnic:
        log(f"Warning: Subnet '{subnet.display_name}' is private (no public IPs). You will not be able to SSH in "
            f"from the internet. Use a public subnet, or leave out --subnet-id to find one.")
    else:
        log(f"Verified Public Subnet: '{subnet.display_name}'")

    try:
        route_table = core_client.get_route_table(subnet.route_table_id).data
        has_internet = any(
            (rule.network_entity_id or "").startswith("ocid1.internetgateway")
            and (rule.destination or rule.cidr_block) == "0.0.0.0/0"
            for rule in (route_table.route_rules or [])
        )
        if not has_internet:
            log("Warning: this subnet's route table has no 0.0.0.0/0 route to an internet gateway, so the server "
                "may not be reachable. The VCN wizard ('Create VCN with Internet Connectivity') sets this up.")
    except Exception as e:
        log(f"Note: could not check the subnet's route table ({e}).")

    return subnet.id


def find_latest_arm_ubuntu_image(compute_client, compartment_id: str, shape: str, custom_image_id: str = None) -> str:
    if custom_image_id:
        return custom_image_id

    images = compute_client.list_images(
        compartment_id=compartment_id,
        shape=shape,
        sort_by="TIMECREATED",
        sort_order="DESC"
    ).data

    for preferred in ["Canonical-Ubuntu-24.04", "Canonical-Ubuntu-22.04", "Canonical-Ubuntu"]:
        for img in images:
            if preferred.lower() in img.display_name.lower() and "minimal" not in img.display_name.lower():
                log(f"Selected OS Image: {img.display_name}")
                return img.id

    for img in images:
        if "ubuntu" in img.display_name.lower():
            log(f"Selected OS Image: {img.display_name}")
            return img.id

    if images:
        return images[0].id

    raise RuntimeError(f"No compatible images found for shape {shape}")


def wait_with_jitter(base_interval: int, jitter: int):
    """Waits base_interval +/- jitter seconds (13-17 minutes by default), never less than a minute."""
    min_delay = max(MIN_INTERVAL_SECONDS, base_interval - jitter)
    max_delay = max(min_delay, base_interval + jitter)
    delay = random.uniform(min_delay, max_delay)

    wake_time = datetime.datetime.now() + datetime.timedelta(seconds=delay)
    print(f"--> Next availability check in {delay / 60:.1f} mins (at {wake_time.strftime('%H:%M:%S')})...\n", flush=True)
    time.sleep(delay)


def launch_instance_safe(
    compute_client,
    compartment_id: str,
    ad_name: str,
    subnet_id: str,
    image_id: str,
    ssh_public_key: str,
    shape: str,
    ocpus: float,
    memory_in_gbs: float,
    boot_volume_gb: int,
    display_name: str,
    retry_token: str
):
    source_details = oci.core.models.InstanceSourceViaImageDetails(
        image_id=image_id,
        boot_volume_size_in_gbs=boot_volume_gb
    )

    vnic_details = oci.core.models.CreateVnicDetails(
        subnet_id=subnet_id,
        assign_public_ip=True,
        display_name=f"{display_name}-vnic"
    )

    metadata = {"ssh_authorized_keys": ssh_public_key}

    launch_kwargs = {
        "compartment_id": compartment_id,
        "availability_domain": ad_name,
        "display_name": display_name,
        "image_id": image_id,
        "shape": shape,
        "create_vnic_details": vnic_details,
        "metadata": metadata,
        "source_details": source_details
    }

    if "Flex" in shape:
        launch_kwargs["shape_config"] = oci.core.models.LaunchInstanceShapeConfigDetails(
            ocpus=ocpus,
            memory_in_gbs=memory_in_gbs
        )

    launch_details = oci.core.models.LaunchInstanceDetails(**launch_kwargs)

    return compute_client.launch_instance(
        launch_instance_details=launch_details,
        opc_retry_token=retry_token
    )


def print_success_summary(instance, compute_client, network_client, compartment_id: str):
    log("=" * 65)
    log("SUCCESS! Oracle Cloud Always Free VPS Successfully Created!")
    log("=" * 65)
    log(f"Instance Name : {instance.display_name}")
    log(f"State         : {instance.lifecycle_state}")
    log(f"Shape         : {instance.shape}")
    log("=" * 65)

    log("Waiting for public IP assignment (takes ~30 seconds)...")
    time.sleep(20)

    try:
        vnics = compute_client.list_vnic_attachments(
            compartment_id=compartment_id,
            instance_id=instance.id
        ).data
        if vnics:
            vnic = network_client.get_vnic(vnics[0].vnic_id).data
            if vnic.public_ip:
                log(f"[+] Assigned Public IP : {vnic.public_ip}")
                log(f"[+] SSH Command        : ssh -i <your_private_key> ubuntu@{vnic.public_ip}")
                return
        log("Note: no public IP yet. Open Compute > Instances in the Oracle Cloud console to see it.")
    except Exception as e:
        log(f"Note: Could not query IP automatically ({e}). View IP in Oracle Cloud Console.")


def main():
    parser = argparse.ArgumentParser(description="Oracle Cloud Always Free ARM VPS auto-provisioner (guide: https://www.codewing.app/oracle)")
    parser.add_argument("--config", default=None, help="Path to your OCI config file (default: ~/.oci/config)")
    parser.add_argument("--shape", default=DEFAULT_SHAPE, help="Shape (default: VM.Standard.A1.Flex)")
    parser.add_argument("--ocpus", type=float, default=DEFAULT_OCPUS, help="OCPUs (default: 1.0; free limit: 4 in total)")
    parser.add_argument("--memory", type=float, default=DEFAULT_MEMORY_IN_GBS, help="RAM in GB (default: 6.0; free limit: 24 in total)")
    parser.add_argument("--boot-size", type=int, default=DEFAULT_BOOT_VOLUME_GB, help="Boot disk in GB (default: 50, minimum 47)")
    parser.add_argument("--name", default=DEFAULT_DISPLAY_NAME, help="Server name (default: oracle-arm-1)")
    parser.add_argument("--interval", type=int, default=DEFAULT_CHECK_INTERVAL_SECONDS, help="Seconds between tries (default: 900 = 15 min; minimum 60)")
    parser.add_argument("--jitter", type=int, default=DEFAULT_JITTER_SECONDS, help="Random +/- seconds added to each wait (default: 120)")
    parser.add_argument("--ssh-key", default=None, help="Your SSH public key: the key itself, or the path to a .pub file")
    parser.add_argument("--subnet-id", default=None, help="Your public subnet's OCID (default: found automatically)")
    parser.add_argument("--image-id", default=None, help="Image OCID (default: the latest Ubuntu for ARM)")
    parser.add_argument("--max-attempts", type=int, default=0, help="Max attempts (0 = run until created)")

    args = parser.parse_args()

    log("=" * 65)
    log("Oracle Cloud Always Free ARM VPS Auto-Provisioner")
    log("=" * 65)

    if args.interval < MIN_INTERVAL_SECONDS:
        log(f"--interval {args.interval} is too fast: using {MIN_INTERVAL_SECONDS} seconds (Oracle limits how often you can ask).")
        args.interval = MIN_INTERVAL_SECONDS

    try:
        config_file = resolve_config_path(args.config)
        log(f"Using OCI Config: {config_file}")
        config = load_oci_config(config_file)
        compartment_id = config["tenancy"]
        log(f"Region: {config.get('region')}")
    except Exception as e:
        log(f"Fatal Configuration Error: {e}")
        sys.exit(1)

    identity_client = oci.identity.IdentityClient(config)
    core_client = oci.core.VirtualNetworkClient(config)
    compute_client = oci.core.ComputeClient(config)

    # 1. Check if instance with this name already exists
    try:
        existing = check_existing_instance(compute_client, compartment_id, args.name)
    except oci.exceptions.ServiceError as e:
        log(f"[FATAL] Oracle refused the first request ({e.status} - {e.code}): {e.message}")
        log("Check the user, fingerprint, tenancy, region and key_file lines in your config file.")
        sys.exit(1)
    if existing:
        log(f"[DUPLICATE SHIELD] Found existing instance '{existing.display_name}' in state '{existing.lifecycle_state}'.")
        log(f"Aborting hunt: You already have '{args.name}' provisioned.")
        print_success_summary(existing, compute_client, core_client, compartment_id)
        sys.exit(0)

    # 2. Check total ARM resource limits to prevent exceeding Always Free tier
    if "A1.Flex" in args.shape:
        try:
            check_arm_resource_limits(compute_client, compartment_id, args.ocpus, args.memory)
        except Exception as e:
            log(f"[RESOURCE LIMIT ERROR] {e}")
            sys.exit(1)

    # 3. Resolve Resources
    try:
        ad_name = find_availability_domain(identity_client, compartment_id)
        log(f"Availability Domain : {ad_name}")

        subnet_id = args.subnet_id or find_public_subnet(core_client, compartment_id)
        subnet_id = verify_public_subnet(core_client, compartment_id, subnet_id)
        image_id = find_latest_arm_ubuntu_image(compute_client, compartment_id, args.shape, args.image_id)
        ssh_public_key = get_or_create_ssh_key(args.ssh_key)
    except Exception as e:
        log(f"Resource Initialization Error: {e}")
        sys.exit(1)

    log(f"Target Instance     : {args.name} | Shape: {args.shape} ({args.ocpus} OCPU, {args.memory} GB RAM)")
    log(f"Boot Volume Size    : {args.boot_size} GB")
    log(f"Retry Schedule      : ~{args.interval / 60:g} mins (+/- {args.jitter}s jitter, never under {MIN_INTERVAL_SECONDS}s)")
    log("Entering safe capacity monitoring loop...")
    log("-" * 65)

    attempts = 0

    while True:
        attempts += 1

        # Check for duplicate instance name before each attempt
        try:
            existing = check_existing_instance(compute_client, compartment_id, args.name)
        except Exception as e:
            log(f"Could not list instances this time ({e}); trying again after the wait.")
            wait_with_jitter(args.interval, args.jitter)
            continue
        if existing:
            log(f"[DUPLICATE SHIELD] Instance '{existing.display_name}' was detected. Stopping loop.")
            print_success_summary(existing, compute_client, core_client, compartment_id)
            break

        retry_token = str(uuid.uuid4())
        log(f"[Attempt #{attempts}] Polling capacity for {args.shape} in {ad_name}...")

        try:
            response = launch_instance_safe(
                compute_client=compute_client,
                compartment_id=compartment_id,
                ad_name=ad_name,
                subnet_id=subnet_id,
                image_id=image_id,
                ssh_public_key=ssh_public_key,
                shape=args.shape,
                ocpus=args.ocpus,
                memory_in_gbs=args.memory,
                boot_volume_gb=args.boot_size,
                display_name=args.name,
                retry_token=retry_token
            )

            created_instance = response.data
            log(f"SUCCESS! Instance created with state: {created_instance.lifecycle_state}")
            print_success_summary(created_instance, compute_client, core_client, compartment_id)
            break

        except oci.exceptions.ServiceError as e:
            is_capacity_exhaustion = (
                e.code == "OutOfCapacity" or
                (e.status == 500 and "out of host capacity" in str(e.message).lower())
            )

            if is_capacity_exhaustion:
                log(f"Host capacity currently exhausted in {ad_name}. (Normal for Always Free A1)")

            elif e.status == 429 or "TooManyRequests" in str(e.code):
                retry_after = 600
                if hasattr(e, "headers") and e.headers and "Retry-After" in e.headers:
                    try:
                        retry_after = int(e.headers["Retry-After"])
                    except Exception:
                        pass
                log(f"[HTTP 429] Rate limit detected. Backing off safely for {retry_after}s...")
                time.sleep(retry_after)
                continue

            elif e.status == 401 or "NotAuthenticated" in str(e.code):
                log(f"[FATAL AUTH ERROR] Authentication failed ({e.status} - {e.code}): {e.message}")
                log("Check the fingerprint and key_file in your config file, and that the API key is still in the console.")
                sys.exit(1)

            elif "LimitExceeded" in str(e.code) or "quota" in str(e.message).lower():
                log(f"[FATAL QUOTA ERROR] Resource limit reached: {e.message}")
                sys.exit(1)

            else:
                log(f"OCI Error ({e.status} - {e.code}): {e.message}")

        except Exception as e:
            log(f"Network / Client Exception: {e}")
            log("Reconciling instance state with Oracle Cloud...")
            try:
                existing = check_existing_instance(compute_client, compartment_id, args.name)
            except Exception:
                existing = None
            if existing:
                log(f"Instance '{existing.display_name}' was successfully created during the request!")
                print_success_summary(existing, compute_client, core_client, compartment_id)
                break

        if args.max_attempts > 0 and attempts >= args.max_attempts:
            log(f"Reached max attempts ({args.max_attempts}). Exiting.")
            break

        wait_with_jitter(args.interval, args.jitter)


if __name__ == "__main__":
    main()
