171 lines
5.0 KiB
Bash
Executable File
171 lines
5.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# Production batch subscription script for LWVWV members
|
|
# Usage: ./subscribe-members.sh [csv_file] [password] [list_id] [connector_url]
|
|
#
|
|
# This script reads a CSV file (email,first_name,last_name) and subscribes
|
|
# all members to the specified Mailman list via the connector API.
|
|
#
|
|
# Arguments (positional, optional if env vars are set):
|
|
# csv_file Path to CSV file with format: email,first_name,last_name
|
|
# password Secret password for the mailman-connector
|
|
# list_id Target mailing list
|
|
# connector_url URL of the mailman-connector
|
|
#
|
|
# Environment Variables (used as defaults if positional args not provided):
|
|
# CONNECTOR_URL - URL of the mailman-connector
|
|
# CONNECTOR_PASSWORD - Secret password for the connector
|
|
# MAILMAN_LIST_ID - Target mailing list ID
|
|
#
|
|
# Examples:
|
|
# # Using positional arguments:
|
|
# ./subscribe-members.sh members.csv mysecretpassword members.lists.lwvwv.org https://connector.example.com
|
|
#
|
|
# # Using environment variables (from ../env):
|
|
# ./subscribe-members.sh members.csv
|
|
#
|
|
# # With some positional, some from env:
|
|
# ./subscribe-members.sh members.csv mysecretpassword
|
|
|
|
# Show help
|
|
show_help() {
|
|
cat << EOF
|
|
Usage: $0 [csv_file] [password] [list_id] [connector_url]
|
|
|
|
Arguments (all optional if environment variables are set):
|
|
csv_file Path to CSV file with format: email,first_name,last_name
|
|
password Secret password for the mailman-connector
|
|
list_id Target mailing list
|
|
connector_url URL of the mailman-connector
|
|
|
|
Environment Variables (used as defaults):
|
|
CONNECTOR_URL URL of the mailman-connector
|
|
CONNECTOR_PASSWORD Secret password for the connector
|
|
MAILMAN_LIST_ID Target mailing list ID
|
|
|
|
Examples:
|
|
# All positional:
|
|
$0 members.csv mysecretpassword members.lists.lwvwv.org https://connector.example.com
|
|
|
|
# Using env vars for connector settings:
|
|
$0 members.csv
|
|
|
|
# Mixed:
|
|
$0 members.csv mysecretpassword members.lists.lwvwv.org
|
|
|
|
CSV Format:
|
|
The CSV file should have one member per line:
|
|
email@example.com,FirstName,LastName
|
|
another@example.com,John,Doe
|
|
|
|
EOF
|
|
}
|
|
|
|
# Check for help flag
|
|
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
|
show_help
|
|
exit 0
|
|
fi
|
|
|
|
# Parse arguments - use positional if provided, fallback to env vars
|
|
CSV_FILE="${1:-}"
|
|
CONNECTOR_PASSWORD="${2:-${CONNECTOR_PASSWORD:-}}"
|
|
LIST_ID="${3:-${MAILMAN_LIST_ID:-members.lists.example.org}}"
|
|
CONNECTOR_URL="${4:-${CONNECTOR_URL:-https://mailman-connector.example.com}}"
|
|
|
|
# Validation
|
|
if [ -z "$CSV_FILE" ]; then
|
|
echo "Error: CSV file is required"
|
|
echo ""
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$CONNECTOR_PASSWORD" ]; then
|
|
echo "Error: Password is required (provide as argument or set CONNECTOR_PASSWORD)"
|
|
echo ""
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$CSV_FILE" ]; then
|
|
echo "Error: File not found: $CSV_FILE"
|
|
exit 1
|
|
fi
|
|
|
|
# Validate CSV format
|
|
if ! grep -q ',' "$CSV_FILE"; then
|
|
echo "Error: CSV file appears invalid (no commas found)"
|
|
echo "Expected format: email,first_name,last_name"
|
|
exit 1
|
|
fi
|
|
|
|
# Read CSV content
|
|
CSV_DATA=$(cat "$CSV_FILE")
|
|
if [ -z "$CSV_DATA" ]; then
|
|
echo "Error: CSV file is empty"
|
|
exit 1
|
|
fi
|
|
|
|
# Count members
|
|
MEMBER_COUNT=$(wc -l < "$CSV_FILE" | tr -d ' ')
|
|
|
|
echo "=========================================="
|
|
echo "LWVWV Member Subscription"
|
|
echo "=========================================="
|
|
echo "Connector URL: $CONNECTOR_URL"
|
|
echo "Target List: $LIST_ID"
|
|
echo "CSV File: $CSV_FILE"
|
|
echo "Members: $MEMBER_COUNT"
|
|
echo "=========================================="
|
|
echo ""
|
|
|
|
# Check if jq is installed
|
|
if ! command -v jq &> /dev/null; then
|
|
echo "Error: jq is required but not installed"
|
|
echo "Install with: apt-get install jq (Debian/Ubuntu) or yum install jq (RHEL/CentOS)"
|
|
exit 1
|
|
fi
|
|
|
|
# Send to connector
|
|
echo "Subscribing members..."
|
|
echo ""
|
|
|
|
RESPONSE=$(curl -s -X POST "$CONNECTOR_URL" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{
|
|
\"subscribe_batch\": \"subscribe_batch\",
|
|
\"password\": \"$CONNECTOR_PASSWORD\",
|
|
\"list_id\": \"$LIST_ID\",
|
|
\"csv_data\": $(echo "$CSV_DATA" | jq -R -s .)
|
|
}")
|
|
|
|
# Check response
|
|
if echo "$RESPONSE" | jq -e . &> /dev/null; then
|
|
echo "Response:"
|
|
echo "$RESPONSE" | jq .
|
|
|
|
# Extract summary if available
|
|
if echo "$RESPONSE" | jq -e '.summary' &> /dev/null; then
|
|
echo ""
|
|
echo "Summary:"
|
|
echo " Total processed: $(echo "$RESPONSE" | jq -r '.summary.total_received // "N/A"')"
|
|
echo " Newly subscribed: $(echo "$RESPONSE" | jq -r '.summary.subscribed // "N/A"')"
|
|
echo " Already subscribed: $(echo "$RESPONSE" | jq -r '.summary.already_subscribed // "N/A"')"
|
|
echo " Errors: $(echo "$RESPONSE" | jq -r '.summary.errors // "N/A"')"
|
|
fi
|
|
|
|
# Check for errors
|
|
if echo "$RESPONSE" | jq -e '.error' &> /dev/null; then
|
|
echo ""
|
|
echo "ERROR: $(echo "$RESPONSE" | jq -r '.error')"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "Error: Invalid response from connector"
|
|
echo "Response: $RESPONSE"
|
|
exit 1
|
|
fi
|
|
|
|
echo ""
|
|
echo "Done!"
|