Example #1
0
 def _connect(self):
     api = Pepper(('http://'
                   + self.salt_connection_details.salt_master
                   + ':'
                   + str(self.salt_connection_details.port)))
     api.login(self.salt_connection_details.username, self.salt_connection_details.password,
               'pam')
     return api
Example #2
0
def get_events():
    api = Pepper(url, ignore_ssl_errors=True)
    # TODO: find a way.
    user = User.objects.first()
    api.login(
        str(user.username),
        user.user_settings.token,
        os.environ.get("SALT_AUTH", "alcali"),
    )
    response = api.req_stream("/events")
    return response
Example #3
0
def verify_password(nickname, password):
    """
    Password verification callback.

    ---
    securityDefinitions:
      UserSecurity:
        type: basic
    """
    if not nickname or not password:
        return False

    api = Pepper('http://master:8080')
    try:
        auth = api.login(nickname, password, 'pam')
    except PepperException:
        return False

    user = User.query.filter_by(nickname=nickname).first()
    if not user:
        user = User.create({'nickname': nickname})

    current_app.logger.debug('auth')
    current_app.logger.debug(auth)
    user.token = auth['token']
    db.session.add(user)
    db.session.commit()

    g.current_user = user

    return True
Example #4
0
def api_connect():
    # TODO fix this!
    user = get_current_user()
    api = Pepper(url, ignore_ssl_errors=True)
    login_ret = api.login(
        str(user.username),
        user.user_settings.token,
        os.environ.get("SALT_AUTH", "alcali"),
    )
    user.user_settings.salt_permissions = json.dumps(login_ret["perms"])
    user.save()
    return api
Example #5
0
def api_connect():
    user = get_current_user()
    api = Pepper(url, ignore_ssl_errors=True)
    try:
        login_ret = api.login(
            str(user.username),
            user.user_settings.token,
            os.environ.get("SALT_AUTH", "alcali"),
        )
    except URLError:
        raise PepperException("URL Error")
    user.user_settings.salt_permissions = json.dumps(login_ret["perms"])
    user.save()
    return api
Example #6
0
#!/usr/bin/python3
from pepper import Pepper

api = Pepper('http://192.168.122.198:8080')

# api = Pepper('http://192.168.178.92:8080')

# print(api)

api.login('test', 'test', 'pam')

# run a command on the minion
#print(api.low([{'client': 'local','tgt': 'salt-master','fun': 'cmd.run','arg': 'salt state.apply utils test=True'}]))

# run connectivy test
# print(api.low([{'client': 'local', 'tgt': 'test-minion', 'fun': 'test.ping'}]))

#
#print(api.low([{'client': 'wheel', 'tgt': '*', 'fun': 'key.list_all'}]))
# accept minion
# print(api.low([{'client': 'wheel', 'tgt': '*', 'fun': 'key.accept', 'match': 'b9840e25adfd'}]))

print(
    api.low([{
        'client': 'local',
        'tgt': 'salt-master',
        'fun': 'state.apply',
        'arg': ["utils", "test=True"]
    }]))
Example #7
0
class SaltApi(object):
    def __init__(self):
        self.api = Pepper(settings.get_variable('NUTS_SALT_REST_API_URL'))

    def connect(self):
        '''connects to the salt-api with the defined username and password.
        configuration can be done in config.yml in your cwd'''
        self.api.login(settings.get_variable('NUTS_SALT_REST_API_USERNAME'),
                       settings.get_variable('NUTS_SALT_REST_API_PASSWORD'),
                       settings.get_variable('NUTS_SALT_REST_API_EAUTH'))

    def start_task(self, args):
        ''' starts a the function defined in args.function on targets (args.targets)
        with the parameters defined in args.arguments

            response format: {u'return': [{u'srv01':True,u'srv020:True}]}
        '''

        response = self.api.low([{
            'client': 'local',
            'tgt': args['targets'],
            'fun': args['function'],
            'arg': args['arguments']
        }])

        return response

    def start_task_async(self, args):
        ''' starts a the function defined in args.function on targets (args.targets) with
        the parameters defined in args.arguments this function is asnyc
        and you'll have to collect the results with get_task_result

            response format: {u'return': [{u'jid': u'20160718141822884336', u'minions': [u'srv01']}]}
        '''
        response = self.api.low([{
            'client': 'local_async',
            'tgt': args['targets'],
            'fun': args['function'],
            'arg': args['arguments']
        }])

        return response

    def abort_task(self, args):
        '''aborts the task with the taskid defined in args.job_id'''
        response = self.api.low([{
            'client': 'local_async',
            'tgt': '*',
            'fun': 'saltutil.kill_job',
            'arg': [args.job_id]
        }])
        return response

    def get_task_result(self, taskresponse=None, taskid=None):
        '''returns the current result of the entered task.
        you can either insert the response you got when you started the task OR
        you can insert the taskid directly

        response format: {u'return': [{u'srv01':True,u'srv020:True}]}
        '''
        if (taskid is not None) and (taskresponse is not None) and (
                taskresponse['return'][0]['jid'] != taskid):
            raise ValueError(
                'You entered both taskresponse and taskid but they didn\'t match'
            )
        if taskid is None:
            taskid = taskresponse['return'][0]['jid']
        return self.api.lookup_jid(taskid)