apt: get settings from mirror + proxy

This commit is contained in:
Dan Bungert 2021-12-08 10:38:28 -07:00
parent 55b2d4cf86
commit 0c28c13887
6 changed files with 66 additions and 13 deletions

View File

@ -53,7 +53,7 @@ class MirrorModel(object):
self.default_mirror = self.get_mirror() self.default_mirror = self.get_mirror()
self.disable_components = set() self.disable_components = set()
def get_config(self): def get_apt_config(self):
config = copy.deepcopy(self.config) config = copy.deepcopy(self.config)
config['disable_components'] = list(self.disable_components) config['disable_components'] = list(self.disable_components)
return config return config

View File

@ -32,13 +32,18 @@ class ProxyModel(object):
def proxy_systemd_dropin(self): def proxy_systemd_dropin(self):
return dropin_template.format(proxy=self.proxy) return dropin_template.format(proxy=self.proxy)
def get_apt_config(self):
if self.proxy:
return {
'http_proxy': self.proxy,
'https_proxy': self.proxy,
}
else:
return {}
def render(self): def render(self):
if self.proxy: if self.proxy:
return { return {
'apt': {
'http_proxy': self.proxy,
'https_proxy': self.proxy,
},
'proxy': { 'proxy': {
'http_proxy': self.proxy, 'http_proxy': self.proxy,
'https_proxy': self.proxy, 'https_proxy': self.proxy,

View File

@ -44,11 +44,11 @@ class TestMirrorModel(unittest.TestCase):
self.assertEqual(model.get_mirror(), "http://mymirror.invalid/") self.assertEqual(model.get_mirror(), "http://mymirror.invalid/")
def test_default_disable_components(self): def test_default_disable_components(self):
config = MirrorModel().get_config() config = MirrorModel().get_apt_config()
self.assertEqual([], config['disable_components']) self.assertEqual([], config['disable_components'])
def test_set_disable_components(self): def test_set_disable_components(self):
model = MirrorModel() model = MirrorModel()
model.disable_components = set(['universe']) model.disable_components = set(['universe'])
config = model.get_config() config = model.get_apt_config()
self.assertEqual(['universe'], config['disable_components']) self.assertEqual(['universe'], config['disable_components'])

View File

@ -119,8 +119,6 @@ class TestSubiquityModel(unittest.TestCase):
config = model.render() config = model.render()
self.assertConfigHasVal(config, 'proxy.http_proxy', proxy_val) self.assertConfigHasVal(config, 'proxy.http_proxy', proxy_val)
self.assertConfigHasVal(config, 'proxy.https_proxy', proxy_val) self.assertConfigHasVal(config, 'proxy.https_proxy', proxy_val)
self.assertConfigHasVal(config, 'apt.http_proxy', proxy_val)
self.assertConfigHasVal(config, 'apt.https_proxy', proxy_val)
confs = self.writtenFilesMatchingContaining( confs = self.writtenFilesMatchingContaining(
config, config,
'etc/systemd/system/snapd.service.d/*.conf', 'etc/systemd/system/snapd.service.d/*.conf',

View File

@ -18,6 +18,7 @@ import os
import shutil import shutil
import tempfile import tempfile
from curtin.config import merge_config
from curtin.util import write_file from curtin.util import write_file
import yaml import yaml
@ -66,6 +67,12 @@ class AptConfigurer:
options=f'lowerdir={source},upperdir={u},workdir={w}') options=f'lowerdir={source},upperdir={u},workdir={w}')
return u return u
def apt_config(self):
cfg = {}
merge_config(cfg, self.app.base_model.mirror.get_apt_config())
merge_config(cfg, self.app.base_model.proxy.get_apt_config())
return {'apt': cfg}
async def configure(self, context): async def configure(self, context):
# Configure apt so that installs from the pool on the cdrom are # Configure apt so that installs from the pool on the cdrom are
# preferred during installation but not in the installed system. # preferred during installation but not in the installed system.
@ -103,15 +110,12 @@ class AptConfigurer:
config_upper = await self.setup_overlay(self.source, self.configured) config_upper = await self.setup_overlay(self.source, self.configured)
config = {
'apt': self.app.base_model.mirror.get_config(),
}
config_location = os.path.join( config_location = os.path.join(
self.app.root, 'var/log/installer/subiquity-curtin-apt.conf') self.app.root, 'var/log/installer/subiquity-curtin-apt.conf')
datestr = '# Autogenerated by Subiquity: {} UTC\n'.format( datestr = '# Autogenerated by Subiquity: {} UTC\n'.format(
str(datetime.datetime.utcnow())) str(datetime.datetime.utcnow()))
write_file(config_location, datestr + yaml.dump(config)) write_file(config_location, datestr + yaml.dump(self.apt_config()))
self.app.note_data_for_apport("CurtinAptConfig", config_location) self.app.note_data_for_apport("CurtinAptConfig", config_location)

View File

@ -0,0 +1,46 @@
# Copyright 2021 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest.mock import Mock
from subiquitycore.tests import SubiTestCase
from subiquitycore.tests.mocks import make_app
from subiquity.server.apt import AptConfigurer
from subiquity.models.mirror import MirrorModel, DEFAULT
from subiquity.models.proxy import ProxyModel
class TestAptConfigurer(SubiTestCase):
def setUp(self):
self.base_config = {'apt': DEFAULT}
self.base_config['apt']['disable_components'] = []
self.model = Mock()
self.model.mirror = MirrorModel()
self.model.proxy = ProxyModel()
self.app = make_app(self.model)
self.configurer = AptConfigurer(self.app, '')
def test_apt_config_noproxy(self):
expected = self.base_config
self.assertEqual(expected, self.configurer.apt_config())
def test_apt_config_proxy(self):
proxy = 'http://apt-cacher-ng:3142'
self.model.proxy.proxy = proxy
expected = self.base_config
expected['apt']['http_proxy'] = proxy
expected['apt']['https_proxy'] = proxy
self.assertEqual(expected, self.configurer.apt_config())