Browse Source

infrastructure: implement create/delete based on docker

keep-around/ed578b1351a60f655ac506f9ba4ed42942eda6ad
singuliere 2 years ago
parent
commit
ed578b1351
No known key found for this signature in database GPG Key ID: 900857755EF189C2
  1. 10
      enough/common/host.py
  2. 13
      enough/internal/cli/host.py
  3. 2
      enough/internal/data/infrastructure-docker-compose.yml
  4. 5
      enough/internal/data/infrastructure.dockerfile
  5. 3
      inventory/group_vars/all/infrastructure.yml
  6. 2
      molecule/infrastructure/create.yml
  7. 2
      molecule/infrastructure/destroy.yml

10
enough/common/host.py

@ -2,6 +2,8 @@ import os
from django.conf import settings
from enough.common import openstack, docker
from abc import ABC, abstractmethod
import shutil
import tempfile
class Host(ABC):
@ -26,9 +28,11 @@ class HostDocker(Host):
def create_image(self):
name = super().create_image()
dockerfile = os.path.join(self.root, 'internal/data/infrastructure.dockerfile')
return self._create_image(None,
'--build-arg', f'IMAGE_NAME={name}',
'-f', dockerfile, '.')
with tempfile.TemporaryDirectory() as d:
shutil.copy(f'{settings.CONFIG_DIR}/infrastructure_key.pub', d)
return self._create_image(None,
'--build-arg', f'IMAGE_NAME={name}',
'-f', dockerfile, d)
def get_compose_content(self):
f = os.path.join(self.root, 'internal/data/infrastructure-docker-compose.yml')

13
enough/internal/cli/host.py

@ -1,8 +1,8 @@
from copy import copy
from cliff.show import ShowOne
from cliff.command import Command
from enough.common.host import host_factory
from enough.common import tcp
def set_common_options(parser):
@ -19,12 +19,13 @@ class Create(ShowOne):
return set_common_options(parser)
def take_action(self, parsed_args):
args = copy(self.app.options)
args.update(parsed_args)
host = host_factory(**vars(args))
r = host.create_or_update()
args = vars(self.app.options)
args.update(vars(parsed_args))
args['port'] = tcp.free_port()
host = host_factory(**args)
host.create_or_update()
columns = ('name', 'user', 'port', 'ip')
data = (parsed_args.name, 'debian', r['port'], r['ipv4'])
data = (parsed_args.name, 'debian', args['port'], '0.0.0.0')
return (columns, data)

2
enough/internal/data/infrastructure-docker-compose.yml

@ -26,7 +26,7 @@ services:
cap_add:
- IPC_LOCK
ports:
- {{ this.get_ports() }}
- "{{ this.port }}:22"
networks:
- default

5
enough/internal/data/infrastructure.dockerfile

@ -1,2 +1,7 @@
ARG IMAGE_NAME
FROM ${IMAGE_NAME}
RUN apt-get install -y sudo
RUN useradd --shell /bin/bash debian && echo "debian ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
COPY infrastructure_key.pub /home/debian/.ssh/authorized_keys
RUN chown -R debian:debian /home/debian/.ssh/authorized_keys

3
inventory/group_vars/all/infrastructure.yml

@ -0,0 +1,3 @@
---
# infrastructure_driver: openstack
infrastructure_driver: docker

2
molecule/infrastructure/create.yml

@ -12,7 +12,7 @@
- name: create instances
register: out
shell: |
python -m enough.internal.cmd host create --format json {{ item.name }}
python -m enough.internal.cmd host create --driver {{ infrastructure_driver }} --format json {{ item.name }}
loop: "{{ molecule_yml.platforms }}"
- name: stacks

2
molecule/infrastructure/destroy.yml

@ -9,5 +9,5 @@
- name: delete instances
shell: |
python -m enough.internal.cmd host delete {{ item.name }}
python -m enough.internal.cmd host delete --driver {{ infrastructure_driver }} {{ item.name }}
loop: "{{ molecule_yml.platforms }}"

Loading…
Cancel
Save