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

Add a "--profile" parameter to cleanbuild #1969

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions snapcraft/cli/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ def clean(parts, step, **kwargs):
@add_build_options()
@click.option('--remote', metavar='<remote>',
help='Use a specific lxd remote instead of a local container.')
@click.option('--profile', metavar='<profile>',
help='Use a specific lxd profile instead of the "default" '
'profile.')
@click.option('--debug', is_flag=True,
help='Shells into the environment if the build fails.')
def cleanbuild(remote, debug, **kwargs):
def cleanbuild(remote, profile, debug, **kwargs):
"""Create a snap using a clean environment managed by lxd.

\b
Expand All @@ -215,7 +218,7 @@ def cleanbuild(remote, debug, **kwargs):
https://linuxcontainers.org/lxd/getting-started-cli/#multiple-hosts
"""
project_options = get_project_options(**kwargs, debug=debug)
lifecycle.cleanbuild(project_options, remote)
lifecycle.cleanbuild(project_options, remote, profile)


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions snapcraft/internal/lifecycle/_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def containerbuild(step, project_options, container_config,
metadata=config.get_metadata()).execute(step, args)


def cleanbuild(project_options, remote=''):
def cleanbuild(project_options, remote='', profile=''):
if remote and not lxd._remote_is_valid(remote):
raise errors.InvalidContainerRemoteError(remote)

Expand All @@ -65,4 +65,5 @@ def cleanbuild(project_options, remote=''):
t.add(os.path.curdir, filter=_create_tar_filter(tar_filename))
lxd.Cleanbuilder(source=tar_filename,
project_options=project_options,
metadata=config.get_metadata(), remote=remote).execute()
metadata=config.get_metadata(), remote=remote,
profile=profile).execute()
11 changes: 8 additions & 3 deletions snapcraft/internal/lxd/_cleanbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@
class Cleanbuilder(Containerbuild):

def __init__(self, *, output=None, source, project_options,
metadata=None, remote=None):
metadata=None, remote=None, profile=None):
self._profile = profile
container_name = petname.Generate(3, '-')
super().__init__(output=output, source=source,
project_options=project_options, metadata=metadata,
container_name=container_name, remote=remote)

def _ensure_container(self):
launch_command = ['lxc', 'launch']
if self._profile:
launch_command = launch_command + ['-p', self._profile]
launch_command = launch_command + [
'-e', self._image, self._container_name]
try:
subprocess.check_call([
'lxc', 'launch', '-e', self._image, self._container_name])
subprocess.check_call(launch_command)
except subprocess.CalledProcessError as e:
raise ContainerConnectionError('Failed to setup container')
self._configure_container()
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/commands/test_cleanbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,16 @@ def test_invalid_remote(self):
InvalidContainerRemoteError,
self.run_command, ['cleanbuild', '--remote', 'foo/bar'])
self.assertThat(exception.remote, Equals('foo/bar'))

def test_lxd_profile(self):
fake_lxd = fixture_setup.FakeLXD()
self.useFixture(fake_lxd)
result = self.run_command(['cleanbuild', '--profile', 'foo'])
self.assertThat(result.exit_code, Equals(0))
call_list = fake_lxd.check_call_mock.call_args_list
# call() wraps a tuple of a list, so we need the first tuple of the
# first item of the call_list here.
call = call_list[0][0][0]
# Casting to string makes sublist searching easier.
string_call = ' '.join(call)
self.assertTrue('-p foo' in string_call)