summaryrefslogtreecommitdiff
path: root/scripts/rvi.sh
blob: 2a42aa37e8a38e2e28c5b513b5737e277fb45d7a (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/sh
#
# Copyright (C) 2014, Jaguar Land Rover
#
# Mozilla Public License, version 2.0.  The full text of the 
# Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
#
#
# Setup a correct configuration and launch an RVI release node.
# If a UUID file has not been created, it will be done at this time.
# Init.d script to start and stop an RVI system installed
# through an RPM.
#

SELF_DIR=$(dirname $(readlink -f "$0"))

ERL=${ERL:=erl}

usage() {
    echo "Usage: $0 -D config_dir -c config_file start|stop|console"
    echo "  -c config_file  Configuration file to launch rvi node with. "
    echo "                  If omitted, previously used config file is used."
    echo
    echo "  start           Start an rvi node with the given configuration file."
    echo
    echo "  stop            Stop an rvi node previously started with start."
    echo
    echo "  console         Start an rvi in foreground mode."
    exit 1
}

CONFIG_FILE=""

SNAME=rvi
COOKIE=rvi_cookie
unset CONFIG_DIR
unset LOG_DIR
while getopts "c:d:" o; do
    case "${o}" in
        c)
            CONFIG_FILE=${OPTARG}
            ;;
        d)
            CONFIG_DIR=${OPTARG}
            ;;
        l)
            LOG_DIR=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done

CONFIG_DIR=${CONFIG_DIR:=${SELF_DIR}/config}

shift $((${OPTIND}-1))
CMD=$1

if [ "${CMD}" != "start" -a "${CMD}" != "stop" -a  "${CMD}" != "console" -a  "${CMD}" != "ping" ]
then
    usage
fi

export ERL_LIBS=${SELF_DIR}/rvi:${SELF_DIR}/rvi/deps:${SELF_DIR}/rvi/components
TMP_DIR=/tmp/rvi/$(basename ${CONFIG_FILE} .config)
LOG_DIR=${LOG_DIR:=${TMP_DIR}/log}


# Check that we have a config dir
if [ ! -d ${CONFIG_DIR} ]
then
    install -d --mode=0755 ${CONFIG_DIR}
fi

# Check if we have a uuid file.
if [ ! -f ${CONFIG_DIR}/device_id ]
then
    echo "Creating device ID in ${CONFIG_DIR}/device_id"
    cat /proc/sys/kernel/random/uuid > ${CONFIG_DIR}/device_id
fi



if [ -s "${CONFIG_FILE}" ]
then
    #
    # Check if we need to prepend current dir
    # to relative config file path
    #
    if [ $(echo ${CONFIG_FILE} | cut -c 1,1) != "/" ]
    then
	CONFIG_FILE=${PWD}/${CONFIG_FILE}
    fi

    # Check that config file can be read.
    if [ ! -r "${CONFIG_FILE}" ]
    then
	echo "${CONFIG_FILE} cannot be opened for reading."
	usage
    fi
    # 
    # Generate a config file that will end up as
    # /tmp/rvi/sys.config
    #
    (
	cd ${CONFIG_DIR}
	${SELF_DIR}/setup_gen rvi ${CONFIG_FILE} rvi
    )

    # Did we succeed with config generation?
    if [ "$?" != "0" ]
    then
	# Nope
	echo "Failed to process configuration file."
	exit "$?"
    fi
elif [ ${CMD} = "start" -o ${CMD} = "console" ]
then
    echo "Missing -c config_file necessary for 'start' and 'console'"
    usage
fi
   

LAUNCH="${ERL} -boot ${CONFIG_DIR}/rvi/start -sname ${SNAME} -config ${CONFIG_DIR}/rvi/sys -setcookie ${COOKIE}"

case "${CMD}" in
   start)
	 install -d --mode 0755  ${TMP_DIR}
	 install -d --mode 0755  ${LOG_DIR}
	 exec run_erl -daemon ${TMP_DIR}/ ${LOG_DIR} "exec ${LAUNCH}"
	 ;;

   console)
       exec ${LAUNCH}
       ;;

   stop)
       exec ${SELF_DIR}/nodetool -sname ${SNAME} -setcookie ${COOKIE} stop
       ;;

   ping)
       exec ${SELF_DIR}/nodetool -sname ${SNAME} -setcookie ${COOKIE} ping
       ;;

   attach) 
       exec to_erl ${TMP_DIR}
       ;;

esac