class VPNServerService:

    def __init__(self, config, auth_key, auth_secret, verbose, output):
        self._config = config
        self._auth_key = auth_key
        self._auth_secret = auth_secret
        self._verbose = verbose

    def create(self, server_password):
        process_uuid = binascii.hexlify(os.urandom(32))

        extra_vars = {'ansible_user': self._config.root_user(),
                      'ansible_ssh_private_key_file': self._config.host_key(),
                      'host_auth_key': self._auth_key,
                      'host_auth_secret': self._auth_secret,
                      'root_server_password': server_password,
                      'root_key': self._config.host_key(),
                      'user_key': self._config.deployer_key(),
                      'inventory': 'vpn',
                      'directory': os.getcwd(),
                      'process_uuid': process_uuid}

        cmd.execute('vpn', 
                    'hosts',
                    extra_vars, 
                    ['server',self._config.host()], 
                    ['secure','format','add','remove',','.join(helpers.ignore_hosts(self._config.host()))], 
                    self._verbose)

        if os.path.exists(os.getcwd() + '/.web-' + process_uuid + '-address.txt'):
            with open(os.getcwd() + '/.web-' + process_uuid + '-address.txt', 'rb') as infile:
                ip = infile.read()

                os.remove(os.getcwd() + '/.web-' + process_uuid + '-address.txt')

                raw = self._config.raw()

                raw['vpn'] = ip.rstrip()

                self._config.save(raw)

            self._config.reload()

    def secure(self):
        extra_vars = {'ansible_user': self._config.root_user(),
                      'ansible_ssh_private_key_file': self._config.host_key(),
                      'deployer_user': self._config.user(),
                      'user_key': self._config.deployer_key()}

        cmd.execute('vpn', 
                    self._config.servers('vpn'), 
                    extra_vars, 
                    ['secure',self._config.host()], 
                    ['server','format','add','remove',','.join(helpers.ignore_hosts(self._config.host()))], 
                    self._verbose)

    def format(self):
        extra_vars = {'ansible_user': self._config.user(),
                      'ansible_ssh_private_key_file': self._config.deployer_key(),
                      'user_key': self._config.deployer_key()}

        cmd.execute('vpn', 
                    self._config.servers('vpn'), 
                    extra_vars, 
                    ['format',self._config.host()], 
                    ['server','secure','add','remove',','.join(helpers.ignore_hosts(self._config.host()))], 
                    self._verbose)