Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lxd: Setup image and target arch for cross-compilation #1286

Merged
merged 3 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions snapcraft/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ def _set_machine(self, target_deb_arch):
self.__machine_info = _ARCH_TRANSLATIONS[self.__target_machine]


def _get_deb_arch(machine):
return _ARCH_TRANSLATIONS[machine].get('deb', None)


def _find_machine(deb_arch):
for machine in _ARCH_TRANSLATIONS:
if _ARCH_TRANSLATIONS[machine].get('deb', '') == deb_arch:
Expand Down
25 changes: 19 additions & 6 deletions snapcraft/internal/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
from time import sleep

import petname
import yaml

from snapcraft.internal.errors import SnapcraftEnvironmentError
from snapcraft.internal import common
from snapcraft._options import _get_deb_arch

logger = logging.getLogger(__name__)

Expand All @@ -52,6 +54,19 @@ def __init__(self, *, output, source, project_options,
remote = _get_default_remote()
_verify_remote(remote)
self._container_name = '{}:snapcraft-{}'.format(remote, container_name)
# Use the server architecture to avoid emulation overhead
kernel = self._get_remote_info()['environment']['kernel_architecture']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kalikiana this causes a regresion on xenial: https://bugs.launchpad.net/snapcraft/+bug/1689712
I've assigned the bug to you.

deb_arch = _get_deb_arch(kernel)
if not deb_arch:
raise SnapcraftEnvironmentError(
'Unrecognized server architecture {}'.format(kernel))
self._host_arch = deb_arch
self._image = 'ubuntu:xenial/{}'.format(deb_arch)

def _get_remote_info(self):
remote = self._container_name.split(':')[0]
return yaml.load(check_output([
'lxc', 'info', '{}:'.format(remote)]).decode())

def _push_file(self, src, dst):
check_call(['lxc', 'file', 'push',
Expand All @@ -68,9 +83,7 @@ def _container_run(self, cmd):

def _ensure_container(self):
check_call([
'lxc', 'launch', '-e',
'ubuntu:xenial/{}'.format(self._project_options.deb_arch),
self._container_name])
'lxc', 'launch', '-e', self._image, self._container_name])
check_call([
'lxc', 'config', 'set', self._container_name,
'environment.SNAPCRAFT_SETUP_CORE', '1'])
Expand All @@ -94,6 +107,8 @@ def execute(self, step='snap', args=None):
command = ['snapcraft', step]
if step == 'snap':
command += ['--output', self._snap_output]
if self._host_arch != self._project_options.deb_arch:
command += ['--target-arch', self._project_options.deb_arch]
if args:
command += args
try:
Expand Down Expand Up @@ -166,9 +181,7 @@ def _get_container_status(self):
def _ensure_container(self):
if not self._get_container_status():
check_call([
'lxc', 'init',
'ubuntu:xenial/{}'.format(self._project_options.deb_arch),
self._container_name])
'lxc', 'init', self._image, self._container_name])
check_call([
'lxc', 'config', 'set', self._container_name,
'environment.SNAPCRAFT_SETUP_CORE', '1'])
Expand Down
5 changes: 5 additions & 0 deletions snapcraft/tests/fixture_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ def call_effect(*args, **kwargs):
return 'local'.encode('utf-8')
elif args[0] == ['lxc', 'list', 'my-remote:'] and fail_on_remote:
raise CalledProcessError(returncode=255, cmd=args[0])
elif args[0][:2] == ['lxc', 'info']:
return '''
environment:
kernel_architecture: x86_64
'''.encode('utf-8')
elif args[0][:3] == ['lxc', 'list', '--format=json']:
return '''
[{"name": "snapcraft-snap-test",
Expand Down
35 changes: 21 additions & 14 deletions snapcraft/tests/test_lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,40 @@
class LXDTestCase(tests.TestCase):

scenarios = [
('local', dict(remote='local')),
('remote', dict(remote='my-remote')),
('local', dict(remote='local', target_arch=None)),
('remote', dict(remote='my-remote', target_arch=None)),
('cross', dict(remote='local', target_arch='armhf')),
]

@patch('petname.Generate')
def test_cleanbuild(self, mock_pet):
@patch('platform.machine')
@patch('platform.architecture')
def test_cleanbuild(self, mock_arch, mock_machine, mock_pet):
fake_lxd = tests.fixture_setup.FakeLXD()
self.useFixture(fake_lxd)
fake_logger = fixtures.FakeLogger(level=logging.INFO)
self.useFixture(fake_logger)

mock_pet.return_value = 'my-pet'

project_options = ProjectOptions()
mock_arch.return_value = ('64bit', 'ELF')
mock_machine.return_value = 'x86_64'
project_options = ProjectOptions(target_deb_arch=self.target_arch)
metadata = {'name': 'project'}
project_folder = 'build_project'
lxd.Cleanbuilder(output='snap.snap', source='project.tar',
metadata=metadata, remote=self.remote,
project_options=project_options).execute()
expected_arch = project_options.deb_arch

self.assertEqual(
'Setting up container with project assets\n'
'Waiting for a network connection...\n'
'Network connection established\n'
'Retrieved snap.snap\n',
fake_logger.output)
expected_arch = 'amd64'

self.assertIn('Setting up container with project assets\n'
'Waiting for a network connection...\n'
'Network connection established\n'
'Retrieved snap.snap\n', fake_logger.output)
args = []
if self.target_arch:
self.assertIn('Setting target machine to \'{}\'\n'.format(
self.target_arch), fake_logger.output)
args += ['--target-arch', self.target_arch]

container_name = '{}:snapcraft-my-pet'.format(self.remote)
fake_lxd.check_call_mock.assert_has_calls([
Expand Down Expand Up @@ -90,7 +97,7 @@ def test_cleanbuild(self, mock_pet):
'apt-get', 'install', 'snapcraft', '-y']),
call(['lxc', 'exec', container_name,
'--env', 'HOME=/{}'.format(project_folder), '--',
'snapcraft', 'snap', '--output', 'snap.snap']),
'snapcraft', 'snap', '--output', 'snap.snap', *args]),
call(['lxc', 'file', 'pull',
'{}/{}/snap.snap'.format(container_name, project_folder),
'snap.snap']),
Expand Down