<commit_before>#!/usr/bin/env python
"""Script used to remove Cloudify cluster components.

Use this after running `cfy teardown` on a 4.0.0 or 4.0.1 manager, to
also remove cluster-related things.

Do not attempt to run this script without leaving the cluster first.
(unless this is run on the only node in a 1-node cluster)

Note that this script must be run with sudo.
"""

import os
import shutil
import subprocess

config = {
    'services': ['check-runner', 'consul-watcher', 'consul-recovery-watcher',
                 'handler-runner', 'iptables-restore'],
    'postgresql': {
        'data_dir': '/var/pgdata'
    },
    'syncthing': {
        'user': 'cfyuser_syncthing',
        'group': 'syncthing'
    },
    'consul': {
        'user': 'cfyuser_consul',
        'group': 'cfyuser_consul'
    },
    'group': 'cluster'
}


def _stop_systemd_unit(name):
    subprocess.check_call(['systemctl', 'stop', name])
    subprocess.check_call(['systemctl', 'disable', name])
    os.unlink(os.path.join('/usr/lib/systemd/system',
                           '{0}.service'.format(name)))


def _userdel(username, ignore_failures=True):
    try:
        subprocess.check_call(['userdel', '--force', username])
    except subprocess.CalledProcessError:
        if not ignore_failures:
            raise


def _groupdel(group, ignore_failures=True):
    try:
        subprocess.check_call(['groupdel', group])
    except subprocess.CalledProcessError:
        if not ignore_failures:
            raise


def _delete_database(config):
    _stop_systemd_unit('cloudify-postgresql')
    shutil.rmtree(config['postgresql']['data_dir'])


def _delete_consul(config):
    for unit in config['services']:
        _stop_systemd_unit('cloudify-{0}'.format(unit))
    _userdel(config['consul']['user'])
    _groupdel(config['consul']['group'])

    # also remove the cluster group here
    _groupdel(config['group'])


def _delete_syncthing(config):
    _userdel(config['syncthing']['user'])
    _groupdel(config['syncthing']['group'])


_delete_syncthing(config)
_delete_database(config)
_delete_consul(config)<commit_msg>Fix to avoid not existed resources (#41)<commit_after>#!/usr/bin/env python
"""Script used to remove Cloudify cluster components.

Use this after running `cfy teardown` on a 4.0.0 or 4.0.1 manager, to
also remove cluster-related things.

Do not attempt to run this script without leaving the cluster first.
(unless this is run on the only node in a 1-node cluster)

Note that this script must be run with sudo.
"""

import os
import shutil
import subprocess

config = {
    'services': ['check-runner', 'consul-watcher', 'consul-recovery-watcher',
                 'handler-runner', 'iptables-restore'],
    'postgresql': {
        'data_dir': '/var/pgdata'
    },
    'syncthing': {
        'user': 'cfyuser_syncthing',
        'group': 'syncthing'
    },
    'consul': {
        'user': 'cfyuser_consul',
        'group': 'cfyuser_consul'
    },
    'group': 'cluster'
}


def _stop_systemd_unit(name, ignore_failures=True):
    try:
        subprocess.check_call(['systemctl', 'stop', name])
        subprocess.check_call(['systemctl', 'disable', name])
        os.unlink(os.path.join('/usr/lib/systemd/system',
                               '{0}.service'.format(name)))
    except subprocess.CalledProcessError:
      if not ignore_failures:
          raise


def _userdel(username, ignore_failures=True):
    try:
        subprocess.check_call(['userdel', '--force', username])
    except subprocess.CalledProcessError:
        if not ignore_failures:
            raise


def _groupdel(group, ignore_failures=True):
    try:
        subprocess.check_call(['groupdel', group])
    except subprocess.CalledProcessError:
        if not ignore_failures:
            raise


def _delete_database(config):
    _stop_systemd_unit('cloudify-postgresql')
    try:
        shutil.rmtree(config['postgresql']['data_dir'])
    except:
        pass


def _delete_consul(config):
    for unit in config['services']:
        _stop_systemd_unit('cloudify-{0}'.format(unit))
    _userdel(config['consul']['user'])
    _groupdel(config['consul']['group'])

    # also remove the cluster group here
    _groupdel(config['group'])


def _delete_syncthing(config):
    _userdel(config['syncthing']['user'])
    _groupdel(config['syncthing']['group'])


_delete_syncthing(config)
_delete_database(config)
_delete_consul(config)
{'fraction_non_alphanumeric': 0.13319764812302126, 'fraction_numerical': 0.004522840343735866, 'mean_word_length': 3.383548067393459, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '645001', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1395, 'n_words': 314}