From 010d54787e38b5720397a6e9f6c1537185cbd08e Mon Sep 17 00:00:00 2001 From: Vaerh Date: Wed, 7 Aug 2024 10:49:29 +0300 Subject: [PATCH] docs: Move the guides to the correct directory --- docs/guides/easy_import.md | 53 ++++++++++++++++ docs/guides/install_package.md | 94 +++++++++++++++++++++++++++++ templates/guides/easy_import.md | 53 ++++++++++++++++ templates/guides/install_package.md | 94 +++++++++++++++++++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 docs/guides/easy_import.md create mode 100644 docs/guides/install_package.md create mode 100644 templates/guides/easy_import.md create mode 100644 templates/guides/install_package.md diff --git a/docs/guides/easy_import.md b/docs/guides/easy_import.md new file mode 100644 index 00000000..f0290170 --- /dev/null +++ b/docs/guides/easy_import.md @@ -0,0 +1,53 @@ +# Install package + +Original [issue](https://github.com/terraform-routeros/terraform-provider-routeros/issues/488) + +## Example +```shell +#!/bin/bash + +USER=admin +PASS= +HOST=http://router.local + +i=0 +curl -s -u ${USER}:${PASS} ${HOST}/rest/ip/firewall/address-list | jq -c '.[] | select(.dynamic | ascii_downcase == "false") | {index: .".id", address: .address, comment: .comment, list: .list}' | while read rec; do + index=$(echo $rec | jq .index) + idx=$(printf "%00004d" $i) + # echo $rec + bash -cv "tofu state rm 'module.dev-gw0.routeros_ip_firewall_addr_list.address_list[\"$idx\"]'" + bash -cv "tofu import 'module.dev-gw0.routeros_ip_firewall_addr_list.address_list[\"$idx\"]' $index" + let i=${i}+1 +done +``` + +```terraform +variable "address_list" { + type = list(object({ + address = string + comment = optional(string) + disabled = optional(bool, false) + dynamic = optional(bool, false) + list = string + })) + + default = [ + { address="192.168.88.11", comment="example 2", list="srv" }, + { address="192.168.88.12", comment="example 2", list="srv" }, + { address="192.168.88.1", comment="example", list="routeros" }, +] + +locals { + # https://discuss.hashicorp.com/t/does-map-sort-keys/12056/2 + # Map keys are always iterated in lexicographical order! + address_list_map = { for idx, rule in var.address_list : format("%00004d", idx) => rule } +} + +resource "routeros_ip_firewall_addr_list" "address_list" { + for_each = local.address_list_map + address = each.value.address + comment = each.value.comment + disabled = each.value.disabled + list = each.value.list +} +``` \ No newline at end of file diff --git a/docs/guides/install_package.md b/docs/guides/install_package.md new file mode 100644 index 00000000..381c3ac9 --- /dev/null +++ b/docs/guides/install_package.md @@ -0,0 +1,94 @@ +# Install package + +The original example package installation is available in the [Schwitzd](https://github.com/Schwitzd/IaC-HomeRouter/blob/main/container_backend.tf) repository. + +## Example +```terraform +resource "null_resource" "download_container_npk" { + provisioner "local-exec" { + command = < /dev/null + do + echo "Waiting for router to reboot and become available..." + sleep 10 + done + EOT + } + + depends_on = [ null_resource.upload_container_npk ] +} +``` + +```shell +#!/bin/bash + +# Input parameters +ARCHITECTURE_NAME=$1 +VERSION=$2 +PACKAGE_NAME_PREFIX=$3 + +# Define the base URL and package format +BASE_URL="https://download.mikrotik.com/routeros" +PACKAGE_FORMAT="all_packages-${ARCHITECTURE_NAME}-${VERSION}.zip" + +# Construct the full URL +FULL_URL="${BASE_URL}/${VERSION}/${PACKAGE_FORMAT}" + +# Define the download and extraction paths +DOWNLOAD_PATH="/tmp/${PACKAGE_FORMAT}" +EXTRACT_PATH="/tmp/routeros_packages" + +# Download the package +echo "Downloading package from: ${FULL_URL}" +curl -o "${DOWNLOAD_PATH}" "${FULL_URL}" + +# Verify download +if [ $? -ne 0 ]; then + echo "Failed to download the package." + exit 1 +fi + +# Create the extraction directory +mkdir -p "${EXTRACT_PATH}" + +# List all files in the ZIP archive and filter by the PACKAGE_NAME_PREFIX +echo "Finding package that starts with: ${PACKAGE_NAME_PREFIX}" +MATCHED_FILES=$(unzip -l "${DOWNLOAD_PATH}" | awk '{print $4}' | grep "^${PACKAGE_NAME_PREFIX}") + +# Check if any files were matched +if [ -z "$MATCHED_FILES" ]; then + echo "No files found starting with '${PACKAGE_NAME_PREFIX}'." + exit 1 +fi + +# Extract matched files +for FILE in $MATCHED_FILES; do + echo "Extracting: ${FILE}" + unzip -jo "${DOWNLOAD_PATH}" "${FILE}" -d "${EXTRACT_PATH}" + + if [ $? -ne 0 ]; then + echo "Failed to extract: ${FILE}" + exit 1 + fi +done + +echo "Extraction completed successfully in: ${EXTRACT_PATH}" +``` \ No newline at end of file diff --git a/templates/guides/easy_import.md b/templates/guides/easy_import.md new file mode 100644 index 00000000..f0290170 --- /dev/null +++ b/templates/guides/easy_import.md @@ -0,0 +1,53 @@ +# Install package + +Original [issue](https://github.com/terraform-routeros/terraform-provider-routeros/issues/488) + +## Example +```shell +#!/bin/bash + +USER=admin +PASS= +HOST=http://router.local + +i=0 +curl -s -u ${USER}:${PASS} ${HOST}/rest/ip/firewall/address-list | jq -c '.[] | select(.dynamic | ascii_downcase == "false") | {index: .".id", address: .address, comment: .comment, list: .list}' | while read rec; do + index=$(echo $rec | jq .index) + idx=$(printf "%00004d" $i) + # echo $rec + bash -cv "tofu state rm 'module.dev-gw0.routeros_ip_firewall_addr_list.address_list[\"$idx\"]'" + bash -cv "tofu import 'module.dev-gw0.routeros_ip_firewall_addr_list.address_list[\"$idx\"]' $index" + let i=${i}+1 +done +``` + +```terraform +variable "address_list" { + type = list(object({ + address = string + comment = optional(string) + disabled = optional(bool, false) + dynamic = optional(bool, false) + list = string + })) + + default = [ + { address="192.168.88.11", comment="example 2", list="srv" }, + { address="192.168.88.12", comment="example 2", list="srv" }, + { address="192.168.88.1", comment="example", list="routeros" }, +] + +locals { + # https://discuss.hashicorp.com/t/does-map-sort-keys/12056/2 + # Map keys are always iterated in lexicographical order! + address_list_map = { for idx, rule in var.address_list : format("%00004d", idx) => rule } +} + +resource "routeros_ip_firewall_addr_list" "address_list" { + for_each = local.address_list_map + address = each.value.address + comment = each.value.comment + disabled = each.value.disabled + list = each.value.list +} +``` \ No newline at end of file diff --git a/templates/guides/install_package.md b/templates/guides/install_package.md new file mode 100644 index 00000000..381c3ac9 --- /dev/null +++ b/templates/guides/install_package.md @@ -0,0 +1,94 @@ +# Install package + +The original example package installation is available in the [Schwitzd](https://github.com/Schwitzd/IaC-HomeRouter/blob/main/container_backend.tf) repository. + +## Example +```terraform +resource "null_resource" "download_container_npk" { + provisioner "local-exec" { + command = < /dev/null + do + echo "Waiting for router to reboot and become available..." + sleep 10 + done + EOT + } + + depends_on = [ null_resource.upload_container_npk ] +} +``` + +```shell +#!/bin/bash + +# Input parameters +ARCHITECTURE_NAME=$1 +VERSION=$2 +PACKAGE_NAME_PREFIX=$3 + +# Define the base URL and package format +BASE_URL="https://download.mikrotik.com/routeros" +PACKAGE_FORMAT="all_packages-${ARCHITECTURE_NAME}-${VERSION}.zip" + +# Construct the full URL +FULL_URL="${BASE_URL}/${VERSION}/${PACKAGE_FORMAT}" + +# Define the download and extraction paths +DOWNLOAD_PATH="/tmp/${PACKAGE_FORMAT}" +EXTRACT_PATH="/tmp/routeros_packages" + +# Download the package +echo "Downloading package from: ${FULL_URL}" +curl -o "${DOWNLOAD_PATH}" "${FULL_URL}" + +# Verify download +if [ $? -ne 0 ]; then + echo "Failed to download the package." + exit 1 +fi + +# Create the extraction directory +mkdir -p "${EXTRACT_PATH}" + +# List all files in the ZIP archive and filter by the PACKAGE_NAME_PREFIX +echo "Finding package that starts with: ${PACKAGE_NAME_PREFIX}" +MATCHED_FILES=$(unzip -l "${DOWNLOAD_PATH}" | awk '{print $4}' | grep "^${PACKAGE_NAME_PREFIX}") + +# Check if any files were matched +if [ -z "$MATCHED_FILES" ]; then + echo "No files found starting with '${PACKAGE_NAME_PREFIX}'." + exit 1 +fi + +# Extract matched files +for FILE in $MATCHED_FILES; do + echo "Extracting: ${FILE}" + unzip -jo "${DOWNLOAD_PATH}" "${FILE}" -d "${EXTRACT_PATH}" + + if [ $? -ne 0 ]; then + echo "Failed to extract: ${FILE}" + exit 1 + fi +done + +echo "Extraction completed successfully in: ${EXTRACT_PATH}" +``` \ No newline at end of file