So snapcraft
is blowing me away with just how easy and complete the feature set is. Got a Raspberry Pi just this weekend, decided to try out the gobot.io
framework, and naturally, on the Ubuntu Core platform, which means making a snap
package.
I've spent time playing with snaps
and snapcraft
before, trying to snap up image processing software like Imagemagick and Gimp. So I knew before today that there's a lot of nice automatic qualities to the C language plugins. If you're not aware, Go is a kind of grandchild of C in a sense, but designed to address the rough edges of actually using C and C++ to get work done. Go is a new language with some radical elements to its design, meaning that there are some real limitations and corner cases of weirdness, but it has a lot of really awesome nice qualities for someone like me (who is not a real programmer).
Snapcraft takes advantage of these nice features, that a single build-time flag takes care of all the fine points of creating a snap package for cross-platform deployment:
~/go/src/gobot/gobot-blink$ snapcraft --target-arch=armhf --debug
Starting snapcraft 2.31 from /usr/lib/python3/dist-packages/snapcraft/cli.
Setting target machine to 'armhf'
"grade" property not specified: defaulting to "stable"
Setting up part 'gobot-blink' with plugin 'go' and properties {'go-importpath': 'bitbucket.org/wandrewkeech/gobot-demo', 'prime': [], 'go-buildtags': ['GOOS=linux', 'GOARCH=arm', 'GOARM=7'], 'source': '.', 'prepare': 'export GOOS=linux\nexport GOARCH=arm\nexport GOARM=7\n', 'plugin': 'go', 'stage': []}.
Setting 'armhf' as the compilation target for 'go'
Marking 'gcc-arm-linux-gnueabihf' (and its dependencies) to be fetched
Marking 'libc6-dev-armhf-cross' (and its dependencies) to be fetched
Marking 'golang-go' (and its dependencies) to be fetched
Preparing to pull gobot-blink
Pulling gobot-blink
go get -t -d ./bitbucket.org/wandrewkeech/gobot-demo/...
Preparing to build gobot-blink
Building gobot-blink
go build -o /home/andy/go/src/gobot/gobot-blink/parts/gobot-blink/go/bin/gobot-demo -tags=GOOS=linux,GOARCH=arm,GOARM=7 bitbucket.org/wandrewkeech/gobot-demo
Staging gobot-blink
Priming gobot-blink
Checking that 'gobot-demo' is in the $PATH
Snapping 'gobot-blink-demo' /
Parallel mksquashfs: Using 8 processors
Creating 4.0 filesystem on gobot-blink-demo_thing_armhf.snap, block size 131072.
[=================================================================|] 23/23 100%
Exportable Squashfs 4.0 filesystem, xz compressed, data block size 131072
compressed data, compressed metadata, compressed fragments, no xattrs
duplicates are removed
Filesystem size 689.59 Kbytes (0.67 Mbytes)
26.06% of uncompressed filesystem size (2646.37 Kbytes)
Inode table size 210 bytes (0.21 Kbytes)
67.74% of uncompressed inode table size (310 bytes)
Directory table size 147 bytes (0.14 Kbytes)
100.00% of uncompressed directory table size (147 bytes)
Number of duplicate files found 0
Number of inodes 7
Number of files 3
Number of fragments 1
Number of symbolic links 0
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 4
Number of ids (unique uids + gids) 1
Number of uids 1
root (0)
Number of gids 1
root (0)
Snapped gobot-blink-demo_thing_armhf.snap
Again, the magic flag in the snapcraft
command was --target-arch=armhf
. I'd tried manually cross-compiling from amd64 to armv7 using prepare
attributes or go-buildtags
, absolutely didn't work at all. Turns out, I was trying to solve a solved problem! Love you snapcraft devs!
Then I followed it up with scp
'ing the snap to my Rpi, and trying to run it:
andy-k@localhost:~$ gobot-blink-demo2017/06/26 07:16:23 Initializing connections...
2017/06/26 07:16:23 Initializing connection RaspberryPi-5ECA3639 ...
2017/06/26 07:16:23 Initializing devices...
2017/06/26 07:16:23 Initializing device LED-3CDCAC3 ...
2017/06/26 07:16:23 Robot snapbot initialized.
2017/06/26 07:16:23 Starting Robot snapbot ...
2017/06/26 07:16:23 Starting connections...
2017/06/26 07:16:23 Starting connection RaspberryPi-5ECA3639...
2017/06/26 07:16:23 Starting devices...
2017/06/26 07:16:23 Starting device LED-3CDCAC3 on pin ?...
2017/06/26 07:16:23 Starting work...
Just magic! Can't wait to try the gobot
framework to do something useful next...