How to compile and Package snap (2) for our snappy Ubuntu app

Source: Internet
Author: User
Tags commit curl ssh git clone docker ps docker run

In my previous article, I have shown how to compile our projects in a desktop environment and successfully deploy them to our KVM environment. Perhaps interested developers would like to deploy our project to a device, such as Raspberry Pi, what should we do? In this article today, we will show you how to achieve this goal. We will use Docker technology to build an environment in our Raspberry Pi that can be compiled.

1) Build a compilation environment in the Raspberry Pi

We know that our current tool Snapcraft does not yet support cross-compile. The only way to do that is to build our own ARMHF compilation environment in our Raspberry Pi. If you have not set your own Raspberry Pi, please refer to the article "How to install snappy Ubuntu to Raspberry Pi (RaspBerry PI)". The main work we do next is to build an environment that can be compiled in snappy Ubuntu.
We can connect to our Raspberry Pi in the following ways:

$ ssh 192.168.1.112-l Ubuntu
Or
$ ssh ubuntu@192.168.1.112
Or a more straightforward approach.
$ ssh ubuntu@webdm.local

Please note that the "192.168.1.112" here is the IP address of our Raspberry Pi. You need to replace it with your own IP address. If a password is required, the default password is Ubuntu. We can start by looking at whether Docker has been installed in our Raspberry Pi:
(RASPBERRYPI2) ubuntu@localhost:~$ snappy list-v
Name          Date       Version      Developer  
ubuntu-core   2015-09-25 2            ubuntu*    
ubuntu-core   2015-09-25 2            ubuntu     
Docker        2015-11-09 1.6.2.004    canonical* 
go-webserver  2015-11-09 ifiujrxoregx sideload*  
mqtt-piglow   2015-11-02 Iewukjcenbwa sideload*  
port-listener 2015-10-31 iersnivokwsf sideload*  
webdm         2015-10-27 0.9.3        sideload   
WEBDM         2015-10-29 0.9.4        sideload*  
pi2           2015-09-25 0.16         canonical* 

In our system we can see that Docker is installed. If you do not see Docker installed, we can install our own Docker by entering the following command:
(RASPBERRYPI2) ubuntu@localhost:~$ sudo snappy install Docker

After installing your own docker, we can check the version of Docker:
(RASPBERRYPI2) ubuntu@localhost:~$ Docker version
client version:1.6.2
client API version:1.18
Go version (client): go1.4.2
Git commit (client): A464a34
os/arch (client): Linux/arm
Server version:1.6.2
Server API version:1.18
Go version (server): go1.4.2
Git commit (server): A464a34
os/arch (server): linux/ Arm
After running the above command, Docker will create a private directory of its own, such as the path shown below.
We then go into the Docker's private directory:
(RASPBERRYPI2) ubuntu@localhost:~$ CD apps/docker/1.6.2.004/
(RASPBERRYPI2) ubuntu@localhost:~/apps/docker/ 1.6.2.004$ mkdir Work

We also create a working directory of our own "work". Please note the ~/apps/docker/1.6.2.004 directory here. Here is the private directory where we install Docker, which can read and write in this directory. No other application can access this item, and Docker will not be able to access any private directories other than this directory. This is determined by the confinement of the snappy system. This directory will save the items that we need to compile later. Next, we use Docker to install an image of ARMHF:
$ (RASPBERRYPI2) ubuntu@localhost:~/apps/docker/1.6.2.004$ Docker pull KICKINZ1/SNAPPY-DEV-CLI-ARMHF

The above command will download a ARMHF image from Hub.docker.com that allows us to compile command line for the snappy application. The entire installation process needs to download about 400M of data, all of us need to wait patiently if the speed is not fast. The careful developer can find that the above command has downloaded the image and stored it in the Docker runtime directory:
(RaspberryPi2) ubuntu@localhost:/var/lib/apps/docker/1.6.2.004$ ls
aufs  containers  etc  graph  init  linkgraph.db  repositories-aufs  tmp  Trust  volumes

Once the download is complete, we can view the images we have downloaded by following the commands below:
(RASPBERRYPI2) ubuntu@localhost:~/apps/docker/1.6.2.004$ sudo docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
kickinz1/snappy-dev-cli-armhf   latest              2531f9e3f23c        4 days ago          1.022 GB

From above you can see the information of the images we have downloaded. Of course, if we do not want this image, we can use the following command to delete:
(RASPBERRYPI2) ubuntu@localhost:~/apps/docker/1.6.2.004$ sudo docker rmi 2531f9e3f23c

Note that the 2531f9e3f23c used here is the "IMAGE ID" that we have shown above. We need to replace this ID with our actual display. We have a ARMHF image, and then we create a container that we use ourselves:
sudo docker run-it  -v/home/ubuntu/apps/docker/1.6.2.004/work:/home/ubuntu/work--name snappy-dev-cli kickinz1/ Snappy-dev-cli-armhf

We used the above command to create a container that belonged to her. Because we used a name. " snappy-dev-cli"So when we exit our container, all the packages that have been installed inside will be retained. At the same time, we used the-V option to map the directory/home/ubuntu/apps/docker/1.6.2.004/work in our snappy system to the/home/ubuntu/work directory in our Ubuntu system. The files between them are shared. After creating our container for the first time, we use the Exit command to exit. We re-enter and then proceed with the following installation action.
We can use the following command to view the container we have installed and all of its information:
(RASPBERRYPI2) ubuntu@localhost:~$ sudo docker ps-a
CONTAINER ID        IMAGE                                  COMMAND                CREATED             STATUS                    PORTS               NAMES
CA5564878CCB        kickinz1/snappy-dev-cli-armhf:latest   "/bin/sh-c ' sudo ch   hours ago        Exited (0) hours ago                       snappy-dev-cli      

With the container above, we can have an Ubuntu environment in ARMHF. When we don't need this container, we can use the following command to delete it:
(RASPBERRYPI2) ubuntu@localhost:~$ sudo docker rm CA5564878CCB

Note that the above CA5564878CCB is the container ID we have shown above. Developers can replace them with the container ID they display. Once we have created our own container, we can use the following command to restart our container:
$ sudo docker start-i snappy-dev-cli

Note that the snappy-dev-cli here is the name of our newly created container. It is already defined in the command line above us (--name snappy-dev-cli)

When we get to container, we run the following command to update the latest Snapcraft:
$ sudo add-apt-repository ppa:snappy-dev/tools
$ sudo apt-get install Snapcraft 
$ snapcraft version
Snapcraft (0.5).
Run ' Snapcraft help ' to get started.


At the moment, everything is already installed and can be used to compile our application directly. If we now go directly to compiling a Golang project of our own, we will see the following error:
ubuntu@34fc56495ebb:~/work$ Snapcraft
Pulling webserver
env gopath=/home/ubuntu/work/parts/webserver/ Build Go get-t-D github.com/liu-xiao-guo/golang-http package
github.com/liu-xiao-guo/golang-http
 Imports Runtime:c source files not allowed if not using CGO or swig:defs.c float.c heapdump.c lfstack.c malloc.c mcache.c Mcen  TRAL.C mem_linux.c mfixalloc.c mgc0.c mheap.c msize.c os_linux.c os_linux_arm.c panic.c parfor.c proc.c runtime.c signal.c  SIGNAL_ARM.C signal_unix.c softfloat_arm.c stack.c string.c sys_arm.c vlrt.c
Failed doing pull for Webserver:command ' ['/bin/sh ', '/tmp/tmpr13qjaa9 ', ' env ', ' gopath=/home/ubuntu/work/parts/webserver/build ', ' go ', ' get ', '-t ', '-d ', ' Github.com/liu-xiao-guo/golang-http '] ' returned Non-zero exit status 1

We can do the following steps to rectify:
$ sudo apt-get update
$ sudo apt-get install Curl
$ bash < < (curl-s-s-l https://raw.githubusercontent.com /moovweb/gvm/master/binscripts/gvm-installer)
$ source/home/ubuntu/.gvm/scripts/gvm


The problem now is that if you re-enter or discover the error condition when compiling the Golang project as described above, we need to re-enter the following command in terminal to correct the error:
$ SOURCE/HOME/UBUNTU/.GVM/SCRIPTS/GVM


2) Compile and deploy our application

We first go to our working directory work and type the following command:
$ git clone https://github.com/liu-xiao-guo/go-webserver.git



We have downloaded our app to our Go-webserver directory. We type the following command in the root directory of our project:
ubuntu@ca5564878ccb:~/work/go-webserver$ Snapcraft


As can be seen from the above, we have successfully compiled our own snap files in ARMHF's Ubuntu environment. This file can be used for installation on ARM boards. We are now out of our Docker environment:
ubuntu@ca5564878ccb:~/work/go-webserver$ exit

This will go back to our snappy system. We go into the private directory of Docker:


As can be seen from the above, we have found the compiled and packaged snap file in the file directory under the snappy system, we can install it by the following command:
(RASPBERRYPI2) ubuntu@localhost:~/apps/docker/1.6.2.004/work/go-webserver$ sudo snappy install-- Allow-unauthenticated Go-webserver_1.0.7_armhf.snap 



From the above we can see that our go-webserver has been successfully installed. We open our Firefox browser:


Apparently it has been successfully deployed and running on our Raspberry Pi. Because it's a service, all of them run automatically when they're installed.
In the same way, we can also compile and package our Python project "Mqtt-for-ubuntu-core".


If you want to learn how to compile and deploy snap for KVM, see the article "How to Compile and package snap (1) for our snappy Ubuntu app"

3) Compiling local source code
Careful developers can check their Snapcraft version by entering the following commands in their own commands.
$ snapcraft Version

If your version is below 0.5, then your Snapcraft version will not compile a local source project, for example, the contents of a project Snapcraft.yaml are as follows:
Name:piglow
version:1.0
Vendor:xiaoguo, Liu <xiaoguo.liu@canonical.com>
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
Above, we can see "./src/piglow", indicating that its source code is local. Version 0.5 of the previous snapcraft is not available to compile such a project. It can only compile the following Snapcraft.yaml files:
Name:piglow
version:1.0
Vendor:xiaoguo, Liu <xiaoguo.liu@canonical.com>
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
In this case, source source is pointing to a git repository. If you do not currently have the Snapcraft version of 0.4 and above, we can use the following method to get the latest version:
$ git clone https://github.com/ubuntu-core/snapcraft

Then by:
$./setup.py build
$ sudo./setup.py Install

To get the latest version of Snapcraft.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.