summaryrefslogtreecommitdiff
blob: 5a9a6e214efb11c1c4c1ccee6931f5efa2a70901 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/sbin/openrc-run

description="Manages the services needed to run Artifactory on a dedicated Tomcat"
command="/opt/artifactory/tomcat/bin/catalina.sh"
pidfile="/opt/artifactory/run/artifactory.pid"
procname="artifactory"
user=${ARTIFACTORY_USER:-artifactory}
group=${group:-artifactory}

# Artifactory can be very slow to start, and without locking,
# restarting via runscript is very error prone
CATALINA_LOCK_FILE="$(dirname $pidfile)/lock"

CATALINA_MGMT_PORT=8015
REQUIRED_JAVA_VERSION="1.8"
START_TIMEOUT=300

# Helper functions:
is_mgmt_port_used() {
    mgmt_port_used=$(netstat -vatn | grep LISTEN | grep $CATALINA_MGMT_PORT | wc -l)
}

get_pid_info() {
    pidValue=""
    javaPs=""
    if [ -e "$pidfile" ]; then
        pidValue=$(cat $pidfile)
        if [ -n "$pidValue" ]; then
            javaPs="$(ps -p $pidValue | grep java)"
        fi
    fi
}

depend() {
    need net nginx
    use dns logger netmount
}

start_pre() {
    if [ -z "$ARTIFACTORY_HOME" ] || [ ! -d "$ARTIFACTORY_HOME" ]; then
        eerror "Artifactory home folder not defined or does not exists at $ARTIFACTORY_HOME"
        return 1
    fi

    if [ -z "$pidfile" ]; then
        eerror "Artifactory pid destination pidfile was not set!"
        return 1
    fi

    if [ -z "$TOMCAT_HOME" ] || [ ! -d "$TOMCAT_HOME" ]; then
        eerror "Tomcat Artifactory folder not defined or does not exists at $TOMCAT_HOME"
        return 1
    fi

    if [ -z "$ARTIFACTORY_USER" ]; then
        ARTIFACTORY_USER=$user
    fi

    current_open_file_limit=$(su -l $ARTIFACTORY_USER -c "ulimit -n")
    desired_open_file_limit=32000
    if [ "$current_open_file_limit" != "unlimited" ] && [ $current_open_file_limit -lt $desired_open_file_limit ]; then
        eerror "Maximum number of open files ($current_open_file_limit) is too small!"
        eerror "You should add:"
        eerror "artifactory soft nofile $desired_open_file_limit"
        eerror "artifactory hard nofile $desired_open_file_limit"
        eerror "to your /etc/security/limits.conf file."
        return 1
    fi

    current_max_process_limit=$(su -l $ARTIFACTORY_USER -c "ulimit -u")
    desired_max_process_limit=1024
    if [ "$current_max_process_limit" != "unlimited" ] && [ $current_max_process_limit -lt $desired_max_process_limit ]; then
        eerror "Number of processes $current_max_process_limit is too small!"
        eerror "You should add:"
        eerror "artifactory soft nproc $desired_max_process_limit"
        eerror "artifactory hard nproc $desired_max_process_limit"
        eerror "to your /etc/security/limits.conf file."
        return 1
    fi

    if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/java" ]; then
        einfo "Found java executable in $JAVA_HOME"
        _java="$JAVA_HOME/bin/java"
    elif command -v java > dev/null 2>&1 ; then
        _java=java
    else
        eerror "No Java Runtime Environment (jre) found! At least java-$REQUIRED_JAVA_VERSION is required."
        return 1
    fi
    if [ "$_java" ]; then
        java_version=$($_java -version 2>&1 | grep 'version' | cut -d ' ' -f3 | tr -d \")
        java_major=$(echo $java_version | cut -d . -f1-2)
        if [ "$java_major" != "$REQUIRED_JAVA_VERSION" ] ; then
            eerror "Java version $java_version is too old, java-$REQUIRED_JAVA_VERSION is required."
            return 1
        fi
    fi
}

# We have to pass start/stop through to catalina.sh, which doesn't understand restart (and restart can't be overridden in OpenRC):
start() {
    get_pid_info
    is_mgmt_port_used
    if [ $mgmt_port_used -ne 0 ] || [ -n "$javaPs" ]; then
        eerror "$RC_SERVICE already started!"
        return 1
    else
        ebegin "Starting $RC_SERVICE"
        start-stop-daemon --exec $command --pidfile $pidfile --user $user:$group --wait $START_TIMEOUT start
        RETVAL=$?
        eend $RETVAL "start-stop-daemon failed to start $RC_SERVICE"
    fi


    is_mgmt_port_used
    seconds=1
    while [ $mgmt_port_used -eq 0 ] && [ $seconds -lt $START_TIMEOUT ]; do
        sleep 1
        seconds=$((seconds+1))
        is_mgmt_port_used
    done
    if [ $mgmt_port_used -eq 0 ]; then
        eerror "$RC_SERVICE did not start in $START_TIMEOUT seconds!"
        return 1
    elif [ $mgmt_port_used -eq 1 ]; then
        einfo "$RC_SERVICE started"
        [ $RETVAL=0 ] && touch $CATALINA_LOCK_FILE
    else
        eerror "Got unexpected result when checking port $CATALINA_MGMT_PORT"
        return 1
    fi
    return $RETVAL
}

stop() {
    ebegin "Stopping $RC_SERVICE"
    start-stop-daemon --stop --pidfile $pidfile --retry $START_TIMEOUT stop
    RETVAL=$?
    eend $RETVAL "Failed to stop $RC_SERVICE!"

    [ $RETVAL=0 ] && rm -f "$CATALINA_LOCK_FILE" "$pidfile"
    return $RETVAL
}

status() {
    is_mgmt_port_used
    if [ $mgmt_port_used -eq 0 ]; then
        if [ -e "$pidfile" ]; then
            eerror "$RC_SERVICE stopped but the pid file $pidfile still exists!"
            RETVAL=1
        else
            if [ -e "$CATALINA_LOCK_FILE" ]; then
                eerror "$RC_SERVICE is stopped but the lock file $CATALINA_LOCK_FILE still exists!"
                RETVAL=2
            else
                eerror "$RC_SERVICE is stopped"
                RETVAL=3
            fi
        fi
    else
        einfo "$RC_SERVICE is running"
        RETVAL=0
    fi
    return $RETVAL
}