Browse Source

Merge branch 'wip-libvirt' into 'master'

enough: enable --driver libvirt

See merge request main/infrastructure!418
keep-around/f4efb51bffac24733a8677562f13ad3c1861e518
Loïc Dachary 4 months ago
parent
commit
f4efb51bff
  1. 22
      docs/release-notes.rst
  2. 1
      enough/common/data/base.dockerfile
  3. 10
      enough/common/dotenough.py
  4. 4
      enough/common/options.py
  5. 2
      enough/internal/data/install.sh
  6. 2
      inventory/group_vars/all/infrastructure.yml
  7. 4
      inventory/group_vars/all/network.yml
  8. 4
      inventory/group_vars/all/openstack.yml
  9. 11
      tests/__init__.py
  10. 2
      tests/enough/common/test_common_options.py
  11. 13
      tests/enough/common/test_dotenough.py
  12. 2
      tests/enough/internal/test_internal_install.py
  13. 1
      tests/tox.dockerfile

22
docs/release-notes.rst

@ -1,6 +1,28 @@
Release Notes
=============
2.1.16
------
* Hosts can now be provisionned using libvirt instead of OpenStack. For instance::
$ enough --domain example.com host create --driver libvirt bind
bind: building image
bind: preparing image
bind: creating host
bind: waiting for ipv4 to be allocated
bind: waiting for 10.23.10.164:22 to come up
Check if SSH is available on 10.23.10.164:22
bind: host is ready
+-------+--------------+
| Field | Value |
+-------+--------------+
| name | bind |
| user | debian |
| port | 22 |
| ip | 10.23.10.164 |
2.1.15
------

1
enough/common/data/base.dockerfile

@ -32,6 +32,7 @@ RUN groupadd --gid $KVM_GID kvm
# kvm will not be considered to be a system group.
#
RUN mv /etc/adduser.conf.backup /etc/adduser.conf
RUN apt-get install --quiet -y libguestfs-tools python-libvirt python-lxml pkg-config libvirt-dev virtinst
RUN if test $USER_NAME != root ; then useradd --no-create-home --home-dir /tmp --uid $USER_ID --groups $DOCKER_GID,$LIBVIRT_GID,$KVM_GID $USER_NAME && echo "$USER_NAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers ; fi
ENV REQUESTS_CA_BUNDLE /etc/ssl/certs

10
enough/common/dotenough.py

@ -108,6 +108,12 @@ class DotEnough(object):
production_domain: {self.domain}
"""))
if not os.path.exists(f'{d}/infrastructure.yml'):
open(f'{d}/infrastructure.yml', 'w').write(textwrap.dedent(f"""\
---
infrastructure_driver: {self.infrastructure_driver}
"""))
if not os.path.exists(f'{d}/certificate.yml'):
self.set_certificate(certificate_authority)
@ -139,6 +145,8 @@ class DotEnough(object):
class DotEnoughLibvirt(DotEnough):
infrastructure_driver = "libvirt"
def __init__(self, config_dir, domain):
super().__init__(config_dir, domain)
@ -153,6 +161,8 @@ class DotEnoughOpenStackUnknownProvider(Exception):
class DotEnoughOpenStack(DotEnough):
infrastructure_driver = "openstack"
def __init__(self, config_dir, domain):
super().__init__(config_dir, domain)
self.clouds_file = f'{self.config_dir}/inventory/group_vars/all/clouds.yml'

4
enough/common/options.py

@ -1,5 +1,7 @@
def set_options(parser):
parser.add_argument('--driver', default='openstack')
parser.add_argument('--driver',
default='openstack',
choices=['libvirt', 'openstack'])
parser.add_argument('--inventory', action='append')
o = parser.add_argument_group(title='OpenStack',
description='Only when --driver=openstack')

2
enough/internal/data/install.sh

@ -2,6 +2,8 @@
{{ this.tty }} \
-v $HOME/.enough:/root/.enough \
-v /etc/ssl/certs:/etc/ssl/certs:ro \
-v /run/libvirt/libvirt-sock:/run/libvirt/libvirt-sock \
-v /var/lib/libvirt/images/enough:/var/lib/libvirt/images/enough \
-v /usr/local/share/ca-certificates:/usr/local/share/ca-certificates:ro \
-v /var/run/docker.sock:/var/run/docker.sock \
--entrypoint enough \

2
inventory/group_vars/all/infrastructure.yml

@ -3,4 +3,4 @@
# Server provisioning infrastructure
#
# infrastructure_driver: libvirt
infrastructure_driver: openstack
# infrastructure_driver: openstack

4
inventory/group_vars/all/network.yml

@ -15,8 +15,8 @@
# the default route is network_secondary_interface
#
network_internal_only: false
network_primary_interface: "{{ infrastructure_driver == 'openstack' | ternary('eth0', 'enp1s0') }}"
network_secondary_interface: "{{ infrastructure_driver == 'openstack' | ternary('eth1', 'enp2s0') }}"
network_primary_interface: "{{ (infrastructure_driver == 'openstack') | ternary(openstack_network_primary_interface, 'enp1s0') }}"
network_secondary_interface: "{{ (infrastructure_driver == 'openstack') | ternary(openstack_network_secondary_interface, 'enp2s0') }}"
#
# Only one of the two interfaces is routed
#

4
inventory/group_vars/all/openstack.yml

@ -37,5 +37,5 @@ openstack_flavor_large: "{{ openstack_providers[_provider].flavors.large }}"
openstack_flavor: '{{ openstack_flavor_small }}'
openstack_image: "{{ openstack_providers[_provider].image }}"
openstack_network: "{{ openstack_providers[_provider].network }}"
network_primary_interface: "{{ openstack_providers[_provider].network_primary_interface }}"
network_secondary_interface: "{{ openstack_providers[_provider].network_secondary_interface }}"
openstack_network_primary_interface: "{{ openstack_providers[_provider].network_primary_interface }}"
openstack_network_secondary_interface: "{{ openstack_providers[_provider].network_secondary_interface }}"

11
tests/__init__.py

@ -134,20 +134,9 @@ class InfrastructureLibvirt(Infrastructure):
---
certificate_authority: ownca
"""))
#
# The network_ variables are required because inventory/group_vars/all/network.yml
# will **not** be re-evaluated when infrastructure_driver is modified in a
# **different** inventory, i.e. the one we're creating here to override values
# for testing purposes
#
open(f'{self.all_dir}/infrastructure.yml', 'w').write(textwrap.dedent("""\
---
infrastructure_driver: libvirt
network_primary_interface: enp1s0
network_secondary_interface: enp2s0
network_interface_unconfigured: noname
network_interface_routed: "{{ network_primary_interface }}"
network_interface_not_routed: "{{ network_secondary_interface }}"
"""))
def config_dir_set(self, enough_dot_dir):

2
tests/enough/common/test_common_options.py

@ -7,6 +7,6 @@ def test_set_options():
parser = argparse.ArgumentParser()
assert options.set_options(parser) == parser
args = parser.parse_args([])
driver = 'DRIVER'
driver = 'libvirt'
args = parser.parse_args(['--driver', driver])
assert args.driver == driver

13
tests/enough/common/test_dotenough.py

@ -8,6 +8,7 @@ from enough.common.dotenough import (
Hosts,
DotEnough,
DotEnoughOpenStack,
DotEnoughLibvirt,
DotEnoughOpenStackUnknownProvider,
)
@ -71,6 +72,18 @@ def test_service_add_to_group(tmpdir):
os.system(f'cat {tmpdir}/services.yml')
@pytest.mark.parametrize("cls,driver", (
(DotEnoughOpenStack, 'openstack'),
(DotEnoughLibvirt, 'libvirt'),
))
def test_dotenough_populate_infrastructure_driver(tmpdir, cls, driver):
d = cls(tmpdir, 'test.com')
d.populate_config('ownca')
all_dir = f'{d.config_dir}/inventory/group_vars/all'
v = yaml.safe_load(open(f'{all_dir}/infrastructure.yml').read())
assert v['infrastructure_driver'] == driver
#
# DotEnoughOpenStack
#

2
tests/enough/internal/test_internal_install.py

@ -9,6 +9,8 @@ def test_enough_install_script(capsys, mocker):
assert main(['--debug', 'install', 'internal/data/install.sh']) == 0
out, err = capsys.readouterr()
assert 'docker run' in out
assert 'libvirt' in out
assert 'docker.sock' in out
assert 'function()' not in out
assert __version__ in out

1
tests/tox.dockerfile

@ -6,7 +6,6 @@ RUN apt-get install -y python
# BEGIN dependencies of test/ssh
RUN apt-get install -y jq
RUN pip install tox yq
RUN apt-get install -y libguestfs-tools virtinst python-libvirt python-lxml pkg-config libvirt-dev # redundant with the libvirt role to speed up the run
RUN apt-get install -y python3-apt python3-libvirt python3-lxml # required because python3 is used not python2
# END dependencies of test/ssh
RUN git init

Loading…
Cancel
Save