Ejemplo n.º 1
0
    def __init__(self, *, output, source, project_options,
                 metadata, container_name, remote=None):
        if not output:
            output = common.format_snap_name(metadata)
        self._snap_output = output
        self._source = os.path.realpath(source)
        self._project_options = project_options
        self._metadata = metadata
        self._project_folder = '/root/build_{}'.format(metadata['name'])

        if not remote:
            remote = _get_default_remote()
        _verify_remote(remote)
        self._container_name = '{}:snapcraft-{}'.format(remote, container_name)
        server_environment = self._get_remote_info()['environment']
        # Use the server architecture to avoid emulation overhead
        try:
            kernel = server_environment['kernel_architecture']
        except KeyError:
            kernel = server_environment['kernelarchitecture']
        self._server_arch = _get_deb_arch(kernel)
        if not self._server_arch:
            raise ContainerConnectionError(
                'Unrecognized server architecture {}'.format(kernel))
        self._image = 'ubuntu:xenial/{}'.format(self._server_arch)
        # Use a temporary folder the 'lxd' snap can access
        lxd_common_dir = os.path.expanduser(
            os.path.join('~', 'snap', 'lxd', 'common'))
        os.makedirs(lxd_common_dir, exist_ok=True)
        self.tmp_dir = tempfile.mkdtemp(prefix='snapcraft', dir=lxd_common_dir)
Ejemplo n.º 2
0
    def __init__(self,
                 *,
                 output,
                 source,
                 project_options,
                 metadata,
                 container_name,
                 remote=None):
        if not output:
            output = common.format_snap_name(metadata)
        self._snap_output = output
        self._source = os.path.realpath(source)
        self._project_options = project_options
        self._metadata = metadata
        self._project_folder = 'build_{}'.format(metadata['name'])

        if not remote:
            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']
        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)
Ejemplo n.º 3
0
 def _get_container_arch(self):
     info = subprocess.check_output(['lxc', 'info',
                                     self._container_name]).decode('utf-8')
     for line in info.splitlines():
         if line.startswith("Architecture:"):
             return _get_deb_arch(line.split(None, 1)[1].strip())
     raise ContainerError("Could not find architecture for container")
Ejemplo n.º 4
0
 def _configure_container(self):
     subprocess.check_call([
         'lxc', 'config', 'set', self._container_name,
         'environment.SNAPCRAFT_SETUP_CORE', '1'])
     for snapcraft_env_var in (
             'SNAPCRAFT_PARTS_URI', 'SNAPCRAFT_BUILD_INFO'):
         if os.getenv(snapcraft_env_var):
             subprocess.check_call([
                 'lxc', 'config', 'set', self._container_name,
                 'environment.{}'.format(snapcraft_env_var),
                 os.getenv(snapcraft_env_var)])
     # Necessary to read asset files with non-ascii characters.
     subprocess.check_call([
         'lxc', 'config', 'set', self._container_name,
         'environment.LC_ALL', 'C.UTF-8'])
     self._set_image_info_env_var()
     info = subprocess.check_output([
         'lxc', 'info', self._container_name]).decode('utf-8')
     for line in info.splitlines():
         if line.startswith("Architecture:"):
             self._container_arch = _get_deb_arch(
                 line.split(None, 1)[1].strip())
             break
     else:
         raise ContainerError("Could not find architecture for container")
Ejemplo n.º 5
0
    def test_cleanbuild(self, mock_pet, mock_inject, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        mock_pet.return_value = 'my-pet'

        project_folder = '/root/build_project'
        self.make_cleanbuilder().execute()
        expected_arch = _get_deb_arch(self.server)

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

        args = []
        if self.target_arch:
            self.assertIn('Setting target machine to \'{}\'\n'.format(
                          self.target_arch), self.fake_logger.output)
            args += ['--target-arch', self.target_arch]

        container_name = '{}:snapcraft-my-pet'.format(self.remote)
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'launch', '-e',
                  'ubuntu:xenial/{}'.format(expected_arch), container_name]),
            call(['lxc', 'config', 'set', container_name,
                  'environment.SNAPCRAFT_SETUP_CORE', '1']),
            call(['lxc', 'config', 'set', container_name,
                  'environment.LC_ALL', 'C.UTF-8']),
            call(['lxc', 'file', 'push', os.path.realpath('project.tar'),
                  '{}/root/build_project/project.tar'.format(container_name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['python3', '-c', 'import urllib.request; ' +
                  'urllib.request.urlopen(' +
                  '"http://start.ubuntu.com/connectivity-check.html"' +
                  ', timeout=5)']),
            call(['apt-get', 'update']),
            call(['mkdir', project_folder]),
            call(['tar', 'xvf', 'project.tar'],
                 cwd=project_folder),
            call(['snapcraft', 'snap', '--output', 'snap.snap', *args],
                 cwd=project_folder),
        ])
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'file', 'pull',
                  '{}{}/snap.snap'.format(container_name, project_folder),
                  'snap.snap']),
            call(['lxc', 'stop', '-f', container_name]),
        ])
Ejemplo n.º 6
0
    def test_cleanbuild(self, mock_pet, mock_inject, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        mock_pet.return_value = 'my-pet'

        project_folder = '/root/build_project'
        self.make_cleanbuilder().execute()
        expected_arch = _get_deb_arch(self.server)

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

        args = []
        if self.target_arch:
            self.assertIn('Setting target machine to \'{}\'\n'.format(
                          self.target_arch), self.fake_logger.output)
            args += ['--target-arch', self.target_arch]

        container_name = '{}:snapcraft-my-pet'.format(self.remote)
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'launch', '-e',
                  'ubuntu:xenial/{}'.format(expected_arch), container_name]),
            call(['lxc', 'config', 'set', container_name,
                  'environment.SNAPCRAFT_SETUP_CORE', '1']),
            call(['lxc', 'config', 'set', container_name,
                  'environment.LC_ALL', 'C.UTF-8']),
            call(['lxc', 'file', 'push', os.path.realpath('project.tar'),
                  '{}/root/build_project/project.tar'.format(container_name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['python3', '-c', 'import urllib.request; ' +
                  'urllib.request.urlopen(' +
                  '"http://start.ubuntu.com/connectivity-check.html"' +
                  ', timeout=5)']),
            call(['apt-get', 'update']),
            call(['mkdir', project_folder]),
            call(['tar', 'xvf', 'project.tar'],
                 cwd=project_folder),
            call(['snapcraft', 'snap', '--output', 'snap.snap', *args],
                 cwd=project_folder),
        ])
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'file', 'pull',
                  '{}{}/snap.snap'.format(container_name, project_folder),
                  'snap.snap']),
            call(['lxc', 'stop', '-f', container_name]),
        ])