File: cuttlefish-user.cuttlefish-operator.init

package info (click to toggle)
android-cuttlefish 1.0.1-0~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,192 kB
  • sloc: cpp: 39,149; sh: 2,523; javascript: 242; exp: 152; python: 125; makefile: 88
file content (151 lines) | stat: -rwxr-xr-x 4,193 bytes parent folder | download
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
#!/usr/bin/env bash
#
### BEGIN INIT INFO
# Provides: cuttlefish-operator
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Cuttlefish Host Orchestrator service
# Description: The Host Orchestrator service provides the signaling
#              server used by all cuttlefish instances running in this
#              host as well as orchestration capabilities.
### END INIT INFO
#
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Make sure calls to this script get redirected to systemctl when
# using systemd

. /lib/lsb/init-functions

if [ -f /etc/default/cuttlefish-operator ]; then
    . /etc/default/cuttlefish-operator
fi

RUN_DIR="/run/cuttlefish"
ASSET_DIR="/usr/share/cuttlefish-common/operator"
DAEMON="/usr/lib/cuttlefish-common/bin/operator"
PIDFILE="${RUN_DIR}"/operator.pid

gen_cert() {
  operator_tls_cert_dir=${operator_tls_cert_dir:-/etc/cuttlefish-common/operator/cert}
  CERT_FILE="${operator_tls_cert_dir}/cert.pem"
  KEY_FILE="${operator_tls_cert_dir}/key.pem"
  if [ -f "$CERT_FILE" ] && [ -f "$KEY_FILE" ]; then
    echo "TLS certificate files for https communication already exist."
  else
    mkdir -p "${operator_tls_cert_dir}"
    openssl req \
      -newkey rsa:4096 \
      -x509 \
      -sha256 \
      -days 36000 \
      -nodes \
      -out "${CERT_FILE}" \
      -keyout "${KEY_FILE}" \
      -subj "/C=US"
    chown _cutf-operator:cvdnetwork "${CERT_FILE}"
    chown _cutf-operator:cvdnetwork "${KEY_FILE}"
  fi
}

start() {
  gen_cert
  mkdir -p "${RUN_DIR}"
  chown _cutf-operator:cvdnetwork "${RUN_DIR}"
  chmod 775 "${RUN_DIR}"

  args=()
  if [[ -n "${operator_http_port}" ]]; then
    args+=(--http_port="${operator_http_port}")
  fi
  if [[ -n "${operator_https_port}" ]]; then
    args+=(--https_port="${operator_https_port}")
  fi
  if [[ -n "${operator_tls_cert_dir}" ]]; then
    args+=(--tls_cert_dir="${operator_tls_cert_dir}")
  fi
  args+=(--socket_path="${RUN_DIR}"/operator)
  if [[ -n "${operator_webui_url}" ]]; then
    args+=(--webui_url="${operator_webui_url}")
  fi
  if [[ -n "${operator_listen_address}" ]]; then
    args+=(--listen_addr="${operator_listen_address}")
  fi

  start-stop-daemon --start \
    --pidfile "${PIDFILE}" \
    --chuid _cutf-operator:cvdnetwork \
    --chdir "${ASSET_DIR}" \
    --background --no-close \
    --make-pidfile \
    --exec "${DAEMON}" -- "${args[@]}"
}

stop() {
  start-stop-daemon --stop \
    --pidfile "${PIDFILE}" \
    --remove-pidfile \
    --exec "${DAEMON}"
  # The presence of the socket will cause devices to try to connect to it
  # instead of starting their own signaling servers, so it needs to be removed
  # once the service isn't running.
  unlink "${RUN_DIR}"/operator
}

status() {
  # Return
  #   0 if daemon is running
  #   1 if daemon is dead and pid file exists
  #   3 if daemon is not running
  #   4 if daemon status is unknown
  start-stop-daemon --start --quiet --pidfile "${PIDFILE}" --exec ${DAEMON} --test > /dev/null
  case "${?}" in
    0) [ -e "${PIDFILE}" ] && return 1 ; return 3 ;;
    1) return 0 ;;
    *) return 4 ;;
  esac
}

usage() {
    echo $0: start\|stop\|status
}

if test $# != 1; then
    usage
else
    case "$1" in
        --help)
            usage 0
            ;;
        start|stop|status)
            "$1"
            ;;
        restart|force-reload|condrestart|try-restart)
            stop && start
            ;;
        reload)
            # Nothing to do
            ;;
        shutdown)
            stop
            ;;
        *)
            usage
            ;;
    esac
fi
exit 0