github.com/yggdrasil-network/yggdrasil-go@v0.5.6/contrib/macos/create-pkg.sh (about)

     1  #!/bin/sh
     2  
     3  # Check if xar and mkbom are available
     4  command -v xar >/dev/null 2>&1 || (
     5    echo "Building xar"
     6    sudo apt-get install libxml2-dev libssl1.0-dev zlib1g-dev -y
     7    mkdir -p /tmp/xar && cd /tmp/xar
     8    git clone https://github.com/mackyle/xar && cd xar/xar
     9    (sh autogen.sh && make && sudo make install) || (echo "Failed to build xar"; exit 1)
    10  )
    11  command -v mkbom >/dev/null 2>&1 || (
    12    echo "Building mkbom"
    13    mkdir -p /tmp/mkbom && cd /tmp/mkbom
    14    git clone https://github.com/hogliux/bomutils && cd bomutils
    15    sudo make install || (echo "Failed to build mkbom"; exit 1)
    16  )
    17  
    18  # Build Yggdrasil
    19  echo "running GO111MODULE=on GOOS=darwin GOARCH=${PKGARCH-amd64} ./build"
    20  GO111MODULE=on GOOS=darwin GOARCH=${PKGARCH-amd64} ./build
    21  
    22  # Check if we can find the files we need - they should
    23  # exist if you are running this script from the root of
    24  # the yggdrasil-go repo and you have ran ./build
    25  test -f yggdrasil || (echo "yggdrasil binary not found"; exit 1)
    26  test -f yggdrasilctl || (echo "yggdrasilctl binary not found"; exit 1)
    27  test -f contrib/macos/yggdrasil.plist || (echo "contrib/macos/yggdrasil.plist not found"; exit 1)
    28  test -f contrib/semver/version.sh || (echo "contrib/semver/version.sh not found"; exit 1)
    29  
    30  # Delete the pkgbuild folder if it already exists
    31  test -d pkgbuild && rm -rf pkgbuild
    32  
    33  # Create our folder structure
    34  mkdir -p pkgbuild/scripts
    35  mkdir -p pkgbuild/flat/base.pkg
    36  mkdir -p pkgbuild/flat/Resources/en.lproj
    37  mkdir -p pkgbuild/root/usr/local/bin
    38  mkdir -p pkgbuild/root/Library/LaunchDaemons
    39  
    40  # Copy package contents into the pkgbuild root
    41  cp yggdrasil pkgbuild/root/usr/local/bin
    42  cp yggdrasilctl pkgbuild/root/usr/local/bin
    43  cp contrib/macos/yggdrasil.plist pkgbuild/root/Library/LaunchDaemons
    44  
    45  # Create the postinstall script
    46  cat > pkgbuild/scripts/postinstall << EOF
    47  #!/bin/sh
    48  
    49  # Normalise the config if it exists, generate it if it doesn't
    50  if [ -f /etc/yggdrasil.conf ];
    51  then
    52    mkdir -p /Library/Preferences/Yggdrasil
    53    echo "Backing up configuration file to /Library/Preferences/Yggdrasil/yggdrasil.conf.`date +%Y%m%d`"
    54    cp /etc/yggdrasil.conf /Library/Preferences/Yggdrasil/yggdrasil.conf.`date +%Y%m%d`
    55    echo "Normalising /etc/yggdrasil.conf"
    56    /usr/local/bin/yggdrasil -useconffile /Library/Preferences/Yggdrasil/yggdrasil.conf.`date +%Y%m%d` -normaliseconf > /etc/yggdrasil.conf
    57  else
    58    /usr/local/bin/yggdrasil -genconf > /etc/yggdrasil.conf
    59  fi
    60  
    61  # Unload existing Yggdrasil launchd service, if possible
    62  test -f /Library/LaunchDaemons/yggdrasil.plist && (launchctl unload /Library/LaunchDaemons/yggdrasil.plist || true)
    63  
    64  # Load Yggdrasil launchd service and start Yggdrasil
    65  launchctl load /Library/LaunchDaemons/yggdrasil.plist
    66  EOF
    67  
    68  # Set execution permissions
    69  chmod +x pkgbuild/scripts/postinstall
    70  chmod +x pkgbuild/root/usr/local/bin/yggdrasil
    71  chmod +x pkgbuild/root/usr/local/bin/yggdrasilctl
    72  
    73  # Pack payload and scripts
    74  ( cd pkgbuild/scripts && find . | cpio -o --format odc --owner 0:80 | gzip -c ) > pkgbuild/flat/base.pkg/Scripts
    75  ( cd pkgbuild/root && find . | cpio -o --format odc --owner 0:80 | gzip -c ) > pkgbuild/flat/base.pkg/Payload
    76  
    77  # Work out metadata for the package info
    78  PKGNAME=$(sh contrib/semver/name.sh)
    79  PKGVERSION=$(sh contrib/semver/version.sh --bare)
    80  PKGARCH=${PKGARCH-amd64}
    81  PAYLOADSIZE=$(( $(wc -c pkgbuild/flat/base.pkg/Payload | awk '{ print $1 }') / 1024 ))
    82  [ "$PKGARCH" = "amd64" ] && PKGHOSTARCH="x86_64" || PKGHOSTARCH=${PKGARCH}
    83  
    84  # Create the PackageInfo file
    85  cat > pkgbuild/flat/base.pkg/PackageInfo << EOF
    86  <pkg-info format-version="2" identifier="io.github.yggdrasil-network.pkg" version="${PKGVERSION}" install-location="/" auth="root">
    87    <payload installKBytes="${PAYLOADSIZE}" numberOfFiles="3"/>
    88    <scripts>
    89      <postinstall file="./postinstall"/>
    90    </scripts>
    91  </pkg-info>
    92  EOF
    93  
    94  # Create the BOM
    95  ( cd pkgbuild && mkbom root flat/base.pkg/Bom )
    96  
    97  # Create the Distribution file
    98  cat > pkgbuild/flat/Distribution << EOF
    99  <?xml version="1.0" encoding="utf-8"?>
   100  <installer-script minSpecVersion="1.000000" authoringTool="com.apple.PackageMaker" authoringToolVersion="3.0.3" authoringToolBuild="174">
   101      <title>Yggdrasil (${PKGNAME}-${PKGVERSION})</title>
   102      <options customize="never" allow-external-scripts="no" hostArchitectures="${PKGHOSTARCH}" />
   103      <domains enable_anywhere="true"/>
   104      <installation-check script="pm_install_check();"/>
   105      <script>
   106      function pm_install_check() {
   107        if(!(system.compareVersions(system.version.ProductVersion,'10.10') >= 0)) {
   108          my.result.title = 'Failure';
   109          my.result.message = 'You need at least Mac OS X 10.10 to install Yggdrasil.';
   110          my.result.type = 'Fatal';
   111          return false;
   112        }
   113        return true;
   114      }
   115      </script>
   116      <choices-outline>
   117          <line choice="choice1"/>
   118      </choices-outline>
   119      <choice id="choice1" title="base">
   120          <pkg-ref id="io.github.yggdrasil-network.pkg"/>
   121      </choice>
   122      <pkg-ref id="io.github.yggdrasil-network.pkg" installKBytes="${PAYLOADSIZE}" version="${VERSION}" auth="Root">#base.pkg</pkg-ref>
   123  </installer-script>
   124  EOF
   125  
   126  # Finally pack the .pkg
   127  ( cd pkgbuild/flat && xar --compression none -cf "../../${PKGNAME}-${PKGVERSION}-macos-${PKGARCH}.pkg" * )