Merge lp:~canonical-platform-qa/snappy-ecosystem-tests/spring-cleanup into lp:snappy-ecosystem-tests

Proposed by Omer Akram
Status: Merged
Approved by: Santiago Baldassin
Approved revision: 39
Merged at revision: 38
Proposed branch: lp:~canonical-platform-qa/snappy-ecosystem-tests/spring-cleanup
Merge into: lp:snappy-ecosystem-tests
Diff against target: 337 lines (+1/-296)
4 files modified
snappy_ecosystem_tests/environment/data/snapd.py (+0/-65)
snappy_ecosystem_tests/helpers/snapd/staging_builder.py (+0/-97)
snappy_ecosystem_tests/utils/lxd.py (+0/-125)
snappy_ecosystem_tests/utils/ssh.py (+1/-9)
To merge this branch: bzr merge lp:~canonical-platform-qa/snappy-ecosystem-tests/spring-cleanup
Reviewer Review Type Date Requested Status
Santiago Baldassin (community) Approve
platform-qa-bot continuous-integration Approve
Review via email: mp+319475@code.launchpad.net

Commit message

Remove unused lxd related code.

Description of the change

Remove unused lxd related code.

To post a comment you must log in.
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Approve (continuous-integration)
39. By Omer Akram

More unused code removals

Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Omer Akram (om26er) wrote :

Note, I have removed the code that was trying to do set environment variables over ssh as those variables were not really doing anything. Snapd decides during runtime based on environment variable in /etc/environment if it should enable staging servers or not.

snapd only reads SNAPPY_USE_STAGING_STORE=1

Revision history for this message
Santiago Baldassin (sbaldassin) wrote :

Looks good

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'snappy_ecosystem_tests/environment/data/snapd.py'
--- snappy_ecosystem_tests/environment/data/snapd.py 2017-03-06 17:09:46 +0000
+++ snappy_ecosystem_tests/environment/data/snapd.py 2017-03-09 17:27:27 +0000
@@ -20,71 +20,6 @@
20"""snapd data"""20"""snapd data"""
21SNAPD_CONTAINER_NAME = 'snapd'21SNAPD_CONTAINER_NAME = 'snapd'
2222
23CONTAINER_HOME = '/root'
24DIRECTORY_CLONE = '{}/src/github.com/snapcore/snapd'.format(CONTAINER_HOME)
25DOMAIN_PING = 'ubuntu.com'
26ENV_STORE_API_ROOT = 'UBUNTU_STORE_API_ROOT_URL'
27ENV_STORE_SEARCH_ROOT = 'UBUNTU_STORE_SEARCH_ROOT_URL'
28ENV_STORE_UPLOAD_ROOT = 'UBUNTU_STORE_UPLOAD_ROOT_URL'
29ENV_SSO_API_ROOT = 'UBUNTU_SSO_API_ROOT_URL'
30PACKAGES_SNAPD = ['snapd', 'snap-confine', 'ubuntu-core-launcher']
31COMMAND_REMOVE_SNAPD = 'apt purge -y {}'.format(' '.join(PACKAGES_SNAPD))
32COMMAND_APT_BUILD_DEP = 'apt build-dep -y ./'
33COMMAND_APT_INSTALL = 'apt install -y {}'
34COMMAND_APT_INSTALL_STAGING_DEBS = COMMAND_APT_INSTALL.format(
35 ' '.join(['../{}*.deb'.format(pkg) for pkg in PACKAGES_SNAPD]))
36COMMAND_APT_UPDATE = 'apt update'
37COMMAND_APT_UPGRADE = 'apt dist-upgrade -y'
38COMMAND_BUILD_SNAPD = 'DEB_BUILD_OPTIONS="nocheck testkeys" ' \
39 'dpkg-buildpackage -tc -b'
40COMMAND_EXPORT_STAGING_STORE_VAR = 'echo SNAPPY_USE_STAGING_STORE=1 >> ' \
41 '/etc/environment'
42COMMAND_GET_GOVENDOR = 'go get -v github.com/kardianos/govendor'
43COMMAND_GIT_CLONE = 'git clone {} {}'
44COMMAND_GOVENDOR_SYNC = 'govendor sync'
45REPOSITORY_GIT = 'https://github.com/snapcore/snapd'
46URL_API_ROOT = 'https://myapps.developer.staging.ubuntu.com/dev/api/'
47URL_SEARCH_ROOT = 'https://search.apps.staging.ubuntu.com/'
48URL_UPLOAD_ROOT = 'https://upload.apps.staging.ubuntu.com/'
49URL_SSO_API_ROOT = 'https://login.staging.ubuntu.com/api/v2/'
50
51CONTAINER_SETUP = [
52 COMMAND_REMOVE_SNAPD,
53 COMMAND_APT_UPDATE,
54 # Hold open-iscsi which is causing apt upgrade errors while running
55 # inside container.
56 'apt-mark hold open-iscsi',
57 COMMAND_APT_UPGRADE,
58 # Needed for snaps to work inside a lxd container
59 # ref: https://stgraber.org/2016/12/07/running-snaps-in-lxd-containers/
60 COMMAND_APT_INSTALL.format('squashfuse'),
61 COMMAND_GIT_CLONE.format(REPOSITORY_GIT, DIRECTORY_CLONE),
62 {'command': COMMAND_APT_BUILD_DEP, 'cwd': DIRECTORY_CLONE},
63 {'command': COMMAND_GET_GOVENDOR, 'cwd': DIRECTORY_CLONE},
64 {'command': COMMAND_GOVENDOR_SYNC, 'cwd': DIRECTORY_CLONE},
65 {'command': COMMAND_BUILD_SNAPD, 'cwd': DIRECTORY_CLONE},
66 COMMAND_EXPORT_STAGING_STORE_VAR,
67 {'command': COMMAND_APT_INSTALL_STAGING_DEBS, 'cwd': DIRECTORY_CLONE}
68]
69
70SNAPCRAFT_STAGING_VARIABLES = {
71 ENV_STORE_API_ROOT: URL_API_ROOT,
72 ENV_STORE_SEARCH_ROOT: URL_SEARCH_ROOT,
73 ENV_STORE_UPLOAD_ROOT: URL_UPLOAD_ROOT,
74 ENV_SSO_API_ROOT: URL_SSO_API_ROOT,
75}
76
77SNAPD_STAGING_VARIABLES = {
78 'SNAPPY_USE_STAGING_STORE': '1'
79}
80
81STAGING_VARIABLES = dict(
82 SNAPCRAFT_STAGING_VARIABLES, **SNAPD_STAGING_VARIABLES)
83
84CONTAINER_ENV_VARS = {
85 'GOPATH': CONTAINER_HOME,
86}
87
88DEFAULT_CONTAINER_CONFIG = {23DEFAULT_CONTAINER_CONFIG = {
89 "name": "snapd",24 "name": "snapd",
90 "source": {25 "source": {
9126
=== removed file 'snappy_ecosystem_tests/helpers/snapd/staging_builder.py'
--- snappy_ecosystem_tests/helpers/snapd/staging_builder.py 2017-03-02 10:51:37 +0000
+++ snappy_ecosystem_tests/helpers/snapd/staging_builder.py 1970-01-01 00:00:00 +0000
@@ -1,97 +0,0 @@
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
3#
4# Snappy Ecosystem Tests
5# Copyright (C) 2017 Canonical
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20
21"""Module to enable staging environment for snapd."""
22
23import logging
24import sys
25from time import sleep
26
27from snappy_ecosystem_tests.environment.data.snapd import (
28 CONTAINER_HOME, CONTAINER_ENV_VARS, CONTAINER_SETUP)
29from snappy_ecosystem_tests.utils.lxd import Container, launch_container
30
31LOGGER = logging.getLogger(__name__)
32
33
34class StagingBuilder:
35 """Class to build and activate snapd staging environment."""
36 def __init__(self, container):
37 if not isinstance(container, Container):
38 raise RuntimeError(
39 '`container` must be an instance of '
40 'snappy_ecosystem_tests.utils.lxd.Container')
41 self.container = container
42
43 def _setup_staging(self):
44 """Export environment variables required to enable snapd/snapcraft
45 staging."""
46 LOGGER.info('Exporting staging environment variables')
47 updated_path = '{}/bin:{}'.format(
48 CONTAINER_HOME, self.container.execute('echo $PATH'))
49 CONTAINER_ENV_VARS['PATH'] = updated_path
50 for key, value in CONTAINER_ENV_VARS.items():
51 self.container.set_environment_variable(key, value, save=False)
52 self.container.save(wait=True)
53 LOGGER.info('Exported staging environment variables')
54
55 def _wait_for_internet(self, attempts_count=10, attempt_interval=1):
56 """Wait for internet connectivity in the container."""
57 for _ in range(attempts_count):
58 if self.container.is_internet_working():
59 break
60 else:
61 LOGGER.info('Sleeping for %s second(s)', attempt_interval)
62 sleep(attempt_interval)
63 else:
64 LOGGER.error(
65 'Failed to get internet access in container %s after %s '
66 'seconds', self.container.name,
67 attempts_count * attempt_interval)
68 sys.exit(1)
69
70 def _setup_and_build(self):
71 """Setup the container, pulls and builds snapd with staging enabled.
72 """
73 for command in CONTAINER_SETUP:
74 if isinstance(command, dict):
75 self.container.execute(**command)
76 else:
77 self.container.execute(command)
78
79 def build(self):
80 """Setup lxd container environment for snapd staging enabled builds."""
81 self._setup_staging()
82 self._wait_for_internet()
83 self._setup_and_build()
84
85
86if __name__ == '__main__':
87 # TODO remove local import
88 import argparse
89 logging.basicConfig(level=logging.INFO)
90 PARSER = argparse.ArgumentParser(
91 description='Builder for snapd staging environment.')
92 PARSER.add_argument('container_name', help='name for the new container.')
93 PARSER.add_argument(
94 'ubuntu_release', help='Ubuntu release version for the container.')
95 ARGS = PARSER.parse_args()
96 StagingBuilder(launch_container(name=ARGS.container_name,
97 release=ARGS.ubuntu_release)).build()
980
=== removed file 'snappy_ecosystem_tests/utils/lxd.py'
--- snappy_ecosystem_tests/utils/lxd.py 2017-02-21 13:46:08 +0000
+++ snappy_ecosystem_tests/utils/lxd.py 1970-01-01 00:00:00 +0000
@@ -1,125 +0,0 @@
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
3#
4# Snappy Ecosystem Tests
5# Copyright (C) 2017 Canonical
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20
21"""Module to manipulate lxd containers."""
22
23import logging
24import shlex
25
26import pylxd
27from pylxd import models
28
29DEFAULT_CONTAINER_CONFIG = {
30 'name': 'snapd',
31 'source': {
32 'type': 'image',
33 'protocol': 'simplestreams',
34 'server': 'https://cloud-images.ubuntu.com/daily',
35 'alias': '16.04'
36 }
37}
38DOMAIN_PING = 'ubuntu.com'
39LOGGER = logging.getLogger(__name__)
40
41
42def launch_container(name, release, delete_existing=True):
43 """Launch a lxd container of the requested name and release and return
44 its object."""
45 lxd_client = pylxd.Client()
46 if delete_existing and lxd_client.containers.exists(name):
47 Container(lxd_client.containers.get(name)).delete()
48 container_config = DEFAULT_CONTAINER_CONFIG.copy()
49 container_config['source']['alias'] = release
50 container_config['name'] = name
51 container = Container(
52 lxd_client.containers.create(container_config, wait=True))
53 container.start(wait=True)
54 return container
55
56
57class Container:
58 """A Class representing a lxd container.
59
60 Extends and overrides some functionalities of pylxd.models.Container
61 instance through delegation since that class does not support inheritance.
62 """
63 def __init__(self, container_instance):
64 # TODO: move to inheritance once upstream supports it.
65 if not isinstance(container_instance, models.Container):
66 raise RuntimeError(
67 '`container_instance` must be an instance of '
68 'pylxd.models.Container'
69 )
70 #TODO: Remove this as the StageBuilder class already has an
71 # instance of the container
72 self._delegate = container_instance
73
74 def __getattr__(self, name):
75 """Set the container instance as delegate."""
76 return getattr(self._delegate, name)
77
78 @staticmethod
79 def _assemble_container_commands(command, cwd=''):
80 """Format and split the requested command to be container friendly."""
81 exec_command_base = 'sh -c \'{}\''
82 if cwd:
83 command = 'cd {}; {}'.format(cwd, command)
84 LOGGER.info('Run command: %s', command)
85 return shlex.split(exec_command_base.format(command))
86
87 #TODO move the following methods to another place which does not require
88 # a container class
89 def execute(self, command, environment=None, cwd=''):
90 """Execute command in the container."""
91 response = self._delegate.execute(
92 self._assemble_container_commands(command, cwd=cwd),
93 environment=environment or {}
94 )
95 if response.exit_code == 0:
96 LOGGER.info(response.stdout)
97 return response.stdout.strip()
98 raise ValueError(response.stderr)
99
100 def set_environment_variable(self, key, value, save=True):
101 """Exports environment variable to be available to
102 Container.execute()."""
103 self._delegate.config.update({'environment.{}'.format(key): value})
104 if save:
105 self._delegate.save(wait=True)
106
107 def is_running(self):
108 """Returns bool representing whether the container is running."""
109 return self._delegate.status == 'Running'
110
111 def delete(self, stop=True, wait=True):
112 """Stops and deletes the container."""
113 if stop and self.is_running():
114 self._delegate.stop(wait=True)
115 LOGGER.info('Deleting container %s', self.name)
116 self._delegate.delete(wait=wait)
117 LOGGER.info('Deleted container %s', self.name)
118
119 def is_internet_working(self, ping_domain=DOMAIN_PING):
120 """Return bool representing if the container have internet access."""
121 try:
122 self.execute('ping -c 1 {}'.format(ping_domain))
123 return True
124 except ValueError:
125 return False
1260
=== modified file 'snappy_ecosystem_tests/utils/ssh.py'
--- snappy_ecosystem_tests/utils/ssh.py 2017-03-07 13:53:31 +0000
+++ snappy_ecosystem_tests/utils/ssh.py 2017-03-09 17:27:27 +0000
@@ -24,9 +24,6 @@
2424
25import paramiko25import paramiko
2626
27from snappy_ecosystem_tests.utils.storeconfig import get_current_store
28from snappy_ecosystem_tests.environment.data.snapd import STAGING_VARIABLES
29
30LOGGER = logging.getLogger(__name__)27LOGGER = logging.getLogger(__name__)
3128
3229
@@ -36,14 +33,9 @@
36 def exec_command(self, command, bufsize=-1, timeout=None, get_pty=False,33 def exec_command(self, command, bufsize=-1, timeout=None, get_pty=False,
37 environment=None):34 environment=None):
38 """Execute the given command over ssh."""35 """Execute the given command over ssh."""
39 if environment is None:
40 # FIXME: get environment variables dinamically
41 if get_current_store() == 'staging':
42 environment = STAGING_VARIABLES
43 _, stdout, stderr = super().exec_command(36 _, stdout, stderr = super().exec_command(
44 command, bufsize, timeout,37 command, bufsize, timeout,
45 get_pty,38 get_pty, environment=environment)
46 environment=environment)
47 if stdout.channel.recv_exit_status() != 0:39 if stdout.channel.recv_exit_status() != 0:
48 raise ValueError(stderr.read().decode().strip())40 raise ValueError(stderr.read().decode().strip())
49 response = stdout.read().decode().strip()41 response = stdout.read().decode().strip()

Subscribers

People subscribed via source and target branches