Use on <architechture> with environment vars

I need to set an environment variable depending on the target architecturej.

I’ve tried the following but I get an syntax error:

apps:
  pi-gation:
    command: tomcat-launch
    daemon: simple
    plugs: [network, network-bind]
    on arm64:
      environment:
        HELLO: WORLD
    else:
        PI4J_PLATFORM : Simulated
        SimulatedPlatform : RaspberryPi GPIO Provider

Have I understood the use of ‘on’. Is there some other way of doing this?

Brett

I don’t know and can’t constructively comment, but I assume you actually mean something like.

apps:
  pi-gation:
    command: tomcat-launch
    daemon: simple
    plugs: [network, network-bind]
    on arm64:
      environment:
        HELLO: WORLD
    else:
      PI4J_PLATFORM : Simulated
      SimulatedPlatform : RaspberryPi GPIO Provider

Umm, I can’t see the difference.

I’ve done some more reading and now notice that the ‘on’ keyword is a property (?) of the stage-packages keyword.

As such it doesn’t look like you can do this for environment variables.

This is unfortunately not supported, as a workaround you can probably integrate the environment setting into your “tomcat-launch” script.

For the record this is how I solved the problem.

I’ve also filled an enhancement request to get snapcraft to support ‘on’ for the environment keyword.

Note ‘tomcat-launch’ was the original launcher (providered by a third party plugin), so my new launcher effectively chains the original tomcat-launcher.
The plugin essentially writes out the new laucher which does the check for which architecture the app is running on and sets the appropriate environment vars.

apps:
  pi-gation:
  command: pi-gation-launch
  daemon: simple
  plugs: [network, network-bind]

pi-gation-launch:
  plugin: dump
  install: |
    cat <<EOF > $SNAPCRAFT_PART_INSTALL/pi-gation-launch
    #!/bin/sh
    ARCH=`uname -m`
    if [ "$SNAP_ARCH" = "arm64" ] || [ "$SNAP_ARCH" = "armhf" ]; then
        # no action require.
    else 
        # Run the simulator as we are not on RPi hardware
        export PI4J_PLATFORM="Simulated"
        export SimulatedPlatform="RaspberryPi GPIO Provider"
    fi
    ./tomcat-launch
    EOF
    chmod +x $SNAPCRAFT_PART_INSTALL/pi-gation-launch

Thanks for sharing your solution!