#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

start_pod

cert_path = '/etc/ssl/certs'
certificate_file = "#{cert_path}/web-backend.crt"
certificate_key_file = "#{cert_path}/web-backend.key"

# If the server has an SSL certificate,
# the initiated scheduler supports both HTTP and HTTPS
conf_file = 'nginx-https.conf' if File.exist?(certificate_file) && File.exist?(certificate_key_file)
conf_file ||= 'nginx.conf'

names = Set.new %w[
  SCHED_PORT
]

defaults = relevant_defaults(names)
SCHED_PORT = defaults['SCHED_PORT']
env = docker_env(defaults)

docker_rm 'scheduler-nginx'

cmd = %w[
  docker run
  --name scheduler-nginx
  --restart=always
  -d
] + env + %W[
  -p #{SCHED_PORT}:#{SCHED_PORT}
  -v #{cert_path}:#{cert_path}
  -v #{ENV['CCI_SRC']}/container/scheduler-nginx/#{conf_file}:/etc/nginx/nginx.conf
  -v /etc/localtime:/etc/localtime:ro
  --log-opt mode=non-blocking
  --log-opt max-buffer-size=4m
  scheduler-nginx
]

system(*cmd)
