How to compile and package snap (1) for our snappy Ubuntu app

Source: Internet
Author: User
Tags install go port number git clone

In today's article, we'll focus on how to use Snapcraft to compile our application and package our app as a. Snap package. Since our snapcraft does not yet support cross-compile, we also need to build a ARMHF environment for our development boards (such as Raspberry Pi) to compile our applications and eventually deploy them to our development version. Here we take the Raspberry Pi development version as an example. If you don't currently know how to build your own hardware environment, please see my article "How to install snappy Ubuntu to Raspberry Pi (RaspBerry pi)".


1) Install our environment on the desktop
We can first refer to my previous article, "Using Snapcraft to package our snappy Ubuntu app" to build our environment on desktop:

$ sudo add-apt-repository ppa:snappy-dev/tools  
$ sudo apt-get update  
$ sudo apt-get install snappy-tools bzr SNAPC Raft  

Of course, if you want to be able to test our final snap application in a KVM environment, we must also install our KVM environment. For specific installation steps, refer to "Snappy Ubuntu primer".
When we finished the above two, our environment was basically set up.

2) Create your own project

As I described in my article, "What exactly is snappy Ubuntu?", the current development on snappy supports a lot of languages and architectures. We can choose a language we are familiar with and develop it on top. In today's exercise, we use Golang as an example. In fact, snappy is implemented in Golang language. I created a directory of my own files and created our project. For convenience, we have chosen a project that I have created myself go-webserver. We can get to this project in the following way:
$ git clone https://github.com/liu-xiao-guo/go-webserver.git

In fact, we don't even need to get the source code for this project, we just need to create an icon file and a Snapcraft.yaml file. We can put our source on our GitHub. Snapcraft can help me to automatically download our source code and compile the package. If you do not have an icon file, you can type the following command in your directory:
$ Touch Go.png

This is enough for our user-interface-free app to create a 0-byte icon file. Our Snapcraft.yaml file is as follows:
Name:go-webserver
Vendor:xiaoguo, Liu <xiaoguo.liu@canonical.com>
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

The entire project has only two files locally:
liuxg@liuxg:~/snappy/examples/go-webserver$ ls
go.png  snapcraft.yaml

As we pointed out in the Snapcraft.yaml file above, the source of our Golang is in the address github.com/liu-xiao-guo/golang-http, and its contents are as follows:


The contents of the Main.go are: Main.go
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")
}

This is one of the simplest go webserver codes. For Golang, the compiled target file is the name of the file directory. For our situation, that is, golang-http. And that's what we see in our Snapcraft.yaml is a service:
Services:
  webserver:
    description: "Go websever"
    start:bin/golang-http
    caps:
     - Network-client
     

Its startup path is "Bin/golang-http". For more snapcraft, see the article "Packaging for our snappy Ubuntu apps with Snapcraft."

3) Compile and deploy our project
In the previous step, we have successfully created the content of our project, although our project only has two files Go.png and Snapcraft.yaml. Let's compile our project and finally get the snap file we need. We enter the following command in terminal (in the directory where Snapcraft.yaml is located):
liuxg@liuxg:~/snappy/examples/go-webserver$ Snapcraft



With the above output, we can see that we have successfully compiled the Go-webserver_1.0.7_amd64.snap file we need. Please noteThe AMD64 here shows that he can only install to the AMD64 system. As the current Snapcraft does not support the cross-compile, so according to the method, we can not get the ARMHF structure of the snap file. We'll cover it in the next article.
In addition, as we described in "using Snapcraft for our snappy Ubuntu app packaging," we can also individually manipulate different stages of it through Snapcraft:



4) Deploy our application to KVM
In the previous step, we have produced our snap files and we can deploy them to our KVM by the following methods. First, start our KVM:
$ kvm-m 512-redir:8090::8081-redir:8022::22-redir:4200::4200  

Please note that the port number used in our main.go is 8081. We also redirect Port 8081 to Port 8090 in desktop. This way we can use 8090 to view the Web server we are running in KVM in desktop.
We can deploy to our KVM in the following ways:
liuxg@liuxg:~/snappy/examples/go-webserver$ snappy-remote--url=ssh://localhost:8022 Install go-webserver_1.0.7_ Amd64.snap 


From the results shown, we see that the Go-webserver has been installed in our KVM. We'll check to see if it's running in our browser. Open our browser Firefox and enter the address in the address bar localhost:8090


As shown in the above figure, we have successfully deployed our go-webserver to our KVM. We can also show it in our KVM:



In this article, we highlight how to compile a go webserver application and deploy it to our KVM. In the next article, "How to compile and Package snap (2) for our snappy Ubuntu app", we'll focus on how to deploy our app to our Raspberry Pi.

5) 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.