Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add command create network #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,21 @@ 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 |
|----------------|-------------|
| **`install`** | Installs Pi-hole as Docker container |
| **`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.

Expand Down
87 changes: 85 additions & 2 deletions syno_pihole.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#======================================================================================================================
Expand Down Expand Up @@ -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.
#======================================================================================================================
Expand Down Expand Up @@ -1229,7 +1303,7 @@ main() {
shift
param_host_ip="$1"
;;
install | network | update | version )
install | network | update | version | create )
command="$1"
;;
* )
Expand Down Expand Up @@ -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"
Expand All @@ -1290,4 +1373,4 @@ main() {
log "Done."
}

main "$@"
main "$@"