Merge lp:~snappy-dev/snapcraft/docs into lp:~snappy-dev/snapcraft/core

Proposed by Michael Terry on 2015-07-29
Status: Merged
Approved by: Ricardo Salveti on 2015-08-03
Approved revision: 116
Merged at revision: 118
Proposed branch: lp:~snappy-dev/snapcraft/docs
Merge into: lp:~snappy-dev/snapcraft/core
Prerequisite: lp:~mterry/snapcraft/arch-fixes
Diff against target: 406 lines (+328/-33)
6 files modified
README.md (+7/-0)
docs/flow.md (+0/-27)
docs/intro.md (+92/-0)
docs/tutorial.md (+223/-0)
examples/webcam-webui-snap/snapcraft.yaml (+6/-2)
examples/webcam-webui-snap/webcam-webui (+0/-4)
To merge this branch: bzr merge lp:~snappy-dev/snapcraft/docs
Reviewer Review Type Date Requested Status
Leo Arias 2015-07-29 Approve on 2015-07-29
Review via email: mp+266243@code.launchpad.net

Commit Message

Add intro and tutorial docs.

The intro file explains Snappy and the general Snapcraft concepts a bit. It covers the same material as flow.md, so I dropped that file.

The tutorial file goes through crafting a webcam demo snap [1].

I want to add an installing/testing/uploading-to-store section too, but I want to get some experience using the webcam bit and dealing with apparmor for it (seeing if I can get kvm to use my laptop webcam or if I need to have a real USB webcam with a beaglebone). But that addition can happen in a separate branch.

This is taking over from lp:~mvo/snapcraft/docs-1 since mvo is on vacation.

[1] Very similar to https://developer.ubuntu.com/en/snappy/guides/appliance-builder-guide-webcam/

Description of the Change

Add intro and tutorial docs.

The intro file explains Snappy and the general Snapcraft concepts a bit. It covers the same material as flow.md, so I dropped that file.

The tutorial file goes through crafting a webcam demo snap [1].

I want to add an installing/testing/uploading-to-store section too, but I want to get some experience using the webcam bit and dealing with apparmor for it (seeing if I can get kvm to use my laptop webcam or if I need to have a real USB webcam with a beaglebone). But that addition can happen in a separate branch.

This is taking over from lp:~mvo/snapcraft/docs-1 since mvo is on vacation.

[1] Very similar to https://developer.ubuntu.com/en/snappy/guides/appliance-builder-guide-webcam/

To post a comment you must log in.
Leo Arias (elopio) wrote :

Cool.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== renamed file 'README.md' => 'HACKING.md'
2=== added file 'README.md'
3--- README.md 1970-01-01 00:00:00 +0000
4+++ README.md 2015-07-31 19:14:03 +0000
5@@ -0,0 +1,7 @@
6+# Snapcraft
7+
8+Snapcraft allows easy crafting of snap packages for the Ubuntu Core
9+transactional system.
10+
11+See docs/intro.md for details about the concepts behind
12+snapcraft. There is also a tutorial in docs/tutorial.md.
13\ No newline at end of file
14
15=== removed file 'docs/flow.md'
16--- docs/flow.md 2015-07-02 17:27:53 +0000
17+++ docs/flow.md 1970-01-01 00:00:00 +0000
18@@ -1,27 +0,0 @@
19-# Part Flow
20-
21-Each part has 3 major user-visible stages: build, stage, and snap. Put respectively into parts/XXX, stage/, and snap/ directories.
22-
23-## Build
24-
25-There are actually several steps rolled into this one conceptual piece.
26-
27-### Pull (parts/XXX/src)
28-
29-This gets the source.
30-
31-### Build (parts/XXX/build and parts/XXX/install)
32-
33-This builds the source in build/ and does 'make install' or equivalent into install/. Other parts that depend upon this one will be able to build against this part's install/ folder.
34-
35-## Stage
36-
37-This copies all files from parts/XXX/install into stage/ (or hardlinks them). It complains if there would be any conflicts.
38-
39-## Snap
40-
41-This copies all files from stage/ to snap/ while filtering some files that shouldn't be delivered to end user.
42-
43-## Assemble
44-
45-This is a final last step, which basically runs "snappy build snap/"
46
47=== added file 'docs/intro.md'
48--- docs/intro.md 1970-01-01 00:00:00 +0000
49+++ docs/intro.md 2015-07-31 19:14:03 +0000
50@@ -0,0 +1,92 @@
51+# Intro
52+
53+Snapcraft allows easy crafting of packages for Snappy Ubuntu. It makes it
54+easy to incorporate components from different sources like GitHub, Launchpad,
55+or npm.
56+
57+# Snappy
58+
59+Snappy Ubuntu Core is a new rendition of Ubuntu with transactional updates — a
60+minimal server image with the same libraries as today’s Ubuntu, but
61+applications are provided through a simpler mechanism.
62+
63+Snappy apps and Ubuntu Core itself can be upgraded atomically and rolled back
64+if needed. Apps are also strictly confined and sandboxed to safeguard your
65+data and system.
66+
67+# Key concepts
68+
69+A .snap package for the Ubuntu Core system contains all its
70+dependencies. This has a couple of advantages over traditional deb or
71+rpm based dependency handling, the most important being that a
72+developer can always be assured that there are no regressions triggered by
73+changes to the system underneath their app.
74+
75+Snapcraft makes bundling these dependencies easy by allowing you to
76+specify them as “parts” in the snapcraft.yaml file.
77+
78+## Parts
79+
80+A central aspect of a snapcraft recipe is a “part”. A part is a piece
81+of software or data that the snap package requires to work or to
82+build other parts. Each part is managed by a snapcraft plugin and parts
83+are usually independent of each other.
84+
85+## Plugins
86+
87+Snapcraft plugins are written in Python and have a yaml
88+description. A lot of default plugins are included, for example for
89+projects written in Go, Java, Python or C. It is also possible
90+to simply download binary content as part of the snapcraft recipe.
91+
92+## Lifecycle
93+
94+Each part goes through the following steps:
95+
96+### Pull
97+
98+The first is that each part is pulled. This step will download
99+content, e.g. checkout a git repository or download a binary component
100+like the Java SDK. Snapcraft will create a parts/ directory with
101+sub-directories like parts/part-name/src for each part that contains
102+the downloaded content.
103+
104+#### Build
105+
106+The next step is that each part is built in its parts/part-name/build
107+directory and installs itself into parts/part-name/install.
108+
109+### Stage
110+
111+After the build of each part the parts are combined into a single
112+directory tree that is called the “staging area”. It can be found
113+under the ./stage directory.
114+
115+This ./stage directory is useful for building outside code that isn’t in the
116+snapcraft.yaml recipe against the snap contents. For example, you might build a
117+local project against the libraries in ./stage by running
118+`snapcraft shell make`. Though in general, you are encouraged to add even local
119+projects to snapcraft.yaml with a local `source:` path.
120+
121+### Snap
122+
123+The snap step moves the data into a ./snap directory. It contains only
124+the content that will be put into the final snap package, unlike the staging
125+area which may include some development files not destined for your package.
126+
127+The Snappy metadata information about your project will also now be placed in
128+./snap/meta.
129+
130+This ./snap directory is useful for inspecting what is going into your snap
131+and to make any final post-processing on snapcraft’s output.
132+
133+### Assemble
134+
135+The final step builds a snap package out of the snap directory. This .snap file
136+can be uploaded to the Ubuntu Store and published directly to Snappy users.
137+
138+# Next
139+
140+After introducing the key concept of snapcraft it is probably a good
141+time to look at the tutorial in docs/tutorial.md to see how it works
142+for an example project.
143
144=== added file 'docs/tutorial.md'
145--- docs/tutorial.md 1970-01-01 00:00:00 +0000
146+++ docs/tutorial.md 2015-07-31 19:14:03 +0000
147@@ -0,0 +1,223 @@
148+# Snapcraft Tutorial
149+
150+Let's make a snap from scratch using Snapcraft! We'll pick something a little
151+interesting: a webcam server.
152+
153+## Preparation
154+
155+You'll want a webcam and a Snappy device. We'll assume you have those,
156+but if you need help setting up a Snappy install, there is help
157+[online](https://developer.ubuntu.com/en/snappy/start/).
158+
159+(Even if you don't have either of those, you can still follow along. You just
160+won't be able to use the final snap package you create. But you'll get to see
161+how Snapcraft works, which is still super rewarding.)
162+
163+You should also install Snapcraft:
164+
165+ $ sudo add-apt-repository ppa:snappy-dev/snapcraft-daily
166+ $ sudo apt-get update
167+ $ sudo apt-get install snapcraft
168+
169+## Approach
170+
171+This example is easy because we won't be doing much of the heavy lifting
172+ourselves. We're going to integrate a couple pieces of code together to make
173+an interesting app.
174+
175+Namely, we'll combine a web server with a webcam program and combine
176+them to serve a new frame every ten seconds.
177+
178+### The Web Server
179+
180+Go has a simple web server in its standard libraries. So let's just use that.
181+
182+It's trivial to write a complete (but basic) web server in a few lines:
183+
184+ package main
185+ import "net/http"
186+ func main() {
187+ panic(http.ListenAndServe(":8080", http.FileServer(http.Dir("."))))
188+ }
189+
190+This will serve the current directory on port :8080. If there is an index.html
191+in the current directory, it will be served. Otherwise a directory listing will
192+be shown.
193+
194+I've provided the above code in a simple GitHub
195+[repository](https://github.com/mikix/golang-static-http).
196+
197+### The Webcam Program
198+
199+There is a webcam program provided in the Ubuntu archives called `fswebcam`.
200+It has a lot of neat features. But all we'll be needing for now is its ability
201+to take a webcam freeze frame and drop it to a file by calling it like so:
202+
203+ $ fswebcam output.jpg
204+
205+## Snapcraft Recipe
206+
207+OK, let's create a Snapcraft recipe that combines the above programs into a
208+useful snap.
209+
210+Snapcraft reads a single file, `snapcraft.yaml`, which tells it how to combine
211+code. It contains a list of `parts`, or pieces of code, and some metadata for
212+the final snap it will create. But let's not worry about the metadata yet.
213+
214+### Web Server Part
215+
216+Let's start with the web server.
217+
218+ parts:
219+ golang-static-http:
220+ plugin: go1.4-project
221+ source: git://github.com/mikix/golang-static-http
222+
223+You've got a `parts` list with one item, named `golang-static-http`, but we
224+could call it anything. That part has a few options. A `plugin` option that
225+tells Snapcraft how to interpret the part. In this case, it's a Go project
226+using Go 1.4. And finally, a `source` option telling Snapcraft where to
227+download the code.
228+
229+Go ahead and create `snapcraft.yaml` with the above contents in an empty
230+directory.
231+
232+Now we can build and "stage" this recipe. Staging just means putting the output
233+of the parts in a common folder that has the same layout as the snap we'll
234+eventually create. It lets you look at how the snap is constructed and make
235+sure everything is in place.
236+
237+ $ snapcraft stage
238+
239+You'll see a bunch of output, including Snapcraft downloading the Go compiler.
240+It will use this to compile the code found on GitHub. Eventually when it is
241+done, you'll be able to inspect the ./stage folder and see the web server
242+executable sitting in ./stage/bin:
243+
244+ $ ls stage/bin
245+ golang-static-http
246+
247+### Webcam Part
248+
249+Now let's add the webcam program `fswebcam` to our snap. Edit `snapcraft.yaml`
250+to look like:
251+
252+ parts:
253+ golang-static-http:
254+ plugin: go1.4-project
255+ source: git://github.com/mikix/golang-static-http
256+ fswebcam:
257+ plugin: ubuntu
258+
259+We've added a new part called `fswebcam` handled by the `ubuntu` plugin. That
260+plugin will include an Ubuntu package and its dependencies into your snap. It
261+will use the name of the part as the package name to include.
262+
263+Now let's stage our recipe again.
264+
265+ $ snapcraft stage
266+
267+You'll note that Snapcraft skipped downloading and building golang-static-htpp
268+again because it knew it had already done so.
269+
270+You'll also see Snapcraft downloading and unpacking all the Ubuntu packages
271+into your snap. If you look at ./stage, you'll see a lot more files now:
272+
273+ $ ls stage
274+ bin etc lib usr var
275+
276+### Gluing the Parts Together
277+
278+OK, so we have the two programs in our staging area. But how do we make them
279+work together?
280+
281+We'll write a tiny little script that runs the server and `fswebcam` together:
282+
283+ #!/bin/sh
284+ set -e
285+
286+ cd "$SNAPP_APP_DATA_PATH"
287+
288+ golang-static-http &
289+
290+ while :; do
291+ fswebcam shot.jpeg
292+ sleep 10
293+ done
294+
295+Save the above as `webcam-webui` and make it executable:
296+
297+ $ chmod a+x webcam-webui
298+
299+Alright, let's put this script in our snap too:
300+
301+ parts:
302+ golang-static-http:
303+ plugin: go1.4-project
304+ source: git://github.com/mikix/golang-static-http
305+ fswebcam:
306+ plugin: ubuntu
307+ glue:
308+ plugin: copy
309+ files:
310+ webcam-webui: bin/webcam-webui
311+
312+The `copy` plugin takes a list of files to just directly copy without building
313+or downloading anything. In this case, we just want to put our glue script in
314+the bin/ directory.
315+
316+If we run Snapcraft again, we won't be surprised:
317+
318+ $ snapcraft stage
319+
320+We should now see both the web server and our script in stage/bin (the webcam
321+program is in stage/usr/bin since it came from Ubuntu):
322+
323+ $ ls stage/bin
324+ golang-static-http webcam-webui
325+
326+### Package Metadata
327+
328+"But how do we actually make a snap?", you may be wondering. To do that, we
329+need some metadata files required by Snappy that describe how our code should
330+be installed and run.
331+
332+You can read all about the [format of this metadata](https://developer.ubuntu.com/en/snappy/guides/packaging-format-apps/),
333+but we'll assume here that you're already familiar.
334+
335+Let's make a new subdirectory called `meta` and put our Snappy package files
336+there. Here's the contents of `meta/package.yaml`:
337+
338+ name: webcam-webui
339+ version: 1
340+ vendor: You <you@example.com>
341+ services:
342+ - name: webcam-webui
343+ start: bin/webcam-webui
344+
345+And a very simple `meta/readme.md`:
346+
347+ # Webcam web UI
348+
349+ Exposes your webcam over a web UI
350+
351+Now that we have that sorted, we can tell Snapcraft where to find our metadata:
352+
353+ parts:
354+ golang-static-http:
355+ plugin: go1.4-project
356+ source: git://github.com/mikix/golang-static-http
357+ fswebcam:
358+ plugin: ubuntu
359+ glue:
360+ plugin: copy
361+ files:
362+ webcam-webui: bin/webcam-webui
363+ snappy-metadata: meta
364+
365+And tell Snapcraft to actually make the snap package:
366+
367+ $ snapcraft assemble
368+
369+You should now have a `webcam-webui_1_amd64.snap` file sitting in your
370+directory (assuming you are running on amd64). Congratulations!
371
372=== modified file 'examples/webcam-webui-snap/snapcraft.yaml'
373--- examples/webcam-webui-snap/snapcraft.yaml 2015-07-15 16:26:06 +0000
374+++ examples/webcam-webui-snap/snapcraft.yaml 2015-07-31 19:14:03 +0000
375@@ -2,6 +2,10 @@
376 golang-static-http:
377 plugin: go1.4-project
378 source: git://github.com/mikix/golang-static-http
379- ubuntu:
380- package: fswebcam
381+ fswebcam:
382+ plugin: ubuntu
383+ glue:
384+ plugin: copy
385+ files:
386+ webcam-webui: bin/webcam-webui
387 snappy-metadata: meta
388
389=== modified file 'examples/webcam-webui-snap/webcam-webui'
390--- examples/webcam-webui-snap/webcam-webui 2015-06-25 13:22:56 +0000
391+++ examples/webcam-webui-snap/webcam-webui 2015-07-31 19:14:03 +0000
392@@ -2,9 +2,6 @@
393
394 set -e
395
396-export PATH="${SNAPP_APP_PATH}/bin:${SNAPP_APP_PATH}/usr/bin${PATH:+:$PATH}"
397-export LD_LIBRARY_PATH="${SNAPP_APP_PATH}/lib/x86_64-linux-gnu:${SNAPP_APP_PATH}/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}"
398-
399 cd "$SNAPP_APP_DATA_PATH"
400
401 golang-static-http &
402@@ -13,4 +10,3 @@
403 fswebcam shot.jpeg
404 sleep 10
405 done
406-

Subscribers

People subscribed via source and target branches

to all changes: