def get_app_info(server_name): """Gets mine inventory for the provided instance. """ server_info = call_master_cmd('mine.get', arg=[server_name, 'inventory']) if not server_info: raise Exception( 'Attempted to get app info for %s but mine.get returned empty. ' % (server_name)) raise tornado.gen.Return(server_info)
def get_openvpn_users(): """ description: Gets openvpn users and current status. Then merges users to find currently active ones and their usage data. output: active: - test_user revoked: - test_user_2 status: {} visible: True """ cl = LocalClient() openvpn_users = call_master_cmd('openvpn.list_users') if type(openvpn_users) == str: raise Exception( "Could not get openvpn users list. Contact your administrator for more information. " ) #openvpn_users returns {"revoked" : [list, of, revoked, users], "active" : [list, of, active, users], "status" : {"client_list" : [], "routing_table" : []}} #We want to convert it to {"revoked" : [], "status" : [client, list], active" : [{"name" : "", "check" : False, "connected" : True/False}]} users = {'revoked': openvpn_users['revoked']} users_names = [ i['Common Name'] for i in openvpn_users['status']['client_list'] ] users['active'] = [{ 'name': x, 'check': False, 'connected': x in users_names } for x in openvpn_users['active']] users['status'] = openvpn_users['status']['client_list'] or [] #Virtual address is missing from client_list, we have to find it in the routing table and update it. for x in openvpn_users['status']['client_list']: x.update({ 'Real Address': [ y.get('Virtual Address') for y in openvpn_users['status']['routing_table'] if y['Real Address'] == x['Real Address'] ][0] }) #Make bytes human readable for k in ['Bytes Received', 'Bytes Sent']: for x in openvpn_users['status']['client_list']: x[k] = bytes_to_readable(x[k]) raise tornado.gen.Return(users)
def list_user_logins(username): """ description: Provides a list of previous openvpn logins. arguments: - name: username description: Will return a list of logins for the user with this username required: True type: string output: A list of user logins. """ success = call_master_cmd('openvpn.list_user_logins', kwarg={'username': username}) if type(success) == str: raise Exception('Listing user logins returned with an error. ') raise tornado.gen.Return(success)
def add_openvpn_user(username): """ description: Creates a new openvpn user. arguments: - name: username description: The username for the new user required: True type: string example: test_user visible: True """ success = call_master_cmd('openvpn.add_user', kwarg={'username': username}) if success: raise Exception('Adding an openvpn user returned with an error. ') raise tornado.gen.Return({ 'success': True, 'data': None, 'message': 'User added successfuly. ' })
def download_vpn_cert(username, handler): """ description: Downloads the vpn certificate for the required user. Works by copying the file to /tmp/{username}_vpn.cert and then serving it through Tornado. arguments: - name: username description: Returns the vpn cert for this username. output: Serves the vpn cert for the user. visible: True """ success = call_master_cmd('openvpn.get_config', kwarg={'username': username}) cert_has_error = yield handler.has_error(cert) if cert_has_error: raise Exception('Getting certificate for %s returned with an error. ' % (username)) vpn_cert_path = '/tmp/' + username + '_vpn.cert' with open(vpn_cert_path, 'w') as f: f.write(cert) handler.serve_file(vpn_cert_path) raise tornado.gen.Return({'data_type': 'file'})
def revoke_openvpn_user(username): """ description: Revokes an existing vpn user arguments: - name: username description: The username of the user to be revoked required: True type: string """ success = call_master_cmd('openvpn.revoke_user', kwarg={'username': username}) if success: raise Exception('Revoking %s returned with an error. ' % (username)) raise tornado.gen.Return({ 'success': True, 'data': None, 'message': 'User revoked successfuly. ' }) raise tornado.gen.Return(success)
def get_openvpn_status(): """Just gets the openvpn status information, which lists the current clients. """ status = call_master_cmd('openvpn.get_status') raise tornado.gen.Return(status)