Skip to content

Commit

Permalink
docs: Add guides
Browse files Browse the repository at this point in the history
Closes #488
Closes #301
  • Loading branch information
vaerh committed Aug 7, 2024
1 parent 55f861a commit 8489e86
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/guides/easy_import.md
Original file line number Diff line number Diff line change
@@ -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
}
```
94 changes: 94 additions & 0 deletions docs/guides/install_package.md
Original file line number Diff line number Diff line change
@@ -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 = <<EOT
chmod +x ./helper/download_routeros_packages.sh
./helper/download_routeros_packages.sh ${local.system_architecture} "${local.system_version}" "container"
EOT
}
}
resource "null_resource" "upload_container_npk" {
provisioner "local-exec" {
command = "scp -i ${local.router_ssh_key} \"/tmp/routeros_packages/${local.container_npk_name}\" ${local.router_user}@${var.router_ip}:/${local.container_npk_name}"
}
depends_on = [ null_resource.download_container_npk ]
}
resource "null_resource" "install_container_npk" {
provisioner "local-exec" {
command = <<EOT
ssh -i ${local.router_ssh_key} ${local.router_user}@${var.router_ip} '/system reboot'; sleep 3
until ssh -i ${local.router_ssh_key} -o ConnectTimeout=2 ${local.router_user}@${var.router_ip} ':put True' 2> /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}"
```

0 comments on commit 8489e86

Please sign in to comment.