如何在snap安装时运行我们的hook脚本

作者: Ubuntu手机 2017-01-17 09:22:19

对于有些snap应用来说,我们很希望在snap安装时能够运行我们的一段脚本来做一些我们想要做的事,比如创建一个文件夹等.那么我们如何能得到这个事件呢?在我们的先前的文章"如何为我们的Ubuntu Core应用进行设置"(http://www.linuxdiyf.com/linux/25590.html)中,我们已经展示了如何设置我们的snap应用.在那里面的configure脚本在设置时会被调用.事实上,它在安装时也会被自动调用.下面,我们以如下的例子来说明:


https://github.com/liu-xiao-guo/helloworld-install


 


在上面的例子中,我们的configure脚本如下:


configure


#!/bin/sh  


echo "This is called during the installation!"  


exit 1


 


这是一个非常简单的脚本程序.在我们的安装过程中,它返回的值是"1",表明它是失败的.那么这个应用将不被成功安装:


liu-xiao-guo@localhost:~/apps/helloworld-install$ sudo snap install *.snap --dangerous  


error: cannot perform the following tasks:  


- Run configure hook of "hello-install" snap if present (This is called during the installation!)  


liu-xiao-guo@localhost:~/apps/helloworld-install$ snap list  


Name            Version       Rev  Developer  Notes  


classic         16.04         17   canonical  devmode  


core            16.04.1       716  canonical  -  


grovepi-server  1.0           x1              devmode  


packageproxy    0.1           3    ogra       -  


pi2             16.04-0.17    29   canonical  -  


pi2-kernel      4.4.0-1030-3  22   canonical  -  


snapweb         0.21.2        25   canonical  -


 


显然通过上面的展示,helloworld-install没有被安装到我们的系统中去.


如果我们把configure脚本修改为:


 


configure


#!/bin/sh  


echo "This is called during the installation!"  


exit 0


 


这个脚本的返回值为"0",表明它的安装是成功的.


 


liu-xiao-guo@localhost:~/apps/helloworld-install$ sudo snap install *.snap --dangerous  


hello-install 1.0 installed  


liu-xiao-guo@localhost:~/apps/helloworld-install$ snap list  


Name            Version       Rev  Developer  Notes  


classic         16.04         17   canonical  devmode  


core            16.04.1       716  canonical  -  


grovepi-server  1.0           x1              devmode  


hello-install   1.0           x1              -  


packageproxy    0.1           3    ogra       -  


pi2             16.04-0.17    29   canonical  -  


pi2-kernel      4.4.0-1030-3  22   canonical  -  


snapweb         0.21.2        25   canonical  -  


liu-xiao-guo@localhost:~/apps/helloworld-install$ vi /var/log/syslog  


liu-xiao-guo@localhost:~/apps/helloworld-install$ sudo vi /var/log/syslog


 


我们可以在系统的/var/log/syslog中找到这个脚本运行时的输出:




 


显然脚本在安装时有被正常运行.我们可以通过运行这样一个hook来对我们的应用做一些初始化,从而为接下来的应用的运行铺好基础.


 


更多阅读:https://kyrofa.com/posts/snap-updates-automatic-rollbacks


 


本文永久更新地址:http://www.linuxdiyf.com/linux/27872.html

相关资讯