在我们编译打包snap应用时,我们时常会发现在我们的代码或snapcraft.yaml中每次做一次小的改动后,重新运行snapcraft命令时,都会从Ubuntu archive中重新下载所需要的包.如果一个包很大的话,这需要很长的时间才可以完成.如果是在Desktop的情况下,我们有时可以使用VPN来解决这个问题.这种情况特别是发生在我们需要使用ARM板子进行编译打包的时候,因为我在这些板子上甚至不能运行VPN.那么我们如何来解决这个问题呢?
很幸运的是,我们的同事ogra帮我们设计了一个叫做packageproxy的snap包.我们可以通过如下的命令来安装:
$ sudo snap install packageproxy
安装完后,我们可以通过snap list来发现:
liu-xiao-guo@localhost:~$ snap list
Name Version Rev Developer Notes
classic 16.04 17 canonical devmode
core 16.04.1 716 canonical -
grovepi-server 1.0 x1 devmode
packageproxy 0.1 3 ogra -
pi2 16.04-0.17 29 canonical -
pi2-kernel 4.4.0-1030-3 22 canonical -
在我们的ARM板子,比如树莓派中,我们通过安装classic应用,进入到classic的环境中:
$ sudo snap install classic --devmode --edge
$ sudo classic
当然具体的步骤,我们可以参照文章"如何为树莓派安装Ubuntu Core并在Snap系统中进行编译"(http://www.linuxdiyf.com/linux/23642.html).在进入到我们的classic环境后,我们需要多/etc/apt中的sources.list文件进行修改.为了保险起见,我们首先可以通过如下的命令来保存原先的sources.list文件
(classic)liu-xiao-guo@localhost:/etc/apt$ sudo cp sources.list sources.list.bak
这样以前的文件被保存于sources.list.bak文件中.如果我们打开sources.list文件,我们可以看见它的内容如下:
sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial main restricted
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-updates main restricted
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial universe
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial universe
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-updates universe
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-updates multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-backports main restricted universe multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security main restricted
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-security main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security universe
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-security universe
deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-security multiverse
显然,在上面的文件中,所有的源都指向http://ports.ubuntu.com/ubuntu-ports/.也就是说每次我们重新编译我们的snap应用时,它都会从上面的地址进行下载.如果一个包很大的话,它就会造成我们的编译的时间过长.这显然不是我们所期望的.如果,我们把上面的http://ports.ubuntu.com/ubuntu-ports/换成http://localhost:9999/ubuntu-ports/,那么整个sources.list文件的内容如下:
sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://localhost:9999/ubuntu-ports/ xenial main restricted
# deb-src http://localhost:9999/ubuntu-ports/ xenial main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://localhost:9999/ubuntu-ports/ xenial-updates main restricted
# deb-src http://localhost:9999/ubuntu-ports/ xenial-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://localhost:9999/ubuntu-ports/ xenial universe
# deb-src http://localhost:9999/ubuntu-ports/ xenial universe
deb http://localhost:9999/ubuntu-ports/ xenial-updates universe
# deb-src http://localhost:9999/ubuntu-ports/ xenial-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://localhost:9999/ubuntu-ports/ xenial multiverse
# deb-src http://localhost:9999/ubuntu-ports/ xenial multiverse
deb http://localhost:9999/ubuntu-ports/ xenial-updates multiverse
# deb-src http://localhost:9999/ubuntu-ports/ xenial-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://localhost:9999/ubuntu-ports/ xenial-backports main restricted universe multiverse
# deb-src http://localhost:9999/ubuntu-ports/ xenial-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://localhost:9999/ubuntu-ports/ xenial-security main restricted
# deb-src http://localhost:9999/ubuntu-ports/ xenial-security main restricted
deb http://localhost:9999/ubuntu-ports/ xenial-security universe
# deb-src http://localhost:9999/ubuntu-ports/ xenial-security universe
deb http://localhost:9999/ubuntu-ports/ xenial-security multiverse
# deb-src http://localhost:9999/ubuntu-ports/ xenial-security multiverse
也就是说,每当我们重新下载我们的包的时候,它将从我们的本地地址http://localhost:9999/ubuntu-ports/进行下载.
如果这个包曾经被下载过,那么packageproxy将帮我们从本地的cache中直接提取,从而不需要重新下载
如果这个包从来没有被现在过,那么packageproxy将帮助我们从网上进行下载,并保存于本地以备以后重复使用
为了能够在命令行中进行修改sources.list文件,我们可以使用如下的命令:
sudo sed -i 's/http:\/\/ports.ubuntu.com\/ubuntu-ports/http:\/\/localhost:9999\/ubuntu-ports/g' /etc/apt/sources.list
这显然是一种非常好的方法.在第一编译的时候,它可能需要一些时间.但是以后的编译,它可以直接从本地提取从而加速我们的编译的速度.
在我们设置完上面的步骤后,我们可以通过如下的命令来进行系统的更新及安装:
$ sudo apt-get update
$ sudo apt install snapcraft git-core build-essential
这样我们就安装好我们的编译的环境了.通过这样的配置过后,在第一次编译我的应用时,如果需要的包从来没有下载过,就会慢一些.第二次编译我们的应用时,就会发现速度快很多.当然,我们可以把我们的地址指向某一个设备的IP地址,而不使用localhost,从而使大家从同一个设备中提取所需要的包.这种方法适合于网路环境比较差的时候.特别适合一些hackathon活动.作为一个展示的例子http://paste.ubuntu.com/23789982/,我们可以看到在clean项目后,编译的速度大大提高了.
如果在使用过程中,出现如下的错误:
(classic)liu-xiao-guo@localhost:~$ sudo apt-get update
Err:1 http://localhost:9999/ubuntu-ports xenial InRelease
Could not connect to localhost:9999 (127.0.0.1). - connect (111: Connection refused) [IP: 127.0.0.1 9999]
Err:2 http://localhost:9999/ubuntu-ports xenial-updates InRelease
Unable to connect to localhost:9999: [IP: 127.0.0.1 9999]
Err:3 http://localhost:9999/ubuntu-ports xenial-backports InRelease
Unable to connect to localhost:9999: [IP: 127.0.0.1 9999]
Err:4 http://localhost:9999/ubuntu-ports xenial-security InRelease
Unable to connect to localhost:9999: [IP: 127.0.0.1 9999]
这种情况可能是由于packageproxy在运行时出现一些问题,我们可以通过删除在如下地址下的文件来解决:
liu-xiao-guo@localhost:/var/snap/packageproxy/3$ ls
approx.conf config.yaml hosts.allow hosts.deny lockfile.lock var
我们可以删除上面的lockfile.lock来解决这个问题.
另外,这种方法也适合在Ubuntu Desktop下的snap编译打包,我们只需要把上面的"ubuntu-ports"修改为"ubuntu"即可.这个练习就留给开发者.
如果大家想删除所有已经下载的包以减少存储空间:
使用snap remove packageproxy命令来删除这个应用
删除rm -rf /var/snap/packageproxy/3/var/cache/approx/*所有的文件