From c27894ae0941830174423bcf87db480024141df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Freisinger?= Date: Sat, 13 Apr 2024 20:13:17 -0300 Subject: [PATCH 1/3] add create command command to create a network outside of docker-compose.yml --- syno_pihole.sh | 87 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/syno_pihole.sh b/syno_pihole.sh index 3f61b92..56be9ae 100644 --- a/syno_pihole.sh +++ b/syno_pihole.sh @@ -802,6 +802,36 @@ init_settings() { fi } + +#====================================================================================================================== +# Initializes the network settings. +#====================================================================================================================== +# Globals: +# - param_interface +# - param_subnet +# - param_gateway +# - param_host_ip +# - param_vlan_name +# - param_ip_range + +# Outputs: +# Displays settings. +#====================================================================================================================== +init_settings_network() { + print_status "Initializing network and Pi-hole settings" + init_auto_detected_values + init_generated_values + validate_settings + + log "Interface: ${param_interface}" + log "Subnet: ${param_subnet}" + log "Gateway: ${param_gateway}" + log "VLAN: ${param_vlan_name}" + log "Docker network IP range: ${param_ip_range}" + } + + + #====================================================================================================================== # Asks the user to confirm the operation, unless in force mode. #====================================================================================================================== @@ -1043,6 +1073,50 @@ execute_create_macvlan() { fi } +#====================================================================================================================== +# Create a network outside of docker-compose.yml.(optional) +# address. +#====================================================================================================================== +# Globals: +# - param_vlan_name +# - param_gateway +# - param_subnet +# - param_ip_range +# - param_interface +# Outputs: +# Exits with a non-zero exit code if the network could not be created or reached. +#====================================================================================================================== + + +execute_create_network() { + print_status "Creating network" + + # Check if the network already exists + status=$(docker inspect "${param_vlan_name}" 2>/dev/null | jq -r '.[0].Name') + if [ "${status}" == "${param_vlan_name}" ]; then + log "Network '${param_vlan_name}' already exists." + return 0 + fi + + # Create the network if it doesn't exist + log "Adding network '${param_vlan_name}'" + docker network create --driver=macvlan \ + --gateway="${param_gateway}" \ + --subnet="${param_subnet}" \ + --ip-range="${param_ip_range}" \ + -o parent="${param_interface}" \ + "${param_vlan_name}" >/dev/null 2>&1 + + # Check if the network was created successfully + status=$(docker inspect "${param_vlan_name}" 2>/dev/null) + if [ -z "${status}" ]; then + terminate "Could not create network '${param_vlan_name}'" + fi + + # Show information about the created network + docker inspect "${param_vlan_name}" | jq -r '.[0] | {Name: .Name, Driver: .Driver, Network: .IPAM.Config[0], Parent: .Options.parent }' + + #====================================================================================================================== # Creates the Pi-hole Docker network and Docker container using a (generated) Docker compose file. #====================================================================================================================== @@ -1229,7 +1303,7 @@ main() { shift param_host_ip="$1" ;; - install | network | update | version ) + install | network | update | version | create ) command="$1" ;; * ) @@ -1282,6 +1356,15 @@ main() { detect_host_versions define_pihole_versions ;; + create ) + total_steps=3 + init_env + detect_dsm_version + detect_host_versions + init_settings_network + confirm_operation + execute_create_network + ;; * ) usage terminate "No command specified" @@ -1290,4 +1373,4 @@ main() { log "Done." } -main "$@" \ No newline at end of file +main "$@" From 220c94521ae088127c9a0c3146c5bcc673d3fbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Freisinger?= Date: Sat, 13 Apr 2024 20:16:29 -0300 Subject: [PATCH 2/3] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 48de819..4c9696e 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,13 @@ Run the following command to update an existing Pi-hole container if a newer ver sudo ./syno_pihole.sh update ``` +Run the following command to create a network outside of docker-compose.yml +```console +sudo ./syno_pihole.sh create +``` ### Commands -*Synology-pihole* supports the following commands. +*Synology-pihole* supports the following commands. | Command | Description | |----------------|-------------| @@ -101,6 +105,7 @@ sudo ./syno_pihole.sh update | **`network`** | Creates or recreates virtual network | | **`update`** | Updates an existing Pi-hole Docker container | | **`version`** | Shows host and Pi-hole versions | +| **`create`** | Create docker network outside of docker-compose.yml | In addition, the following options are available. From bfb61c852f990b585b639c5a03b8d906babd109a Mon Sep 17 00:00:00 2001 From: Adrian Freisinger Date: Sat, 13 Apr 2024 20:35:54 -0300 Subject: [PATCH 3/3] fix function --- syno_pihole.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syno_pihole.sh b/syno_pihole.sh index 56be9ae..f2ee172 100644 --- a/syno_pihole.sh +++ b/syno_pihole.sh @@ -1115,7 +1115,7 @@ execute_create_network() { # Show information about the created network docker inspect "${param_vlan_name}" | jq -r '.[0] | {Name: .Name, Driver: .Driver, Network: .IPAM.Config[0], Parent: .Options.parent }' - +} #====================================================================================================================== # Creates the Pi-hole Docker network and Docker container using a (generated) Docker compose file.