Browse Source

enough: make clone idempotent

keep-around/3b03c36929eaa2226f2d9542d24d37cf97af77ad
Loïc Dachary 1 year ago
committed by Loic Dachary
parent
commit
a08c5c9a73
Signed by: dachary GPG Key ID: 992D23B392F9E4F2
  1. 39
      copy-playbook.yml
  2. 11
      enough/common/__init__.py
  3. 20
      tests/enough/common/test_init.py

39
copy-playbook.yml

@ -0,0 +1,39 @@
---
- name: configure copy host
hosts: copy-group
become: true
pre_tasks:
- name: Enable root login
authorized_key:
user: root
state: present
key: "{{ lookup('file', ssh_private_keyfile + '.pub') }}"
- name: upload private key
copy:
src: "{{ ssh_private_keyfile }}"
dest: "/root/.ssh/id_rsa"
mode: '0400'
- name: /etc/ssh/ssh_config StrictHostKeyChecking no
lineinfile:
path: /etc/ssh/ssh_config
line: StrictHostKeyChecking no
create: yes
- name: echo nameserver 8.8.8.8 > /etc/resolv.conf
copy:
dest: /etc/resolv.conf
content: |
nameserver 8.8.8.8
- name: apt-get install rsync
apt:
name: rsync
state: present
roles:
- role: encrypted_device
encrypted_device_mount_point: /srv

11
enough/common/__init__.py

@ -37,11 +37,10 @@ class Enough(object):
self.playbook = ansible_utils.Playbook(self.config_dir, self.share_dir)
self.ansible = ansible_utils.Ansible(self.config_dir, self.share_dir)
def clone(self, target_domain, target_clouds, assert_region_is_empty):
def clone(self, target_domain, target_clouds):
config_base = os.path.dirname(self.config_dir)
config_dir = f'{config_base}/{target_domain}'
assert not os.path.exists(config_dir)
sh.rsync('-a',
sh.rsync('-a', '--delete',
'--exclude=private-key.yml',
'--exclude=hosts.yml',
'--exclude=group_vars/all/domain.yml',
@ -60,8 +59,6 @@ class Enough(object):
'driver': self.args['driver'],
}
clone = Enough(config_dir, self.share_dir, **kwargs)
if assert_region_is_empty:
assert clone.openstack.region_empty()
return clone
def create_copy_host(self, name, original_volume, copy_volume):
@ -153,8 +150,8 @@ class Enough(object):
self.service.create_or_update()
return host
def restore_remote(self, domain, clouds, snapshot, assert_region_is_empty=True):
clone = self.clone(domain, clouds, assert_region_is_empty)
def restore_remote(self, domain, clouds, snapshot):
clone = self.clone(domain, clouds)
to_volume = self.clone_volume_from_snapshot(clone, snapshot)
host = clone.create_service_matching_snapshot(snapshot)
clone.openstack.replace_volume(host, to_volume)

20
tests/enough/common/test_init.py

@ -33,7 +33,7 @@ def test_clone_and_destroy(tmpdir):
assert original.openstack.o.server.list().strip() == ''
clone_domain = 'clone.com'
clone = original.clone(clone_domain, clone_clouds, assert_region_is_empty=True)
clone = original.clone(clone_domain, clone_clouds)
assert password == open(f'{clone.config_dir}.pass').read()
assert clone.openstack.o.server.list().strip() == ''
@ -64,16 +64,12 @@ def create_enough(tmpdir, clouds, dotenough, **kwargs):
def create_and_clone_server_and_volume(tmpdir, clone_clouds, test_clouds):
original = create_enough(tmpdir, test_clouds, 'backup')
original.set_args(
name='sample',
playbook=os.path.abspath(f'{original.config_dir}/encrypted-volume-playbook.yml'))
original.set_args(name='sample', playbook='enough-playbook.yml')
original.service.create_or_update()
clone_domain = 'clone.com'
clone = original.clone(clone_domain, clone_clouds)
clone.set_args(
name='sample',
playbook=os.path.abspath(f'{clone.config_dir}/encrypted-volume-playbook.yml'))
clone.set_args(name='sample', playbook='enough-playbook.yml')
clone.service.create_or_update()
return (original, clone)
@ -107,7 +103,7 @@ def test_create_copy_host(tmpdir):
enough = create_enough(tmpdir, clouds, 'copy')
if 'copy-volume' not in enough.openstack.o.volume.list():
enough.openstack.o.volume.create('--size', '1', 'copy-volume')
ip = enough.create_copy_host('some-volume', 'copy-volume')
ip = enough.create_copy_host('copy-from-host', 'some-volume', 'copy-volume')
r = sh.ssh('-i', enough.dotenough.private_key(), f'root@{ip}', 'id')
assert 'uid=0(root)' in r
r = sh.ssh('-i', enough.dotenough.private_key(), f'root@{ip}', 'mountpoint', '/srv').strip()
@ -126,9 +122,7 @@ def test_clone_volume_from_snapshot(tmpdir):
try:
original = create_enough(tmpdir, test_clouds, 'backup')
original.set_args(
name='sample',
playbook=os.path.abspath(f'{original.config_dir}/encrypted-volume-playbook.yml'))
original.set_args(name='sample', playbook='enough-playbook.yml')
original.service.create_or_update()
ip = original.hosts.load().get_ip('sample-host')
sh.ssh('-i', original.dotenough.private_key(), f'debian@{ip}', 'touch', '/srv/STONE')
@ -138,7 +132,7 @@ def test_clone_volume_from_snapshot(tmpdir):
assert snapshot in original.openstack.o.volume.snapshot.list()
clone_domain = 'clone.com'
clone = original.clone(clone_domain, clone_clouds, assert_region_is_empty=False)
clone = original.clone(clone_domain, clone_clouds)
(from_ip, to_ip, from_volume, to_volume) = original._clone_volume_from_snapshot_body(
clone, snapshot)
@ -193,7 +187,7 @@ def test_restore_remote(tmpdir):
snapshot = f'{original.openstack.backup_date()}-sample-volume'
assert snapshot in original.openstack.o.volume.snapshot.list()
clone = original.restore_remote('test.com', clone_clouds, snapshot, False)
clone = original.restore_remote('test.com', clone_clouds, snapshot)
assert 'sample-volume' in clone.openstack.o.volume.list()
hosts = clone.hosts.load()
Stack.wait_for_ssh(hosts.get_ip('sample-host'), hosts.get_port('sample-host'))

Loading…
Cancel
Save