github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/pfsagentd/container/insert/MakePFSAgentDockerfile (about)

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