Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This new command's intention is to make building with a clean slate and easy chore. LP: #1480144 Signed-off-by: Sergio Schvezov <sergio.schvezov@ubuntu.com>
- Loading branch information
1 parent
0adb5cc
commit 5e527fb
Showing
15 changed files
with
463 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/python3 | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright (C) 2015, 2016 Canonical Ltd | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
""" | ||
snapcraft cleanbuild | ||
Creates a lxd container to build the snap. | ||
This is a way to guarantee that the snapcraft.yaml used with associated | ||
local sources is not using any dependencies local to the developer system. | ||
The cleanbuild command requires a properly setup lxd environment that | ||
can connect to external networks. Refer to the "Ubuntu Desktop and | ||
Ubuntu Server" section on | ||
https://linuxcontainers.org/lxd/getting-started-cli | ||
to get started. | ||
Usage: | ||
cleanbuild [options] | ||
Options: | ||
-h --help show this help message and exit. | ||
""" | ||
|
||
import logging | ||
import os.path | ||
import tarfile | ||
|
||
from docopt import docopt | ||
|
||
from snapcraft import repo | ||
from snapcraft.lxd import Cleanbuilder | ||
from snapcraft.common import format_snap_name | ||
|
||
from snapcraft.yaml import load_config | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _create_tar_filter(tar_filename): | ||
def _tar_filter(tarinfo): | ||
fn = tarinfo.name | ||
if fn.startswith('./parts/') and not fn.startswith('./parts/plugins'): | ||
return None | ||
elif fn in ('./stage', './snap', tar_filename): | ||
return None | ||
elif fn.endswith('.snap'): | ||
return None | ||
return tarinfo | ||
return _tar_filter | ||
|
||
|
||
def main(argv=None): | ||
argv = [] if argv is None else argv | ||
docopt(__doc__, argv=argv) | ||
|
||
if not repo.is_package_installed('lxd'): | ||
raise EnvironmentError( | ||
'The lxd package is not installed, in order to use `cleanbuild` ' | ||
'you must install lxd onto your system. Refer to the ' | ||
'"Ubuntu Desktop and Ubuntu Server" section on ' | ||
'https://linuxcontainers.org/lxd/getting-started-cli/' | ||
'#ubuntu-desktop-and-ubuntu-server to enable a proper setup.') | ||
|
||
config = load_config() | ||
tar_filename = '{}_{}_source.tar.bz2'.format( | ||
config.data['name'], config.data['version']) | ||
|
||
with tarfile.open(tar_filename, 'w:bz2') as t: | ||
t.add(os.path.curdir, filter=_create_tar_filter(tar_filename)) | ||
|
||
snap_filename = format_snap_name(config.data) | ||
Cleanbuilder(snap_filename, tar_filename).execute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/python3 | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright (C) 2016 Canonical Ltd | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import logging | ||
import os | ||
from contextlib import contextmanager | ||
from subprocess import check_call, CalledProcessError | ||
from time import sleep | ||
|
||
import petname | ||
|
||
from snapcraft.common import get_arch | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_DEFAULT_IMAGE_SERVER = 'https://images.linuxcontainers.org:8443' | ||
_NETWORK_PROBE_COMMAND = \ | ||
'import urllib.request; urllib.request.urlopen("{}", timeout=5)'.format( | ||
'http://start.ubuntu.com/connectivity-check.html') | ||
_PROXY_KEYS = ['http_proxy', 'https_proxy', 'no_proxy', 'ftp_proxy'] | ||
|
||
|
||
class Cleanbuilder: | ||
|
||
def __init__(self, snap_output, project, server=_DEFAULT_IMAGE_SERVER): | ||
self._snap_output = snap_output | ||
self._project = project | ||
self._container_name = 'snapcraft-{}'.format( | ||
petname.Generate(3, '-')) | ||
self._server = server | ||
|
||
def _push_file(self, src, dst): | ||
check_call(['lxc', 'file', 'push', | ||
src, '{}/{}'.format(self._container_name, dst)]) | ||
|
||
def _pull_file(self, src, dst): | ||
check_call(['lxc', 'file', 'pull', | ||
'{}/{}'.format(self._container_name, src), dst]) | ||
|
||
def _container_run(self, cmd): | ||
check_call(['lxc', 'exec', self._container_name, '--'] + cmd) | ||
|
||
@contextmanager | ||
def _create_container(self): | ||
try: | ||
remote_tmp = petname.Generate(2, '-') | ||
check_call(['lxc', 'remote', 'add', remote_tmp, self._server]) | ||
check_call(['lxc', 'launch', | ||
'{}:ubuntu/xenial/{}'.format(remote_tmp, get_arch()), | ||
self._container_name]) | ||
yield | ||
finally: | ||
check_call(['lxc', 'stop', self._container_name]) | ||
check_call(['lxc', 'remote', 'remove', remote_tmp]) | ||
|
||
def execute(self): | ||
with self._create_container(): | ||
self._setup_project() | ||
self._wait_for_network() | ||
self._container_run(['apt-get', 'update']) | ||
self._container_run(['apt-get', 'install', 'snapcraft', '-y']) | ||
self._container_run( | ||
['snapcraft', 'snap', '--output', self._snap_output]) | ||
self._pull_snap() | ||
|
||
def _setup_project(self): | ||
logger.info('Setting up container with project assets') | ||
dst = os.path.join('/root', os.path.basename(self._project)) | ||
self._push_file(self._project, dst) | ||
self._container_run(['tar', 'xvf', dst]) | ||
|
||
def _pull_snap(self): | ||
src = os.path.join('/root', self._snap_output) | ||
self._pull_file(src, self._snap_output) | ||
logger.info('Retrieved {}'.format(self._snap_output)) | ||
|
||
def _wait_for_network(self): | ||
logger.info('Waiting for a network connection...') | ||
not_connected = True | ||
retry_count = 5 | ||
while not_connected: | ||
sleep(5) | ||
try: | ||
self._container_run(['python3', '-c', _NETWORK_PROBE_COMMAND]) | ||
not_connected = False | ||
except CalledProcessError as e: | ||
retry_count -= 1 | ||
if retry_count == 0: | ||
raise e | ||
logger.info('Network connection established') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.