diff --git a/VirtualMachineService/VirtualMachineHandler.py b/VirtualMachineService/VirtualMachineHandler.py index d7ae8d88..8f56ed18 100644 --- a/VirtualMachineService/VirtualMachineHandler.py +++ b/VirtualMachineService/VirtualMachineHandler.py @@ -2712,7 +2712,7 @@ def update_playbooks(self): self.ALL_TEMPLATES = [ name for name in os.listdir(PLAYBOOKS_DIR) - if name != "packer" and os.path.isdir(os.path.join(PLAYBOOKS_DIR, name)) + if name not in ["optional","packer",".github"] and os.path.isdir(os.path.join(PLAYBOOKS_DIR, name)) ] LOG.info(self.ALL_TEMPLATES) diff --git a/VirtualMachineService/ancon/Playbook.py b/VirtualMachineService/ancon/Playbook.py index cdbef8a9..3cd1d49c 100644 --- a/VirtualMachineService/ancon/Playbook.py +++ b/VirtualMachineService/ancon/Playbook.py @@ -35,15 +35,15 @@ class Playbook(object): PLAYBOOK_FAILED = "PLAYBOOK_FAILED" def __init__( - self, - ip, - port, - playbooks_information, - osi_private_key, - public_key, - pool, - loaded_metadata_keys, - cloud_site, + self, + ip, + port, + playbooks_information, + osi_private_key, + public_key, + pool, + loaded_metadata_keys, + cloud_site, ): self.loaded_metadata_keys = loaded_metadata_keys self.cloud_site = cloud_site @@ -87,12 +87,9 @@ def __init__( mode="w+", dir=self.directory.name, delete=False ) - inventory_string = ( - "[vm]\n" + ip + ":" + port + " ansible_user=ubuntu " - "ansible_ssh_private_key_file=" - + self.private_key.name - + " ansible_python_interpreter=/usr/bin/python3" - ) + inventory_string = f"[vm]\n" \ + f"{ip} ansible_port={port} ansible_user=ubuntu ansible_ssh_private_key_file={self.private_key.name} ansible_python_interpreter=/usr/bin/python3" + self.inventory.write(inventory_string) self.inventory.close() @@ -107,12 +104,12 @@ def copy_playbooks_and_init(self, playbooks_information, public_key): self.playbooks_dir + "/change_key_vars_file.yml", self.directory.name ) with open( - self.directory.name + "/change_key_vars_file.yml", mode="r" + self.directory.name + "/change_key_vars_file.yml", mode="r" ) as key_file: data_ck = self.yaml_exec.load(key_file) data_ck["change_key_vars"]["key"] = public_key.strip('"') with open( - self.directory.name + "/change_key_vars_file.yml", mode="w" + self.directory.name + "/change_key_vars_file.yml", mode="w" ) as key_file: self.yaml_exec.dump(data_ck, key_file) self.add_to_playbook_always_lists("change_key") @@ -122,14 +119,14 @@ def copy_playbooks_and_init(self, playbooks_information, public_key): self.playbooks_dir + "/" + self.playbook_exec_name, self.directory.name ) with open( - self.directory.name + "/" + self.playbook_exec_name, mode="r" + self.directory.name + "/" + self.playbook_exec_name, mode="r" ) as generic_playbook: data_gp = self.yaml_exec.load(generic_playbook) data_gp[0]["vars_files"] = self.vars_files data_gp[0]["tasks"][0]["block"] = self.tasks data_gp[0]["tasks"][0]["always"] = self.always_tasks with open( - self.directory.name + "/" + self.playbook_exec_name, mode="w" + self.directory.name + "/" + self.playbook_exec_name, mode="w" ) as generic_playbook: self.yaml_exec.dump(data_gp, generic_playbook) @@ -270,7 +267,7 @@ def get_logs(self): return self.returncode, self.stdout, self.stderr def cleanup(self, openstack_id): - self.directory.cleanup() + # self.directory.cleanup() self.redis.delete(openstack_id) def stop(self, openstack_id): diff --git a/VirtualMachineService/ancon/playbooks/generic_playbook.yml b/VirtualMachineService/ancon/playbooks/generic_playbook.yml index fb9ba6cf..46235292 100644 --- a/VirtualMachineService/ancon/playbooks/generic_playbook.yml +++ b/VirtualMachineService/ancon/playbooks/generic_playbook.yml @@ -1,10 +1,57 @@ - name: Setting up your virtual machine - hosts: all + hosts: vm become: yes - connection: paramiko_ssh gather_facts: yes vars_files: + pre_tasks: + - name: PRE_TASK Disable unattended upgrades + lineinfile: + path: /etc/apt/apt.conf.d/10periodic + regexp: "^APT::Periodic::Unattended-Upgrade" + line: 'APT::Periodic::Unattended-Upgrade "0";' + create: yes + - name: PRE_TASK Stop apt-daily.* systemd services + service: + name: "{{ item }}" + state: stopped + with_items: + - unattended-upgrades + - apt-daily + - apt-daily.timer + - apt-daily-upgrade + - apt-daily-upgrade.timer + + - name: PRE_TASK Wait for automatic system updates 1 + shell: while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 1; done; + + - name: PRE_TASK Wait for automatic system updates 2 + shell: while sudo fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do sleep 1; done; + + - name: PRE_TASK Update apt cache + become: true + apt: + upgrade: true + update_cache: true + autoremove: true + autoclean: true + tasks: - name: Setting up your virtual machine block: always: + + post_tasks: + - name: POST_TASK enable unattended upgrades + lineinfile: + path: /etc/apt/apt.conf.d/10periodic + regexp: "^APT::Periodic::Unattended-Upgrade" + line: 'APT::Periodic::Unattended-Upgrade "1";' + create: yes + - name: POST_TASK Start apt-daily.* systemd services + service: + name: "{{ item }}" + state: started + with_items: + - unattended-upgrades + - apt-daily + - apt-daily.timer diff --git a/ansible.cfg b/ansible.cfg index 911e2da9..0fd55e45 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,6 +1,6 @@ [defaults] host_key_checking = False -callbacks_enabled = ansible.posix.profile_tasks +callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer roles_path = ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/code/VirtualMachineService/ancon/playbooks/roles diff --git a/requirements.txt b/requirements.txt index 4da4a1f7..93469245 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,5 +10,5 @@ paramiko==2.11.0 ruamel.yaml==0.17.21 pyvim==3.0.3 redis==4.3.3 -requests==2.27.1 +requests==2.28.0 pyyaml==6.0