summaryrefslogtreecommitdiffstats
path: root/restraint/nohup/remotehost-sync.sh
blob: c9a63c7403d7d7c58a81d2cf51e87e85abda3aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# remotehost-sync-setup
# - Set variables for PORT/DELAY/DIR
# - create directory for state_messages
# - run web server used for sharing/checking state_messages
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
function remotehost-sync-setup()
{
    echo "$FUNCNAME $@"

    local  RETURNCODE=0
    export IPA_SYNC_PORT=8907
    export IPA_SYNC_DELAY=30
    export IPA_SYNC_DIR=/tmp/ipasync

    mkdir -p $IPA_SYNC_DIR
    pushd $IPA_SYNC_DIR
    if [ $(python --version 2>&1|awk '{print $2}'|cut -f1 -d.) -eq 2 ]; then
        IPA_SYNC_WEBMOD=SimpleHTTPServer;
    else
        IPA_SYNC_WEBMOD=http.server;
    fi

    ps -ef|grep "Simple[H]TTPServer.*${IPA_SYNC_PORT}"
    if [ $? -eq 1 ]; then
        echo "remotehost-sync web server not running on $IPA_SYNC_PORT.  starting"
        python -m $IPA_SYNC_WEBMOD $IPA_SYNC_PORT > /var/log/python_web_server.log 2>&1 &
    else
        echo "remotehost-sync web server running on $IPA_SYNC_PORT.  Using that one."
    fi

    if [ $? -eq 0 ]; then
        export IPA_SYNC_PID=$(ps -ef|grep "py[t]hon.*$IPA_SYNC_PORT"|awk '{print $2}')
    else
        echo "[   FAIL    ] :: WEB SERVER FAILED TO START!!!!!!!!!!!!!!!"
        export IPA_SYNC_PID="SERVERFAILED"
        RETURNCODE=1
    fi
    popd
    return $RETURNCODE
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# remotehost-sync-block -s <state_message> <hostname> [<hostname> ...]
# - block actions on server where it is run until <hostname> sets
#   expected <state_message>
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
function remotehost-sync-block()
{
    echo "$FUNCNAME $@"
    export IPA_SYNC_PORT=8907
    export IPA_SYNC_DELAY=30
    export IPA_SYNC_DIR=/tmp/ipasync

    local DASH_S=$1
    local MESSAGE=$2
    local HOSTS="$3 $4 $5 $6 $7 $8 $9"
    local HOST=""

    ps -ef|grep "${IPA_SYNC_PID}.*Simple[H]TTPServer.*${IPA_SYNC_PORT}"
    if [ $? -eq 1 ]; then
        echo "ipa-sync web server not running on $IPA_SYNC_PORT.  starting"
        remotehost-sync-setup
    fi

    while true; do
        for HOST in $HOSTS; do
            #echo "wget -O/dev/null http://$HOST:$IPA_SYNC_PORT/$MESSAGE"
            wget -qO/dev/null http://$HOST:$IPA_SYNC_PORT/$MESSAGE
            if [ $? -eq 0 ]; then
                HOSTS="$(echo $HOSTS|sed s/$HOST//|sed 's/  / /g')"
            fi
        done
        if [ -n "$HOSTS" ]; then
            sleep $IPA_SYNC_DELAY
        else
            break
        fi
    done
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# remotehost-sync-set -s <state_message> [-m <hostname>]
# - set <state_message> on localhost (even if <hostname given>)
# - unblocks servers using remotehost-sync-block waiting on <state_message>
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
function remotehost-sync-set()
{
    echo "$FUNCNAME $@"
    export IPA_SYNC_PORT=8907
    export IPA_SYNC_DELAY=30
    export IPA_SYNC_DIR=/tmp/ipasync

    local DASH_S=$1
    local MESSAGE=$2

    ps -ef|grep "${IPA_SYNC_PID}.*Simple[H]TTPServer.*${IPA_SYNC_PORT}"
    if [ $? -eq 1 ]; then
        echo "ipa-sync web server not running on $IPA_SYNC_PORT.  starting"
        remotehost-sync-setup
    fi

    touch "$IPA_SYNC_DIR/$MESSAGE"
}