github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/filesystem/rootfs/docker/scripts/init-registry.sh (about)

     1  #!/bin/bash
     2  # Copyright © 2021 Alibaba Group Holding Ltd.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  
    17  set -e
    18  set -x
    19  # prepare registry storage as directory
    20  cd $(dirname "$0")
    21  
    22  REGISTRY_PORT=${1-5000}
    23  VOLUME=${2-/var/lib/registry}
    24  REGISTRY_DOMAIN=${3-sea.hub}
    25  
    26  container=sealer-registry
    27  rootfs=$(dirname "$(pwd)")
    28  config="$rootfs/etc/registry_config.yml"
    29  htpasswd="$rootfs/etc/registry_htpasswd"
    30  certs_dir="$rootfs/certs"
    31  image_dir="$rootfs/images"
    32  
    33  mkdir -p "$VOLUME" || true
    34  
    35  startRegistry() {
    36      n=1
    37      while (( n <= 3 ))
    38      do
    39          echo "attempt to start registry"
    40          (docker start $container && break) || (( n < 3))
    41          (( n++ ))
    42          sleep 3
    43      done
    44  }
    45  
    46  load_images() {
    47  for image in "$image_dir"/*
    48  do
    49   if [ -f "${image}" ]
    50   then
    51    docker load -q -i "${image}"
    52   fi
    53  done
    54  }
    55  
    56  check_registry() {
    57      n=1
    58      while (( n <= 3 ))
    59      do
    60          registry_status=$(docker inspect --format '{{json .State.Status}}' sealer-registry)
    61          if [[ "$registry_status" == \"running\" ]]; then
    62              break
    63          fi
    64          if [[ $n -eq 3 ]]; then
    65             echo "sealer-registry is not running, status: $registry_status"
    66             exit 1
    67          fi
    68          (( n++ ))
    69          sleep 3
    70      done
    71  }
    72  
    73  load_images
    74  
    75  ## rm container if exist.
    76  if [ "$(docker ps -aq -f name=$container)" ]; then
    77      docker rm -f $container
    78  fi
    79  
    80  regArgs="-d --restart=always \
    81  --net=host \
    82  --name $container \
    83  -v $certs_dir:/certs \
    84  -v $VOLUME:/var/lib/registry \
    85  -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/$REGISTRY_DOMAIN.crt \
    86  -e REGISTRY_HTTP_TLS_KEY=/certs/$REGISTRY_DOMAIN.key"
    87  
    88  if [ -f $config ]; then
    89      sed -i "s/5000/$1/g" $config
    90      regArgs="$regArgs \
    91      -v $config:/etc/docker/registry/config.yml"
    92  fi
    93  
    94  if [ -f $htpasswd ]; then
    95      docker run $regArgs \
    96              -v $htpasswd:/htpasswd \
    97              -e REGISTRY_AUTH=htpasswd \
    98              -e REGISTRY_AUTH_HTPASSWD_PATH=/htpasswd \
    99              -e REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm" registry:2.7.1 || startRegistry
   100  else
   101      docker run $regArgs registry:2.7.1 || startRegistry
   102  fi
   103  
   104  check_registry