github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/pfsagentd/container/insert/MakePFSAgentDockerfile (about)

     1  #!/bin/bash
     2  
     3  # Copyright (c) 2015-2021, NVIDIA CORPORATION.
     4  # SPDX-License-Identifier: Apache-2.0
     5  
     6  function usage {
     7      echo "usage:"
     8      echo "  $0 -h"
     9      echo "      outputs this usage and conf file format"
    10      echo "  $0 --clean"
    11      echo "      cleans current directory of generated files"
    12      echo "  $0 <conf-file>"
    13      echo "      derives a PFSAgent-enabled Dockerfile"
    14      echo "      based on directives in <conf-file>"
    15  }
    16  
    17  trim()
    18  {
    19      local trimmed="$1"
    20      while [[ $trimmed == ' '* ]]; do
    21         trimmed="${trimmed## }"
    22      done
    23      while [[ $trimmed == *' ' ]]; do
    24          trimmed="${trimmed%% }"
    25      done
    26      echo "$trimmed"
    27  }
    28  
    29  if [[ $# -eq 1 ]]
    30  then
    31      if [[ "$1" = "-h" ]]
    32      then
    33          usage
    34          echo
    35          echo "<conf-file> is in .INI format"
    36          echo
    37          echo "Commented lines begin with '#'"
    38          echo
    39          echo "The first section, [Globals], should have the following Key:Value pairs:"
    40          echo
    41          echo "SourceImage:   <repository:tag>"
    42          echo "PFSAgentImage: <repository:tag>"
    43          echo
    44          echo "Other sections enumerate each PFSAgent instance to configure."
    45          echo "The name of each section should be unique, ane be neither"
    46          echo "\"Globals\" nor \"TEMPLATE\" and enclosed in '[' and ']'."
    47          echo
    48          echo "Each such section (and there must be at least one of them)"
    49          echo "should have the following Key:Value pairs:"
    50          echo
    51          echo "FUSEVolumeName:     <e.g. CommonVolume>"
    52          echo "FUSEMountPointPath: <e.g. AgentMountPoint>"
    53          echo "PlugInEnvValue:     <e.g. {\"AuthURL\":\"http://localhost:8080/auth/v1.0\"\\u002C"
    54          echo "                           \"AuthUser\":\"test:tester\"\\u002C"
    55          echo "                           \"AuthKey\":\"testing\"\\u002C"
    56          echo "                           \"Account\":\"AUTH_test\"}>"
    57          echo "HTTPServerTCPPort:  <e.g. 9090>"
    58          exit 0
    59      elif [[ "$1" = "--clean" ]]
    60      then
    61          rm -f pfsagentd pfsagentd-init pfsagentd-swift-auth-plugin pfsagent.conf_* Dockerfile
    62          exit 0
    63      else
    64          CONF_FILE=$1
    65      fi
    66  else
    67      usage
    68      exit 1
    69  fi
    70  
    71  CURRENT_SECTION_NAME=""
    72  SOURCE_IMAGE=""
    73  PFS_AGENT_IMAGE=""
    74  
    75  while IFS= read -r line
    76  do
    77      if [[ ${#line} -ne 0 ]]
    78      then
    79          if [[ ${line:0:1} != "#" ]]
    80          then
    81              if [[ ${line:0:1} = "[" ]]
    82              then
    83                  NEW_SECTION_NAME=${line:1:${#line}-2}
    84                  if [[ ${#NEW_SECTION_NAME} -eq 0 ]]
    85                  then
    86                      echo "Section name cannot be empty"
    87                      exit 1
    88                  fi
    89                  if [[ $NEW_SECTION_NAME = "Globals" ]]
    90                  then
    91                      if [[ ${CURRENT_SECTION_NAME} != "" ]]
    92                      then
    93                          echo "Encountered 2nd [Globals] section"
    94                          exit 1
    95                      fi
    96                      CURRENT_SECTION_NAME="Globals"
    97                  else
    98                      if [[ ${CURRENT_SECTION_NAME} = "" ]]
    99                      then
   100                          echo "First section must be [Globals]"
   101                          exit 1
   102                      elif [[ ${CURRENT_SECTION_NAME} = "Globals" ]]
   103                      then
   104                          if [[ $NEW_SECTION_NAME = "TEMPLATE" ]]
   105                          then
   106                              echo "Encountered illegal section [TEMPLATE]"
   107                              exit 1
   108                          fi
   109                          if [[ ${SOURCE_IMAGE} = "" ]]
   110                          then
   111                              echo "Missing SourceImage in [Globals] section"
   112                              exit 1
   113                          fi
   114                          if [[ ${PFS_AGENT_IMAGE} = "" ]]
   115                          then
   116                              echo "Missing PFSAgentImage in [Globals] section"
   117                              exit 1
   118                          fi
   119                          CONTAINER=`docker create -ti pfsagent-build:latest`
   120                          docker cp $CONTAINER:/opt/PFSAgent/GOPATH/bin/pfsagentd .
   121                          docker cp $CONTAINER:/opt/PFSAgent/GOPATH/bin/pfsagentd-init .
   122                          docker cp $CONTAINER:/opt/PFSAgent/GOPATH/bin/pfsagentd-swift-auth-plugin .
   123                          docker cp $CONTAINER:/opt/PFSAgent/GOPATH/src/github.com/swiftstack/ProxyFS/pfsagentd/pfsagent.conf pfsagent.conf_TEMPLATE
   124                          docker container rm $CONTAINER > /dev/null
   125                      else
   126                          if [[ $NEW_SECTION_NAME = "TEMPLATE" ]]
   127                          then
   128                              echo "Encountered illegal section [TEMPLATE]"
   129                              exit 1
   130                          fi
   131                     fi
   132                      CURRENT_SECTION_NAME=$NEW_SECTION_NAME
   133                      cp pfsagent.conf_TEMPLATE pfsagent.conf_$CURRENT_SECTION_NAME
   134                  fi
   135              else
   136                  if [[ ${CURRENT_SECTION_NAME} == "" ]]
   137                  then
   138                      echo "Found Key:Value line before any section"
   139                      exit 1
   140                  elif [[ ${CURRENT_SECTION_NAME} == "Globals" ]]
   141                  then
   142                      if [[ $line == "SourceImage:"* ]]
   143                      then
   144                          SOURCE_IMAGE="$(trim "${line:12}")"
   145                      elif [[ $line == "PFSAgentImage:"* ]]
   146                      then
   147                          PFS_AGENT_IMAGE="$(trim "${line:14}")"
   148                     else
   149                          echo "Encountered unexpected Key:Value line in [Globals] section: $line"
   150                          exit 1
   151                      fi
   152                  else
   153                      echo $line >> pfsagent.conf_$CURRENT_SECTION_NAME
   154                  fi
   155              fi
   156          fi
   157      fi
   158  done < $CONF_FILE
   159  
   160  if [[ ${CURRENT_SECTION_NAME} = "" ]]
   161  then
   162      echo "Must have properly populated [Globals] section"
   163      echo "and at least one other section"
   164      exit 1
   165  fi
   166  
   167  if [[ ${CURRENT_SECTION_NAME} = "Globals" ]]
   168  then
   169      if [[ ${SOURCE_IMAGE} = "" ]]
   170      then
   171          echo "Missing SourceImage in [Globals] section"
   172          exit 1
   173      fi
   174      if [[ ${PFS_AGENT_IMAGE} = "" ]]
   175      then
   176          echo "Missing PFSAgentImage in [Globals] section"
   177          exit 1
   178      fi
   179      CONTAINER=`docker create -ti pfsagent-build:latest`
   180      docker cp $CONTAINER:/opt/PFSAgent/GOPATH/bin/pfsagentd .
   181      docker cp $CONTAINER:/opt/PFSAgent/GOPATH/bin/pfsagentd-init .
   182      docker cp $CONTAINER:/opt/PFSAgent/GOPATH/bin/pfsagentd-swift-auth-plugin .
   183      docker cp $CONTAINER:/opt/PFSAgent/GOPATH/src/github.com/swiftstack/ProxyFS/pfsagentd/pfsagent.conf pfsagent.conf_TEMPLATE
   184      docker container rm $CONTAINER > /dev/null
   185  fi
   186  
   187  ENTRYPOINT=`docker inspect --format '{{json .Config.Entrypoint}}' ${SOURCE_IMAGE}`
   188  CMD=`docker inspect --format '{{json .Config.Cmd}}' ${SOURCE_IMAGE}`
   189  WORKING_DIR=`docker inspect --format '{{json .Config.WorkingDir}}' ${SOURCE_IMAGE}`
   190  
   191  echo "FROM ${SOURCE_IMAGE}"                                                                   >  Dockerfile
   192  echo "RUN yum install -y fuse"                                                                >> Dockerfile
   193  echo "WORKDIR /opt/PFSAgent"                                                                  >> Dockerfile
   194  echo "COPY pfsagentd ."                                                                       >> Dockerfile
   195  echo "COPY pfsagentd-init ."                                                                  >> Dockerfile
   196  echo "COPY pfsagentd-swift-auth-plugin ."                                                     >> Dockerfile
   197  echo "COPY pfsagent.conf_* /opt/PFSAgent/"                                                    >> Dockerfile
   198  echo "ENV PATH /opt/PFSAgent:$PATH"                                                           >> Dockerfile
   199  echo "ENV PFSAGENT_PARENT_ENTRYPOINT  '${ENTRYPOINT}'"                                        >> Dockerfile
   200  echo "ENV PFSAGENT_PARENT_CMD         '${CMD}'"                                               >> Dockerfile
   201  echo "ENV PFSAGENT_PARENT_WORKING_DIR  ${WORKING_DIR}"                                        >> Dockerfile
   202  echo "ENTRYPOINT [\"pfsagentd-init\"]"                                                        >> Dockerfile
   203  echo ""                                                                                       >> Dockerfile
   204  echo "# To run a container for this image:"                                                   >> Dockerfile
   205  echo "#"                                                                                      >> Dockerfile
   206  echo "#   docker run -it --rm --device /dev/fuse --cap-add SYS_ADMIN -p 9090:9090 <IMAGE ID>" >> Dockerfile