refactor UFW_ACTION

This commit is contained in:
Chai Feng 2018-10-05 19:31:09 +08:00
parent a3195297d7
commit b20a989c24
No known key found for this signature in database
GPG Key ID: 2DCD9A24E523FFD2

View File

@ -4,11 +4,12 @@ set -euo pipefail
PATH="/bin:/usr/bin:/sbin:/usr/sbin" PATH="/bin:/usr/bin:/sbin:/usr/sbin"
UFW_ACTION="${1:-help}"
GREP_REGEXP_INSTANCE_NAME="[-_.[:alnum:]]\\+" GREP_REGEXP_INSTANCE_NAME="[-_.[:alnum:]]\\+"
DEFAULT_PROTO=tcp DEFAULT_PROTO=tcp
ufw_docker_agent=ufw-docker-agent
ufw_docker_agent_image="${ufw_docker_agent_image:-chaifeng/${ufw_docker_agent}:181005}"
function ufw-docker--status() { function ufw-docker--status() {
ufw-docker--list "$GREP_REGEXP_INSTANCE_NAME" ufw-docker--list "$GREP_REGEXP_INSTANCE_NAME"
} }
@ -141,10 +142,9 @@ function ufw-docker--service() {
allow) allow)
shift || true shift || true
declare service_id_or_name="${1:?Missing swarm service name or service ID}" declare service_id_or_name="${1:?Missing swarm service name or service ID}"
declare service_name="$(docker service inspect "$service_id_or_name" --format '{{.Spec.Name}}')"
declare service_port="${2:?Missing the port number, such as '80/tcp'.}" declare service_port="${2:?Missing the port number, such as '80/tcp'.}"
"ufw-docker--service-${service_action}" "${service_name}" "${service_port}" "ufw-docker--service-${service_action}" "${service_id_or_name}" "${service_port}"
;; ;;
*) *)
ufw-docker--help ufw-docker--help
@ -152,9 +152,6 @@ function ufw-docker--service() {
esac esac
} }
ufw_docker_agent=ufw-docker-agent
ufw_docker_agent_image="${ufw_docker_agent_image:-chaifeng/${ufw_docker_agent}:181003}"
function ufw-docker--get-service-id() { function ufw-docker--get-service-id() {
declare service_name="$1" declare service_name="$1"
docker service inspect "${service_name}" --format "{{.ID}}" docker service inspect "${service_name}" --format "{{.ID}}"
@ -329,7 +326,9 @@ if ! ufw status 2>/dev/null | grep -Fq "Status: active" ; then
die "UFW is disabled or you are not root user." die "UFW is disabled or you are not root user."
fi fi
case "$UFW_ACTION" in ufw_action="${1:-help}"
case "$ufw_action" in
delete) delete)
shift || true shift || true
if [[ "${1:?Invalid 'delete' command syntax.}" != "allow" ]]; then if [[ "${1:?Invalid 'delete' command syntax.}" != "allow" ]]; then
@ -355,14 +354,14 @@ case "$UFW_ACTION" in
INSTANCE_PORT="${INSTANCE_PORT%/*}" INSTANCE_PORT="${INSTANCE_PORT%/*}"
"ufw-docker--$UFW_ACTION" "$INSTANCE_NAME" "$INSTANCE_PORT" "$PROTO" "ufw-docker--$ufw_action" "$INSTANCE_NAME" "$INSTANCE_PORT" "$PROTO"
;; ;;
service|raw-command|add-service-rule) service|raw-command|add-service-rule)
shift || true shift || true
"ufw-docker--$UFW_ACTION" "$@" "ufw-docker--$ufw_action" "$@"
;; ;;
status|install) status|install)
ufw-docker--"$UFW_ACTION" ufw-docker--"$ufw_action"
;; ;;
*) *)
ufw-docker--help ufw-docker--help