在今天的这篇文章中,我们来重点介绍如何利用snapcraft来编译我们的应用,并打包我们的应用成为.snap包.由于目前我们的snapcraft还不支持cross-compile,所有我们还需要为我们的开发板(比如树莓派)建立一个armhf的环境来编译我们的应用,并最终部署到我们的开发版上.在这里我们以树莓派的开发版为例.如果大家目前还不知道如何构建自己的硬件环境,请参阅我的文章"如何安装Snappy Ubuntu到树莓派(RaspBerry PI)上".
$ sudo add-apt-repository ppa:snappy-dev/tools $ sudo apt-get update $ sudo apt-get install snappy-tools bzr snapcraft
$ git clone https://github.com/liu-xiao-guo/go-webserver.git
$ touch go.png
name: go-webserver vendor: XiaoGuo, Liu <[email protected]> icon: go.png version: 1.0.7 summary: Go webserver description: This is a simple go webserver. It is a service services: webserver: description: "Go websever" start: bin/golang-http caps: - network-client - network-service parts: webserver: plugin: go source: git://github.com/liu-xiao-guo/golang-http
liuxg@liuxg:~/snappy/examples/go-webserver$ ls go.png snapcraft.yaml
package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", handleMainPage) log.Println("Starting webserver on :8081") if err := http.ListenAndServe(":8081", nil); err != nil { log.Fatal("http.ListendAndServer() failed with %s\n", err) } } func handleMainPage(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } fmt.Fprintf(w, "Hello World, xiaoguo\n") }
services: webserver: description: "Go websever" start: bin/golang-http caps: - network-client - network-service
$ kvm -m 512 -redir :8090::8081 -redir :8022::22 -redir :4200::4200 ubuntu-15.04-snappy-amd64-generic.img
liuxg@liuxg:~/snappy/examples/go-webserver$ snappy-remote --url=ssh://localhost:8022 install go-webserver_1.0.7_amd64.snap
$ snapcraft version
name: piglow version: 1.0 vendor: XiaoGuo, Liu <[email protected]> summary: Piglow API description: This is the webserver API to control the piglow icon: icon.png services: piglow: start: bin/piglow caps: - network-client - network-service parts: piglow: plugin: go source: ./src/piglow在上面,我们可以看到"./src/piglow",表明它的源码是在本地.0.5版本以前的snapcraft是不可以编译这样的项目的.它只能编译如下的snapcraft.yaml文件:
name: piglow version: 1.0 vendor: XiaoGuo, Liu <[email protected]> summary: Piglow API description: This is the webserver API to control the piglow icon: icon.png services: piglow: start: bin/piglow caps: - network-client - network-service parts: piglow: plugin: go source: git://github.com/mikix/golang-static-http在这里,source的源码是指向一个git的repository.
$ git clone https://github.com/ubuntu-core/snapcraft
$ ./setup.py build $ sudo ./setup.py install