Canonical Voices

Benjamin Zeller

Ubuntu SDK meets snapcraft

Everyone who has followed Ubuntu lately for sure stumbled across the snappy technology, which does not only bring the new cross-distro packaging format “snap” but also a sandboxing technology for apps, as well as transactional updates that can be rolled back in case of an update going wrong and a new way of installing and upgrading Ubuntu called “Ubuntu Core”.

Together with all those new technologies came new tools that make it possible for app developers to build and pack their applications to target Snappy and Core. The central tool for that is snapcraft and it aims to unite a lot of tasks that were separate before. It can set up your build environment, build your projects and even package it with just one call in the project directory: “snapcraft”.

We took the last few weeks to start the work on supporting those new tools and now we have the first release of the IDE with direct support for building with snapcraft, as well as a basic template to get you started.

New technologies usually come with certain limitations. This one is not an exception and we hope that these issues will be eliminated in the near future.:

  • Snapcraft uses sudo when it needs to install build packages, however that does not work when run from the QtCreator, simply because sudo does not have a console to ask the password on. So make sure build dependencies are installed before building.

  • “Out of source” builds are not yet implemented in snapcraft, but since QtCreator always uses a extra build directory we had to work around that problem. So for now we rsync the full project into a build directory and run the build there.

  • Also incremental builds are yet not supported, so every build is a complete rebuild.

Snapcraft projects are described in a snapcraft.yaml file, so it made sense for us to use it as the project file in the IDE as well, so instead of opening a .pro or CMakeList.txt file the snapcraft.yaml is opened directly. Naturally implementing a completely new project type manager is not a trivial task, so many key features are still missing.

  • Code model support: while completion does work in the file scope, it does not for the full project.

  • Debugging mode: currently the profiling and debugging run modes do not work, so snap projects can only be executed normally.

Those limitations aside it can be already used to create snap packaged applications.

With this new release we consider the IDE as feature complete for the time being. Since the development of snapcraft is moving in a very fast pace we need to let it evolve to a certain degree to be sure new features added to the IDE represent the future way of building with snapcraft.

Read more
Tim Peeters

Introduction

One of the advantages of snap packages is that they are self-contained. When you install a snap, you know that you don’t need to install additional dependencies (besides the automatically-installed core snap that provides the basic operating system layer), and it will simply work on every Linux distribution that supports snaps.

Here, we show how to create self-contained snap packages for Qt-based applications, and we show an additional approach where some of the app dependencies are provided by a separate snap: the Ubuntu app platform snap. The platform snap provides an (optional) approach for the software provider, and can save disk space in some cases. Below we will explain the two approaches to building a snap for Qt-based software: a snap that is self-contained and includes Qt, and one that uses the platform snap, and we show the advantages of each approach. However, before showing these two approaches that you can apply to your own QML code, we demonstrate how to create a snap from deb packages in the Ubuntu archive so that you can get started right away even before you write any code.

We assume that before reading this blog post, you have acquired knowledge about how to use Snapcraft. So if you haven’t, we recommend reading the documentation on snapcraft.io and the snap-codelabs tutorials.

All the commands that are listed below are executed on an Ubuntu 16.04 LTS machine with the stable-phone-overlay PPA enabled. Some of the snapcraft commands may run on other configurations, but for the “Ubuntu App Platform Snap” section it is a hard requirement because the version of Qt - upstream 5.6 long term support version - and other libraries used to build the snap need to match the versions in the ubuntu-app-platform snap. Installing the snap packages works on different versions of Ubuntu and even different Linux distributions. The examples were tested on amd64 architecture with Intel graphics. If you are running this on a different CPU architecture, naturally the architecture in the directory and snap file names listed below must be modified. If you have an Nvidia GPU and use the Nvidia proprietary drivers there can be problems when running some snapped applications, so in that case we currently recommend to use the open source Nouveau drivers.

The examples are also available in a repository linked to from the Evaluation section.

Qt cloud parts - a simple use case

We will demonstrate how to build a simple app snap that includes the Qt release and Ubuntu UI Toolkit (UITK) from the Ubuntu archives. For this example, we use the UITK gallery which is part of the ubuntu-ui-toolkit-examples deb package on classic Ubuntu systems, so no additional code is needed. Because of that, we can simply use the nil plugin and pull in the examples as stage-packages. We create a directory called uitk-gallery which contains only a snapcraft.yaml file with the following contents:

name: uitk-gallery
version: '0.1'
summary: Showcase gallery that demonstrates the components in the Ubuntu UI Toolkit.
description: |
  See https://developer.ubuntu.com/api/qml/ for the API documentation of the components.

grade: devel
confinement: strict

parts:
  ubuntu-ui-toolkit-examples:
    plugin: nil
    stage-packages:
      - qmlscene
      - qml-module-qtqml-models2
      - ubuntu-ui-toolkit-examples
    after: [desktop-qt5]

apps:
  uitk-gallery:
    command: desktop-launch "qmlscene $SNAP/usr/lib/*/qt5/examples/ubuntu-ui-toolkit/examples/ubuntu-ui-toolkit-gallery/ubuntu-ui-toolkit-gallery.qml"
    plugs: [unity7, opengl]

(notes: the command line assumes you are on and targeting amd64 system. the plugs line is needed so that you have access to graphical subsystem from your confined app)

Under stage-packages we listed all the packages that need to be pulled from the Ubuntu archive, including their dependencies. ubuntu-ui-toolkit-examples contains all the QML code for the UITK gallery that we want to run using qmlscene. We also included qml-module-qtqml-models2 because some pages of the UITK gallery import QtQml.Models. The line after: [desktop-qt5] fetches the desktop-qt5 part from the remote parts repository. It will automatically pull in Qt 5 from the Ubuntu archive, set-up environment variables, and provide the desktop-launch script that is called to start the app. The snap file can be created simply by going to the uitk-gallery directory which contains the snapcraft.yaml file, and running:

snapcraft

Note that Snapcraft will ask for the sudo password to install the Qt5 dev packages that are required to compile Qt apps, but can be left out if all the dependencies are already present. Running snapcraft will create (on an amd64 machine) the file uitk-gallery_0.1_amd64.snap which can then be installed by:

snap install --dangerous uitk-gallery_0.1_amd64.snap

where the dangerous parameter is required because we are installing an unsigned snap that does not come from the Ubuntu store. Note that you do not need to use sudo if you have logged in with snap login. The UITK gallery can now be launched using:

uitk-gallery

The desktop-qt5 cloud part pulls in the current stable version of Qt of the Ubuntu 16.04 LTS release - 5.5.1 normally or 5.6.1 in the case of stable overlay PPA. To uninstall the UITK gallery snap before going to the next section, run:

snap remove uitk-gallery

QML project using parts from the cloud

If your existing QML code is not available as a deb package, then obviously you cannot pull it in from the archive when creating the snap using stage-packages. To show how to include your own QML code, we copy the UITK gallery code to the ubuntu-ui-toolkit-gallery directory inside the snapcraft (uitk-gallery) directory. Go to the parent directory of the uitk-gallery of the previous section, and run:

bzr branch lp:ubuntu-ui-toolkit
cp -R ubuntu-ui-toolkit/examples/ubuntu-ui-toolkit-gallery uitk-gallery/

Alternatively, if you have the ubuntu-ui-toolkit-examples package installed, you can run:

cp -R /usr/lib/*/qt5/examples/ubuntu-ui-toolkit/examples/ubuntu-ui-toolkit-gallery/ uitk-gallery/

You should now have both the snapcraft.yaml and the copied ubuntu-ui-toolkit-gallery directory that contains the source code of the UITK gallery under the uitk-gallery. We can now remove the ubuntu-ui-toolkit-examples from the stage-packages in the snapcraft.yaml file. Because that line is removed, the dependencies of the UITK gallery are no longer pulled in automatically, and we must add them to the YAML file, which then becomes:

name: uitk-gallery
version: '0.2'
summary: Showcase gallery that demonstrates the components in the Ubuntu UI Toolkit.
description: |
  See https://developer.ubuntu.com/api/qml/ for the API documentation of the components.

grade: devel
confinement: strict

parts:
  ubuntu-ui-toolkit-gallery:
    plugin: dump
    source: .
    stage-packages:
      - qmlscene
      - qml-module-qtqml-models2
      - qml-module-qt-labs-folderlistmodel
      - qml-module-qtquick-xmllistmodel
      - qml-module-ubuntu-components
      - ubuntu-ui-toolkit-theme
      - ubuntu-ui-toolkit-tools
    after: [desktop-qt5]

apps:
  uitk-gallery:
    command: desktop-launch "qmlscene $SNAP/ubuntu-ui-toolkit-gallery/ubuntu-ui-toolkit-gallery.qml"
    plugs: [unity7, opengl]

Note that besides the changes in stage-packages, also the location of ubuntu-ui-toolkit-gallery.qml was updated in the uitk-gallery command because the QML files are no longer installed in usr/lib inside the snap, but copied in the root of the snap filesystem. As before, the snap package can be created by executing:

snapcraft

inside the uitk-gallery directory. The UITK gallery can then be installed and started using:

snap install --dangerous uitk-gallery_0.2_amd64.snap
uitk-gallery

and uninstalled by:

snap remove uitk-gallery

Now that you have seen how to package the UITK gallery from source into a snap, you can do the same for your own QML application by using the dump plugin with the dependencies as stage-packages. If your application includes C++ code as well, you need to use another plugin, for example the qmake plugin. For that we refer to the Snapcraft tutorials mentioned in the introduction.

For those who like to experiment with newer versions of upstream Qt, we provide qt57 and qt58 cloud parts in the parts repository for Qt 5.7.1 and 5.8 (in development). However, the qt57 and qt58 cloud parts do not yet include a wrapper script similar to desktop-launch, so one must be included with with snap configuration, see for example timostestapp2. When using these cloud parts, you should usually omit any Qt/QML package from stage-packages, as the ones compiled from newer Qt are used directly, and you should also omit the after: [desktop-qt5].

Ubuntu app platform snap

The snap files we created in the previous sections contain everything that is needed in order to run the UITK gallery application, resulting in a snap file of 86MB. Here we will explain how to use the Ubuntu app platform snap when you have multiple app snaps that all use the same Qt version.

Benefits of this approach include disk space saving, download time and bandwidth usage if metered.

When your snap uses the ubuntu-app-platform snap for Qt and other platform libraries, we can remove the stage-packages from the snapcraft.yaml file because (in this case), all the needed libraries are included in ubuntu-app-platform. We must also replace after: [desktop-qt5] by after: [desktop-ubuntu-app-platform]. This will set-up your snap to use the global desktop theme, icon theme, gsettings integration, etc. A more elaborate description of the desktop-ubuntu-app-platform is given in the parts list on the Ubuntu wiki. In the uitk-gallery directory we must currently create a directory where the files from the platform snap can be mounted using the content interface:

mkdir ubuntu-app-platform

and this empty directory (mount point) must be added in the YAML file as well. At this point the directory structure is as follows:

uitk-gallery/
 .. snapcraft.yaml
 .. ubuntu-ui-toolkit-gallery/
    .. [gallery contents]
 .. ubuntu-app-platform/

The whole YAML is:

name: uitk-gallery
version: '0.3'
summary: Showcase gallery that demonstrates the components in the Ubuntu UI Toolkit.
description: |
  See https://developer.ubuntu.com/api/qml/ for the API documentation of the components. ubuntu-app-platform snap must be installed for this snap to work.

grade: devel
confinement: strict

plugs:
    platform: # plug name, to be used later
        interface: content
        content: ubuntu-app-platform1 # content being mounted and the version, currently 1
        target: ubuntu-app-platform # the mount directory created
        default-provider: ubuntu-app-platform # default content source snap, currently the only provider too

parts:
  ubuntu-ui-toolkit-gallery:
    plugin: dump
    source: .
    after: [desktop-ubuntu-app-platform]
    snap: [ubuntu-ui-toolkit-gallery, ubuntu-app-platform]

apps:
  uitk-gallery:
    command: desktop-launch "qmlscene $SNAP/ubuntu-ui-toolkit-gallery/ubuntu-ui-toolkit-gallery.qml"
    plugs: [platform, unity7, opengl]

Again, the new snap file can be created using:

snapcraft

This time, before we can use the snap, the ubuntu-app-platform snap must be installed and connected to the new uitk-gallery snap. So, execute the following commands:

snap install ubuntu-app-platform
snap install --dangerous uitk-gallery_0.3_amd64.snap
snap connect uitk-gallery:platform ubuntu-app-platform:platform
uitk-gallery

Note that the snaps must be connected before running uitk-gallery for the first time. If uitk-gallery has been executed before the snap connect you will see an error message. To fix the problem, uninstall the uitk-gallery snap, then re-install it and run the snap connect command before executing uitk-gallery. This is a known limitation in snapd which will be resolved soon.

Another note: once support for the default-provider, already defined above, will correctly be implemented in snap, there will no longer be a need to install the platform snap separately - it will be pulled from the store automatically and the interface connects automatically.

Evaluation

We demonstrated three different approaches to creating a UITK gallery snap, which we gave the version numbers 0.1, 0.2 and 0.3. For each of the approaches, the table below lists the time needed for the different stages of a snapcraft run, but the pull and build stages have been combined because when doing pull, some of the prerequisites need to be built already. The all stages row shows the total time when running the snapcraft command in a clean directory, so that all stages are executed sequentially, so the value is less than the sum of the previous rows in the table because in each stage it is not necessary to check completion of the previous stages.

Version (bzr revision)

0.1 (r1)

0.2 (r2)

0.3 (r3)

build (includes pull)

1m49s

1m48s

3.6s

stage

7s

7s

1.5s

prime

33s

34s

1.8s

snap

1m11s

1m13s

1.7s

all stages

3m32s

3m20s

4.0s

install

2.2s

2.4s

1.2s

snap file size

86 MB

86 MB

1.3 MB

The measurements were done on a laptop with an Intel Core i5-6200U CPU with 8 GB RAM and an solid-state drive by running each command three times and listing the average execution time. All build-dependencies were pre-installed so their installation time is not included in the measurements. Note that this table only serves as an illustration, and execution times will vary greatly depending on your system configuration and internet connection, but it can easily be tested on your own hardware by bzr branching revisions r1, r2 and r3 of lp:~tpeeters/+junk/blog-snapping-qt-apps.

The times and file size listed in the last column (version 0.3) do not include downloading and installing the ubuntu-app-platform snap, which is 135 MB (it includes more than just the minimal Qt and UITK dependencies of the UITK gallery). It can be seen that (depending on the internet connection speed, and which files were downloaded already), using the ubuntu-app-platform can speed up the pull and build time when creating a new snap file. However, the most important advantage is the reduction of the sum of the file sizes when installing multiple app snaps that all depend on Qt or other libraries that are part of the platform snap, because the libraries need to be installed only once. The disadvantage of this approach is that the app snap must be built using the exact same Qt (and other libraries) version as the ubuntu-app-platform snap, so the choice whether the snap should be fully self-contained or depend on the platform snap must be made individually for each use case.

Future work

The UITK gallery snap is not yet available in the Ubuntu store, so our next step will be to publish a UITK examples snap that includes the UITK gallery, and to set-up automatic publishing of that snap to different channels when we update the UITK or the examples. We will also evaluate what is the best way to make newer versions of Qt available and determine whether we need to provide prebuilt binaries to replace the qt57 and qt58 cloud parts.

Finally, we will determine which libraries need to be included in the ubuntu-app-platform snap. The plan is to include all APIs that are listed on https://developer.ubuntu.com/api/qml/ and if APIs are missing we will add them to that webpage as well as to the platform snap. Of course, if you think we are forgetting a library that is useful and used in many different applications, please leave a comment below.

Read more
Colin Watson

Launchpad has had Git-to-Bazaar code imports since 2009, along with imports from a few other systems.  These form part of Launchpad’s original mission to keep track of free software, regardless of where it’s hosted.  They’re also very useful for automatically building other artifacts, such as source package recipes or snap packages, from code hosted elsewhere.  Unfortunately they’re quite complicated: they need to be able to do a full round-trip conversion of every revision from the other version control system, which has made it difficult to add support for Git features such as signed commits or submodules.  Once one of these features is present anywhere in the history of a branch, importing it to Bazaar becomes impossible.  This has been a headache for many users.

We can do better nowadays.  As of last year, we have direct Git hosting support in Launchpad, and we can already build snaps and recipes straight from Git, so we can fulfil our basic goal more robustly now with a lot less code.  So, Launchpad now supports Git-to-Git code imports, also known as Git mirroring.  You can use this to replace many uses of Git-to-Bazaar imports (note that there’s no translations integration yet, and of course you won’t be able to branch the resulting import using bzr).

See our Git documentation for more details.

Read more
kevin gunn

more sample client updates

I can’t even remotely take credit for this. Alberto from the Mir team took the mir-client snap and updated to utilize the mir-libs snap through the content interface. This is helpful as a guide for others who want to avoid making useless copies of libraries in a mir-client app snap. He also added some additional example client applications to run on the mir-kiosk, along with using the snap set command to dynamically change those from the command line. I’ve updated the mir-snaps wiki on how to utilize this. enjoy! If you wanna discuss or have issues, find me (kgunn) on freenode #ubuntu-mir

Read more
Daniel Holbach

Ubuntu Online Summit coming up 15-16 Nov

Ubuntu Online Summit

Ubuntu Online Summit is here again! 15th and 16th November 2016 we are talking about the great stuff which landed in Ubuntu 16.10 and we talk about our plans for 17.04.

Now is the last call for adding sessions, find out how to do that here.

See you all next week!

Read more
liam zheng

Canonical 今日发布了面向物联网 (IoT) 的 Ubuntu Core 16,除了提供定期的可靠安全更新外,还为智能互联设备带来应用商店服务。

目前,Ubuntu Core 在柜顶交换机、工业网关、家庭网关、无线接入网络、数字标牌、机器人和无人机等领域得到了广泛应用。“Ubuntu Core 可为物联网提供安全保护,并为每部设备带去精彩的应用商店。”Ubuntu 和 Canonical 的创始人 Mark Shuttleworth 说。

“随着许多公司陆续拥抱物联网解决方案,强大的安全性和简单快速的系统更新就变得至关重要。”戴尔物联网战略及合作总监 Jason Shepherd 表示。“戴尔与 Canonical 就 Ubuntu Core 开展合作已经一年有余。目前,我们的 Dell Edge Gateway 系列网关已获得认证,全面支持 Ubuntu Core 16。这使戴尔能够提供工厂和楼宇自动化等物联网应用场合需要的长期支持和安全保护。”

新的 Ubuntu Core 系统提供突破性的安全性、管理功能、操作和升级能力,并且由于采用 Snap 应用包,整个平台具有体积小、对开发者友好的特性。Snap 应用包是受安全限制、只读且防篡改的应用程序映像,它们以数字签名的方式保证 IoT 软件的完整性。

IBM 副总裁兼新兴技术与高级创新部门首席技术官 Mac Devine 评价道:“认知时代的到来催生了更多的智能设备,这需要我们从根本上做出改变,以便处理庞大的数据和进行实时认知分析。Snap 让开发者能以一种可轻松在多个物联网设备间移植和升级的格式构建和部署应用程序,从而能够建立一种云与网络边缘之间的认知关系。”

更新控制功能让软件发行者和制造商可以在应用更新之前,先在生态系统中验证这些更新。Snap 更新属于事务性更新,这意味着一旦更新失败,系统将自动还原,因此开发者将更有信心定期更新他们的应用程序。

开源机器人基金会 (OSRF) 的首席执行官 Brian Gerkey 补充道:“OSRF 与全球各地的工程师、科学家和创业者开展合作,致力于让机器人走出研究实验室,进入我们的日常生活,广泛应用于工厂、施工现场、家庭生活和交通等领域。在将机器人部署到现实生活的过程中,有着许许多多的挑战。其中,远程管理和升级机器人软件是大家共同面临的一个重大问题。我认为,Ubuntu Core 16 能够通过 Snap 的形式部署 ROS 应用程序,让整个软件管理过程变得更加安全和简单,从而为开发者和组织部署机器人提供巨大帮助。”

Ubuntu Core 的操作系统和内核同样采用 Snap 架构,因此整个平台都可以进行事务性升级。所有 Ubuntu Core 设备,无论出自哪家制造商,都将获得免费、定期、可靠的操作系统安全更新。

英特尔制造商与创新集团总经理 James Jackson 表示:“从人们的健康到太空探索,机器人技术几乎对每个行业都能产生深远影响。这样的未来需要一个安全、可靠的平台。我们认为 Ubuntu Core 16 就是一个这样的平台,在 ROS 领域,Ubuntu 平台已经一路领先。”

通用或针对设备的 Snap 应用商店,可在设备的整个生命周期内为开发者提供支持,从 Beta 测试版一直到正式发布版,让开发者能够像云软件、企业软件和移动软件那样,轻松销售物联网软件。贴牌 (White Label) 应用商店则可帮助设备制造商打造品牌化、差异化的设备和软件体验。

“迈入物联网时代后,我们生活的方方面面将出现数十亿部设备。”Linaro 的首席执行官 George Grey 说。“Ubuntu Core 16 将使用 Snap 帮助开发者快速将产品推向市场,为市场带来新一代的基于 Linux 的物联网智能设备。”

Gartner 认为,到 2020 年,超过半数的新业务流程将包含物联网的某些要素。Ubuntu Core 16 带来的 OTA 更新、签名 Snap 和安全模型,可帮助开发者和设备制造商缩短产品上市时间。设备制造商可以从众多支持 Ubuntu Core 的芯片、SoC 及单板机供应商中进行选择,例如 Raspberry Pi 2 和 3、Qualcomm Dragonboard 410c 以及 Intel Joule。
 

Read more
Brandon Schaefer

Mir windowing system in Kodi

Here is a video of Kodi running on mir [1]. The source for the port is sitting [2 xbmc/xbmc/windowing/mir ]. My plan is to upstream the changes and maintain the port [6]. There are some more things to fix up, but the main part is ready to upstream.

It will be compile time support, meaning you’ll need to compile with mir support specifically. As Kodi does not support runtime windowing selection. This also means it’ll have a low risk to the main code base. The port supports both opengl and opengles. I also need to test this out on an embedded device (such as raspi/dragon board). Ideally creating a nice kiosk Kodi media center would be awesome running on mir!

I have also snapped up Kodi with mir support [3] with help from [4] and Alberto Aguirre [5]. This is an amazing use case of a snap, having Kodi in a kiosk type system. The main goal as well is to get this working on an embedded device so you install a ubuntu core image, then just install this snap through the store and you’ve a fully working media center!

If you’ve any questions feel free to poke me (bschaefer) in freenode #ubuntu-mir anytime or email me at brandon.schaefer@canonical.com

Read more
Brandon Schaefer

Hello world!

Welcome to Canonical Voices. This is your first post. Edit or delete it, then start blogging!

Read more
Alan Griffiths

MirAL 0.4

There’s a new MirAL release (0.4.0) available in ‘Zesty Zapus’ (Ubuntu 17.04) and the so-called “stable phone overlay” ppa for ‘Xenial Xerus’. MirAL is a project aimed at simplifying the development of Mir servers and particularly providing a stable ABI and sensible default behaviors.

Unsurprisingly, given the project’s original goal, the ABI is unchanged. The changes in 0.4.0 fall into two categories:

  1. Displaying titles in titlebars of the miral-shell example; and,
  2. additional features for shell developers to use.

Displaying Titles in Titlebars of the miral-shell Example

Titlebars now contain the window title. The font can be changed by using the –shell-titlebar-font command line option.

Additional Features For Shell Developers To Use

A new class miral::CommandLineOption that allows the developer to add and process command line options. (These can also be supplied via environment variables or config files in the same way as other options.)

A bunch of functions for managing Applications:

void apply_lifecycle_state_to(Application const& application, MirLifecycleState state);
auto name_of(Application const& application) -> std::string;
auto pid_of(Application const& application) -> pid_t;

Add miral::WindowManagerTools::force_close(Window const& window) to forcefully close a window (i.e. without a close request to the client)

Read more
Tom Macfarlane

Yakkety Yak

The Yakkety Yak 16.10 release animal artwork is now available to download here.

16.10 release animal

db_yak_logo-artwork

Origami Yak

img_7731

Initial design exploration

Design development – head detail

db_yak_heads

Final Yak

Colourways

T-shirt design

db_yak_t-shirt

Read more
bmichaelsen

Meist scheint manches auf den ersten Blick unmöglich.
Manches ist es auch, doch es wäre tödlich, das selbst zu glauben solange noch nichts feststeht
und die Party zu verlassen, bevor sie losgeht.

— Die Sterne, Stell die Verbindung her

Yesterday, two nice things happened: For one, LibreOffice 5.2.3 has been released and secondly Ubuntu Core 16 has been released. But beyond that, something in the middle between these two has happened: LibreOffice 5.2.3 has been released to the stable channel of the snap store at the same day. Now LibreOffice has been in the snap store for some time — and has also been on the stable channel since the Ubuntu 16.10 release. But this is the first time the LibreOffice snap is released in sync with The Document Foundation announcing the general availability of the final downloads. This was possible even though I was on vacation yesterday: LibreOffice snap packages are now being build on launchpad, which simplifies a lot, and launchpad can be asked to populate the edge channel of the store. This is making life very easy. Having smoketested the amd64 build from that channel before, to release LibreOffice 5.2.3 to the beta/candidate/stable channels too all I had to do was push three buttons on a web interface and it was available to all.

Building on launchpad, I also had the opportunity to create builds for armhf and i386 along with the usual amd64 builds with little extra effort. If you are adventurous you are encouraged to test these builds too: Be aware though that these so far aren’t even smoketested, I havent looked at them at all yet, so use them at your own risk.

All in all, this is great progress: LibreOffice 5.2.3 is available to users of Ubuntu 16.10 and Ubuntu 16.04 LTS as a snap on the day of the upstream release. And beyond that on all other distributions where snap is available — quite a few these days.

Update: ICYMI here is how to get the LibreOffice snap: http://www.libreoffice.org/download/snap/ — although strictly speaking you dont need the --channel=beta option anymore now. I will fix that soon.


Read more
Sergio Schvezov

Now that Ubuntu Core has been officially released, it might be a good time to get your snaps into the Store.

Delivery and Store Concepts

So let’s start with a refresher on what we have available on the Store side to manage your snaps.

Every time you push a snap to the store, the store will assign it a revision, this revision is unique in the store for this particular snap.

However to be able to push a snap for the first time, the name needs to be registered which is pretty easy to do given the name is not already taken.

Any revision on the store can be released to a number of channels which are defined conceptually to give your users the idea of a stability or risk level, these channel names are:

  • stable
  • candidate
  • beta
  • edge

Ideally anyone with a CI/CD process would push daily or on every source update to the edge channel. During this process there are two things to take into account.

The first thing to take into account is that at the beginning of the snapping process you will likely get started with a non confined snap as this is where the bulk of the work needs to happen to adapt to this new paradigm. With that in mind, your project gets started with a confinement set to devmode. This makes it possible to get going on the early phases of development and still get your snap into the store. Once everything is fully supported with the security model snaps work in, this confinement entry can be switched to strict. Given the confinement level of devmode this snap is only releasable on the edge and beta channels which hints your users on how much risk they are taking by going there.

So let’s say you are good to go on the confinement side and you start a CI/CD process against edge but you also want to make sure in some cases that early releases of a new iteration against master never make it to stable or candidate and for this we have a grade entry. If the grade of the snap is set to devel the store will never allow you to release to the most stable channels (stable and candidate). not be possible.

Somewhere along the way we might want to release a revision into beta which some users are more likely want to track on their side (which given good release management process should be to some level more usable than a random daily build). When that stage in the process is over but want people to keep getting updates we can choose to close the beta channel as we only plan to release to candidate and stable from a certain point in time, by closing this beta channel we will make that channel track the following open channel in the stability list, in this case it is candidate, if candidate is tracking stable whatever is in stable is what we will get.

Enter Snapcraft

So given all these concepts how do we get going with snapcraft, first of all we need to login:

$ snapcraft login
Enter your Ubuntu One SSO credentials.
Email: sxxxxx.sxxxxxx@canonical.com
Password: **************
Second-factor auth: 123456

After logging in we are ready to get our snap registered, for examples sake let’s say we wanted to register awesome-database, a fantasy snap we want to get started with:

$ snapcraft register awesome-database
We always want to ensure that users get the software they expect
for a particular name.

If needed, we will rename snaps to ensure that a particular name
reflects the software most widely expected by our community.

For example, most people would expect ‘thunderbird’ to be published by
Mozilla. They would also expect to be able to get other snaps of
Thunderbird as 'thunderbird-sergiusens'.

Would you say that MOST users will expect 'a' to come from
you, and be the software you intend to publish there? [y/N]: y

You are now the publisher for 'awesome-database'.

So assuming we have the snap built already, all we have to do is push it to the store. Let’s take advantage of a shortcut and --release in the same command:

$ snapcraft push awesome-databse_0.1_amd64.snap --release edge
Uploading awesome-database_0.1_amd64.snap [=================] 100%
Processing....
Revision 1 of 'awesome-database' created.

Channel    Version    Revision
stable     -          -
candidate  -          -
beta       -          -
edge       0.1        1

The edge channel is now open.

If we try to release this to stable the store will block us:

$ snapcraft release awesome-database 1 stable
Revision 1 (devmode) cannot target a stable channel (stable, grade: devel)                                                                           

We are safe from messing up and making this available to our faithful users. Now eventually we will push a revision worthy of releasing to the stable channel:

$ snapcraft push awesome-databse_0.1_amd64.snap
Uploading awesome-database_0.1_amd64.snap [=================] 100%
Processing....
Revision 10 of 'awesome-database' created.

Notice that the version is just a friendly identifier and what really matters is the revision number the store generates for us. Now let’s go ahead and release this to stable:

$ snapcraft release awesome-database 10 stable
Channel    Version    Revision
stable     0.1        10
candidate  ^          ^
beta       ^          ^
edge       0.1        10

The 'stable' channel is now open.

In this last channel map view for the architecture we are working with, we can see that edge is going to be stuck on revision 10, and that beta and candidate will be following stable which is on revision 10. For some reason we decide that we will focus on stability and make our CI/CD push to beta instead. This means that our edge channel will slightly fall out of date, in order to avoid things like this we can decide to close the channel:

$ snapcraft close awesome-database edge
Arch    Channel    Version    Revision
amd64   stable     0.1        10
        candidate  ^          ^
        beta       ^          ^
        edge       ^          ^

The edge channel is now closed.

In this current state, all channels are following the stable channel so people subscribed to candidate, beta and edge would be tracking changes to that channel. If revision 11 is ever pushed to stable only, people on the other channels would also see it.

This listing also provides us with a full architecture view, in this case we have only been working with amd64.

Getting more information

So some time passed and we want to know what was the history and status of our snap in the store. There are two commands for this, the straightforward one is to run status which will give us a familiar result:

$ snapcraft status awesome-database
Arch    Channel    Version    Revision
amd64   stable     0.1        10
        candidate  ^          ^
        beta       ^          ^
        edge       ^          ^

We can also get the full history:

$ snapcraft history awesome-database
Rev.    Uploaded              Arch       Version    Channels
3       2016-09-30T12:46:21Z  amd64      0.1        stable*
...
...
...
2       2016-09-30T12:38:20Z  amd64      0.1        -
1       2016-09-30T12:33:55Z  amd64      0.1        -

Closing remarks

I hope this gives an overview of the things you can do with the store and more people start taking advantage of it!

Read more
Alan Griffiths

Miral on Dragonboard

Miral on Dragonboard

Having seen Kevin Gunn’s post on the mir-kiosk I thought I’d give it a try.  This is what I found.

Preliminaries

First, I dug out the Dragonboard I’d been testing Mir on a few months ago from a drawer and borrowed the Logitech k400r keyboard I use in my “den”. (This is convenient there because it provides both keyboard and mouse input from one device I can use from my lap.)

There was still a micro SD card in the board, so I extracted that and inserted it via a micro SD adapter into my desktop. Then I downloaded the dragonboard image from the link in Kevin’s article, verified the checksum and discovered that “Disk Image Writer” was the default app for opening the file. That sounded promising, so I opened the file with it, selected the SD card and started writing the image.

While that was going on I found an HDMI/VGA adapter and an old monitor to connect to the Dragonboard and started checking how to install from the SD. The main thing was to get the boot switches on the back of the board into 0110 positions (which means it will boot from the micro SD card).

With all that ready I went to brew tea while “Disk Image Writer” continued reporting progress.

It boots!

Returning with my tea I found that the image was written, so I disconnected the card and put it into the Dragonboard and connected the power.

I was greeted by a blank screen.

After a bit of experimentation I found that I need to connect to a real HDMI monitor during boot, but can switch to the adapter+VGA monitor after that. Annoying, but it works. (As I’m not yet sure where the fault lies I won’t mention the brands.)

Setup is a few simple questions, the only annoyance I had was that the keyboard layout defaults to US which makes typing my network password “interesting” on a UK keyboard.

One thing that could be a trap is that it asked for my email address to connect to the snap store. I’ve never been there before but I took a punt and used my canonical email address. That seemed to work and pick up the credentials on Launchpad.

That meant I could ssh into the Dragonboard from my desktop.

Installing miral-kiosk

Installing the mir libraries and the miral kiosk looks really easy using the commands Kevin provides (in the ssh session):

$ sudo snap install mir-libs --channel=edge --devmode
error: cannot perform the following tasks:
- Download snap "mir-libs" (3) from channel "edge" (unexpected EOF)

Actually, before that message was shown I got one estimating the download time at over an hour! Try again…

$ sudo snap install mir-libs --channel=edge --devmode
mir-libs (edge) 0.1 from 'albaguirre' installed

That’s better! (And only took a few seconds.)

$ sudo snap install mir-kiosk --channel=edge --devmode
mir-kiosk (edge) 0.1 from 'albaguirre' installed

Wow, I see the orange miral-kiosk startup “splash” and a mouse pointer! miral-kiosk is running on the Dragonboard.

A Client Application

To get a client application kg’s blog continues with the instructions:

“Download the appropriate architecture of the mir-client snap and then copy that over to your running ubuntu-core image. “

In this case we’re arm64, so I followed the link and picked out mir-client_0.24.1_arm64.snap. And then copied it to the dragonboard (back to a desktop terminal session):

$ scp Downloads/mir-client_0.24.1_arm64.snap alan-griffiths@192.168.1.159:~
mir-client_0.24.1_arm64.snap 100% 103MB 173.7KB/s 10:05

That took an unexpectedly long time. Back to ssh session:

$ sudo snap install mir-client_0.24.1_arm64.snap --channel=edge --devmode 
mir-client 0.24.1 installed

And there it is – kg’s sample client application running on miral-kiosk.

Read more
UbuntuTouch

[原]安装Ubuntu Core系统

今天很高兴看到Ubuntu Core 16终于发布了.Ubuntu Core 16系统是一个完全基于snap的系统.它包括foundation, applications,kernel,core操作系统及gadget等.所有的这些都被一个叫做为snapd的后台运行的工具进行安装及更新.最新的Ubuntu Core镜像可以在如下的地址:

http://releases.ubuntu.com/ubuntu-core/16/

他目前包括PC (amd64,i386)的发行版,同时它也对raspberry Pi2/3及QualComm的dragonboard 410c进行了支持.镜像一旦被解压缩,它就是可以直接启动的.PC版本可以直接运行于qemu-kvm,virtualbox或货真价实的x86机器上(比如intel的NUC).


在Ubuntu Desktop上进行测试Ubuntu Core


为了能够方便在PC上进行测试,我们可以在我们的Ubuntu Desktop系统上进行如下的操作:

1)安装qemu-kvm


$ sudo apt-get install qemu-kvm  
...say yes and install the packages...  

我们来检查一下我们的PC是否支持硬件虚拟化:

$ kvm-ok  
INFO: /dev/kvm exists  
KVM acceleration can be used  

如果你看见上面的结果,那么我恭喜你啦!这是最好的结局 - 这意味着Snappy将在你的系统运行速度快,并充分利用硬件加速。如果KVM不支持您的系统上,我们建议您尝试Ubuntu的核心在云上运行

2)下载Ubutu Core镜像


我们通过下面的命令来下载我们所需要的镜像(这里以64bit为例)

$ wget http://releases.ubuntu.com/ubuntu-core/16/ubuntu-core-16-amd64.img.xz

等下载完后,我们使用如下的命令来进行解压缩:

$ unxz ubuntu-core-16-amd64.img.xz 

3)启动KVM


现在,您可以启动该虚拟机KVM:

$ kvm -smp 2 -m 1500 -redir tcp:10022::22 ubuntu-core-16-amd64.img

在这里虚拟机的口地址我们已经做了重定向:

-10022 本地端口将指向虚拟机中的22口 (ssh)

在启动的过程中,console-conf所展示出来的信息"ssh USER@10.0.2.15"有点让人误解.事实上,我们可以运用如下的命令来登陆我们的kvm:



$ ssh -p 10022 USER@localhost

特别值得注意的是,我们必须把上面的USER换成我们自己的Ubuntu One上面的用户名.针对我的情况:

$ ssh -p 10022 liu-xiao-guo@localhost 



如果你在你的Desktop的目录~/.ssh/config中添加如下的内容,而且它的内容是:

Host kvm.snappy
     Hostname localhost
     Port 10022
     User USER
     UserKnownHostsFile /dev/null
     StrictHostKeyChecking no

那么你就直接使用如下的命令来启动:

$ ssh kvm.snappy


在Raspberry Pi2/3 dragonboard上进行安装



大家可以参照我先前的文章"如何为树莓派安装Ubuntu Core并在Snap系统中进行编译"来安装Ubuntu Core到我们的所支持的板子上.在这里,我们介绍另外一种方法供大家使用:

1)安装snapd


如果大家还没有在Ubuntu Desktop的桌面(16.04)上安装snapd的话,就使用如下的命令来安装.这样我的系统就可以支持snap应用的安装了:

$ sudo apt update  
$ sudo apt install snapd  


2)把image刷入到Ubuntu Core硬件中


我们可以通过如下的命令来把image写到我们所希望的SD卡中:

$ sudo snap install --devmode --beta godd
$ sudo /snap/bin/godd ubuntu-core-16-pi2.img.xz
[this will print a message showing what devices are removable]
$ xzcat ubuntu-core-16-pi2-rc2.img.xz | sudo /snap/bin/godd - /dev/sdXX

记得在实际的操作中,我们需要把上面的sdXX换成我们自己的SD卡的设备



比如针对我的情况,我需要换成mmcblk0.



我们把拷好image的SD卡插入到我们的树莓派板子中,然后启动.在启动的时候,你需要输入你的Ubuntu One账号的电子邮件地址,那么它将自动为你生产相应的和你账号匹配的ssh keys.如果你还没有Ubuntu SSO (single sign on)账号的话,请在如下的地址进行申请:


当然我们在创建完自己的账号后,千万不要忘记把自己的public ssh keys添加到你自己的账号中.

作者:UbuntuTouch 发表于2016/11/4 14:42:15 原文链接
阅读:25 评论:0 查看评论

Read more
UbuntuTouch

[原]如何在Snap包中定义全局的plug

我们知道在我们snap应用中,我们可以通过定义plug来访问我们所需要的资源.在一个snap包中,我们也可以定义许多的应用,每个应用可以分别定义自己的plug.假如一个Snap包有一个plug是共同的,那么,我们有上面办法来定义一个全局的plug,这样所有的在同一个包中的所有的snap应用都可以同时拥有这个plug.这个到底怎么做呢?

关于snap中的interface及plug概念,请参阅我之前的文章"安装snap应用到Ubuntu 16.4桌面系统".

最近,我读了一篇关于snap interface的文章.很受启发.文章可以在地址找到.其中有一段非常有意思的一段话:


Note that only the links app refers to plugs, the bookmarks app does not. If a plug or slot is declared in a snap, but not associated with a specific application they will be implicitly bound to all apps. When a plug or slot is specified by one or more apps, as in the above example, it will be bound only to those applications. Compare that to the following code:

它的意思就是如果我们在我们的snapcraft.yaml中定义一个plug,但是它不被任何的应用所应用,那么它隐含地就是所有的应用都有这个plug.


我们拿我们的例程https://github.com/liu-xiao-guo/helloworld-plug为例,我们定义如下:


name: hello-xiaoguo
version: "1.0"
architectures: [ all ]
summary: The 'hello-world' of snaps
description: |
    This is a simple snap example that includes a few interesting binaries
    to demonstrate snaps and their confinement.
    * hello-world.env  - dump the env of commands run inside app sandbox
    * hello-world.evil - show how snappy sandboxes binaries
    * hello-world.sh   - enter interactive shell that runs in app sandbox
    * hello-world      - simply output text
confinement: strict
type: app  #it can be gadget or framework

apps:
 env:
   command: bin/env
 evil:
   command: bin/evil
 sh:
   command: bin/sh
 hello-world:
   command: bin/echo
 createfile:
   command: bin/createfile
 createfiletohome:
   command: bin/createfiletohome
 writetocommon:
   command: bin/writetocommon

plugs:
    home:
        interface: home

parts:
 hello:
  plugin: copy
  files:
    ./bin: bin

在上面的snapcraft.yaml文件中,我们写了如下的句子:

plugs:
    home:
        interface: home

由于在我们的任何一个应用中都没有引用home plug,所有这个plug将被定义为包级的plug,也就是说所有的app都享有这个plug.我们可以做一个简单的测试.我们安装好我们的hello snap后,执行如下的命令:

liuxg@liuxg:~$ hello-xiaoguo.createfiletohome 
Hello a nice World!
This example demonstrates the app confinement
This app tries to write to its own user directory
Succeeded! Please find a file created at /home/liuxg/snap/hello-xiaoguo/x1/test.txt
If do not see this, please file a bug


我们的createtohome脚本如下:

#!/bin/sh

set -e
echo "Hello a nice World!"

echo "This example demonstrates the app confinement"
echo "This app tries to write to its own user directory"

echo "Haha" > /home/$USER/test.txt

echo "Succeeded! Please find a file created at $HOME/test.txt"
echo "If do not see this, please file a bug"

显然它向home里写入一个叫做test.txt的文件.我们的写入操作是成功的.


从我们的文件目录中,我们可以看出来我们刚刚创建的文件test.txt.细心的开发者也可以出去上面定义的plug,在试一试是否成功?







作者:UbuntuTouch 发表于2016/8/19 17:07:51 原文链接
阅读:419 评论:0 查看评论

Read more
UbuntuTouch

Snap是一个在Ubuntu系统上一个新的技术.如果大家对这个技术还不是很理解的话,可以参阅文章"安装snap应用到Ubuntu 16.4桌面系统".我们知道MySQLTomcat是在Java服务器端常用的技术.那么我们怎么来把它打包到我们的snap应用中去呢?很多人可能以为这个很简单.我们可以直接在snapcraft.yaml中使用stage-package来安装现有的debian包,不就可以了吗?其实,由于在snap系统的安全性限制,在一个snap应用被成功安装后,它自己所在的目录是只读的,不可以写入任何的数据.我们只有规定的部分目录才是可以写入的.我们以hello例程为例,我们有如下的目录可以使用:


在上面,有两个最重要的目录:

  • SNAP_DATA
  • SNAP
在我们snap我们应用的时候,我们需要configure我们的应用尽量使用上面的两个目录,而不需要hard-code我们的目录.特别是由于我们的应用不能向自己的安装目录SNAP写入任何的数据(read-only),所以,我们必须把我们的数据库及需要写入数据的文件设置到SNAP_DATA目录中.另外必须注意的是,访问SNAP_DATA文件目录时需要root权限.这对于一个daemon的应用来说,应该没有问题,但是对于一般的应用来说,我们需要使用sudo来访问才可以访问到数据库.


1)snap Tomcat


在我们的snapcraft中有一个例程叫做"tomcat-webapp-demo".它提供了一个很好的基础.

snapcraft.yaml

name: tomcat-webapp-demo
version: 1.0
architectures:
 - amd64
summary: Demo of Tomcat-hosted Webapp
description: This is a demo snap of a Tomcat-hosted webapp produced by snapcraft with maven.
confinement: strict

apps:
 tomcat:
   command: bin/wrapper
   daemon: simple
   plugs: [network-bind]

parts:
    webapp:
        plugin: maven
        source: https://github.com/lool/snappy-mvn-demo.git
        source-type: git
    tomcat:
        plugin: dump
        source: https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.29/bin/apache-tomcat-8.0.29.tar.gz
    local-files:
        plugin: make
        source: .

从上面我们可以看出来,它使用了dump plugin.这个plugin只有在snapcraft 2.14中才开始适使用,并最终代替copy plugin. 对于以前的版本我们可以使用tar-content来代替dump.从上面的代码中可以看出来,它知己从官网上下载所需要的版本,并做一个安装.在启动的时候,它使用bin/wrapper来启动.wrapper的内容如下:

wrapper

#!/bin/sh

set -e
set -x

# installation pathes are based of CATALINA_HOME
export CATALINA_HOME="$SNAP"
# writable pathes are based of CATALINA_BASE
export CATALINA_BASE="$SNAP_DATA"

# create runtime data
mkdir -p "$CATALINA_BASE/logs"
mkdir -p "$CATALINA_BASE/temp"

if ! [ -d $CATALINA_BASE/conf ]; then
	echo "conf directory does not exist"
    cp -rd $CATALINA_HOME/tomcat-conf $CATALINA_BASE/conf
fi

if ! [ -d $CATALINA_BASE/webapps ]; then
	echo "webapps directory  does not exist"
    cp -rd $CATALINA_HOME/webapps $CATALINA_BASE/
    cp $CATALINA_HOME/war/*.war $CATALINA_BASE/webapps/
fi

$CATALINA_HOME/bin/catalina.sh run

从上面的代码中,我们可以看出来我们已经把我们所需要的目录用SNAP及SNAP_DATA来代替了.


2)snap MySQL


我们可以先看一下文章"Snapping Nextcloud: MySQL".从该文章中,我们可以看出来,直接通过stage-package的方式来snap MySQL是不可能的.我们可以直接编译MySQL源码的方式来snap MySQL.

snapcraft.yaml

    # Download the boost headers for MySQL. Note that the version used may need to
    # be updated if the version of MySQL changes.
    boost:
        plugin: copy
        source: http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
        files:
          '*': boost/
        snap:
          - -*
        
    mysql:
        plugin: cmake
        source: https://github.com/kyrofa/mysql-server.git
        source-type: git
        source-branch: feature/support_no_setpriority
        after: [boost]
        configflags:
          - -DWITH_BOOST=$SNAPCRAFT_STAGE/boost
          - -DWITH_INNODB_PAGE_CLEANER_PRIORITY=OFF
          - -DCMAKE_INSTALL_PREFIX=/
          - -DBUILD_CONFIG=mysql_release
          - -DWITH_UNIT_TESTS=OFF
          - -DWITH_EMBEDDED_SERVER=OFF
          - -DWITH_EMBEDDED_SHARED_LIBRARY=OFF
          - -DWITH_ARCHIVE_STORAGE_ENGINE=OFF
          - -DWITH_BLACKHOLE_STORAGE_ENGINE=OFF
          - -DWITH_FEDERATED_STORAGE_ENGINE=OFF
          - -DWITH_PARTITION_STORAGE_ENGINE=OFF
          - -DINSTALL_MYSQLTESTDIR=
        build-packages:
          - wget
          - g++
          - cmake
          - bison
          - libncurses5-dev
          - libaio-dev
        stage:
         # Remove scripts that we'll be replacing with our own
          - -support-files/mysql.server
        snap:
         # Remove scripts that we'll be replacing with our own
         - -support-files/mysql.server

         # Remove unused binaries that waste space
         - -bin/innochecksum
         - -bin/lz4_decompress
         - -bin/myisam*
         - -bin/mysqladmin
         - -bin/mysqlbinlog
         - -bin/mysql_client_test
         - -bin/mysql_config*
         - -bin/mysqld_multi
         - -bin/mysqldump*
         - -bin/mysqlimport
         - -bin/mysql_install_db
         - -bin/mysql_plugin
         - -bin/mysqlpump
         - -bin/mysql_secure_installation
         - -bin/mysqlshow
         - -bin/mysqlslap
         - -bin/mysql_ssl_rsa_setup
         - -bin/mysqltest
         - -bin/mysql_tzinfo_to_sql
         - -bin/perror
         - -bin/replace
         - -bin/resolveip
         - -bin/resolve_stack_dump
         - -bin/zlib_decompress

    # Copy over our MySQL scripts
    mysql-customizations:
        plugin: copy
        files:
           # This one is what creates the initial database and account for ownCloud.
           src/mysql/start_mysql: bin/
           src/mysql/my.cnf: my.cnf
           src/mysql/mysql.server: support-files/
           sample.war: war/sample.war
           mysql-connector-java-6.0.3.jar: lib/mysql-connector-java-6.0.3.jar

由于编译MySQL时需要用到boost库,所以直接把boost库的源码下载下来,当然,在我们snap时,我们并不需要它,所以:

        snap:
          - -*

就想我们之前snap我们的tomcat时一样,我们需要对MySQL的配置进行改变才可以:


src/mysql/my.cnf


[mysqld]
user=root
max_allowed_packet=100M
secure-file-priv=NULL
port=3306
bind-address=0.0.0.0
[client]
protocol=tcp

在这里,我们配置了所需要的port号码及使用的协议TCP.这样我们的数据库可以位于任何一个单独的服务器中(我们可以修改这里的bind-address).在我们的设计中,MySQL的数据库位于本机.这样当我们的MySQL服务器运行时,可以看到:

liuxg@liuxg:~$ netstat -lnp46 | grep -w 3306
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      -               

src/mysql/mysql.server

这个文件是我们在我们的下面的start_mysql的脚本中被使用的:

$SNAP/support-files/mysql.server start

该脚本的内容如下:

#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO
 
# If you install MySQL on some other places than /, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=$SNAP
datadir=$SNAP_DATA/mysql

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start. 
# Value here is overriden by value in my.cnf. 
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

# Lock directory for RedHat / SuSE.
lockdir="$SNAP_DATA/mysql/lock"
lock_file_path="$lockdir/mysql"

# The following variables are only set for letting mysql.server find things.

# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/
  bindir=//bin
  if test -z "$datadir"
  then
    datadir=//data
  fi
  sbindir=//bin
  libexecdir=//bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi

# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
  . $lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH

mode=$1    # start or stop

[ $# -ge 1 ] && shift


other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
           # Expected: "--skip-networking --skip-grant-tables"
           # They are not checked here, intentionally, as it is the resposibility
           # of the "spec" file author to give correct arguments only.

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac

parse_server_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
                    bindir="$basedir/bin"
		    if test -z "$datadir_set"; then
		      datadir="$basedir/data"
		    fi
		    sbindir="$basedir/sbin"
		    libexecdir="$basedir/libexec"
        ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
		    datadir_set=1
	;;
      --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
    esac
  done
}

wait_for_pid () {
  verb="$1"           # created | removed
  pid="$2"            # process ID of the program operating on the pid-file
  pid_file_path="$3" # path to the PID file.

  i=0
  avoid_race_condition="by checking again"

  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s "$pid_file_path" && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s "$pid_file_path" && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
        exit 1
        ;;
    esac

    # if server isn't running, then pid-file will never be updated
    if test -n "$pid"; then
      if kill -0 "$pid" 2>/dev/null; then
        :  # the server still runs
      else
        # The server may have exited between the last pid-file check and now.  
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg "The server quit without updating PID file ($pid_file_path)."
        return 1  # not waiting any more.
      fi
    fi

    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1

  done

  if test -z "$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x ./bin/my_print_defaults
then
  print_defaults="./bin/my_print_defaults"
elif test -x $bindir/my_print_defaults
then
  print_defaults="$bindir/my_print_defaults"
elif test -x $bindir/mysql_print_defaults
then
  print_defaults="$bindir/mysql_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf=/etc/my.cnf
  print_defaults=
  if test -r $conf
  then
    subpat='^[^=]*basedir[^=]*=\(.*\)$'
    dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
    for d in $dirs
    do
      d=`echo $d | sed -e 's/[ 	]//g'`
      if test -x "$d/bin/my_print_defaults"
      then
        print_defaults="$d/bin/my_print_defaults"
        break
      fi
      if test -x "$d/bin/mysql_print_defaults"
      then
        print_defaults="$d/bin/mysql_print_defaults"
        break
      fi
    done
  fi

  # Hope it's in the PATH ... but I doubt it
  test -z "$print_defaults" && print_defaults="my_print_defaults"
fi

#
# Read defaults file from 'basedir'.   If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#

extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
else
  if test -r "$datadir/my.cnf"
  then
    extra_args="-e $datadir/my.cnf"
  fi
fi

parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`

#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
  mysqld_pid_file_path=$datadir/`hostname`.pid
else
  case "$mysqld_pid_file_path" in
    /* ) ;;
    * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  esac
fi

case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" --lc-messages-dir="$SNAP/share" $other_args >/dev/null 2>&1 &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"
      then
        touch "$lock_file_path"
      fi

      exit $return_value
    else
      log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
    fi
    ;;

  'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.

    if test -s "$mysqld_pid_file_path"
    then
      # signal mysqld_safe that it needs to stop
      touch "$mysqld_pid_file_path.shutdown"

      mysqld_pid=`cat "$mysqld_pid_file_path"`

      if (kill -0 $mysqld_pid 2>/dev/null)
      then
        echo $echo_n "Shutting down MySQL"
        kill $mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      if test -f "$lock_file_path"
      then
        rm -f "$lock_file_path"
      fi
      exit $return_value
    else
      log_failure_msg "MySQL server PID file could not be found!"
    fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if $0 stop  $other_args; then
      $0 start $other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  'reload'|'force-reload')
    if test -s "$mysqld_pid_file_path" ; then
      read mysqld_pid <  "$mysqld_pid_file_path"
      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
      touch "$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    # First, check to see if pid file exists
    if test -s "$mysqld_pid_file_path" ; then 
      read mysqld_pid < "$mysqld_pid_file_path"
      if kill -0 $mysqld_pid 2>/dev/null ; then 
        log_success_msg "MySQL running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=`pidof $libexecdir/mysqld`

      # test if multiple pids exist
      pid_count=`echo $mysqld_pid | wc -w`
      if test $pid_count -gt 1 ; then
        log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
        exit 5
      elif test -z $mysqld_pid ; then 
        if test -f "$lock_file_path" ; then 
          log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
          exit 2
        fi 
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      basename=`basename "$0"`
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0

这个脚本的内容其实和标准的从MySQL编译后的脚本没有什么大的差别.除了如下的地方发生改变:

basedir=$SNAP
datadir=$SNAP_DATA/mysql

这个地方的改变是为了提供我们运行文件的参考目录位置及数据库的文件位置.

src/mysql/start_mysql



这是一个我们需要的脚本来真正启动我们的MySQL服务器:

 mysql:
   command: start_mysql
   stop-command: support-files/mysql.server stop
   daemon: simple
   plugs: [network, network-bind]

显然这是一个daemon应用.它的内容如下:

#!/bin/sh

root_option_file="$SNAP_DATA/mysql/root.ini"
demouser_password_file="$SNAP_DATA/mysql/demouser_password"
mysqld_pid_file_path=$SNAP_DATA/mysql/`hostname`.pid
#mysql_socket_file_path=$SNAP_DATA/mysql/mysql.sock
new_install=false

# Make sure the database is initialized (this is safe to run if already
# initialized)
mysqld --initialize-insecure --basedir="$SNAP" --datadir="$SNAP_DATA/mysql" --lc-messages-dir="$SNAP/share"

# If the above command succeeded, it means this is a new installation.
if [ $? -eq 0 ]; then
	echo "it is new install"
	new_install=true
fi

# Start mysql
$SNAP/support-files/mysql.server start

# Initialize new installation if necessary.
if [ $new_install = true ]; then
	# Generate a password for the root mysql user.
	echo -n "Generating root mysql password... "
	root_password="123"
	echo "done."

	# Generate a password for the owncloud mysql user.
	echo -n "Generating owncloud mysql password... "
	demouser_password="123"
	echo "done."

	# Save root user information
	echo "writing client"
	echo "[client]" >> $root_option_file
	echo "writing user=root"
	echo "user=root" >> $root_option_file
	chmod 600 $root_option_file
	
	echo "dump the root option file"
	cat $root_option_file

	# Now set everything up in one step:
	# 1) Set the root user's password
	# 2) Create the 'demouser' user
	# 3) Create the 'demodb' database
	# 4) Grant the 'demodb' user privileges on the 'demodb' database
	echo -n "Setting up users and owncloud database... "
	mysql --defaults-file=$root_option_file <<SQL
ALTER USER 'root'@'localhost' IDENTIFIED BY '$root_password';
CREATE USER 'demouser'@'localhost' IDENTIFIED BY '$demouser_password';
CREATE DATABASE demodb;
GRANT ALL PRIVILEGES ON demodb.* TO 'demouser'@'localhost' IDENTIFIED BY '$demouser_password';
USE demodb;
create table Employees
    (
     id int not null,
     age int not null,
     first varchar (255),
     last varchar (255)
    );
INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');    
INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
SQL
	if [ $? -eq 0 ]; then
		echo "done."
	else
		echo "Failed to initialize-- reverting..."
		$SNAP/support-files/mysql.server stop
		rm -rf $SNAP_DATA/mysql/*
	fi

	# Now the root mysql user has a password. Save that as well.
	echo "writing root password"
	echo "password=$root_password" >> $root_option_file
fi

# Wait here until mysql is running
echo "Waiting for server..."
#while [ ! -f "$mysqld_pid_file_path" -o ! -S "$mysql_socket_file_path" ]; do
while [ ! -f "$mysqld_pid_file_path" -o ]; do
	sleep 1
done

# Check and upgrade mysql tables if necessary. This will return 0 if the upgrade
# succeeded, in which case we need to restart mysql.
echo "Checking/upgrading mysql tables if necessary..."
mysql_upgrade --defaults-file=$root_option_file
if [ $? -eq 0 ]; then
	echo "Restarting mysql server after upgrade..."
	$SNAP/support-files/mysql.server restart

	echo "Waiting for server to come back after upgrade..."
#	while [ ! -f "$mysqld_pid_file_path" -o ! -S "$mysql_socket_file_path" ]; do
	while [ ! -f "$mysqld_pid_file_path" -o ]; do
        	sleep 1
	done
fi

# If this was a new installation, wait until the server is all up and running
# before saving off the owncloud user's password. This way the presence of the
# file can be used as a signal that mysql is ready to be used.
if [ $new_install = true ]; then
	echo "$demouser_password" > $demouser_password_file
fi

# Wait here until mysql exits (turn a forking service into simple). This is
# only needed for Ubuntu Core 15.04, as 16.04 supports forking services.
mysqld_pid=$(cat "$mysqld_pid_file_path")
while kill -0 $mysqld_pid 2>/dev/null; do
	sleep 1
done

我们在table中也创建了几个记录.在这里也请大家注意,我hard-code了数据库的密码"123".这个密码在我们以后的数据库访问中需要用到.注意我们在这里已经创建了两个用户,一个叫做root,另外一个叫做demouser

	mysql --defaults-file=$root_option_file <<SQL
ALTER USER 'root'@'localhost' IDENTIFIED BY '$root_password';
CREATE USER 'demouser'@'localhost' IDENTIFIED BY '$demouser_password';
CREATE DATABASE demodb;
GRANT ALL PRIVILEGES ON demodb.* TO 'demouser'@'localhost' IDENTIFIED BY '$demouser_password';
USE demodb;
create table Employees
    (
     id int not null,
     age int not null,
     first varchar (255),
     last varchar (255)
    );
INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');    
INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
SQL

如何调用MySQL命令行


我们在snapcraft.yaml文档中,定义了如下的command:

 mysql-client:
   command: mysql --defaults-file=$SNAP_DATA/mysql/root.ini --protocol=TCP
   plugs: [network, network-bind]

由于在MySQL的命令行中需要访问到数据库,需要root权限.我们可以通过su命令进入到root用户:



我们可以通过运行我们定义的命令来启动MySQL客户端.通过这个命令,我们可以创建我们的数据等操作.



3)JSP 数据库访问


最后,我们需要一个JSP的程序来访问我们的数据库.我们的设计如下:

index.jsp

<%@ page import="java.sql.*"%>
<html>
<head>
<title>JDBC Connection example</title>
</head>

<body>
<h1>JDBC Connection example</h1>

<%
  String user = "demouser";
  String password = "123";
  
  try {
    java.sql.Connection con;
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demodb", user, password);
    out.println (user + " account opens database successfully opened.");
    
    String query="select * from Employees";
    Statement stmt=con.createStatement();
	ResultSet rs=stmt.executeQuery(query);
%>	   
    <br />
    <h3>The data read from database is:</h3>
    <br />
    
	<table border="2">
	<tr>
		<td>Id</td>
		<td>Age</td>
		<td>First</td>
		<td>Last</td>
	</tr>
<%
	while(rs.next())
	{
%>
	<tr>
		<td><%=rs.getInt("id")%></td>
		<td><%=rs.getInt("age")%></td>
		<td><%=rs.getString("first")%></td>
		<td><%=rs.getString("last")%></td>
	</tr>
<%
	}
%>
	</table>
	
<%
    // close the connection
    rs.close();
    stmt.close();
    con.close();
  }
  catch(SQLException e) {
    out.println("SQLException caught: " +e.getMessage());
  }
%>

</body>
</html>


最终打包我们的应用,并部署我们的应用.我们可以看到最终的结果为:




在实际的使用中,我们也可以把我们已经开发好的war包放入到我们的tomcat的目录中:

    mysql-customizations:
        plugin: copy
        files:
           # This one is what creates the initial database and account for ownCloud.
           src/mysql/start_mysql: bin/
           src/mysql/my.cnf: my.cnf
           src/mysql/mysql.server: support-files/
           sample.war: war/sample.war
           mysql-connector-java-6.0.3.jar: lib/mysql-connector-java-6.0.3.jar

比如上面的sample.war,我们可以通过如下的方式来运行该应用:




更多关于snap的介绍,可以参阅文章"安装snap应用到Ubuntu 16.4桌面系统

作者:UbuntuTouch 发表于2016/8/16 10:57:12 原文链接
阅读:741 评论:0 查看评论

Read more
UbuntuTouch

在今天的例程中,我们将重点介绍如何使用Ubuntu手机SDK所提供的SDK来创建一个可以聊天的应用.通过这个例程,我们来展示如何利用Bluetooth API接口在两个Ubuntu手机或Ubuntu电脑上进行聊天.


    


1)创建一个Bluetooth Chat server


chatserver.cpp

#include "chatserver.h"

#include <qbluetoothserver.h>
#include <qbluetoothsocket.h>
#include <qbluetoothlocaldevice.h>

static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");

ChatServer::ChatServer(QObject *parent)
:   QObject(parent), rfcommServer(0)
{
}

ChatServer::~ChatServer()
{
    stopServer();
}

void ChatServer::startServer(const QBluetoothAddress& localAdapter)
{
    if (rfcommServer)
        return;

    rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
    connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
    bool result = rfcommServer->listen(localAdapter);
    if (!result) {
        qWarning() << "Cannot bind chat server to" << localAdapter.toString();
        return;
    }

    //serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);

    QBluetoothServiceInfo::Sequence classId;

    classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
    serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
                             classId);

    classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid)));

    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
    serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,classId);

    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server"));
    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
                             tr("Example bluetooth chat server"));
    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("qt-project.org"));

    serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));

    QBluetoothServiceInfo::Sequence publicBrowse;
    publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
    serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
                             publicBrowse);

    QBluetoothServiceInfo::Sequence protocolDescriptorList;
    QBluetoothServiceInfo::Sequence protocol;
    protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
    protocolDescriptorList.append(QVariant::fromValue(protocol));
    protocol.clear();
    protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
             << QVariant::fromValue(quint8(rfcommServer->serverPort()));
    protocolDescriptorList.append(QVariant::fromValue(protocol));
    serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                             protocolDescriptorList);

    serviceInfo.registerService(localAdapter);
}

void ChatServer::stopServer()
{
    // Unregister service
    serviceInfo.unregisterService();

    // Close sockets
    qDeleteAll(clientSockets);

    // Close server
    delete rfcommServer;
    rfcommServer = 0;
}

void ChatServer::disconnect()
{
    qDebug() << "Going to disconnect in server";

    foreach (QBluetoothSocket *socket, clientSockets) {
        qDebug() << "sending data in server!";
        socket->close();
    }
}

void ChatServer::sendMessage(const QString &message)
{
    qDebug() << "Going to send message in server: " << message;
    QByteArray text = message.toUtf8() + '\n';

    foreach (QBluetoothSocket *socket, clientSockets) {
        qDebug() << "sending data in server!";
        socket->write(text);
    }
    qDebug() << "server sending done!";
}

void ChatServer::clientConnected()
{
    qDebug() << "clientConnected";

    QBluetoothSocket *socket = rfcommServer->nextPendingConnection();
    if (!socket)
        return;

    connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    clientSockets.append(socket);
    emit clientConnected(socket->peerName());
}

void ChatServer::clientDisconnected()
{
    QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
    if (!socket)
        return;

    emit clientDisconnected(socket->peerName());

    clientSockets.removeOne(socket);

    socket->deleteLater();
}

void ChatServer::readSocket()
{
    QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
    if (!socket)
        return;

    while (socket->canReadLine()) {
        QByteArray line = socket->readLine().trimmed();
        emit messageReceived(socket->peerName(),
                             QString::fromUtf8(line.constData(), line.length()));
    }
}


在这里我们通过QBluetoothServer来创建一个Bluetooth基于RFCOMM协议的server.我们发布一个基于这个协议的server.其它想连接这个服务器的client,必须寻找我们在这里所定义的ServiceUuid:

 classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
    serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
                             classId);

    classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid)));

这里所定义的serviceUuid:

static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");

2)创建一个Bluetooth client


我们可以创建一个Bluetooth的client用来发起一个向Bluetooth server的连接请求.

chatclient.cpp


#include "chatclient.h"

#include <qbluetoothsocket.h>

ChatClient::ChatClient(QObject *parent)
:   QObject(parent), socket(0)
{
}

ChatClient::~ChatClient()
{
    stopClient();
}

void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
{
    if (socket)
        return;

    // Connect to service
    socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
    qDebug() << "Create socket";
    socket->connectToService(remoteService);
    qDebug() << "ConnectToService done";

    connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
    connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
}

void ChatClient::stopClient()
{
    delete socket;
    socket = 0;
}

void ChatClient::readSocket()
{
    if (!socket)
        return;

    while (socket->canReadLine()) {
        QByteArray line = socket->readLine();
        emit messageReceived(socket->peerName(),
                             QString::fromUtf8(line.constData(), line.length()));
    }
}

void ChatClient::sendMessage(const QString &message)
{
    qDebug() << "Sending data in client: " + message;

    QByteArray text = message.toUtf8() + '\n';
    socket->write(text);
}

void ChatClient::connected()
{
    emit connected(socket->peerName());
}

void ChatClient::disconnect() {
    qDebug() << "Going to disconnect in client";
    if ( socket ) {
        qDebug() << "diconnecting...";
        socket->close();
    }
}

当我们发生一个请求的时候,我们可以通过如下的方式来连接:

chat.cpp

void Chat::connectToDevice(QString name)
{
    qDebug() << "Connecting to " << name;

    // Trying to get the service
    QBluetoothServiceInfo service;
    QMapIterator<QString, QBluetoothServiceInfo> i(remoteSelector->m_discoveredServices);
    bool found = false;
    while (i.hasNext()){
        i.next();
        QString key = i.key();
        if ( key == name ) {
            qDebug() << "The device is found";
            service = i.value();
            qDebug() << "value: " << i.value().device().address();
            found = true;
            break;
        }
    }

    if ( found ) {
        qDebug() << "Going to create client";
        ChatClient *client = new ChatClient(this);
        qDebug() << "Connecting...";

        connect(client, SIGNAL(messageReceived(QString,QString)),
                this, SIGNAL(showMessage(QString,QString)));
        connect(client, SIGNAL(disconnected()), this, SIGNAL(clientDisconnected()));
        connect(client, SIGNAL(disconnected()), this, SLOT(clientIsDisconnected()));
        connect(client, SIGNAL(connected(QString)), this, SIGNAL(connected(QString)));
        connect(this, SIGNAL(sendMessage(QString)), client, SLOT(sendMessage(QString)));
        connect(this, SIGNAL(disconnect()), client, SLOT(disconnect()));

        qDebug() << "Start client";
        client->startClient(service);

        clients.append(client);
    }
}



3)扫描Bluetooth device


我们可以通过如下的方法来扫描附件的Bluetooth设备:

remoteselector.cpp


#include <qbluetoothdeviceinfo.h>
#include <qbluetoothaddress.h>
#include <qbluetoothlocaldevice.h>

#include "remoteselector.h"

QT_USE_NAMESPACE

RemoteSelector::RemoteSelector(QBluetoothAddress &localAdapter, QObject *parent)
: QObject(parent)
{
    m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(localAdapter);

    connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
            this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
    connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
    connect(m_discoveryAgent, SIGNAL(canceled()), this, SLOT(discoveryFinished()));
}

RemoteSelector::~RemoteSelector()
{
    delete m_discoveryAgent;
}

void RemoteSelector::startDiscovery(const QBluetoothUuid &uuid)
{
    qDebug() << "startDiscovery";
    if (m_discoveryAgent->isActive()) {
        qDebug() << "stop the searching first";
        m_discoveryAgent->stop();
    }

    m_discoveryAgent->setUuidFilter(uuid);
    m_discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);

}

void RemoteSelector::stopDiscovery()
{
    qDebug() << "stopDiscovery";
    if (m_discoveryAgent){
        m_discoveryAgent->stop();
    }
}

QBluetoothServiceInfo RemoteSelector::service() const
{
    return m_service;
}

void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo)
{
#if 0
    qDebug() << "Discovered service on"
             << serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"
             << serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
#endif

    QString remoteName;
    if (serviceInfo.device().name().isEmpty())
        remoteName = serviceInfo.device().address().toString();
    else
        remoteName = serviceInfo.device().name();

    qDebug() << "adding to the list....";
    qDebug() << "remoteName: "  << remoteName;
    m_discoveredServices.insert(remoteName, serviceInfo);
    emit newServiceFound();
}

void RemoteSelector::discoveryFinished()
{
    qDebug() << "discoveryFinished";
    emit finished();
}

我们可以通过调用startDiscovery()来扫描附近的设备.在调用时,我们可以设置我们想要的serviceUuid:

chat.cpp


void Chat::searchForDevices()
{      
    qDebug() << "search for devices!";
    if ( remoteSelector ) {
        delete remoteSelector;
        remoteSelector = NULL;
    }

    QBluetoothAddress adapter = QBluetoothAddress();
    remoteSelector = new RemoteSelector(adapter, this);

    connect(remoteSelector, SIGNAL(newServiceFound()), this, SLOT(newServiceFound()));

    remoteSelector->m_discoveredServices.clear();
    remoteSelector->startDiscovery(QBluetoothUuid(serviceUuid));
    connect(remoteSelector, SIGNAL(finished()), this, SIGNAL(discoveryFinished()));
}

这样就可以扫描到具有我们所需要的serviceUuid的设备来提供连接.在扫描时,Bluetooth server必须是在运行的状态.


4)应用UI设计


我们的应用UI设计比较简单.就想上面显示的图一样,我们最上面的显示当前正在运行该应用的Bluetooth server的设备.当我们点击该设备时,就开始向该设备发送连接请求.在我们的下面的对话框中,会显示连接的状态及对话:

Main.qml


import QtQuick 2.4
import Ubuntu.Components 1.3
import QtBluetooth 5.3

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "btchat.xiaoguo"

    width: units.gu(60)
    height: units.gu(85)

    function getBluetoothState(state) {
        switch (state ) {
        case BluetoothSocket.Unconnected:
            return "Unconnected";
        case BluetoothSocket.ServiceLookup:
            return "ServiceLookup";
        case BluetoothSocket.Connecting:
            return "Connecting";
        case BluetoothSocket.Connected:
            return "Connected";
        case BluetoothSocket.Bound:
            return "Bound";
        case BluetoothSocket.Closing:
            return "Closing";
        case BluetoothSocket.Listening:
            return "Listening";
        case BluetoothSocket.ServiceLookup:
            return "ServiceLookup";
        case BluetoothSocket.NoServiceSet:
            return "NoServiceSet";
        default:
            return "Unknow state"
        }
    }

    function appendMessage(msg, alignright) {
        mymodel.append({ "msg": msg, "alignright": alignright} )
        listview.positionViewAtIndex(mymodel.count - 1, ListView.Beginning)
    }

    Connections {
        target: chat
        onConnected: {
            console.log("Connected: " + name)
            appendMessage( "connected to " + name, false )
        }

        onDisconnected: {
            console.log("Disconnected: " + name )
            appendMessage( "disconnected " + name, false )
        }

        onClientDisconnected: {
            console.log("Client Disconnected")
            appendMessage("Client Disconnected", false)
        }

        onShowMessage: {
            console.log("sender: " + sender )
            console.log("message: " + message )
            message = message.replace(/(\r\n|\n|\r)/gm,"");
            var msg = '<font color = "green">' + message + '</font>';
            console.log("msg: " + msg)
            appendMessage(msg, false)
        }

        onNewServicesFound: {
            console.log("new services found!")
            devlist.model = list;
        }

        onDiscoveryFinished: {
            console.log("discovery finished");
            indicator.running = false;
        }
    }

    ListModel {
        id: mymodel
    }

    ListModel {
        id: services
    }

    BluetoothDiscoveryModel {
        id: btModel
        running: false
        discoveryMode: BluetoothDiscoveryModel.FullServiceDiscovery
        onRunningChanged : {
        }

        onErrorChanged: {
        }

        onServiceDiscovered: {
            console.log("service has been found!")
            services.append( {"service": service })
        }
        uuidFilter: "e8e10f95-1a70-4b27-9ccf-02010264e9c8"
    }

    BluetoothSocket {
        id: socket
        connected: true

        onSocketStateChanged: {
            console.log("socketState: " + socketState);
            console.log("Connected to server! ")
            appendMessage( "State: " + getBluetoothState(socketState) )
        }

        onStringDataChanged: {
            console.log("Received data: " )
            var data = "Going to send: " + socket.stringData;
            data = data.substring(0, data.indexOf('\n'))
            console.log(data);
            appendMessage("Received: " + data)
        }
    }

    Component {
        id: highlight
        Rectangle {
            width: devlist.width
            height: devlist.delegate.height
            color: "lightsteelblue"; radius: 5
            Behavior on y {
                SpringAnimation {
                    spring: 3
                    damping: 0.2
                }
            }
        }
    }

    Page {
        id: page
        header: standardHeader

        PageHeader {
            id: standardHeader
            visible: page.header === standardHeader
            title: "Bluetooth chat"
            trailingActionBar.actions: [
                Action {
                    iconName: "edit"
                    text: "Edit"
                    onTriggered: page.header = editHeader
                }
            ]
        }

        PageHeader {
            id: editHeader
            visible: page.header === editHeader
            leadingActionBar.actions: [
                Action {
                    iconName: "back"
                    text: "Back"
                    onTriggered: {
                        page.header = standardHeader
                    }
                }
            ]
            contents: Row {
                id: layout
                anchors {
                    left: parent.left
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }
                spacing: units.gu(2)

                TextField {
                    id: input
                    width: parent.width*2/3
                    placeholderText: "input words .."
                    text: "I love you!"

                    onAccepted: {
                        console.log("going to send: " + text)
                    }
                }

                Button {
                    text: "Send"
                    width: parent.width - input.width - layout.spacing;
                    onClicked: {
                        console.log("send is clicked")
                        console.log("chat length: " + input.text.length)
                        chat.sendMessage(input.text);
                        appendMessage(input.text, true)
                    }
                }
            }
        }

        Item {
            anchors {
                left: parent.left
                right: parent.right
                bottom: parent.bottom
                top: page.header.bottom
            }

            ActivityIndicator {
                id: indicator
                anchors.centerIn: parent
            }

            Column {
                anchors.fill: parent

                Label {
                    text: "Devices are:"
                    fontSize: "x-large"
                }

                ListView {
                    id: devlist
                    width: parent.width
                    height: parent.height/4
                    model: services
                    highlight: highlight
                    delegate: Label {
                        width: parent.width
                        text: modelData
                        fontSize: "x-large"
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                console.log("it is selected")
                                chat.connectToDevice(modelData)
                            }
                        }
                    }
                }

                Rectangle {
                    width: parent.width
                    height: units.gu(0.4)
                    color: "green"
                }

                ListView {
                    id: listview
                    width: parent.width
                    height: parent.height - devlist.height
                    model: mymodel
                    delegate: Item {
                        width: page.width
                        height: txt.height * 1.2 /*+ div.height*/

                        Rectangle {
                            width: txt.contentWidth
                            height: parent.height
                            color: alignright? "green" : "white"
                            radius: units.gu(0.5)
                            anchors.right: alignright ? parent.right : undefined
                            anchors.verticalCenter: parent.verticalCenter
                        }

                        Label {
                            id: txt
                            width: parent.width*0.7
                            text: msg
                            anchors.right: alignright ? parent.right : undefined
                            horizontalAlignment: alignright ? Text.AlignRight :
                                                              Text.AlignLeft
                            fontSize: "large"
                            anchors.verticalCenter: parent.verticalCenter
                            wrapMode: Text.WordWrap
                        }

//                        Rectangle {
//                            id: div
//                            width: parent.width
//                            height: units.gu(0.2)
//                            color: "blue"
//                        }
                    }
                }
            }


            Row {
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: units.gu(1)
                spacing: units.gu(2)

                Button {
                    text: "Search"
                    onClicked: {
                        console.log("btModel.running: " + btModel.running )
                        var list = []
                        devlist.model = list;
                        chat.searchForDevices()
                        indicator.running = true
                    }
                }

                Button {
                    text: "Stop search"
                    onClicked: {
                        console.log("Stop is clicked!")
                        chat.stopSearch();
                        indicator.running = false
                    }
                }

                Button {
                    text: "Disconnect"
                    onClicked: {
                        console.log("Disconnect is clicked!")
                        chat.disconnect();
                    }
                }
            }
        }
    }
}

整个应用的代码在:https://github.com/liu-xiao-guo/btchat


作者:UbuntuTouch 发表于2016/7/5 14:19:11 原文链接
阅读:422 评论:0 查看评论

Read more
UbuntuTouch

对于一些Ubuntu Geek来说,他们有时更喜欢使用命令行的方式来启动在手机中的应用,并通过这样的方式来调试我们的应用.我们可以通过如下的方式进入到手机的shell中:




我们可以在手机中寻找如下的预先安装好的应用:



对于这里面的应用来说,我们可以通如下的命令来启动我们的应用:


phablet@ubuntu-phablet:~$ ubuntu-app-launch messaging-app

这样我们就可以启动messaging应用了.

当然,我们也可以启动我们已经安装过得应用:


我们可以通过click list命令来查看所有已经安装的应用.我们可以通过如下的方式来启动该应用

$ ubuntu-app-launch `ubuntu-app-triplet com.ubuntu.camera`



在上面的例子中,我们可以启动camera应用.


作者:UbuntuTouch 发表于2016/7/6 17:33:21 原文链接
阅读:615 评论:0 查看评论

Read more
UbuntuTouch

Canonical公司于最近2016年4月发布了一个新的Ubuntu 16.04系统,并且这个系统是长期支持版(LongTerm Support - LTS).它一如既往地支持debian安装包,但同时它也支持最新的snap安装包.snap安装包是Canonical公司最新发布的一种安装包的格式,它甚至可以在其它的Linux发行版上安装.更多的信息可以在我们的官方开发者网站:https://developer.ubuntu.com/en/desktop/http://snapcraft.io/查看.


1)什么是snap?


一个snap包:
  • 是一个基于squashFS文件系统的文件.它包含应用代码及包含有一个应用特有的叫做snap.yaml的metadata文件.它含有一个只读的文件系统.一旦安装,它会创建一个应用特有可以写的区域,任何其它的应用都不可以访问这个区域
  • 它完全独立于系统.在snap包里,它包含了它可以运行的所有需要的库及runtime(比如python或Java等),并且它可以通过网路更新,同时也可以退回到上一个版本,而不影响系统的其它部分的运行
  • 它是受限的.通过安全机制,它具有沙箱的属性,不可以随意访问外部资源,并和系统的其它部分进行隔离.它可以通过良好设计的安全策略和其它的snap进行交互.


2)16.04桌面支持


如果大家还没自己的16.04的桌面系统,大家可以在地址下载最新的16.04的系统.



从上面的图中,我们可以看出来在16.04的桌面中支持两种格式的安装包:snapdebian.另外我们可以看出,snap包每个安装的应用都是自成一体:每个snap应用包含运行所需要的任何依赖(dependencies);同时我们可以看出每个snap应用都是互相隔离的(请注意OS也是一个snap).和debian包相比较,我们可以看出来每个debian应用的安装依赖于其它包的安装;debian应用之间可以不受限制地互相访问而造成安全问题;删除其中的一个debian应用或包可能导致其它的应用不可以正常运行.相比较而言,不同的snap应用可以安装同样一个软件的不同版本(比如一个安装python 2.7,另外一个应用安装python 3.3)而不造成任何的干扰.从理论上讲,一个snap应用可以安装到任何一个Linux的发行版上,因为它不依赖于操作系统及其发布版本.这对于应用的维护来说是非常好的.

目前在如下的Linux发行版上支持snap包的安装.大家如果有兴趣的话,可以试一下.大家甚至可以直接从源代码编译在它上面运行的snapd环境.



Canonical公司目前正在号召全社区把应用移植成为snap包,并最终把操作系统变为ubuntu core系统,从而打造最安全的操作系统及良好的应用维护.

对于一个All-snap Ubuntu core系统来说(如上面的右图所示),它可以分为两个逻辑部分:

  • 只读的最基本的系统
    • 这部分包括配置文件,标准目录,库,工具及核心的服务(比如network services, libc, systemd及其它).系统的这部分是只读的,里面的每个元素不可以被分别更新.这个被称之为"system-image".在一个系统中,这种image可以达到两个及以上.这些最基本的系统是一种root filesystem的形式出现的.在启动后它们之间可以互相roll back,也即如果一个系统启动有问题,可以自动切换到先前的或指定的系统image去.这个部分也是通过snap打包来实现的.
  • 可写的snap应用及在其之上的架构(framework).它们利用上面的系统所提供的服务达成.

3)安装


为了能够使得在Ubuntu 16.04的系统上运行snap应用,我们必须做一些安装.我们直接使用Ctrl+Alt+T打开terminal:

$ sudo apt update
$ sudo apt install snapd
$ sudo apt install snapcraft build-essential

在我们的Ubuntu 16.04系统中,我们必须打开universe,这样我们可以在以后的开发中安装snapcraft工具了.snapcraft是为了我们能够编译一个snap项目而必须的一个工具,尽管在运行时并不需要.它位于下图所示的universe channel中.这个可以在我们的Ubuntu系统中的设置中进行选择:

  

你也可以通过命令行的方式来添加这个universe的仓库.

在这里,我们简单地介绍一下所使用的术语:
  • snapd:它是一个帮我们管理snap安装,卸载及通过事务性更新(transactional update)的一个环境.同时也帮我对老的版本的snap进行垃圾回收(garbage collection)
  • snapcraft:这是一个帮我们打包一个snap应用的工具.snapcraft.yaml是用来定义如何把一个应用打包为snap包的yaml文件格式.snapcraft工具利用它打包.

然后,你就可以在我们的terminal中安装及运用一个我们所需要的应用:

$ sudo snap install ubuntu-calculator-app
$ ubuntu-calculator-app.calculator

我们可以在我们的电脑的dash中直接运行我们所安装的应用:





如果大家想安装更多的应用的话,可以直接到我们桌面系统的应用商店进行安装:



当一个应用被成功安装以后,我们也可以通过如下的命令来查看:

liuxg@liuxg:~/snappy/desktop/rssreader$ snap list
Name                   Version               Rev  Developer      Notes
hello-world            6.3                   27   canonical      -
hello-world-cli        0.1                   x1                  -
hello-xiaoguo          1.0                   x2                  -
rssreader-app          1.0                   x2                  -
snappy-debug           0.23                  22   canonical      -
telegram-sergiusens    0.9.50                x1                  -
test-license           0.1                   x1                  -
ubuntu-calculator-app  2.1+snap3             5    ubuntucoredev  -
ubuntu-core            16.04+20160531.11-56  122  canonical      -
webcam-webui           1.0                   x1                  -
我们可以从上面看出来所有已经被成功安装过的应用.每个应用被安装后,就有一个自己的Version号码,同时也有一个Rev号码.对于从Ubuntu Store商店里安装后的应用,这个Rev是一个数字号码,比如上面的ubuntu-calculator-app应用的Rev号码是5,单对于其它的不是从商店安装的应用来说,这个号码不是一个数字.

一般来说,我们安装snap应用时在默认的情况下,我们是从stable channel进行安装的.我们可以通过如下的命令从beta/edge channel进行安装:

$ snap install hello --channel-beta

或:

$ snap refresh hello --channel=beta
Name    Version   Rev   Developer   Notes
hello   2.10.1    29    canonical   -
hello  (beta) installed

从上面我们可以看出来,calculator应用也是在里面的.如果大家想知道这个应用是如何实现的,请参考源码

https://code.launchpad.net/~dpm/ubuntu-calendar-app/snap-all-things

细心的开发者也许会发现,这个应用实际上是使用了同样一个和Ubuntu手机一样的代码.没有做任何的改变.从某种意义上讲,Ubuntu实现了真正意义上的融合(Convergence)应用设计.在为了,我们只需要一个应用的snap包,它就可以直接运行于不同屏幕尺寸上,并自动适配屏幕尺寸从而得到最佳的显示效果.比如在我们的另外一个教程中"如何把一个qmake的Ubuntu手机应用打包为一个snap应用",它展示了如何把一个手机的应用转换为一个可以在桌面系统运行的snap应用.

从另外一个角度上讲,这个snap应用时间上可以部署到任何一个支持snap包安装的Linux的发行版上,只要有它支持snap包,并且它将不依赖于操作系统的版本发布.维护性应该是非常好的.

在通常情况下,一个snap应用每天会在后台检查最新的snap版本,并自动安装.当然,我们也可以通过如下的命令来更新我们的snap应用:
$ snap refresh <snap name>
我们也可以通过如下的命令来rollback到以前的版本(从snapd 2.11版本开始支持)
$ snap revert <snap name>

我们怎么通过命令行来查找我们所需要的snap应用呢?

liuxg@liuxg:~$ snap find calculator
Name                   Version    Developer      Notes  Summary
ubuntu-calculator-app  2.1+snap3  ubuntucoredev  -      Ubuntu Calculator application for the Unity 7 desktop

目前find命令只支持搜索在stable channel的应用.我们可以通过上面的命令来查找在商店里应用名字含有calculator的应用.我们可以通过如下的命令来寻找所有在商店里发布的snap应用:

liuxg@liuxg:~$ snap find 
Name                       Version                    Developer             Notes    Summary
ab                         1.0                        snappy-test           -        Test snap with shortest name
ag-mcphail                 1.0.1                      njmcphail             -        The Silver Searcher - mcphail's build and upstream git version
alsa-utils                 1.1.0-1                    woodrow               -        Utilities for configuring and using ALSA
apktool                    2.1.1                      ligboy                -        A tool for reverse engineering 3rd party, closed, binary Android apps.
...

当然,我们也可以通过如下的方法找寻到我们所需要的应用:
liuxg@liuxg:~$ snap find | grep hello
hello                      2.10                       canonical             -        GNU Hello, the "hello world" snap
hello-bluet                0.1                        bluet                 -        Qt Hello World example
hello-huge                 1.0                        noise                 -        a really big snap
hello-snap                 0.01                       muhammad              -        GNU hello-snap, the "Hello, Snap!" snap

4)删除一个snap应用


刚才我们已经成功安装了一个snap应用到我们的桌面系统中.我们现在可以通过如下的命令来删除该应用.我们首先在命令行中显示已经被安装的应用:

liuxg@liuxg:~$ snap list
Name                   Version               Rev  Developer      Notes
hello-world            6.1                   26   canonical      -
rssreader              1.0                   x1                  devmode
rssreader-app          1.0                   x2                  -
snaptest               1                     x1                  devmode
snaptest-app           1                     x3                  devmode
ubuntu-calculator-app  2.1+snap3             5    ubuntucoredev  -
ubuntu-core            16.04+20160531.11-56  122  canonical      -
webcam-webui           1                     x1                  -

在上面,我们看到已经安装了ubuntu-calculator-app应用.我们可以通过如下的方法来删除它.
liuxg@liuxg:~$ sudo snap remove ubuntu-calculator-app
[sudo] password for liuxg: 

Done

重新显示我们已经安装的snap应用列表:

liuxg@liuxg:~$ snap list
Name           Version               Rev  Developer  Notes
hello-world    6.1                   26   canonical  -
rssreader      1.0                   x1              devmode
rssreader-app  1.0                   x2              -
snaptest       1                     x1              devmode
snaptest-app   1                     x3              devmode
ubuntu-core    16.04+20160531.11-56  122  canonical  -
webcam-webui   1                     x1 
显然我们再也找不到ubuntu-calculator-app应用了.


5)在哪里找到安装的文件


当我们把一个应用到我们的系统中后,我们可以通过如下的命令来查看在我们的系统中所安装的所有的snap应用:

liuxg@liuxg:~$ snap list
Name                   Version               Rev  Developer      Notes
hello-world            6.1                   26   canonical      -
rssreader              1.0                   x1                  devmode
rssreader-app          1.0                   x2                  -
snaptest               1                     x1                  devmode
snaptest-app           1                     x3                  devmode
ubuntu-calculator-app  2.1+snap3             5    ubuntucoredev  -
ubuntu-core            16.04+20160531.11-56  122  canonical      -
webcam-webui           1                     x1                  -

对于一些开发者来说,snap的一些命令可能比较陌生.我们可以通过如下的方法来得到帮助:

$ snap --help      # Or use 'snap <command> --help' for help on a specific command

安装好我们的应用后,我们可以在如下的路径找到我们的snap安装文件:

liuxg@liuxg:/var/lib/snapd/snaps$ ls
hello-world_26.snap    rssreader_x1.snap     snaptest-app_x3.snap          ubuntu-core_122.snap
rssreader-app_x1.snap  snaptest-app_x1.snap  snaptest_x1.snap              webcam-webui_x1.snap
rssreader-app_x2.snap  snaptest-app_x2.snap  ubuntu-calculator-app_5.snap
我们可以通过如下的方法查看系统中的mount的情况:
liuxg@liuxg:~$ mount | grep calculator
/var/lib/snapd/snaps/ubuntu-calculator-app_5.snap on /snap/ubuntu-calculator-app/5 type squashfs (ro,relatime)

在我们删除一个snap应用时,实际上就只要删除在系统的这个.snap文件即可.从上面我们可以看出来,实际上我们是把/var/lib/snapd/snaps/ubuntu-calculator-app_5.snap文件通过mount的方法使之可以在/snap/ubuntu-calculator-app/5目录中可以看见.一般来说,一个snap应用在被成功安装后,它位于/snap/$name/$version/目录中.

liuxg@liuxg:/snap/ubuntu-calculator-app/5$ tree -L 2
.
├── bin
│   └── calculator
├── build
│   └── ubuntu-calculator-app
├── command-calculator.wrapper
├── etc
│   ├── apparmor.d
│   ├── dbus-1
│   ├── default
│   ├── drirc
│   ├── fonts
│   ├── gps.conf
│   ├── gss
│   ├── init
│   ├── init.d
│   ├── ldap
│   ├── pki
│   ├── pulse
│   ├── ucf.conf
│   ├── X11
│   └── xdg
├── lib
│   ├── systemd
│   └── x86_64-linux-gnu
├── meta
│   ├── gui
│   └── snap.yaml
├── usr
│   ├── bin
│   ├── lib
│   └── share
└── var
    └── lib

细心的读者也许已经发现,在被mount的目录中的文件就像另外一个Linux的安装文件结构.它实际上是把这个calculator所需要的所有需要的文件安装到同样的一个根目录中,从而摆脱对系统文件的任何需求.理论上讲,我们的应用不会因为系统的升级或版本的变化而造成不能运行的情况.这对于一些软件开发商来说无疑是一个天大的利好!我们今天设计好的软件,在Ubuntu升级为未来的20.4时或其它版本时,我们不需要做任何的修改.这样做有一个非常大的好处就是我们应用的设计完全摆脱了对发行版本的依赖.当然,我们也可以把我们的应用部署到其它的任何一个支持snap包安装的Linux发行版上,它也可以运行得非常好.我们不需要考虑它到底运行的是什么版本的Linux系统及什么版本的发行.

一个snap系统包含一系列的snap应用.每个应用都是独立的,并且都是只读的.每个snap应用都通过下节中描述的interface进行交流.


snap系统将所有的snap应用在/snap/bin中呈现给我们使用.你的系统$PATH中含有这个路径,所有你可以在任何的位置启动你的snap应用:

liuxg@liuxg:/snap/bin$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
当然对于一个snap应用来说,它的snap文件包的大小可能也是非常大的.
-rw-r--r--  1 liuxg liuxg 122M 7月  12 12:00 ubuntu-calculator-app_2.1+snap3_amd64.snap

从上面可以看出来,我们的snap包的大小达到122M.如果我们想查看我们所在包里面的内容,我们可以通过如下的命令来实现:

 $ unsquashfs -l ubuntu-calculator-app_2.1+snap3_amd64.snap | less
squashfs-root
squashfs-root/bin
squashfs-root/bin/calculator
squashfs-root/command-calculator.wrapper
squashfs-root/etc
squashfs-root/etc/X11
squashfs-root/etc/X11/Xreset
squashfs-root/etc/X11/Xreset.d
squashfs-root/etc/X11/Xreset.d/README
squashfs-root/etc/X11/Xresources
squashfs-root/etc/X11/Xresources/x11-common
squashfs-root/etc/X11/Xsession
squashfs-root/etc/X11/Xsession.d
squashfs-root/etc/X11/Xsession.d/20x11-common_process-args
squashfs-root/etc/X11/Xsession.d/30x11-common_xresources
squashfs-root/etc/X11/Xsession.d/35x11-common_xhost-local
squashfs-root/etc/X11/Xsession.d/40x11-common_xsessionrc
squashfs-root/etc/X11/Xsession.d/50x11-common_determine-startup
squashfs-root/etc/X11/Xsession.d/60x11-common_localhost
squashfs-root/etc/X11/Xsession.d/60x11-common_xdg_path
...

我们也可以直接通过如下的命令来得到在snap包中所有的文件:

$ unsquashfs ubuntu-calculator-app_2.1+snap3_amd64.snap
$ cd cd squashfs-root
# Hack hack hack
$ snapcraft snap

我们可以通过最后的命名snapcraft snap来重新打包我们的应用.

我们来看一看我们安装后的应用所占的空间大小:

375M	./ubuntu-calculator-app/

也就是说一个应用安装后的空间大小是350M大小.当然这也依赖于我们所安装的应用类型.针对我们的ubuntu-calculator-app来说,我们在包里把我们所需要的Qt库及其它需要的任何东西都打入到包里面了.对于其它的任何python应用来说,也是同样的,我们可以把python版本所需要的任何库都打入到我们的包里面.我们根本不需要担心它会不会对其它的应用造成任何的影响.

在未来的设计中,我们可以使一些特别的库(比如Qt库)通过content sharing的方法而使得每个应用都可以分享这个库,这样我们可以大大减小我们的商店中的应用的大小.另外对于同样一个publisher来说,我们也可以使用同样的方法而不需要在多个应用中复制多份的共享库.


6)发布我们的应用到商店


我们可以很方便地把我们已经开发好的应用通过"My Apps"发布到我们的应用商店.在上传我们的应用时,我们一定要记得选择"Ubuntu Core"作为商店来上传.




为了上传一个新的snap应用到商店,我们只需要填入我们所需要的metadata信息及上传我们开发的snap文件即可:



如果您是一个设备制造商或运营上,您甚至可以创建属于自己的store.一个简单的制造商店的snap应用可以在地址找到.
我们也可以通过命令行的方式来发布我们的应用:
$ snapcraft login
我们首先通过上面的命令登陆,让后使用如下的命令上传应用:
$ snapcraft register
$ snapcraft upload
在我们上传一个应用时,我们必须先注册一个snap的package名称.具体的操作可以参阅文章"Learn to make a snap"中的Store一节.
最后我们通过如下的命令退出:
$ snapcraft logout




7)受限的snap应用



当一个snap应用被安装后,在运行时,它被置于一个受限的安全的沙箱之中,并且每个应用都是互相隔离的.在默认的情况下,每个snap包中的每个应用都可以互相访问对方,并协同工作.但是,如果它访问其它的不在自己包里的应用或其它资源,它将是受限的.

实现这个安全沙箱的技术叫做AppArmorseccomp及device cgroups.每个应用都有自己的/tmp目录,devpts等.这个受限的设置是由一个叫做snapcraft.yaml的项目文件所定义的.在这个文件中,snap申明它想要访问的资源,系统将会为它生产相应的限制.关于snapcraft.yaml的详细介绍,我们会在以后的文章中逐步介绍.

每个snap应用都有自己受限的文件目录可以访问.我们可以通过安装在store里的hello-world应用来查看这些目录:

$ sudo snap install hello-world
$ hello-world.env | grep SNAP

liuxg@liuxg:~$ hello-world.env | grep SNAP
SNAP_USER_COMMON=/home/liuxg/snap/hello-world/common
SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl:
SNAP_COMMON=/var/snap/hello-world/common
SNAP_USER_DATA=/home/liuxg/snap/hello-world/27
SNAP_DATA=/var/snap/hello-world/27
SNAP_REVISION=27
SNAP_NAME=hello-world
SNAP_ARCH=amd64
SNAP_VERSION=6.3
SNAP=/snap/hello-world/27
从上面可以看出,我们的应用是可以访问上面的$SNAP_COMMON目录,它对于某个snap应用的所有版本都是一样的.$SNAP_USER_DATA 目录里的数据是可以被我们的应用访问的.另外对于$SNAP_DATA里的数据,需要使用sudo才可以访问.一般来说,作为daemon的service的snap应用是可以访问这个目录里的数据的,因为它们具有sudo的权限.

由于在snap系统中,每个应用的运行是受限的.每个snap应用想访问沙箱以外的资源(或者让自己的资源暴露给其它的snap),它必须要使用interface.interface让我们可以分享一个snap的资源,和其它的snap进行交互及访问我们想得到的硬件资源.一个interface定义了两端之间的交互规则.在两端被称之为plug及slot.我们也可以理解slot为提供放(provider),而plug为消费方(consumer).我们可以通过自动或手动的方式来把plug和slot一起联系起来.当然,我们也可以删除这种连接.关于如何手动建立这种连接,请参阅我的文章"WebCam snap应用实例".我们可以使用诸如如下的命令来建立一个手动的interface plug及slot的连接:

$ sudo snap connect webcam-webui:camera ubuntu-core:camera  



有了interface,我们就可以把两个snap应用连接起来(请注意ubuntu core自己也是一个snap).我们可以通过interface来连接系统OS来分享共同的资源或一些服务(service)比如OpenGL.作为一个例子,当我们使用snapcraft来生产我们想要的snap文件时,我们想要我们的snap应用最终能够访问我们用户的$HOME文件目录.我们可以通过如下的命令来查看我们已经存在的plug及slot.

liuxg@liuxg:~$ snap interfaces
Slot                 Plug
:camera              -
:cups-control        -
:firewall-control    -
:gsettings           -
:home                rssreader-app,snaptest-app
:locale-control      -
:log-observe         -
:modem-manager       -
:mount-observe       -
:network             -
:network-bind        webcam-webui
:network-control     -
:network-manager     -
:network-observe     -
:opengl              rssreader-app,snaptest-app,ubuntu-calculator-app
:optical-drive       -
:ppp                 -
:pulseaudio          -
:snapd-control       -
:system-observe      -
:timeserver-control  -
:timezone-control    -
:unity7              rssreader-app,snaptest-app,ubuntu-calculator-app
:x11                 -

上面显示了在我的电脑系统中每个snap应用所定义的plug.在上面的左边显示的所有的slot其实是OS snap (ubuntu-core)的尽管显示的很简捷.右边显示的是每个应用所定义的plug.

关于interfaces的更详细的介绍可以参阅我们的文档"Interfaces".在文章里,它详细地介绍每个plugs.当我们的应用需要访问到我们所需要的资源时,在我们的snapcraft.yaml项目文件中,我们必须申明这个权限,这样我们的应用就可以访问到我们所需要的资源.比如,针对我们的snap,如果我们想要访问$HOME目录时,我们可以在snapcraft.yaml中这样定义:

name: foo
apps:
  bar:
    command: bin/bar
    plugs: [ home, unity7 ] 

在这里,home及unity7和ubuntu core直接的连接是自动完成的.我们可以在文档Snaps interfaces看到所有的interface及它们是否可以自动连接.有了这样的plugs的定以后,这样我们的snap应用就可以访问到$HOME目录了.否则我们就可能在/var/log/syslog文件中发现denied错误信息.更多关于安全的介绍可以参阅文章"Snap security policy and sandboxing".

另外我们值得指出的是:如果我们想我们的应用还是像我们以前在ubuntu的桌面上运行而不受snap安全机制的限制,我们可以使用如下的命令来安装我们的应用:

$ sudo snap install <package.snap> --devmode

就像上面指出的那样,这是一种在developer mode下的开发.它可以让开发者在起始开发应用时放开安全问题(不受限制)大胆开发.在发布应用时,我们再进行安全的调试.更多这方面的介绍,我们可以参阅文章"Learn to make a snap"或文章"helloworld Snap例程".

我们必须注意的是

Snaps can be uploaded to the edge and beta channels only

关于snapcraft.yaml的知识,我们会在以后的章节中详细介绍,所以大家先不要着急!
虽然每个应用在每次的安装的过程中(比如在upgrade时),都会生产一个新的版本的文件目录,但是有些文件数据在不同的版本之间是共同的,它们在不同的版本运行时是不会改变的的,比如如下的这些目录:

/var/snap/<name>/current/  ← $SNAP_DATA is the versioned snap data directory
/var/snap/<name>/common/   ← $SNAP_COMMON will not be versioned on upgrades

总结上面所说的,每个应用和OS及其它应用直接的交互是通过如下的方式进行的:



另外,我们可以使用$ snap interfaces的如下命令得到更多的信息:

$ snap interfaces <snap> to find the slots offered and plugs used by the specified snap.
$ snap interfaces <snap>:<slot or plug> for details of only the specified slot or plug.
$ snap interfaces -i=<interface> [<snap>] to get a filtered list of plugs and/or slots.
比如,我们使用如下的命令可以得到该应用的所有的plugs:
liuxg@liuxg:~$ snap interfaces telegram-sergiusens
Slot           Plug
:home          telegram-sergiusens
:network       telegram-sergiusens
:network-bind  telegram-sergiusens
:unity7        telegram-sergiusens
当一个应用被更新后,他先前版本的所有的writable区域里的数据(SNAP_USER_DATA及SNAP_DATA)将被自动拷入新的版本的应用中,并被新的版本所使用.







7)如何得到snap帮助


就想我们上面所写的那样,我们可以通过如下命令来得到snap的帮助:

liuxg@liuxg:~$ snap --help
Usage:
  snap [OPTIONS] <command>

The snap tool interacts with the snapd daemon to control the snappy software platform.


Application Options:
      --version  print the version and exit

Help Options:
  -h, --help     Show this help message

Available commands:
  abort        Abort a pending change
  ack          Adds an assertion to the system
  change       List a change's tasks
  changes      List system changes
  connect      Connects a plug to a slot
  create-user  Creates a local system user
  disconnect   Disconnects a plug from a slot
  find         Finds packages to install
  help         Help
  install      Install a snap to the system
  interfaces   Lists interfaces in the system
  known        Shows known assertions of the provided type
  list         List installed snaps
  login        Authenticates on snapd and the store
  logout       Log out of the store
  refresh      Refresh a snap in the system
  remove       Remove a snap from the system
  run          Run the given snap command
  try          Try an unpacked snap in the system

针对每个snap下面的命令,我们可以通过如下的方式来得它的帮助信息:

liuxg@liuxg:~$ snap install -h
Usage:
  snap [OPTIONS] install [install-OPTIONS] <snap>

The install command installs the named snap in the system.

Application Options:
      --version        print the version and exit

Help Options:
  -h, --help           Show this help message

[install command options]
          --channel=   Use this channel instead of stable
          --edge       Install from the edge channel
          --beta       Install from the beta channel
          --candidate  Install from the candidate channel
          --stable     Install from the stable channel
          --devmode    Install the snap with non-enforcing security

通过上面的方法,我们可以对snap命令有更深的理解.
如果大家对开发snap应用感兴趣,但是希望得到别人的帮助,大家可以向snapcraft@lists.snapcraft.io Mailinglist发邮件来参入讨论.同时也可以通过如下的在freenode上的Snappy channel来参入讨论.我们有很多的专家及社区的牛人帮你回答你的问题.另外,我们也可以在AskUbuntu上提出我们的问题.如果大家对参加我们的playpen开发,可以在gitter上参入我们的讨论并交流.更多交流渠道,请参阅我们的连接:https://developer.ubuntu.com/en/snappy/support/




8)如编译一个snap应用


如果大家已经有一个snap的项目,你只需要:
  • 安装snapcraft.请参阅文章的开始部分
  • 在项目的根目录下,直接键入"snapcraft"即可.在项目的根目录下通常含有snapcraft.yaml文件或.snapcraft.yaml文件.
Canonical公司已经号召很多的全球开发者开发snap应用.我们已经把已经开发好的应用放在如下的仓库里了.如果大家对这个感兴趣,请安装如下的指令来下载这些应用作为参考:

$ git clone https://github.com/ubuntu/snappy-playpen.git
$ cd snappy-playpen

目前已经有如下的项目可以供我们参考:

atom/               idea/                openttd/         tinyproxy/
cloudfoundry-cli/   imagemagick-edge/    plank/           tyrant-unleashed-optimizer/
consul/             imagemagick-stable/  qcomicbook/      ubuntu-clock-app/
dcos-cli/           keepassx/            qdriverstation/  ubuntukylin-icon-theme/
deis-workflow-cli/  kpcli/               ristretto/       vault/
dosbox/             leafpad/             scummvm/         vlc/
ffmpeg/             minetest/            shotwell/        wallpaperdownloader/
galculator/         moon-buggy/          smplayer/        youtube-dl/
gitter-im/          mpv/                 snap-template/
heroku/             openjdk-demo/        snaptest/

我们可以直接进入到每个项目的根目录下,键入如下的命令即可:
$ snapcraft
当项目被成功编译完后,我们可以直接在项目的根目录下找到一个扩展名为.snap的文件.这就是我们所需要的snap安装文件.我们可以参照我们上面讲述的方法来安装这个应用.

如果想清除一个snap应用在编译过程中的文件,我们可以打入如下的命令:

$ snapcraft clean

更多关于snapcraft的知识可以参阅它的帮助:

liuxg@liuxg:~$ snapcraft --help
snapcraft

Usage:
 ...

The available commands are:
  help         Obtain help for a certain plugin or topic
  init         Initialize a snapcraft project.
  list-plugins List the available plugins that handle different types of part.
  login        Authenticate session against Ubuntu One SSO.
  logout       Clear session credentials.
  register     Register the package name in the store.
  tour         Setup the snapcraft examples tour in the specified directory,
               or ./snapcraft-tour/.
  upload       Upload a snap to the Ubuntu Store.

The available lifecycle commands are:
  clean        Remove content - cleans downloads, builds or install artifacts.
  cleanbuild   Create a snap using a clean environment managed by lxd.
  pull         Download or retrieve artifacts defined for a part.
  build        Build artifacts defined for a part. Build systems capable of
               running parallel build jobs will do so unless
               "--no-parallel-build" is specified.
  stage        Stage the part's built artifacts into the common staging area.
  prime        Final copy and preparation for the snap.
  snap         Create a snap.

Parts ecosystem commands
  update       Updates the parts listing from the cloud.
  define       Shows the definition for the cloud part.
  search       Searches the remotes part cache for matching parts.

Calling snapcraft without a COMMAND will default to 'snap'

在snapcraft打包的过程中,它经历如下的几个阶段:

  pull         Download or retrieve artifacts defined for a part.
  build        Build artifacts defined for a part. Build systems capable of
               running parallel build jobs will do so unless
               "--no-parallel-build" is specified.
  stage        Stage the part's built artifacts into the common staging area.
  prime        Final copy and preparation for the snap.
  snap         Create a snap.

我们可以通过snapcraft来对每个阶段分别处理来查看每一步到底做什么.比如"snapcraft pull"等.打包的顺序是按照上面所列举的顺序执行的.更多关于如何打包的过程请参阅连接http://snapcraft.io/create/

如果大家对如何开发一个Ubuntu桌面的应用感兴趣的话,可以参阅我的文章"如何把一个qmake的Ubuntu手机应用打包为一个snap应用"或"helloworld Snap例程".



9)如何运行一个snap应用


我们可以在Dash中运行我们的应用,同时,我们也可以在terminal中通过命令行的方式来启动我们的应用.在我们设计我们的snap应用时,通常我们使用一个叫做snapcraft.yaml的项目文件.我们现在来用一个例子来说明:

snapcraft.yaml

name: snaptest-app
version: 1
summary: This is a summary
description: This is the description
confinement: devmode

apps:
  test:
    command: desktop-launch $SNAP/lib/x86_64-linux-gnu/bin/snaptest
    plugs: [home,unity7,opengl]

parts:
  application:
    source: ./src
    plugin: qmake
    qt-version: qt5
    build-packages:
      - cmake
      - gettext
      - intltool
      - ubuntu-touch-sounds
      - suru-icon-theme
      - qml-module-qttest
      - qml-module-qtsysteminfo
      - qml-module-qt-labs-settings
      - qtdeclarative5-u1db1.0
      - qtdeclarative5-qtmultimedia-plugin
      - qtdeclarative5-qtpositioning-plugin
      - qtdeclarative5-ubuntu-content1
      - qt5-default
      - qtbase5-dev
      - qtdeclarative5-dev
      - qtdeclarative5-dev-tools
      - qtdeclarative5-folderlistmodel-plugin
      - qtdeclarative5-ubuntu-ui-toolkit-plugin
      - xvfb
    stage-packages:
      - ubuntu-sdk-libs
      - qtubuntu-desktop
      - qml-module-qtsysteminfo
    stage:
      - -usr/share/pkgconfig/xkeyboard-config.pc       
    snap:
      - -usr/share/doc
      - -usr/include
    after: [desktop/qt5]

在上面的snapcraft.yaml文件中,我们的包的名字叫做"snaptest-app".在apps下定义了一个应用叫做"test".那么在命令行中,我们可以通过如下的方式来运行我们的最终的应用:

$ snaptest-app.test

也就是说,我们可以通过<<package-name>>.<<application-name>>来启动我们的应用.如果大家想对snapcraft有更多的了解,请参阅我们录制的视频"Snapcraft操作演示--教你如何snap一个应用

特别值得指出的是:如果一个snap的包名和应用的名称是完全一致的,那么你可以直接打入包名来运行这个应用.比如:

hello-world:

$ cat meta/snap.yaml
name: hello-world
version: 6.1
architectures: [ all ]
summary: "The 'hello-world' of snaps"
description: |
    This is a simple snap example that includes a few interesting binaries
    to demonstrate snaps and their confinement.
    * hello-world.env  - dump the env of commands run inside app sandbox
    * hello-world.evil - show how snappy sandboxes binaries
    * hello-world.sh   - enter interactive shell that runs in app sandbox
    * hello-world      - simply output text
apps:
  env:
    command: bin/env
  evil:
    command: bin/evil
  sh:
    command: bin/sh
  hello-world:
    command: bin/echo

从上面我们可以看出来,包名和应用的名称都是hello-world,那么我们可以直接使用hello-world来运行这个应用.对于其它的应用来说,我们必须使用诸如hhello-world.env来运行.


10)如何让我们的系统恢复到没有snap安装的起始状态


我们在开发snap的过程中,我们发现经常可能的情况是一个snap占用太多的空间.加之如果有多个版本被安装的话(每次安装都会生产一个新的版本),那么我们硬盘的空间占用将会变得很大.如果我们去手动去除这些版本的话,非常麻烦.我们可能需要用到umount,并且容易造成如下目录中的文件:

/var/lib/snapd/state.json

的破损,以至于我们在正常使用snap list命令不能产生我们想要的结果.那么我们如何处理这个问题呢?
我们可以在如下的地址获取我们想要的脚本:

git clone https://github.com/zyga/devtools/

让后运行如下下载的命令:

$ sudo ./reset-state

我们按照命令所指出的提示进行操作.这样我们就可以把我之前所有已经安装过的snap应用及环境都删除.我们的系统恢复到原来没有安装过任何snap的状态!


11)如何启动或取消一个已经安装的snap


假如说有一天我不想运行我已经安装的一个snap应用,但是我也不想把它从系统中删除.又或者我想我的service能够重新启动(对于一些daemon应用).我们可以通过如下的方法:

liuxg@liuxg:~$ snap list
Name           Version  Rev  Developer  Notes
hello-xiaoguo  1.0      x1              -
ubuntu-core    16.04.1  423  canonical  -
webcamhtml     0.1      x1              devmode
liuxg@liuxg:~$ sudo snap disable hello-xiaoguo
hello-xiaoguo disabled
liuxg@liuxg:~$ snap list
Name           Version  Rev  Developer  Notes
hello-xiaoguo  1.0      x1              disabled
ubuntu-core    16.04.1  423  canonical  -
webcamhtml     0.1      x1              devmode
liuxg@liuxg:~$ hello-xiaoguo.env
hello-xiaoguo.env: command not found
liuxg@liuxg:~$ sudo snap enable hello-xiaoguo
hello-xiaoguo enabled
liuxg@liuxg:~$ snap list
Name           Version  Rev  Developer  Notes
hello-xiaoguo  1.0      x1              -
ubuntu-core    16.04.1  423  canonical  -
webcamhtml     0.1      x1              devmode
liuxg@liuxg:~$ hello-xiaoguo.env
runtime/cgo: LC_PAPER=zh_CN.UTF-8

这里,我们可以看到我们可以通过snap disable/enable的方法来把我们的snap进行启动或取消的动作.



12) 在哪里可以下载Ubuntu Core Image


Ubuntu Core 的Image可以在如下的地址下载:


正式发布版本:

关于如何把自己的板子刷成所需要的Ubuntu Core,可以参阅文章"如何为树莓派安装Ubuntu Core并在Snap系统中进行编译".

13)更多的学习资源


我们可以通过如下的方法得到我们需要的例程:

$ sudo apt update
$ sudo apt install snapd snapcraft snapcraft-examples

当然我们也可以直接把snapcraft源码下载下来:

$ git clone https://github.com/snapcore/snapcraft
我们可以在snapcraft的目录下的"demos"目录下找到所有的例程.我们可以从这些例程中学习如何使用snapcraft来打包我们的应用.

我们也可以通过如下的方式得到我们已经开发好的应用的源码:

$ git clone https://github.com/ubuntu/snappy-playpen

snapcraft tour:
$ snapcraft tour
Snapcraft tour initialized in ./snapcraft-tour/
Instructions are in the README, or http://snapcraft.io/create/#tour

最后你也可以安装我们的snap codelabs来学习我们的教程:

$ sudo snap install snap-codelabs

安装后可以在你的浏览器中直接键入如下的地址即可:

http://localhost:8123/ 


我们可以在我们的home里的snapcraft-tour目录中找到我们所需要学习的例程.


我们可以利用这些资源来参考学习所有已经有的应用.

大家如果有兴趣的话,也可以参考我录制的视频资料"Ubuntu Core 介绍(视频)".

作者:UbuntuTouch 发表于2016/7/12 11:06:39 原文链接
阅读:1508 评论:0 查看评论

Read more