Пример #1
0
def ansible_jeneric(job_id, user_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + user_id
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))

    # finally, get the actual job
    job = myExternalData.get(URL, job_id)

    myHostList = job['host_list'] + ','
    myModuleName = job['module_name']
    myModuleArgs = job['module_args']
    myPattern = job['pattern']
    myRemoteUser = job['remote_user']
    myRemotePass = job['remote_pass']

    runString = ""

    for arg in myHostList, myModuleName, myModuleArgs, myPattern, myRemoteUser, myRemotePass:
        if (arg):
            runString = runString + arg

    results = ansible.runner.Runner(
        pattern=myPattern,
        forks=10,
        module_name=myModuleName,
        module_args=myModuleArgs,
        remote_user=myRemoteUser,
        remote_pass=myRemotePass,
        host_list=myHostList,
    ).run()
    # run the ansible stuffs
    #results = ansible.runner.Runner(
    #    pattern=myHost, forks=10,
    #    module_name='command', module_args=myCommand,
    #).run()

    # get it to a good format
    #data = json.loads(results)
    #data = json.dumps(results)

    # set status to COMPLETE
    myExternalData.patch(job_id, json.loads('{"status":"COMPLETE"}'))

    if type(results) == dict:
        results = utils.jsonify(results)

    # post results to firebase
    myExternalData.post(job_id + '/returns', results)
    #returns.patch(job_id + '/returns', json.dumps(results))
    return results
Пример #2
0
def ansible_jeneric_testing(job_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    URL = 'https://deploynebula.firebaseio.com/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))

    # finally, get the actual job
    job = myExternalData.get(URL, job_id)

    myHostList = job['host_list'] + ','
    myModuleName = job['module_name']
    if (job['module_args']):
        myModuleArgs = job['module_args']
    else:
        myModuleArgs = ''
    myPattern = job['pattern']
    myRemoteUser = job['remote_user']
    myRemotePass = job['remote_pass']

    #myKeyFile = job['private_key_file']

    #tmpFile = open("/tmp/" + job_id, "w")
    #tmpFile.write(myKeyFile)
    #tmpFile.close()

    results = ansible.runner.Runner(pattern=myPattern,
                                    forks=10,
                                    module_name=myModuleName,
                                    module_args=myModuleArgs,
                                    remote_user=myRemoteUser,
                                    remote_pass=myRemotePass,
                                    host_list=myHostList
                                    #private_key_file='/tmp/keykey'
                                    ).run()

    # get it to a good format
    #data = json.loads(results)
    #data = json.dumps(results)

    # set status to COMPLETE
    myExternalData.patch(job_id, json.loads('{"status":"COMPLETE"}'))

    #if type(results) == dict:
    #    results = utils.jsonify(results)

    # post results to firebase
    myExternalData.post(job_id + '/returns', json.loads(results),
                        {'print': 'pretty'}, {'X_FANCY_HEADER': 'VERY FANCY'})
    #returns.patch(job_id + '/returns', json.dumps(results))
    return results
Пример #3
0
def ansible_jeneric(job_id, user_id):
    
    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + user_id
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))
    
    # finally, get the actual job
    job = myExternalData.get(URL, job_id)

    myHostList = job['host_list'] +','
    myModuleName = job['module_name']
    myModuleArgs = job['module_args']
    myPattern = job['pattern']
    myRemoteUser = job['remote_user']
    myRemotePass = job['remote_pass']

    runString = ""

    for arg in myHostList, myModuleName, myModuleArgs, myPattern, myRemoteUser, myRemotePass:
        if ( arg ):
            runString = runString + arg
    
    results = ansible.runner.Runner(
        pattern=myPattern,
        forks=10,
        module_name=myModuleName,
        module_args=myModuleArgs,
        remote_user=myRemoteUser,
        remote_pass=myRemotePass,
        host_list=myHostList,
    ).run()
    # run the ansible stuffs
    #results = ansible.runner.Runner(
    #    pattern=myHost, forks=10,
    #    module_name='command', module_args=myCommand,
    #).run()    

    # get it to a good format
    #data = json.loads(results)
    #data = json.dumps(results)

    # set status to COMPLETE
    myExternalData.patch(job_id, json.loads('{"status":"COMPLETE"}'))
    
    if type(results) == dict:
        results = utils.jsonify(results)
    
    # post results to firebase
    myExternalData.post(job_id + '/returns', results)
    #returns.patch(job_id + '/returns', json.dumps(results))
    return results
Пример #4
0
def ansible_jeneric_testing(job_id):
    
    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    URL = 'https://deploynebula.firebaseio.com/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))
    
    # finally, get the actual job
    job = myExternalData.get(URL, job_id)

    myHostList = job['host_list'] +','
    myModuleName = job['module_name']
    if (job['module_args']): 
        myModuleArgs = job['module_args']
    else:
        myModuleArgs = ''
    myPattern = job['pattern']
    myRemoteUser = job['remote_user']
    myRemotePass = job['remote_pass']

    #myKeyFile = job['private_key_file']

    #tmpFile = open("/tmp/" + job_id, "w")
    #tmpFile.write(myKeyFile)
    #tmpFile.close()

    results = ansible.runner.Runner(
        pattern=myPattern,
        forks=10,
        module_name=myModuleName,
        module_args=myModuleArgs,
        remote_user=myRemoteUser,
        remote_pass=myRemotePass,
        host_list=myHostList
        #private_key_file='/tmp/keykey'
    ).run()

    # get it to a good format
    #data = json.loads(results)
    #data = json.dumps(results)

    # set status to COMPLETE
    myExternalData.patch(job_id, json.loads('{"status":"COMPLETE"}'))

    #if type(results) == dict:
    #    results = utils.jsonify(results)

    # post results to firebase
    myExternalData.post(job_id + '/returns', json.loads(results), {'print': 'pretty'}, {'X_FANCY_HEADER': 'VERY FANCY'})
    #returns.patch(job_id + '/returns', json.dumps(results))
    return results
Пример #5
0
def firebase_inspector(request):
    """a independent inspector that clean after itself
    """
    from firebase import FirebaseApplication
    client = FirebaseApplication(FIRE_URL)

    def teardown():
        print '---- firebase cleanup ----'
        client.delete('test', None)
        client.delete('chat', None)
        print '-- firebase cleanup end --'

    request.addfinalizer(teardown)
    return client
Пример #6
0
def ansible_command_run(job_id, user_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + user_id
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))

    # finally, get the actual job
    job = myExternalData.get(URL, job_id)

    myHost = job['host']
    myCommand = job['command']

    # run the ansible stuffs
    results = ansible.runner.Runner(
        pattern=myHost,
        forks=10,
        module_name='command',
        module_args=myCommand,
    ).run()

    # get it to a good format
    #data = json.loads(results)
    #data = json.dumps(results)

    # set status to COMPLETE
    myExternalData.patch(job_id, json.loads('{"status":"COMPLETE"}'))

    # post results to firebase
    myExternalData.post(job_id + '/returns', json.dumps(results))
    #returns.patch(job_id + '/returns', json.dumps(results))

    return results
Пример #7
0
def ansible_command_run(job_id, user_id):
    
    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + user_id
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))
    
    # finally, get the actual job
    job = myExternalData.get(URL, job_id)

    myHost = job['host']
    myCommand = job['command']

    # run the ansible stuffs
    results = ansible.runner.Runner(
        pattern=myHost, forks=10,
        module_name='command', module_args=myCommand,
    ).run()    

    # get it to a good format
    #data = json.loads(results)
    #data = json.dumps(results)

    # set status to COMPLETE
    myExternalData.patch(job_id, json.loads('{"status":"COMPLETE"}'))
    
    # post results to firebase
    myExternalData.post(job_id + '/returns', json.dumps(results))
    #returns.patch(job_id + '/returns', json.dumps(results))

    return results
Пример #8
0
class Firebase_Backend(PiSmoker_Backend):
    firebase_inst = None
    """@type self.firebase_inst: firebase"""
    fb_params = {'print': 'silent'}
    _polling_interval = 3  # Frequency to poll web for new parameters
    _read_program_interval = 60  # Frequency to poll web for new program

    _last_write_parameters = time()
    _last_read_parameters = time()
    _last_write_program = time()
    _last_read_program = time()
    _last_write_control = time()
    _last_read_control = time()
    _do_async = True
    _init_args = None
    _init_kwargs = None

    def __init__(self, app_url, auth_secret, async=True, *args, **kwargs):
        auth = FirebaseAuthentication(secret=auth_secret,
                                      email='*****@*****.**')
        self.firebase_inst = FirebaseApplication(app_url, authentication=auth)
        self._do_async = async
Пример #9
0
def populate_playbooks(user_id, project_id, playbook_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesgit/'
    #myExternalData = FirebaseApplication(URL)
    myExternalData = FirebaseApplication(URL, authentication)

    job = myExternalData.get(URL, playbook_id)

    # get playbooks here
    # git clone project
    git_url = myExternalData.get(URL + playbook_id, '/url')
    git_dir = '/tmp/' + project_id + myExternalData.get(URL + playbook_id, '/name')
    cloned_proj = Repo.clone_from(git_url, git_dir)

    # get the git project description and store in firebase
    #desc = cloned_proj.description
    #myExternalData.put(playbook_id + '/description', desc)
    ## Out[8]: u"Unnamed repository; edit this file 'description' to name the repository."


    # find all .yml and .yaml files for run listing
    os.chdir(git_dir)
    playbooks = glob.glob('*.y*ml')
    ## Out[5]: ['new.yaml', 'controller.yml', 'compute-node.yml', 'site.yml']

    # remove git project directory
    #os.chdir('/')
    shutil.rmtree(git_dir)

    # add playbooks found here
    #playbooks = ['new.yaml', 'controller.yml', 'compute-node.yml', 'site.yml']
    for play in playbooks:
        myExternalData.post(playbook_id + '/playbooks', {'name': play})

    return
Пример #10
0
def run_ansible_playbook(user_id, project_id, playbook_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/roles/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status":"SETUP"})

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, playbook_id)


    ##
    ## Create full Ansible Inventory, playbook defines hosts to run on
    ##
    # set and get Ansible Project Inventory
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    inventory_list = myExternalData.get(URL, '/inventory')


    tmpHostList = []
    for key, host in inventory_list.iteritems():
        tmpHostList.append(host['name'])

    # creating Ansible Inventory based on host_list
    myInventory = ansible.inventory.Inventory(tmpHostList)

    # set Host objects in Inventory object based on hosts_lists
    # NEED: to set other host options
    # BUG: hostnames with periods (.) do not work
    for key, host in inventory_list.iteritems():
        tmpHost = myInventory.get_host(host['name'])
        tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
        tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
        if host.has_key('ansible_ssh_user'):
            tmpHost.set_variable("ansible_ssh_pass", host['ansible_ssh_pass'])
        # Group Stuffs
        if myInventory.get_group(host['group']) is None:
            # set up new group
            tmpGroup = ansible.inventory.Group(host['group'])
            tmpGroup.add_host(myInventory.get_host(host['name']))
            myInventory.add_group(tmpGroup)
        else:
            # just add to existing group
            tmpGroup = myInventory.get_group(host['group'])
            tmpGroup.add_host(myInventory.get_host(host['name']))


    ##
    ## Create temp playbook file
    ##
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/roles/'
    playbook = myExternalData.get(URL, playbook_id)

    # order tasks in playbook base on order field in dict
    def func(key):
        return playbook['modules'][key]['order']
    sorted_tasks_keys = sorted(playbook['modules'].keys(), key=func)

    tmpPlay = open("/tmp/" + playbook_id + '.yml', "w")

    tmpPlay.write("---\n")
    tmpPlay.write("- name: %s\n" % playbook['name'])
    if playbook.has_key('playHosts'):
        tmpPlay.write("  hosts: %s\n" % playbook['playHosts'])
    if playbook.has_key('playUsername'):
        tmpPlay.write("  remote_user: %s\n" % playbook['playUsername'])
    tmpPlay.write("\n")
    # if has variables
    if playbook.has_key('variables'):
        tmpPlay.write("  vars:\n")
        for key, var in playbook['variables'].iteritems():
            tmpPlay.write("    %s: %s\n" % (var['name'], var['value']))
        tmpPlay.write("\n")
    # if has tasks
    if playbook.has_key('modules'):
        tmpPlay.write("  tasks:\n")
        # if has includes
        if playbook.has_key('includes'):
            for key, include in playbook['includes']:
                tmpPlay.write("    - include: %s" % include['name'])
        # now for the tasks
        for key in sorted_tasks_keys:
            tmpPlay.write("    - name: %s\n" % playbook['modules'][key]['name'])
            tmpPlay.write("      %s: " % playbook['modules'][key]['option'])
            for option in playbook['modules'][key]['options']:
                # for command and shell modules one-off crapness
                if option['paramater']:
                    tmpPlay.write("%s=%s " % (option['paramater'], option['value']))
                else:
                    tmpPlay.write("%s " % option['value'])
            tmpPlay.write("\n")
            # if has notify
            if option.has_key('notify'):
                tmpPlay.write("      notify:\n")
                tmpPlay.write("        - %s\n" % option['notify'])
            tmpPlay.write("\n")
    # if has handlers
    if playbook.has_key('handlers'):
        tmpPlay.write("  handlers:\n")
        for key, handler in playbook['handlers'].iteritems():
            tmpPlay.write("    - name: %s\n" % handler['name'])
            tmpPlay.write("      service: name=%s state=%s\n\n" % (handler['service_name'], handler['service_state']))

    # close file
    tmpPlay.close()


    # get ssh key file
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    ssh_key = myExternalData.get(URL, '/ssh_key')

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status":"RUNNING"})
    try:
        prev = sys.stdout
        prev2 = sys.stderr
        sys.stdout = StringIO()
        sys.stderr = StringIO()

        # Run Ansible PLaybook
        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        if ssh_key is not None:
            # set up ssh key in tmp
            tmpKey = open("/tmp/" + playbook_id + '_key', "w")
            tmpKey.write(ssh_key)
            # close file
            tmpKey.close()
            os.chmod("/tmp/" + playbook_id + '_key', 0600)

            # now ansible playbook
            play = ansible.playbook.PlayBook(
                playbook='/tmp/' + playbook_id + '.yml',
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                private_key_file='/tmp/' + playbook_id + '_key',
                forks=10
            ).run()
            # delete tmp key file
            os.remove('/tmp/' + playbook_id + '_key')
        else:
            play = ansible.playbook.PlayBook(
                playbook='/tmp/' + playbook_id + '.yml',
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                forks=10
            ).run()

        myStdout = sys.stdout.getvalue()
        myStderr = sys.stderr.getvalue()
        sys.stdout = prev
        sys.stderr = prev2
        #myExternalData.patch(playbook_id, {'stdout': myStdout})
        # update status to RUNNING in firebase
        myExternalData.patch(playbook_id, {"status":"COMPLETE"})
        myExternalData.post(playbook_id + '/returns', {'stats': sanitize_keys(play), 'stdout': convert_bash_colors(myStdout)})
        #myExternalData.patch(playbook_id, {'stderr': myStderr})
    except AnsibleError as e:
        # set status to error
        myExternalData.patch(playbook_id, {"status":"ERROR"})
        myExternalData.post(playbook_id + '/returns', {'stdout': e.message})
    except:
        # set status to error
        myExternalData.patch(playbook_id, {"status":"ERROR"})
        myExternalData.post(playbook_id + '/returns', {'stdout': sys.exc_info()[0]})

    finally:
        #print myStdout
        #print myStderr
        #print play
        sys.stdout = prev
        sys.stderr = prev2
        # set end timestamp
        myExternalData.patch(playbook_id, {"endedAt": {".sv": "timestamp"}})
        # delete tmp playbook file
        os.remove('/tmp/' + playbook_id + '.yml')

    return
Пример #11
0
def ansible_playbook_manual(user_id, project_id, playbook_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesmanual/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status": "RUNNING"})

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, playbook_id)

    ##
    ## Create full Ansible Inventory, playbook defines hosts to run on
    ##
    # set and get Ansible Project Inventory
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    inventory_list = myExternalData.get(URL, '/inventory')

    tmpHostList = []
    if inventory_list:
        for key, host in inventory_list.iteritems():
            tmpHostList.append(host['name'])

        # creating Ansible Inventory based on host_list
        myInventory = ansible.inventory.Inventory(tmpHostList)

        # set Host objects in Inventory object based on hosts_lists
        # NEED: to set other host options
        # BUG: hostnames with periods (.) do not work
        for key, host in inventory_list.iteritems():
            tmpHost = myInventory.get_host(host['name'])
            tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
            tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
            if host.has_key('ansible_ssh_pass'):
                tmpHost.set_variable("ansible_ssh_pass",
                                     host['ansible_ssh_pass'])
            # Group Stuffs
            if myInventory.get_group(host['group']) is None:
                # set up new group
                tmpGroup = ansible.inventory.Group(host['group'])
                tmpGroup.add_host(myInventory.get_host(host['name']))
                myInventory.add_group(tmpGroup)
            else:
                # just add to existing group
                tmpGroup = myInventory.get_group(host['group'])
                tmpGroup.add_host(myInventory.get_host(host['name']))
    else:
        myInventory = ansible.inventory.Inventory()

    ##
    ## Create temp playbook file
    ##
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesmanual/' + playbook_id
    playbook = myExternalData.get(URL, '/playbook')

    tmpPlay = open("/tmp/" + playbook_id + '.yml', "w")

    tmpPlay.write(playbook)

    # close file
    tmpPlay.close()

    # get ssh key file
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    ssh_key = myExternalData.get(URL, '/ssh_key')

    tmpKey = open("/tmp/" + playbook_id + '_key', "w")
    tmpKey.write(ssh_key)
    # close file
    tmpKey.close()
    os.chmod("/tmp/" + playbook_id + '_key', 0600)

    prev = sys.stdout
    prev2 = sys.stderr
    try:
        sys.stdout = StringIO()
        sys.stderr = StringIO()

        # Run Ansible PLaybook
        stats = ansible.callbacks.AggregateStats()
        playbook_cb = ansible.callbacks.PlaybookCallbacks(
            verbose=utils.VERBOSITY)
        runner_cb = ansible.callbacks.PlaybookRunnerCallbacks(
            stats, verbose=utils.VERBOSITY)

        if ssh_key is not None:
            play = ansible.playbook.PlayBook(
                playbook='/tmp/' + playbook_id + '.yml',
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                private_key_file='/tmp/' + playbook_id + '_key',
                forks=10).run()
        else:
            play = ansible.playbook.PlayBook(playbook='/tmp/' + playbook_id +
                                             '.yml',
                                             inventory=myInventory,
                                             runner_callbacks=runner_cb,
                                             stats=stats,
                                             callbacks=playbook_cb,
                                             forks=10).run()

        #play = ansible.playbook.PlayBook(
        #    playbook='/tmp/' + playbook_id + '.yml',
        #    inventory=myInventory,
        #    runner_callbacks=runner_cb,
        #    stats=stats,
        #    callbacks=playbook_cb,
        #    forks=10
        #).run()

        myStdout = sys.stdout.getvalue()
        myStderr = sys.stderr.getvalue()
        #myExternalData.patch(playbook_id, {'stdout': myStdout})
        myExternalData.post(playbook_id + '/returns', {
            'stats': sanitize_keys(play),
            'stdout': myStdout
        })
        #myExternalData.patch(playbook_id, {'stderr': myStderr})
    finally:
        sys.stdout = prev
        sys.stderr = prev2

    ##
    ## Post play results in to firebase
    ##
    ## WHERE?
    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status": "COMPLETE"})
    #myExternalData.post(playbook_id + '/returns', play)

    # delete tmp playbook file
    #os.remove("/tmp/" + playbook_id + '.yml')

    return play
Пример #12
0
    def get(self, user_id, project_id, job_id):

        # firebase authentication
        SECRET = os.environ['SECRET']
        authentication = FirebaseAuthentication(SECRET, True, True)

        # set the specific job from firebase with user
        user = '******' + str(user_id)
        URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/external_data/'
        #myExternalData = FirebaseApplication(URL)
        myExternalData = FirebaseApplication(URL, authentication)

        # update status to RUNNING in firebase
        myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))

        # finally, get the actual job and set ansible options
        job = myExternalData.get(URL, job_id)

        # set vars from job data in firebase
        myHostList = job['host_list']
        myModuleName = job['module_name']
        if 'module_args' in job.keys():
            myModuleArgs = job['module_args']
        else:
            myModuleArgs = ''
        myPattern = job['pattern']
        myRemoteUser = job['remote_user']
        myRemotePass = job['remote_pass']
        myProjectID = job['project_id']

        # set and get Ansible Project Inventory
        URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + myProjectID
        job = myExternalData.get(URL, 'inventory')

        # creating Ansible Inventory based on host_list
        myInventory = ansible.inventory.Inventory(myHostList)

        # set Host objects in Inventory object based on hosts_lists
        # NEED: to set other host options
        # BUG: hostnames with periods (.) do not work
        for key, host in job.iteritems():
            if host['name'] in myHostList:
                tmpHost = myInventory.get_host(host['name'])
                tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
                tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
                if host.has_key('ansible_ssh_pass'):
                    tmpHost.set_variable("ansible_ssh_pass", host['ansible_ssh_pass'])
                # Group Stuffs
                if myInventory.get_group(host['group']) is None:
                    # set up new group
                    tmpGroup = ansible.inventory.Group(host['group'])
                    tmpGroup.add_host(myInventory.get_host(host['name']))
                    myInventory.add_group(tmpGroup)
                else:
                    # just add to existing group
                    tmpGroup = myInventory.get_group(host['group'])
                    tmpGroup.add_host(myInventory.get_host(host['name']))

        # get ssh key file
        URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
        ssh_key = myExternalData.get(URL, '/ssh_key')

        tmpKey = open("/tmp/" + project_id + '_key', "w")
        tmpKey.write(ssh_key)
        # close file
        tmpKey.close()
        os.chmod("/tmp/" + project_id + '_key', 0600)

        # run ansible module
        if ssh_key is not None:
            results = ansible.runner.Runner(
    	    pattern=myPattern,
           	    forks=10,
     	    module_name=myModuleName,
    	    module_args=myModuleArgs,
    	    #remote_user=myRemoteUser,
    	    #remote_pass=myRemotePass,
                private_key_file='/tmp/' + project_id + '_key',
    	    inventory=myInventory,
            ).run()
        else:
            results = ansible.runner.Runner(
    	    pattern=myPattern,
           	    forks=10,
     	    module_name=myModuleName,
    	    module_args=myModuleArgs,
    	    #remote_user=myRemoteUser,
    	    #remote_pass=myRemotePass,
    	    inventory=myInventory
            ).run()


        # set status to COMPLETE
        myExternalData.patch(job_id, {"status":"COMPLETE"})

        # jsonify the results
        json_results = ansible.utils.jsonify(results)

        #
        # HELP! can't get a proper json object to pass, but below string works
        #
        myExternalData.post(job_id + '/returns/', sanitize_keys(results))

        os.remove('/tmp/' + project_id + '_key')

        return results
Пример #13
0
def ansible_playbook(user_id, project_id, playbook_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/roles/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status": "RUNNING"})

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, playbook_id)

    ##
    ## Create full Ansible Inventory, playbook defines hosts to run on
    ##
    # set and get Ansible Project Inventory
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    inventory_list = myExternalData.get(URL, '/inventory')

    tmpHostList = []
    for key, host in inventory_list.iteritems():
        tmpHostList.append(host['name'])

    # creating Ansible Inventory based on host_list
    myInventory = ansible.inventory.Inventory(tmpHostList)

    # set Host objects in Inventory object based on hosts_lists
    # NEED: to set other host options
    # BUG: hostnames with periods (.) do not work
    for key, host in inventory_list.iteritems():
        tmpHost = myInventory.get_host(host['name'])
        tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
        tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
        if host.has_key('ansible_ssh_user'):
            tmpHost.set_variable("ansible_ssh_pass", host['ansible_ssh_pass'])
        # Group Stuffs
        if myInventory.get_group(host['group']) is None:
            # set up new group
            tmpGroup = ansible.inventory.Group(host['group'])
            tmpGroup.add_host(myInventory.get_host(host['name']))
            myInventory.add_group(tmpGroup)
        else:
            # just add to existing group
            tmpGroup = myInventory.get_group(host['group'])
            tmpGroup.add_host(myInventory.get_host(host['name']))

    ##
    ## Create temp playbook file
    ##
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/roles/'
    playbook = myExternalData.get(URL, playbook_id)

    # order tasks in playbook base on order field in dict
    def func(key):
        return playbook['modules'][key]['order']

    sorted_tasks_keys = sorted(playbook['modules'].keys(), key=func)

    tmpPlay = open("/tmp/" + playbook_id + '.yml', "w")

    tmpPlay.write("---\n")
    tmpPlay.write("- name: %s\n" % playbook['name'])
    tmpPlay.write("  hosts: %s\n" % playbook['playHosts'])
    tmpPlay.write("  remote_user: %s\n" % playbook['playUsername'])
    tmpPlay.write("\n")
    # if has variables
    if playbook.has_key('variables'):
        tmpPlay.write("  vars:\n")
        for key, var in playbook['variables'].iteritems():
            tmpPlay.write("    %s: %s\n" % (var['name'], var['value']))
        tmpPlay.write("\n")
    # if has tasks
    if playbook.has_key('modules'):
        tmpPlay.write("  tasks:\n")
        # if has includes
        if playbook.has_key('includes'):
            for key, include in playbook['includes']:
                tmpPlay.write("    - include: %s" % include['name'])
        # now for the tasks
        for key in sorted_tasks_keys:
            tmpPlay.write("    - name: %s\n" %
                          playbook['modules'][key]['name'])
            tmpPlay.write("      %s: " % playbook['modules'][key]['option'])
            for option in playbook['modules'][key]['options']:
                # for command and shell modules one-off crapness
                if option['paramater']:
                    tmpPlay.write("%s=%s " %
                                  (option['paramater'], option['value']))
                else:
                    tmpPlay.write("%s " % option['value'])
            tmpPlay.write("\n")
            # if has notify
            if option.has_key('notify'):
                tmpPlay.write("      notify:\n")
                tmpPlay.write("        - %s\n" % option['notify'])
            tmpPlay.write("\n")
    # if has handlers
    if playbook.has_key('handlers'):
        tmpPlay.write("  handlers:\n")
        for key, handler in playbook['handlers'].iteritems():
            tmpPlay.write("    - name: %s\n" % handler['name'])
            tmpPlay.write("      service: name=%s state=%s\n\n" %
                          (handler['service_name'], handler['service_state']))

    # close file
    tmpPlay.close()

    # get ssh key file
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    ssh_key = myExternalData.get(URL, '/ssh_key')

    tmpKey = open("/tmp/" + playbook_id + '_key', "w")
    tmpKey.write(ssh_key)
    # close file
    tmpKey.close()
    os.chmod("/tmp/" + playbook_id + '_key', 0600)

    prev = sys.stdout
    prev2 = sys.stderr
    try:
        sys.stdout = StringIO()
        sys.stderr = StringIO()

        # Run Ansible PLaybook
        stats = ansible.callbacks.AggregateStats()
        playbook_cb = ansible.callbacks.PlaybookCallbacks(
            verbose=utils.VERBOSITY)
        runner_cb = ansible.callbacks.PlaybookRunnerCallbacks(
            stats, verbose=utils.VERBOSITY)

        if ssh_key is not None:
            play = ansible.playbook.PlayBook(
                playbook='/tmp/' + playbook_id + '.yml',
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                private_key_file='/tmp/' + playbook_id + '_key',
                forks=10).run()
        else:
            play = ansible.playbook.PlayBook(playbook='/tmp/' + playbook_id +
                                             '.yml',
                                             inventory=myInventory,
                                             runner_callbacks=runner_cb,
                                             stats=stats,
                                             callbacks=playbook_cb,
                                             forks=10).run()

        myStdout = sys.stdout.getvalue()
        myStderr = sys.stderr.getvalue()
        #myExternalData.patch(playbook_id, {'stdout': myStdout})
        myExternalData.post(playbook_id + '/returns', {
            'stats': sanitize_keys(play),
            'stdout': myStdout
        })
        #myExternalData.patch(playbook_id, {'stderr': myStderr})
    finally:
        sys.stdout = prev
        sys.stderr = prev2

    ##
    ## Post play results in to firebase
    ##
    ## WHERE?
    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status": "COMPLETE"})
    #myExternalData.post(playbook_id + '/returns', play)

    # delete tmp playbook file
    os.remove('/tmp/' + playbook_id + '.yml')
    # delete tmp key file
    os.remove('/tmp/' + playbook_id + '_key')

    return play
Пример #14
0
from firebase import FirebaseApplication
from firebase import firebase
from flask import Flask, render_template, request, session, redirect, jsonify, url_for
import flask
import re
import ast
import difflib

app = Flask(__name__)
firebase_search = FirebaseApplication(
    'https://inf551-project-e0336.firebaseio.com', None)
firebase_register = FirebaseApplication('https://sign-up-d8e33.firebaseio.com',
                                        None)
firebase_savesSearch = FirebaseApplication(
    'https://history-data.firebaseio.com/.json', None)

result = firebase_search.get('/', '')  # dict

app.config.update(
    dict(SECRET_KEY="powerful secretkey",
         WTF_CSRF_SECRET_KEY="a csrf secret key"))

# global display
# global searchByFacet


def convert_keys_to_string(dictionary):
    if not isinstance(dictionary, dict):
        return dictionary
    return {str(k).replace("u", ""): str(v) for k, v in dictionary.items()}
Пример #15
0
def run_rax_create_server(user_id, project_id, job_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, {"status":"BUILDING"})

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, job_id)

    tmpUsername = job['rax_username']
    tmpAPIkey = job['rax_apikey']
    tmpFlavor = job['flavor']
    tmpImage = job['image']
    tmpServerName = job['servername']
    tmpGroup = job['group']
    tmpRegion = job['region']

    # set RAX cloud authentication
    pyrax.set_setting("identity_type", "rackspace")
    try:
        pyrax.set_credentials(job['rax_username'], job['rax_apikey'])
    except:
        pass
    try:
        pyrax.set_credentials(str(job['rax_username']), str(job['rax_apikey']))
    except:
        pass

    # set region
    cs = pyrax.connect_to_cloudservers(tmpRegion)

    # create objects
    #cs = pyrax.cloudservers

    # create cloud server
    server = cs.servers.create(tmpServerName, tmpImage, tmpFlavor)

    # add return object to firebase, but wait for networks
    myExternalData.patch(job_id, {'password': server.adminPass})

    # wait for networks then add to firebase
    #while not (server.networks):
    #    server = cs.servers.get(server.id)
    newServer = pyrax.utils.wait_for_build(server)

    # write results to firebase
    myExternalData.patch(job_id, {'networks':newServer.networks})

    # update firebase, add new server to inventory list
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/'
    myInventory = FirebaseApplication(URL, authentication)
    if ':' in newServer.networks['public'][0]:
        myNetwork = newServer.networks['public'][1]
    else:
        myNetwork = newServer.networks['public'][0]
    myInventory.post('inventory', {'user_id': user,
			                          'name': tmpServerName,
			                          'group': tmpGroup,
			                          'ansible_ssh_host': myNetwork,
			                          'ansible_ssh_user': '******',
			                          'ansible_ssh_pass': server.adminPass })


    # update firebase with status COMPLETE
    myExternalData.patch(job_id, {"status":"COMPLETE"})

    return
Пример #16
0
def ansible_playbook_manual(user_id, project_id, playbook_id):
    
    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesmanual/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status":"RUNNING"})
    
    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, playbook_id)


    ##
    ## Create full Ansible Inventory, playbook defines hosts to run on
    ##
    # set and get Ansible Project Inventory
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    inventory_list = myExternalData.get(URL, '/inventory')


    tmpHostList = []
    if inventory_list:
        for key, host in inventory_list.iteritems():
            tmpHostList.append(host['name'])

        # creating Ansible Inventory based on host_list
        myInventory = ansible.inventory.Inventory(tmpHostList)

        # set Host objects in Inventory object based on hosts_lists
        # NEED: to set other host options
        # BUG: hostnames with periods (.) do not work
        for key, host in inventory_list.iteritems():
            tmpHost = myInventory.get_host(host['name'])
            tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
            tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
            if host.has_key('ansible_ssh_pass'):
                tmpHost.set_variable("ansible_ssh_pass", host['ansible_ssh_pass'])
            # Group Stuffs
            if myInventory.get_group(host['group']) is None:
                # set up new group
                tmpGroup = ansible.inventory.Group(host['group'])
                tmpGroup.add_host(myInventory.get_host(host['name']))
                myInventory.add_group(tmpGroup)
            else:
                # just add to existing group
                tmpGroup = myInventory.get_group(host['group'])
                tmpGroup.add_host(myInventory.get_host(host['name']))
    else:
        myInventory = ansible.inventory.Inventory()

    ##
    ## Create temp playbook file
    ##
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesmanual/' + playbook_id
    playbook = myExternalData.get(URL, '/playbook')

    tmpPlay = open("/tmp/" + playbook_id + '.yml', "w")

    tmpPlay.write(playbook)
   
    # close file
    tmpPlay.close()


    # get ssh key file
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    ssh_key = myExternalData.get(URL, '/ssh_key')

    tmpKey = open("/tmp/" + playbook_id + '_key', "w")
    tmpKey.write(ssh_key)
    # close file
    tmpKey.close()
    os.chmod("/tmp/" + playbook_id + '_key', 0600)


    prev = sys.stdout
    prev2 = sys.stderr
    try:
        sys.stdout = StringIO()
        sys.stderr = StringIO()
        
        # Run Ansible PLaybook
        stats = ansible.callbacks.AggregateStats()
        playbook_cb = ansible.callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = ansible.callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        if ssh_key is not None:
            play = ansible.playbook.PlayBook(
                playbook='/tmp/' + playbook_id + '.yml',
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                private_key_file='/tmp/' + playbook_id + '_key',
                forks=10
            ).run()
        else:
            play = ansible.playbook.PlayBook(
                playbook='/tmp/' + playbook_id + '.yml',
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                forks=10
            ).run()

        #play = ansible.playbook.PlayBook(
        #    playbook='/tmp/' + playbook_id + '.yml',
        #    inventory=myInventory,
        #    runner_callbacks=runner_cb,
        #    stats=stats,
        #    callbacks=playbook_cb,
        #    forks=10
        #).run()

        myStdout = sys.stdout.getvalue()
        myStderr = sys.stderr.getvalue()
        #myExternalData.patch(playbook_id, {'stdout': myStdout})
        myExternalData.post(playbook_id + '/returns', {'stats': sanitize_keys(play), 'stdout': myStdout})
        #myExternalData.patch(playbook_id, {'stderr': myStderr})
    finally:
        sys.stdout = prev
        sys.stderr = prev2

    ##
    ## Post play results in to firebase
    ##
    ## WHERE?
    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status":"COMPLETE"})
    #myExternalData.post(playbook_id + '/returns', play)

    # delete tmp playbook file
    #os.remove("/tmp/" + playbook_id + '.yml')

    return play
Пример #17
0
def rax_create_server(user_id, project_id, job_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/external_data/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, {"status": "BUILDING"})

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, job_id)

    tmpUsername = job['rax_username']
    tmpAPIkey = job['rax_apikey']
    tmpFlavor = job['flavor']
    tmpImage = job['image']
    tmpServerName = job['servername']
    tmpGroup = job['group']
    tmpRegion = job['region']

    # set RAX cloud authentication
    pyrax.set_setting("identity_type", "rackspace")
    try:
        pyrax.set_credentials(job['rax_username'], job['rax_apikey'])
    except:
        pass
    try:
        pyrax.set_credentials(str(job['rax_username']), str(job['rax_apikey']))
    except:
        pass

    # set region
    cs = pyrax.connect_to_cloudservers(tmpRegion)

    # create objects
    #cs = pyrax.cloudservers

    # create cloud server
    server = cs.servers.create(tmpServerName, tmpImage, tmpFlavor)

    # add return object to firebase, but wait for networks
    myExternalData.patch(job_id, {'password': server.adminPass})

    # wait for networks then add to firebase
    #while not (server.networks):
    #    server = cs.servers.get(server.id)
    newServer = pyrax.utils.wait_for_build(server)

    # write results to firebase
    myExternalData.patch(job_id, {'networks': newServer.networks})

    # update firebase, add new server to inventory list
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/'
    myInventory = FirebaseApplication(URL, authentication)
    if ':' in newServer.networks['public'][0]:
        myNetwork = newServer.networks['public'][1]
    else:
        myNetwork = newServer.networks['public'][0]
    myInventory.post(
        'inventory', {
            'user_id': user,
            'name': tmpServerName,
            'group': tmpGroup,
            'ansible_ssh_host': myNetwork,
            'ansible_ssh_user': '******',
            'ansible_ssh_pass': server.adminPass
        })

    # update firebase with status COMPLETE
    myExternalData.patch(job_id, {"status": "COMPLETE"})

    return
Пример #18
0
def ansible_jeneric(user_id, project_id, job_id):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/external_data/'
    #myExternalData = FirebaseApplication(URL)
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(job_id, json.loads('{"status":"RUNNING"}'))

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, job_id)

    # set vars from job data in firebase
    myHostList = job['host_list']
    myModuleName = job['module_name']
    if 'module_args' in job.keys():
        myModuleArgs = job['module_args']
    else:
        myModuleArgs = ''
    myPattern = job['pattern']
    myRemoteUser = job['remote_user']
    myRemotePass = job['remote_pass']
    myProjectID = job['project_id']

    # set and get Ansible Project Inventory
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + myProjectID
    job = myExternalData.get(URL, 'inventory')

    # creating Ansible Inventory based on host_list
    myInventory = ansible.inventory.Inventory(myHostList)

    # set Host objects in Inventory object based on hosts_lists
    # NEED: to set other host options
    # BUG: hostnames with periods (.) do not work
    for key, host in job.iteritems():
        if host['name'] in myHostList:
            tmpHost = myInventory.get_host(host['name'])
            tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
            tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
            if host.has_key('ansible_ssh_pass'):
                tmpHost.set_variable("ansible_ssh_pass",
                                     host['ansible_ssh_pass'])
            # Group Stuffs
            if myInventory.get_group(host['group']) is None:
                # set up new group
                tmpGroup = ansible.inventory.Group(host['group'])
                tmpGroup.add_host(myInventory.get_host(host['name']))
                myInventory.add_group(tmpGroup)
            else:
                # just add to existing group
                tmpGroup = myInventory.get_group(host['group'])
                tmpGroup.add_host(myInventory.get_host(host['name']))

    # get ssh key file
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    ssh_key = myExternalData.get(URL, '/ssh_key')

    tmpKey = open("/tmp/" + project_id + '_key', "w")
    tmpKey.write(ssh_key)
    # close file
    tmpKey.close()
    os.chmod("/tmp/" + project_id + '_key', 0600)

    # run ansible module
    if ssh_key is not None:
        results = ansible.runner.Runner(
            pattern=myPattern,
            forks=10,
            module_name=myModuleName,
            module_args=myModuleArgs,
            #remote_user=myRemoteUser,
            #remote_pass=myRemotePass,
            private_key_file='/tmp/' + project_id + '_key',
            inventory=myInventory,
        ).run()
    else:
        results = ansible.runner.Runner(
            pattern=myPattern,
            forks=10,
            module_name=myModuleName,
            module_args=myModuleArgs,
            #remote_user=myRemoteUser,
            #remote_pass=myRemotePass,
            inventory=myInventory).run()

    # set status to COMPLETE
    myExternalData.patch(job_id, {"status": "COMPLETE"})

    # jsonify the results
    #json_results = ansible.utils.jsonify(results)

    #
    # HELP! can't get a proper json object to pass, but below string works
    #
    myExternalData.post(job_id + '/returns/', sanitize_keys(results))

    os.remove('/tmp/' + project_id + '_key')

    return results
Пример #19
0
def run_ansible_playbook_git(user_id, project_id, playbook_id, play_name):

    # firebase authentication
    SECRET = os.environ['SECRET']
    authentication = FirebaseAuthentication(SECRET, True, True)

    # set the specific job from firebase with user
    user = '******' + str(user_id)
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesgit/'
    myExternalData = FirebaseApplication(URL, authentication)

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status":"SETUP"})

    # finally, get the actual job and set ansible options
    job = myExternalData.get(URL, playbook_id)

    playbooks_url = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id + '/rolesgit/' + playbook_id
    playbooks_data = FirebaseApplication(URL, authentication)
    git_name = playbooks_data.get(playbooks_url, 'name')
    git_url = playbooks_data.get(playbooks_url, 'url')
    run_play = play_name

    ##
    ## Create full Ansible Inventory, playbook defines hosts to run on
    ##
    # set and get Ansible Project Inventory
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    inventory_list = myExternalData.get(URL, '/inventory')

    tmpHostList = []
    for key, host in inventory_list.iteritems():
        tmpHostList.append(host['name'])

    # creating Ansible Inventory based on host_list
    myInventory = ansible.inventory.Inventory(tmpHostList)

    # set Host objects in Inventory object based on hosts_lists
    # NEED: to set other host options
    # BUG: hostnames with periods (.) do not work
    for key, host in inventory_list.iteritems():
        tmpHost = myInventory.get_host(host['name'])
        tmpHost.set_variable("ansible_ssh_host", host['ansible_ssh_host'])
        tmpHost.set_variable("ansible_ssh_user", host['ansible_ssh_user'])
        if host.has_key('ansible_ssh_user'):
            tmpHost.set_variable("ansible_ssh_pass", host['ansible_ssh_pass'])
        # Group Stuffs
        if myInventory.get_group(host['group']) is None:
            # set up new group
            tmpGroup = ansible.inventory.Group(host['group'])
            tmpGroup.add_host(myInventory.get_host(host['name']))
            myInventory.add_group(tmpGroup)
        else:
            # just add to existing group
            tmpGroup = myInventory.get_group(host['group'])
            tmpGroup.add_host(myInventory.get_host(host['name']))

    cloned_proj = Repo.clone_from(git_url, '/tmp/' + project_id + git_name)
    os.chdir('/tmp/' + project_id + git_name)

    # get ssh key file
    URL = 'https://deploynebula.firebaseio.com/users/' + user + '/projects/' + project_id
    ssh_key = myExternalData.get(URL, '/ssh_key')

    # update status to RUNNING in firebase
    myExternalData.patch(playbook_id, {"status":"RUNNING"})
    try:
        prev = sys.stdout
        prev2 = sys.stderr
        sys.stdout = StringIO()
        sys.stderr = StringIO()

        # Run Ansible PLaybook
        stats = ansible.callbacks.AggregateStats()
        playbook_cb = ansible.callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = ansible.callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        if ssh_key is not None:
            # create ssh key file
            tmpKey = open("/tmp/" + playbook_id + '_key', "w")
            tmpKey.write(ssh_key)
            # close file
            tmpKey.close()
            os.chmod("/tmp/" + playbook_id + '_key', 0600)
            # run playbook
            play = ansible.playbook.PlayBook(
                playbook=run_play,
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                private_key_file='/tmp/' + playbook_id + '_key',
                forks=10
            ).run()
            # delete tmp key file
            os.remove('/tmp/' + playbook_id + '_key')
        else:
            play = ansible.playbook.PlayBook(
                playbook=run_play,
                inventory=myInventory,
                runner_callbacks=runner_cb,
                stats=stats,
                callbacks=playbook_cb,
                forks=10
            ).run()

        myStdout = sys.stdout.getvalue()
        myStderr = sys.stderr.getvalue()
        #myExternalData.patch(playbook_id, {'stdout': myStdout})
        myExternalData.patch(playbook_id, {"status":"COMPLETE"})
        myExternalData.post(playbook_id + '/returns', {'stats': sanitize_keys(play), 'stdout': convert_bash_colors(myStdout)})
        #myExternalData.patch(playbook_id, {'stderr': myStderr})
    except AnsibleError as e:
        # set status to error
        myExternalData.patch(playbook_id, {"status":"ERROR"})
        myExternalData.post(playbook_id + '/returns', {'stdout': e.message})
    except:
        # set status to error
        myExternalData.patch(playbook_id, {"status":"ERROR"})
        myExternalData.post(playbook_id + '/returns', {'stdout': sys.exc_info()[0]})
    finally:
        sys.stdout = prev
        sys.stderr = prev2
        # set end timestamp
        myExternalData.patch(playbook_id, {"endedAt": {".sv": "timestamp"}})
        # clean up
        #os.chdir('/tmp')
        # remove git project directory
        shutil.rmtree('/tmp/' + project_id + git_name)

    return
Пример #20
0
 def __init__(s, cred=None, basenode='/1d/control/'):
     if isinstance(cred, basestring):
         with open(cred) as f:
             cred = f.read().split()
     s.conn = FirebaseApplication(*cred)
     s.basenode = basenode