Ros launch writable install


#1

I have a ros application that I am currently creating a snap with, and I was wandering if it is possible to install the launch files so they could be user modified after installing the snap on another system. If that is not possible is the best approach to create a separate config file and use the install in the part to put it into the SNAP_USER_DATA directory? Thanks for any help.


#2

you can add a wrapper script to copy files around before starting your app, like:

#! /bin/sh

[ -e $SNAP_USER_DATA/config.conf ] || cp $SNAP/config.conf $SNAP_USER_DATA/

$SNAP/bin/myapp --config $SNAP_USER_DATA/config.conf

and then use the wrapper script in your command: in the app section of your snapcraft.yaml


#3

Indeed, as @ogra implies, there’s no way to put the config file into a snap and say “hey, this should be user-modifiable.” You’ll need to actually put that config file in a writable place, such as $SNAP_USER_DATA (i.e. the second option you mentioned).

Does the launch file need to be user-specific though? If you want it to be writable system-wide, and put it in $SNAP_DATA, the install hook might be a better option for putting it in place. The downside is that it runs as root, so $SNAP_USER_DATA won’t be what you want.


#4

Thanks for the advice. I just found the install hook and I think that will accomplish what I want to do. The problem is when I install the snap it isn’t running it. I tried the configure hook and it does run that. Here are my scripts:

hooks:
   install:
     plugs: [network]
   configure:
     plugs: [network]

my install file in snap/hooks/install
#!/bin/sh -e

echo "sleep_time 5" > "$SNAP_USER_DATA/test.conf"
cp $SNAP/opt/ros/kinetic/share/merlin/merlin_config.yaml $SNAP_USER_DATA/
exit 1

This doesn’t fail in the install

my configure
#!/bin/sh -e

echo "sleep_time 5" > "$SNAP_DATA/merlin_test.conf"
cp $SNAP/opt/ros/kinetic/share/merlin/merlin_config.yaml $SNAP_DATA/

This does work. Here is the output of my changes and it doesn’t look like it is running the install script:

Status Spawn Ready Summary
Done today at 10:41 EDT today at 10:41 EDT Ensure prerequisites for “merlin” are available
Done today at 10:41 EDT today at 10:41 EDT Prepare snap “/tmp/snapd-sideload-pkg-824565653” (unset)
Done today at 10:41 EDT today at 10:41 EDT Mount snap “merlin” (unset)
Done today at 10:41 EDT today at 10:41 EDT Run pre-refresh hook of “merlin” snap if present
Done today at 10:41 EDT today at 10:41 EDT Stop snap “merlin” services
Done today at 10:41 EDT today at 10:41 EDT Remove aliases for snap “merlin”
Done today at 10:41 EDT today at 10:41 EDT Make current revision for snap “merlin” unavailable
Done today at 10:41 EDT today at 10:41 EDT Copy snap “merlin” data
Done today at 10:41 EDT today at 10:41 EDT Setup snap “merlin” (unset) security profiles
Done today at 10:41 EDT today at 10:41 EDT Make snap “merlin” (unset) available to the system
Done today at 10:41 EDT today at 10:41 EDT Setup snap “merlin” (unset) security profiles (phase 2)
Done today at 10:41 EDT today at 10:41 EDT Automatically connect eligible plugs and slots of snap “merlin”
Done today at 10:41 EDT today at 10:41 EDT Set automatic aliases for snap “merlin”
Done today at 10:41 EDT today at 10:41 EDT Setup snap “merlin” aliases
Done today at 10:41 EDT today at 10:41 EDT Run post-refresh hook of “merlin” snap if present
Done today at 10:41 EDT today at 10:41 EDT Start snap “merlin” (unset) services
Done today at 10:41 EDT today at 10:41 EDT Remove data for snap “merlin” (x18)
Done today at 10:41 EDT today at 10:41 EDT Remove snap “merlin” (x18) from the system
Done today at 10:41 EDT today at 10:41 EDT Clean up “merlin” (unset) install
Done today at 10:41 EDT today at 10:41 EDT Run configure hook of “merlin” snap if present

Did something change with the install hook or do you see anything that is wrong?
Thanks.


#5

haha you responded as i was typing and thanks I found the install hook from your blog.


#6

Can I see the output of snap version, please?


#7

snap 2.33.1
snapd 2.33.1
series 16
ubuntu 16.04
kernel 4.13.0-45-generic

wow that is weird for the first time it just ran it just now.


#8

Okay that’s fine. Note that the install hook only runs the first time you install the snap. Any chance you already had it installed before you tried adding it in? In that case, running snap install --dangerous foo.snap is actually an update, not an install, and won’t run the install hook.


#9

Ahhh that is definitely what the problem was. Thanks! I am installing --dangerous always because it is for a robot that is not connected to the internet. Do you think it is better to snap remove before I install each time and use the install hook or use a different hook?


#10

not only that … it also runs as root … so $SNAP_USER_DATA is essentially /root/snap/<snapname>/current … not helpful if you want to have it in an actual users homedir :slight_smile:

EDIT: oops, ignore that, seems you actually mentioned the same issue a few posts above, i missed that …


#11

My answer is twofold. First of all, do you need to use the install hook? If you can answer all the following questions with “yes”, then yeah, I think it makes sense:

  • Should this operation only happen once?
    • The install hook is only run when the snap is initially installed, not updates.
  • Is it okay that hooks run as root?
    • Don’t worry, they’re still confined, but it means that $SNAP_USER_DATA is /root/snap/blah/blah, not users you really care about. Hooks are typically only used to access the system directories, like $SNAP_DATA.

Installing config files is I think a perfect case for the install hook. If you actually intend for a user to modify them, it would seem to be in poor taste to then overwrite their modifications when an update occurs. So put them in place on first install and forget about them.

Second, if you have arrived at “yes, I should use the install hook,” there are two ways to test it out. The first one is easy but annoying: make the snap, remove the old snap and install the new one again (this is less painful if you can use snap try, are you familiar with that?). An easier way is to install your new snap without removing the old one, which won’t run the hook, but then you can use snap run --hook install <snap name> to run it on demand and test it out.


#12

Thanks @ogra and @kyrofa these were great descriptions! Based on your description, the install hook sounds like the best way to go. Thanks again so much.