Python traceback.format_exc() Examples

The following are code examples for showing how to use traceback.format_exc(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: ubittool   Author: carlosperate   File: cmds.py    MIT License 6 votes vote down vote up
def read_python_code():
    """Read the MicroPython user code from the micro:bit flash.

    :return: String with the MicroPython code.
    """
    flash_data = programmer.read_flash(
        address=programmer.PYTHON_CODE_START,
        count=(programmer.PYTHON_CODE_END - programmer.PYTHON_CODE_START),
    )
    py_code_hex = _bytes_to_intel_hex(
        flash_data, offset=programmer.PYTHON_CODE_START
    )
    try:
        python_code = extract_script(py_code_hex)
    except Exception as e:
        sys.stderr.write(format_exc(e))
        raise Exception("Could not decode the MicroPython code from flash")
    return python_code


#
# Hex comparison commands
# 
Example 2
Project: pyblish-win   Author: pyblish   File: reduction.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _serve():
    from .util import is_exiting, sub_warning

    while 1:
        try:
            conn = _listener.accept()
            handle_wanted, destination_pid = conn.recv()
            _cache.remove(handle_wanted)
            send_handle(conn, handle_wanted, destination_pid)
            close(handle_wanted)
            conn.close()
        except:
            if not is_exiting():
                import traceback
                sub_warning(
                    'thread for sharing handles raised exception :\n' +
                    '-'*79 + '\n' + traceback.format_exc() + '-'*79
                    )

#
# Functions to be used for pickling/unpickling objects with handles
# 
Example 3
Project: ssm_parameter_store   Author: BWITS   File: ssm_parameter_store.py    GNU General Public License v3.0 6 votes vote down vote up
def create_update_parameter(client, module):
    changed = False
    response = {}

    args = dict(
        Name=module.params.get('name'),
        Value=module.params.get('value'),
        Type=module.params.get('string_type'),
        Overwrite=module.params.get('overwrite')
    )

    if module.params.get('description'):
        args.update(Description=module.params.get('description'))

    if module.params.get('string_type') == 'SecureString':
        args.update(KeyId=module.params.get('key_id'))

    try:
        response = client.put_parameter(**args)
        changed = True
    except ClientError as e:
        module.fail_json(msg=e.message, exception=traceback.format_exc(),
                         **camel_dict_to_snake_dict(e.response))

    return changed, response 
Example 4
Project: ssm_parameter_store   Author: BWITS   File: ssm_parameter_store.py    GNU General Public License v3.0 6 votes vote down vote up
def delete_parameter(client, module):
    changed = False
    response = {}

    try:
        get_response = client.get_parameters(
            Names=[module.params.get('name')]
        )
    except ClientError as e:
        module.fail_json(msg=e.message, exception=traceback.format_exc(),
                         **camel_dict_to_snake_dict(e.response))

    if get_response['Parameters']:
        try:
            response = client.delete_parameter(
                Name=module.params.get('name')
            )
            changed = True
        except ClientError as e:
            module.fail_json(msg=e.message, exception=traceback.format_exc(),
                             **camel_dict_to_snake_dict(e.response))

    return changed, response 
Example 5
Project: ssm_parameter_store   Author: BWITS   File: ssm_parameter_store.py    GNU General Public License v3.0 6 votes vote down vote up
def create_update_parameter(client, module):
    changed = False
    response = {}

    args = dict(
        Name=module.params.get('name'),
        Value=module.params.get('value'),
        Type=module.params.get('string_type'),
        Overwrite=module.params.get('overwrite')
    )

    if module.params.get('description'):
        args.update(Description=module.params.get('description'))

    if module.params.get('string_type') == 'SecureString':
        args.update(KeyId=module.params.get('key_id'))

    try:
        response = client.put_parameter(**args)
        changed = True
    except ClientError as e:
        module.fail_json(msg=e.message, exception=traceback.format_exc(),
                         **camel_dict_to_snake_dict(e.response))

    return changed, response 
Example 6
Project: ssm_parameter_store   Author: BWITS   File: ssm_parameter_store.py    GNU General Public License v3.0 6 votes vote down vote up
def delete_parameter(client, module):
    changed = False
    response = {}

    try:
        get_response = client.get_parameters(
            Names=[module.params.get('name')]
        )
    except ClientError as e:
        module.fail_json(msg=e.message, exception=traceback.format_exc(),
                         **camel_dict_to_snake_dict(e.response))

    if get_response['Parameters']:
        try:
            response = client.delete_parameter(
                Name=module.params.get('name')
            )
            changed = True
        except ClientError as e:
            module.fail_json(msg=e.message, exception=traceback.format_exc(),
                             **camel_dict_to_snake_dict(e.response))

    return changed, response 
Example 7
Project: AutoDL   Author: tanguofu   File: handlers.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def default(self, request, exception):
        self.log(format_exc())
        if issubclass(type(exception), SanicException):
            return text(
                'Error: {}'.format(exception),
                status=getattr(exception, 'status_code', 500),
                headers=getattr(exception, 'headers', dict())
            )
        elif self.debug:
            html_output = self._render_traceback_html(exception, request)

            response_message = ('Exception occurred while handling uri: '
                                '"%s"\n%s')
            logger.error(response_message, request.url, format_exc())
            return html(html_output, status=500)
        else:
            return html(INTERNAL_SERVER_ERROR_HTML, status=500) 
Example 8
Project: AutoDL   Author: tanguofu   File: setup.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_mpi_flags():
    show_command = os.environ.get('HOROVOD_MPICXX_SHOW', 'mpicxx -show')
    try:
        mpi_show_output = subprocess.check_output(
            shlex.split(show_command), universal_newlines=True).strip()
        mpi_show_args = shlex.split(mpi_show_output)
        if not mpi_show_args[0].startswith('-'):
            # Open MPI and MPICH print compiler name as a first word, skip it
            mpi_show_args = mpi_show_args[1:]
        # strip off compiler call portion and always escape each arg
        return ' '.join(
            ['"' + arg.replace('"', '"\'"\'"') + '"' for arg in mpi_show_args])
    except Exception:
        raise DistutilsPlatformError(
            '%s failed (see error below), is MPI in $PATH?\n'
            'Note: If your version of MPI has a custom command to show compilation flags, '
            'please specify it with the HOROVOD_MPICXX_SHOW environment variable.\n\n'
            '%s' % (show_command, traceback.format_exc())) 
Example 9
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: dummy_handler.py    Apache License 2.0 6 votes vote down vote up
def process(self,txt):
        try:
            resp=self.parser.parse_text(txt)
        except:
            self.send_response(500,"Internal server error")
            self.end_headers()
            self.wfile.write(traceback.format_exc().encode("utf-8"))
            self.wfile.flush()
            self.close_connection=True
            return

        self.send_response(200, 'OK')
        self.send_header("Content-type", "text/plain; charset=utf-8")
        self.end_headers()
        self.wfile.write(resp.encode("utf-8"))
        self.wfile.flush()
        self.close_connection=True 
Example 10
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: datadog_event.py    MIT License 6 votes vote down vote up
def _post_event(module):
    try:
        if module.params['host'] is None:
            module.params['host'] = platform.node().split('.')[0]
        msg = api.Event.create(title=module.params['title'],
                               text=module.params['text'],
                               host=module.params['host'],
                               tags=module.params['tags'],
                               priority=module.params['priority'],
                               alert_type=module.params['alert_type'],
                               aggregation_key=module.params['aggregation_key'],
                               source_type_name='ansible')
        if msg['status'] != 'ok':
            module.fail_json(msg=msg)

        module.exit_json(changed=True, msg=msg)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 11
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: circonus_annotation.py    MIT License 6 votes vote down vote up
def main():
    '''Main function, dispatches logic'''
    module = AnsibleModule(
        argument_spec=dict(
            start=dict(type='int'),
            stop=dict(type='int'),
            category=dict(required=True),
            title=dict(required=True),
            description=dict(required=True),
            duration=dict(default=0, type='int'),
            api_key=dict(required=True, no_log=True)
        )
    )

    check_requests_dep(module)

    annotation = create_annotation(module)
    try:
        resp = post_annotation(annotation, module.params['api_key'])
    except requests.exceptions.RequestException as e:
        module.fail_json(msg='Request Failed', reason=to_native(e), exception=traceback.format_exc())
    module.exit_json(changed=True, annotation=resp.json()) 
Example 12
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: datadog_monitor.py    MIT License 6 votes vote down vote up
def _update_monitor(module, monitor, options):
    try:
        kwargs = dict(id=monitor['id'], query=module.params['query'],
                      name=module.params['name'], message=_fix_template_vars(module.params['message']),
                      options=options)
        if module.params['tags'] is not None:
            kwargs['tags'] = module.params['tags']
        msg = api.Monitor.update(**kwargs)

        if 'errors' in msg:
            module.fail_json(msg=str(msg['errors']))
        elif _equal_dicts(msg, monitor, ['creator', 'overall_state', 'modified', 'matching_downtimes', 'overall_state_modified']):
            module.exit_json(changed=False, msg=msg)
        else:
            module.exit_json(changed=True, msg=msg)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 13
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: datadog_monitor.py    MIT License 6 votes vote down vote up
def mute_monitor(module):
    monitor = _get_monitor(module)
    if not monitor:
        module.fail_json(msg="Monitor %s not found!" % module.params['name'])
    elif monitor['options']['silenced']:
        module.fail_json(msg="Monitor is already muted. Datadog does not allow to modify muted alerts, consider unmuting it first.")
    elif (module.params['silenced'] is not None and len(set(monitor['options']['silenced']) ^ set(module.params['silenced'])) == 0):
        module.exit_json(changed=False)
    try:
        if module.params['silenced'] is None or module.params['silenced'] == "":
            msg = api.Monitor.mute(id=monitor['id'])
        else:
            msg = api.Monitor.mute(id=monitor['id'], silenced=module.params['silenced'])
        module.exit_json(changed=True, msg=msg)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 14
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bcf_switch.py    MIT License 6 votes vote down vote up
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            fabric_role=dict(choices=['spine', 'leaf'], required=True),
            leaf_group=dict(type='str', required=False),
            mac=dict(type='str', required=True),
            controller=dict(type='str', required=True),
            state=dict(choices=['present', 'absent'], default='present'),
            validate_certs=dict(type='bool', default='True'),
            access_token=dict(type='str', no_log=True)
        ),
        supports_check_mode=True,
    )

    try:
        switch(module, check_mode=module.check_mode)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 15
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ldap_entry.py    MIT License 6 votes vote down vote up
def _connect_to_ldap(self):
        if not self.verify_cert:
            ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)

        connection = ldap.initialize(self.server_uri)

        if self.start_tls:
            try:
                connection.start_tls_s()
            except ldap.LDAPError as e:
                self.module.fail_json(msg="Cannot start TLS.", details=to_native(e),
                                      exception=traceback.format_exc())

        try:
            if self.bind_dn is not None:
                connection.simple_bind_s(self.bind_dn, self.bind_pw)
            else:
                connection.sasl_interactive_bind_s('', ldap.sasl.external())
        except ldap.LDAPError as e:
            self.module.fail_json(
                msg="Cannot bind to the server.", details=to_native(e),
                exception=traceback.format_exc())

        return connection 
Example 16
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: vmadm.py    MIT License 6 votes vote down vote up
def get_vm_prop(module, uuid, prop):
    # Lookup a property for the given VM.
    # Returns the property, or None if not found.
    cmd = '{0} lookup -j -o {1} uuid={2}'.format(module.vmadm, prop, uuid)

    (rc, stdout, stderr) = module.run_command(cmd)

    if rc != 0:
        module.fail_json(
            msg='Could not perform lookup of {0} on {1}'.format(prop, uuid), exception=stderr)

    try:
        stdout_json = json.loads(stdout)
    except Exception as e:
        module.fail_json(
            msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(prop),
            details=to_native(e), exception=traceback.format_exc())

    if len(stdout_json) > 0 and prop in stdout_json[0]:
        return stdout_json[0][prop]
    else:
        return None 
Example 17
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: dimensiondata_network.py    MIT License 6 votes vote down vote up
def _delete_network(self, network):
        try:
            if self.mcp_version == '1.0':
                deleted = self.driver.ex_delete_network(network)
            else:
                deleted = self.driver.ex_delete_network_domain(network)

            if deleted:
                self.module.exit_json(
                    changed=True,
                    msg="Deleted network with id %s" % network.id
                )

            self.module.fail_json(
                "Unexpected failure deleting network with id %s", network.id
            )

        except DimensionDataAPIException as e:
            self.module.fail_json(
                msg="Failed to delete network: %s" % to_native(e), exception=traceback.format_exc()
            ) 
Example 18
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: atomic_image.py    MIT License 6 votes vote down vote up
def main():
    module = AnsibleModule(
        argument_spec=dict(
            backend=dict(type='str', choices=['docker', 'ostree']),
            name=dict(type='str', required=True),
            state=dict(type='str', default='latest', choices=['absent', 'latest', 'present']),
            started=dict(type='bool', default=True),
        ),
    )

    # Verify that the platform supports atomic command
    rc, out, err = module.run_command('atomic -v', check_rc=False)
    if rc != 0:
        module.fail_json(msg="Error in running atomic command", err=err)

    try:
        core(module)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 19
Project: phrydy   Author: Josef-Friedrich   File: mediafile.py    MIT License 5 votes vote down vote up
def mutagen_call(action, path, func, *args, **kwargs):
    """Call a Mutagen function with appropriate error handling.

    `action` is a string describing what the function is trying to do,
    and `path` is the relevant filename. The rest of the arguments
    describe the callable to invoke.

    We require at least Mutagen 1.33, where `IOError` is *never* used,
    neither for internal parsing errors *nor* for ordinary IO error
    conditions such as a bad filename. Mutagen-specific parsing errors and IO
    errors are reraised as `UnreadableFileError`. Other exceptions
    raised inside Mutagen---i.e., bugs---are reraised as `MutagenError`.
    """
    try:
        return func(*args, **kwargs)
    except mutagen.MutagenError as exc:
        log.debug(u'%s failed: %s', action, six.text_type(exc))
        raise UnreadableFileError(path, six.text_type(exc))
    except Exception as exc:
        # Isolate bugs in Mutagen.
        log.debug(u'%s', traceback.format_exc())
        log.error(u'uncaught Mutagen exception in %s: %s', action, exc)
        raise MutagenError(path, exc)


# Utility. 
Example 20
Project: pyblish-win   Author: pyblish   File: loader.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _make_failed_import_test(name, suiteClass):
    message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())
    return _make_failed_test('ModuleImportFailure', name, ImportError(message),
                             suiteClass) 
Example 21
Project: pyblish-win   Author: pyblish   File: test_socket.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def testInterruptedTimeout(self):
        # XXX I don't know how to do this test on MSWindows or any other
        # plaform that doesn't support signal.alarm() or os.kill(), though
        # the bug should have existed on all platforms.
        self.serv.settimeout(5.0)   # must be longer than alarm
        class Alarm(Exception):
            pass
        def alarm_handler(signal, frame):
            raise Alarm
        old_alarm = signal.signal(signal.SIGALRM, alarm_handler)
        try:
            signal.alarm(2)    # POSIX allows alarm to be up to 1 second early
            try:
                foo = self.serv.accept()
            except socket.timeout:
                self.fail("caught timeout instead of Alarm")
            except Alarm:
                pass
            except:
                self.fail("caught other exception instead of Alarm:"
                          " %s(%s):\n%s" %
                          (sys.exc_info()[:2] + (traceback.format_exc(),)))
            else:
                self.fail("nothing caught")
            finally:
                signal.alarm(0)         # shut off alarm
        except Alarm:
            self.fail("got Alarm in wrong place")
        finally:
            # no alarm can be pending.  Safe to restore old handler.
            signal.signal(signal.SIGALRM, old_alarm) 
Example 22
Project: pyblish-win   Author: pyblish   File: test_signal.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_main(self):
        # This function spawns a child process to insulate the main
        # test-running process from all the signals. It then
        # communicates with that child process over a pipe and
        # re-raises information about any exceptions the child
        # raises. The real work happens in self.run_test().
        os_done_r, os_done_w = os.pipe()
        with closing(os.fdopen(os_done_r)) as done_r, \
             closing(os.fdopen(os_done_w, 'w')) as done_w:
            child = os.fork()
            if child == 0:
                # In the child process; run the test and report results
                # through the pipe.
                try:
                    done_r.close()
                    # Have to close done_w again here because
                    # exit_subprocess() will skip the enclosing with block.
                    with closing(done_w):
                        try:
                            self.run_test()
                        except:
                            pickle.dump(traceback.format_exc(), done_w)
                        else:
                            pickle.dump(None, done_w)
                except:
                    print 'Uh oh, raised from pickle.'
                    traceback.print_exc()
                finally:
                    exit_subprocess()

            done_w.close()
            # Block for up to MAX_DURATION seconds for the test to finish.
            r, w, x = select.select([done_r], [], [], self.MAX_DURATION)
            if done_r in r:
                tb = pickle.load(done_r)
                if tb:
                    self.fail(tb)
            else:
                os.kill(child, signal.SIGKILL)
                self.fail('Test deadlocked after %d seconds.' %
                          self.MAX_DURATION) 
Example 23
Project: django   Author: ipinfo   File: middleware.py    Apache License 2.0 5 votes vote down vote up
def process_request(self, request):
        """Middleware hook that acts on and modifies request object."""
        try:
            if self.filter and self.filter(request):
                request.ipinfo = None
            else:
                request.ipinfo = self.ipinfo.getDetails()
        except Exception as exc:
            request.ipinfo = None
            LOGGER.error(traceback.format_exc()) 
Example 24
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 5 votes vote down vote up
def on_get(self, req, resp):
        """Handler for GET method."""
        try:
            task_model_list = self.state_manager.get_tasks()
            task_list = [x.to_dict() for x in task_model_list]
            resp.body = json.dumps(task_list)
            resp.status = falcon.HTTP_200
        except Exception as ex:
            self.error(
                req.context,
                "Unknown error: %s\n%s" % (str(ex), traceback.format_exc()))
            self.return_error(
                resp, falcon.HTTP_500, message="Unknown error", retry=False) 
Example 25
Project: drydock   Author: airshipit   File: tasks.py    Apache License 2.0 5 votes vote down vote up
def on_post(self, req, resp):
        """Handler for POST method."""
        # A map of supported actions to the handlers for tasks for those actions
        supported_actions = {
            'validate_design': TasksResource.task_validate_design,
            'verify_site': TasksResource.task_verify_site,
            'prepare_site': TasksResource.task_prepare_site,
            'verify_nodes': TasksResource.task_verify_nodes,
            'prepare_nodes': TasksResource.task_prepare_nodes,
            'deploy_nodes': TasksResource.task_deploy_nodes,
            'destroy_nodes': TasksResource.task_destroy_nodes,
            'relabel_nodes': TasksResource.task_relabel_nodes,
        }

        try:
            json_data = self.req_json(req)

            action = json_data.get('action', None)
            if supported_actions.get(action, None) is None:
                self.error(req.context, "Unsupported action %s" % action)
                self.return_error(
                    resp,
                    falcon.HTTP_400,
                    message="Unsupported action %s" % action,
                    retry=False)
            else:
                supported_actions.get(action)(self, req, resp, json_data)
        except Exception as ex:
            self.error(
                req.context,
                "Unknown error: %s\n%s" % (str(ex), traceback.format_exc()))
            self.return_error(
                resp, falcon.HTTP_500, message="Unknown error", retry=False) 
Example 26
Project: aospy   Author: spencerahill   File: automate.py    Apache License 2.0 5 votes vote down vote up
def _compute_or_skip_on_error(calc, compute_kwargs):
    """Execute the Calc, catching and logging exceptions, but don't re-raise.

    Prevents one failed calculation from stopping a larger requested set
    of calculations.
    """
    try:
        return calc.compute(**compute_kwargs)
    except Exception:
        msg = ("Skipping aospy calculation `{0}` due to error with the "
               "following traceback: \n{1}")
        logging.warning(msg.format(calc, traceback.format_exc()))
        return None 
Example 27
Project: BASS   Author: Cisco-Talos   File: server.py    GNU General Public License v2.0 5 votes vote down vote up
def job_create():
    try:
        job = bass.create_job()
        return jsonify(message = "ok", job = job.json())
    except Exception as ex:
        return make_response(jsonify(message = str(ex), trace = traceback.format_exc()), 400) 
Example 28
Project: BASS   Author: Cisco-Talos   File: server.py    GNU General Public License v2.0 5 votes vote down vote up
def job_get_status(job_id):
    try:
        return jsonify(message = "ok", job = bass.get_job(job_id).json())
    except KeyError:
        return make_response(jsonify(message = "Invalid job id"), 400)
    except Exception as ex:
        return make_response(jsonify(message = str(ex), trace = traceback.format_exc()), 400) 
Example 29
Project: dnsbin   Author: thomas-maurice   File: error_handler.py    Do What The F*ck You Want To Public License 5 votes vote down vote up
def error_handler(exce, req, resp, params):
    if issubclass(exce.__class__, falcon.HTTPError):
        resp.status = exce.status
        req.context['result'] = {'error': "%s: %s" % (exce.title, exce.description)}
    else:
        print traceback.print_exc()
        resp.status = falcon.HTTP_500
        req.context['result'] = {'error': str(traceback.format_exc())} 
Example 30
Project: AutoDL   Author: tanguofu   File: server.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def data_received(self, data):
        # Check for the request itself getting too large and exceeding
        # memory limits
        self._total_request_size += len(data)
        if self._total_request_size > self.request_max_size:
            exception = PayloadTooLarge('Payload Too Large')
            self.write_error(exception)

        # Create parser if this is the first time we're receiving data
        if self.parser is None:
            assert self.request is None
            self.headers = []
            self.parser = HttpRequestParser(self)

        # requests count
        self.state['requests_count'] = self.state['requests_count'] + 1

        # Parse request chunk or close connection
        try:
            self.parser.feed_data(data)
        except HttpParserError:
            message = 'Bad Request'
            if self._debug:
                message += '\n' + traceback.format_exc()
            exception = InvalidUsage(message)
            self.write_error(exception) 
Example 31
Project: AutoDL   Author: tanguofu   File: server.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def bail_out(self, message, from_error=False):
        if from_error or self.transport.is_closing():
            logger.error("Transport closed @ %s and exception "
                         "experienced during error handling",
                         self.transport.get_extra_info('peername'))
            logger.debug('Exception:\n%s', traceback.format_exc())
        else:
            exception = ServerError(message)
            self.write_error(exception)
            logger.error(message) 
Example 32
Project: AutoDL   Author: tanguofu   File: handlers.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def response(self, request, exception):
        """Fetches and executes an exception handler and returns a response
        object

        :param request: Request
        :param exception: Exception to handle
        :return: Response object
        """
        handler = self.lookup(exception)
        response = None
        try:
            if handler:
                response = handler(request, exception)
            if response is None:
                response = self.default(request, exception)
        except Exception:
            self.log(format_exc())
            if self.debug:
                url = getattr(request, 'url', 'unknown')
                response_message = ('Exception raised in exception handler '
                                    '"%s" for uri: "%s"\n%s')
                logger.error(response_message,
                             handler.__name__, url, format_exc())

                return text(response_message % (
                    handler.__name__, url, format_exc()), 500)
            else:
                return text('An error occurred while handling an error', 500)
        return response 
Example 33
Project: AutoDL   Author: tanguofu   File: setup.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_cpp_flags(build_ext):
    last_err = None
    default_flags = ['-std=c++11', '-fPIC', '-O2']

    if sys.platform == 'darwin':
        # Darwin most likely will have Clang, which has libc++.
        flags_to_try = [default_flags + ['-stdlib=libc++'], default_flags]
    else:
        flags_to_try = [default_flags, default_flags + ['-stdlib=libc++']]

    for cpp_flags in flags_to_try:
        try:
            test_compile(
                build_ext,
                'test_cpp_flags',
                extra_preargs=cpp_flags,
                code=textwrap.dedent('''\
                    #include <unordered_map>
                    void test() {
                    }
                    '''))

            return cpp_flags
        except (CompileError, LinkError):
            last_err = 'Unable to determine C++ compilation flags (see error above).'
        except Exception:
            last_err = 'Unable to determine C++ compilation flags.  ' \
                       'Last error:\n\n%s' % traceback.format_exc()

    raise DistutilsPlatformError(last_err) 
Example 34
Project: AutoDL   Author: tanguofu   File: setup.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_tf_libs(build_ext, lib_dirs, cpp_flags):
    last_err = None
    for tf_libs in [['tensorflow_framework'], []]:
        try:
            lib_file = test_compile(
                build_ext,
                'test_tensorflow_libs',
                library_dirs=lib_dirs,
                libraries=tf_libs,
                extra_preargs=cpp_flags,
                code=textwrap.dedent('''\
                    void test() {
                    }
                    '''))

            from tensorflow.python.framework import load_library
            load_library.load_op_library(lib_file)

            return tf_libs
        except (CompileError, LinkError):
            last_err = 'Unable to determine -l link flags to use with TensorFlow (see error above).'
        except Exception:
            last_err = 'Unable to determine -l link flags to use with TensorFlow.  ' \
                       'Last error:\n\n%s' % traceback.format_exc()

    raise DistutilsPlatformError(last_err) 
Example 35
Project: AutoDL   Author: tanguofu   File: setup.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_tf_abi(build_ext, include_dirs, lib_dirs, libs, cpp_flags):
    last_err = None
    cxx11_abi_macro = '_GLIBCXX_USE_CXX11_ABI'
    for cxx11_abi in ['0', '1']:
        try:
            lib_file = test_compile(
                build_ext,
                'test_tensorflow_abi',
                macros=[(cxx11_abi_macro, cxx11_abi)],
                include_dirs=include_dirs,
                library_dirs=lib_dirs,
                libraries=libs,
                extra_preargs=cpp_flags,
                code=textwrap.dedent('''\
                #include <string>
                #include "tensorflow/core/framework/op.h"
                #include "tensorflow/core/framework/op_kernel.h"
                #include "tensorflow/core/framework/shape_inference.h"
                void test() {
                    auto ignore = tensorflow::strings::StrCat("a", "b");
                }
                '''))

            from tensorflow.python.framework import load_library
            load_library.load_op_library(lib_file)

            return cxx11_abi_macro, cxx11_abi
        except (CompileError, LinkError):
            last_err = 'Unable to determine CXX11 ABI to use with TensorFlow (see error above).'
        except Exception:
            last_err = 'Unable to determine CXX11 ABI to use with TensorFlow.  ' \
                       'Last error:\n\n%s' % traceback.format_exc()

    raise DistutilsPlatformError(last_err) 
Example 36
Project: zmirror   Author: aploium   File: zmirror.py    MIT License 5 votes vote down vote up
def dump_zmirror_snapshot(folder="error_dump", msg=None, our_response=None):
    """
    dump当前状态到文件
    :param folder: 文件夹名
    :type folder: str
    :param our_response: Flask返回对象, 可选
    :type our_response: Response
    :param msg: 额外的信息
    :type msg: str
    :return: dump下来的文件绝对路径
    :rtype: Union[str, None]
    """
    import pickle
    try:
        if not os.path.exists(zmirror_root(folder)):
            os.mkdir(zmirror_root(folder))
        _time_str = datetime.now().strftime('snapshot_%Y-%m-%d_%H-%M-%S')

        import config

        snapshot = {
            "time": datetime.now(),
            "parse": parse.dump(),
            "msg": msg,
            "traceback": traceback.format_exc(),
            "config": attributes(config, to_dict=True),
            "FlaskRequest": attributes(request, to_dict=True),
        }
        if our_response is not None:
            our_response.freeze()
        snapshot["OurResponse"] = our_response

        dump_file_path = os.path.abspath(os.path.join(zmirror_root(folder), _time_str + '.dump'))

        with open(dump_file_path, 'wb') as fp:
            pickle.dump(snapshot, fp, pickle.HIGHEST_PROTOCOL)
        return dump_file_path
    except:
        return None 
Example 37
Project: kuaa   Author: rafaelwerneck   File: common.py    GNU General Public License v3.0 5 votes vote down vote up
def gridSearchExponential(max_or_min, of, *params): # PEDRORMJUNIOR: 20130806: add max_or_min
    """
    Input:
    of = objective function.  A function that receives 'n' parameters and return an accuracy measure.
    *params = lists of parameters.  The first list constains values to be grid searched as the first parameters of the 'of'.  The second list contains values to be grid searched as the second parameters of the 'of'.  And so on...

    Output:
    ret = a list of parameters of size 'n'.  The best combination of parameters according to the 'of'.
    """
    assert max_or_min in ['min', 'max']

    lenranges = map(len, params)
    assert all(map(lambda x: x > 1, lenranges))
    positions = map(lambda i: int(np.prod(lenranges[:i])),
                    range(len(lenranges)))

    meshparams = mymeshgrid(*params)

    results = []
    for i in range(len(meshparams)):
        try:                    # Maybe, the params are not valid.
            res = (of(*meshparams[i]),
                   (-i if max_or_min == 'min' else i),
                   i)
            results.append(res)
        except Exception as expt:
            red_err(traceback.format_exc()[:-1])
            red_err('gridSearch: except: i, params = {0} {1}'.format(i, meshparams[i]))

    _, _, best_params_idx = max(results)
    yellow_err('gridSearch(): best_params = {0}'.format(meshparams[best_params_idx]))

    ret = meshparams[best_params_idx]

    return ret 
Example 38
Project: disentangling_conditional_gans   Author: zalandoresearch   File: dataset_tool.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.value = sys.exc_info()[1]
        self.traceback = traceback.format_exc()

#---------------------------------------------------------------------------- 
Example 39
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: dummy_handler.py    Apache License 2.0 5 votes vote down vote up
def do_POST(self):
        try:
            content_length = int(self.headers['Content-Length'])
            txt=self.rfile.read(content_length).decode("utf-8")
            sys.stderr.flush()
        except:
            self.send_response(400, 'Bad request')
            self.end_headers()
            self.wfile.write(traceback.format_exc().encode("utf-8"))
            self.close_connection=True
            return
        self.process(txt) 
Example 40
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: datadog_monitor.py    MIT License 5 votes vote down vote up
def _post_monitor(module, options):
    try:
        kwargs = dict(type=module.params['type'], query=module.params['query'],
                      name=module.params['name'], message=_fix_template_vars(module.params['message']),
                      options=options)
        if module.params['tags'] is not None:
            kwargs['tags'] = module.params['tags']
        msg = api.Monitor.create(**kwargs)
        if 'errors' in msg:
            module.fail_json(msg=str(msg['errors']))
        else:
            module.exit_json(changed=True, msg=msg)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 41
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: datadog_monitor.py    MIT License 5 votes vote down vote up
def unmute_monitor(module):
    monitor = _get_monitor(module)
    if not monitor:
        module.fail_json(msg="Monitor %s not found!" % module.params['name'])
    elif not monitor['options']['silenced']:
        module.exit_json(changed=False)
    try:
        msg = api.Monitor.unmute(monitor['id'])
        module.exit_json(changed=True, msg=msg)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 42
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: zabbix_template.py    MIT License 5 votes vote down vote up
def load_json_template(self, template_json):
        try:
            return json.loads(template_json)
        except ValueError as e:
            self._module.fail_json(
                msg='Invalid JSON provided',
                details=to_native(e),
                exception=traceback.format_exc()
            ) 
Example 43
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bigmon_chain.py    MIT License 5 votes vote down vote up
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            controller=dict(type='str', required=True),
            state=dict(choices=['present', 'absent'], default='present'),
            validate_certs=dict(type='bool', default='True'),
            access_token=dict(type='str', no_log=True)
        )
    )

    try:
        chain(module)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 44
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: _netscaler.py    MIT License 5 votes vote down vote up
def main():

    module = AnsibleModule(
        argument_spec=dict(
            nsc_host=dict(type='str', required=True),
            nsc_protocol=dict(type='str', default='https'),
            user=dict(type='str', required=True),
            password=dict(type='str', required=True, no_log=True),
            action=dict(type='str', default='enable', choices=['disable', 'enable']),
            name=dict(type='str', default=socket.gethostname()),
            type=dict(type='str', default='server', choices=['server', 'service']),
            validate_certs=dict(type='bool', default=True),
        ),
    )

    rc = 0
    try:
        rc, result = core(module)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc())

    if rc != 0:
        module.fail_json(rc=rc, msg=result)
    else:
        result['changed'] = True
        module.exit_json(**result) 
Example 45
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: omapi_host.py    MIT License 5 votes vote down vote up
def remove_host(self):
        try:
            self.omapi.del_host(self.module.params['macaddr'])
            self.module.exit_json(changed=True)
        except OmapiErrorNotFound:
            self.module.exit_json()
        except OmapiError as e:
            self.module.fail_json(msg="OMAPI error: %s" % to_native(e), exception=traceback.format_exc()) 
Example 46
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: omapi_host.py    MIT License 5 votes vote down vote up
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(required=True, type='str', choices=['present', 'absent']),
            host=dict(type='str', default="localhost"),
            port=dict(type='int', default=7911),
            key_name=dict(required=True, type='str', default=None),
            key=dict(required=True, type='str', default=None, no_log=True),
            macaddr=dict(required=True, type='str', default=None),
            hostname=dict(type='str', default=None, aliases=['name']),
            ip=dict(type='str', default=None),
            ddns=dict(type='bool', default=False),
            statements=dict(type='list', default=[])
        ),
        supports_check_mode=False
    )

    if not pureomapi_found:
        module.fail_json(msg="pypureomapi library is required by this module.")

    if module.params['key'] is None or len(module.params["key"]) == 0:
        module.fail_json(msg="'key' parameter cannot be empty.")

    if module.params['key_name'] is None or len(module.params["key_name"]) == 0:
        module.fail_json(msg="'key_name' parameter cannot be empty.")

    host_manager = OmapiHostManager(module)
    try:
        if module.params['state'] == 'present':
            host_manager.setup_host()
        elif module.params['state'] == 'absent':
            host_manager.remove_host()
    except ValueError as e:
        module.fail_json(msg="OMAPI input value error: %s" % to_native(e), exception=traceback.format_exc()) 
Example 47
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: profitbricks_volume.py    MIT License 5 votes vote down vote up
def _attach_volume(module, profitbricks, datacenter, volume):
    """
    Attaches a volume.

    This will attach a volume to the server.

    module : AnsibleModule object
    profitbricks: authenticated profitbricks object.

    Returns:
        True if the volume was attached, false otherwise
    """
    server = module.params.get('server')

    # Locate UUID for Server
    if server:
        if not (uuid_match.match(server)):
            server_list = profitbricks.list_servers(datacenter)
            for s in server_list['items']:
                if server == s['properties']['name']:
                    server = s['id']
                    break

        try:
            return profitbricks.attach_volume(datacenter, server, volume)
        except Exception as e:
            module.fail_json(msg='failed to attach volume: %s' % to_native(e), exception=traceback.format_exc()) 
Example 48
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: profitbricks.py    MIT License 5 votes vote down vote up
def _remove_boot_volume(module, profitbricks, datacenter_id, server_id):
    """
    Remove the boot volume from the server
    """
    try:
        server = profitbricks.get_server(datacenter_id, server_id)
        volume_id = server['properties']['bootVolume']['id']
        volume_response = profitbricks.delete_volume(datacenter_id, volume_id)
    except Exception as e:
        module.fail_json(msg="failed to remove the server's boot volume: %s" % to_native(e), exception=traceback.format_exc()) 
Example 49
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: vmadm.py    MIT License 5 votes vote down vote up
def get_vm_uuid(module, alias):
    # Lookup the uuid that goes with the given alias.
    # Returns the uuid or '' if not found.
    cmd = '{0} lookup -j -o uuid alias={1}'.format(module.vmadm, alias)

    (rc, stdout, stderr) = module.run_command(cmd)

    if rc != 0:
        module.fail_json(
            msg='Could not retrieve UUID of {0}'.format(alias), exception=stderr)

    # If no VM was found matching the given alias, we get back an empty array.
    # That is not an error condition as we might be explicitly checking it's
    # absence.
    if stdout.strip() == '[]':
        return None
    else:
        try:
            stdout_json = json.loads(stdout)
        except Exception as e:
            module.fail_json(
                msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(alias),
                details=to_native(e), exception=traceback.format_exc())

        if len(stdout_json) > 0 and 'uuid' in stdout_json[0]:
            return stdout_json[0]['uuid'] 
Example 50
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: vmadm.py    MIT License 5 votes vote down vote up
def get_all_vm_uuids(module):
    # Retrieve the UUIDs for all VMs.
    cmd = '{0} lookup -j -o uuid'.format(module.vmadm)

    (rc, stdout, stderr) = module.run_command(cmd)

    if rc != 0:
        module.fail_json(msg='Failed to get VMs list', exception=stderr)

    try:
        stdout_json = json.loads(stdout)
        return [v['uuid'] for v in stdout_json]
    except Exception as e:
        module.fail_json(msg='Could not retrieve VM UUIDs', details=to_native(e),
                         exception=traceback.format_exc()) 
Example 51
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: vmadm.py    MIT License 5 votes vote down vote up
def new_vm(module, uuid, vm_state):
    payload_file = create_payload(module, uuid)

    (rc, stdout, stderr) = vmadm_create_vm(module, payload_file)

    if rc != 0:
        changed = False
        module.fail_json(msg='Could not create VM', exception=stderr)
    else:
        changed = True
        # 'vmadm create' returns all output to stderr...
        match = re.match('Successfully created VM (.*)', stderr)
        if match:
            vm_uuid = match.groups()[0]
            if not is_valid_uuid(vm_uuid):
                module.fail_json(msg='Invalid UUID for VM {0}?'.format(vm_uuid))
        else:
            module.fail_json(msg='Could not retrieve UUID of newly created(?) VM')

        # Now that the VM is created, ensure it is in the desired state (if not 'running')
        if vm_state != 'running':
            ret = set_vm_state(module, vm_uuid, vm_state)
            if not ret:
                module.fail_json(msg='Could not set VM {0} to state {1}'.format(vm_uuid, vm_state))

    try:
        os.unlink(payload_file)
    except Exception as e:
        # Since the payload may contain sensitive information, fail hard
        # if we cannot remove the file so the operator knows about it.
        module.fail_json(msg='Could not remove temporary JSON payload file {0}: {1}'.format(payload_file, to_native(e)),
                         exception=traceback.format_exc())

    return changed, vm_uuid 
Example 52
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: os_auth.py    MIT License 5 votes vote down vote up
def main():

    argument_spec = openstack_full_argument_spec()
    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    shade, cloud = openstack_cloud_from_module(module)
    try:
        module.exit_json(
            changed=False,
            ansible_facts=dict(
                auth_token=cloud.auth_token,
                service_catalog=cloud.service_catalog))
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc()) 
Example 53
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: dimensiondata_network.py    MIT License 5 votes vote down vote up
def _create_network(self):

        # Make sure service_plan argument is defined
        if self.mcp_version == '2.0' and 'service_plan' not in self.module.params:
            self.module.fail_json(
                msg='service_plan required when creating network and location is MCP 2.0'
            )

        # Create network
        try:
            if self.mcp_version == '1.0':
                network = self.driver.ex_create_network(
                    self.location,
                    self.name,
                    description=self.description
                )
            else:
                network = self.driver.ex_create_network_domain(
                    self.location,
                    self.name,
                    self.module.params['service_plan'],
                    description=self.description
                )
        except DimensionDataAPIException as e:

            self.module.fail_json(
                msg="Failed to create new network: %s" % to_native(e), exception=traceback.format_exc()
            )

        if self.module.params['wait'] is True:
            network = self._wait_for_network_state(network.id, 'NORMAL')

        return network 
Example 54
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: dimensiondata_network.py    MIT License 5 votes vote down vote up
def _wait_for_network_state(self, net_id, state_to_wait_for):
        try:
            return self.driver.connection.wait_for_state(
                state_to_wait_for,
                self.driver.ex_get_network_domain,
                self.module.params['wait_poll_interval'],
                self.module.params['wait_time'],
                net_id
            )
        except DimensionDataAPIException as e:
            self.module.fail_json(
                msg='Network did not reach % state in time: %s' % (state_to_wait_for, to_native(e)),
                exception=traceback.format_exc()
            ) 
Example 55
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: atomic_host.py    MIT License 5 votes vote down vote up
def main():
    module = AnsibleModule(
        argument_spec=dict(
            revision=dict(type='str', default='latest', aliases=["version"]),
        ),
    )

    # Verify that the platform is atomic host
    if not os.path.exists("/run/ostree-booted"):
        module.fail_json(msg="Module atomic_host is applicable for Atomic Host Platforms only")

    try:
        core(module)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 56
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_cluster_facts.py    MIT License 5 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        pattern=dict(default='', required=False),
    )
    module = AnsibleModule(argument_spec)

    if module._name == 'ovirt_clusters_facts':
        module.deprecate("The 'ovirt_clusters_facts' module is being renamed 'ovirt_cluster_facts'", version=2.8)

    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        clusters_service = connection.system_service().clusters_service()
        clusters = clusters_service.list(search=module.params['pattern'])
        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_clusters=[
                    get_dict_of_struct(
                        struct=c,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for c in clusters
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 57
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_datacenter_facts.py    MIT License 5 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        pattern=dict(default='', required=False),
    )
    module = AnsibleModule(argument_spec)

    if module._name == 'ovirt_datacenters_facts':
        module.deprecate("The 'ovirt_datacenters_facts' module is being renamed 'ovirt_datacenter_facts'", version=2.8)

    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        datacenters_service = connection.system_service().data_centers_service()
        datacenters = datacenters_service.list(search=module.params['pattern'])
        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_datacenters=[
                    get_dict_of_struct(
                        struct=d,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for d in datacenters
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 58
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_storage_templates_facts.py    MIT License 5 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        all_content=dict(default=False, type='bool'),
        case_sensitive=dict(default=True, type='bool'),
        storage_domain=dict(default=None),
        max=dict(default=None, type='int'),
        unregistered=dict(default=False, type='bool'),
    )
    module = AnsibleModule(argument_spec)
    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        storage_domains_service = connection.system_service().storage_domains_service()
        sd_id = get_id_by_name(storage_domains_service, module.params['storage_domain'])
        storage_domain_service = storage_domains_service.storage_domain_service(sd_id)
        templates_service = storage_domain_service.templates_service()

        # Find the the unregistered Template we want to register:
        if module.params.get('unregistered'):
            templates = templates_service.list(unregistered=True)
        else:
            templates = templates_service.list()
        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_storage_templates=[
                    get_dict_of_struct(
                        struct=c,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for c in templates
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 59
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_storage_domains_facts.py    MIT License 5 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        pattern=dict(default='', required=False),
    )
    module = AnsibleModule(argument_spec)
    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        storage_domains_service = connection.system_service().storage_domains_service()
        storage_domains = storage_domains_service.list(search=module.params['pattern'])
        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_storage_domains=[
                    get_dict_of_struct(
                        struct=c,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for c in storage_domains
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 60
Project: pyblish-win   Author: pyblish   File: SimpleXMLRPCServer.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def do_POST(self):
        """Handles the HTTP POST request.

        Attempts to interpret all HTTP POST requests as XML-RPC calls,
        which are forwarded to the server's _dispatch method for handling.
        """

        # Check that the path is legal
        if not self.is_rpc_path_valid():
            self.report_404()
            return

        try:
            # Get arguments by reading body of request.
            # We read this in chunks to avoid straining
            # socket.read(); around the 10 or 15Mb mark, some platforms
            # begin to have problems (bug #792570).
            max_chunk_size = 10*1024*1024
            size_remaining = int(self.headers["content-length"])
            L = []
            while size_remaining:
                chunk_size = min(size_remaining, max_chunk_size)
                chunk = self.rfile.read(chunk_size)
                if not chunk:
                    break
                L.append(chunk)
                size_remaining -= len(L[-1])
            data = ''.join(L)

            data = self.decode_request_content(data)
            if data is None:
                return #response has been sent

            # In previous versions of SimpleXMLRPCServer, _dispatch
            # could be overridden in this class, instead of in
            # SimpleXMLRPCDispatcher. To maintain backwards compatibility,
            # check to see if a subclass implements _dispatch and dispatch
            # using that method if present.
            response = self.server._marshaled_dispatch(
                    data, getattr(self, '_dispatch', None), self.path
                )
        except Exception, e: # This should only happen if the module is buggy
            # internal error, report as HTTP server error
            self.send_response(500)

            # Send information about the exception if requested
            if hasattr(self.server, '_send_traceback_header') and \
                    self.server._send_traceback_header:
                self.send_header("X-exception", str(e))
                self.send_header("X-traceback", traceback.format_exc())

            self.send_header("Content-length", "0")
            self.end_headers() 
Example 61
Project: AutoDL   Author: tanguofu   File: testing.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _sanic_endpoint_test(
            self, method='get', uri='/', gather_request=True,
            debug=False, server_kwargs={"auto_reload": False},
            *request_args, **request_kwargs):
        results = [None, None]
        exceptions = []

        if gather_request:
            def _collect_request(request):
                if results[0] is None:
                    results[0] = request
            self.app.request_middleware.appendleft(_collect_request)

        @self.app.exception(MethodNotSupported)
        async def error_handler(request, exception):
            if request.method in ['HEAD', 'PATCH', 'PUT', 'DELETE']:
                return text(
                    '', exception.status_code, headers=exception.headers
                )
            else:
                return self.app.error_handler.default(request, exception)

        @self.app.listener('after_server_start')
        async def _collect_response(sanic, loop):
            try:
                response = await self._local_request(
                    method, uri, *request_args,
                    **request_kwargs)
                results[-1] = response
            except Exception as e:
                logger.error(
                    'Exception:\n{}'.format(traceback.format_exc()))
                exceptions.append(e)
            self.app.stop()

        self.app.run(host=HOST, debug=debug, port=self.port, **server_kwargs)
        self.app.listeners['after_server_start'].pop()

        if exceptions:
            raise ValueError("Exception during request: {}".format(exceptions))

        if gather_request:
            try:
                request, response = results
                return request, response
            except BaseException:
                raise ValueError(
                    "Request and response object expected, got ({})".format(
                        results))
        else:
            try:
                return results[-1]
            except BaseException:
                raise ValueError(
                    "Request object expected, got ({})".format(results)) 
Example 62
Project: AutoDL   Author: tanguofu   File: setup.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def get_pybind11_flags(build_ext, includes, abi_compile_flags):

    pybind11_dir = os.environ.get('PYBIND11_HOME')

    if pybind11_dir:
        pybind11_dir += '/include'
    else:
        try:
            import pybind11
            pybind11_dir = [pybind11.get_include()]
        except Exception:
            last_err = 'Unable found PYBIND11_HOME,please pip3 install pybind11 or set PYBIND11_HOME'
            raise DistutilsPlatformError(last_err)

    last_err = None
    cpp_flags = ['-I' + i for i in includes] + abi_compile_flags

    try:
        lib_file = test_compile(
            build_ext,
            'example',
            extra_preargs=cpp_flags,
            include_dirs=pybind11_dir,
            code=textwrap.dedent('''\
                    #include <pybind11/pybind11.h>
                    
                    namespace py = pybind11;
                    
                    int add(int i, int j) {
                        return i + j;
                    }
                    
                    PYBIND11_MODULE(example, m) {
                        m.doc() = "pybind11 example plugin"; // optional module docstring
                        m.def("add", &add, "A function which adds two numbers",
                              py::arg("i"), py::arg("j"));
                    }
                    '''))

        lib_dir = os.path.dirname(lib_file)

        if lib_dir not in sys.path:
            sys.path.append(lib_dir)

        import example
        example.add(1, 2)
        sys.path.pop()

        return pybind11_dir

    except (CompileError, LinkError):
        last_err = 'Unable to determine -I include flags to use with pybind11 (see error above).'
    except Exception:
        last_err = 'Unable to determine -I include flags to use with pybind11.  ' \
                   'Last error:\n\n%s' % traceback.format_exc()

    raise DistutilsPlatformError(last_err) 
Example 63
Project: neural-fingerprinting   Author: StephanZheng   File: cloud_client.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def iterate_with_exp_backoff(base_iter,
                             max_num_tries=6,
                             max_backoff=300.0,
                             start_backoff=4.0,
                             backoff_multiplier=2.0,
                             frac_random_backoff=0.25):
  """Iterate with exponential backoff on failures.

  Useful to wrap results of datastore Query.fetch to avoid 429 error.

  Args:
    base_iter: basic iterator of generator object
    max_num_tries: maximum number of tries for each request
    max_backoff: maximum backoff, in seconds
    start_backoff: initial value of backoff
    backoff_multiplier: backoff multiplier
    frac_random_backoff: fraction of the value of random part of the backoff

  Yields:
    values of yielded by base iterator
  """
  try_number = 0
  if hasattr(base_iter, '__iter__'):
    base_iter = iter(base_iter)
  while True:
    try:
      yield next(base_iter)
      try_number = 0
    except StopIteration:
      break
    except TooManyRequests as e:
      logging.warning('TooManyRequests error: %s', tb.format_exc())
      if try_number >= max_num_tries:
        logging.error('Number of tries exceeded, too many requests: %s', e)
        raise
      # compute sleep time for truncated exponential backoff
      sleep_time = start_backoff * math.pow(backoff_multiplier, try_number)
      sleep_time *= (1.0 + frac_random_backoff * random.random())
      sleep_time = min(sleep_time, max_backoff)
      logging.warning('Too many requests error, '
                      'retrying with exponential backoff %.3f', sleep_time)
      time.sleep(sleep_time)
      try_number += 1 
Example 64
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: honeybadger_deployment.py    MIT License 4 votes vote down vote up
def main():

    module = AnsibleModule(
        argument_spec=dict(
            token=dict(required=True, no_log=True),
            environment=dict(required=True),
            user=dict(required=False),
            repo=dict(required=False),
            revision=dict(required=False),
            url=dict(required=False, default='https://api.honeybadger.io/v1/deploys'),
            validate_certs=dict(default='yes', type='bool'),
        ),
        supports_check_mode=True
    )

    params = {}

    if module.params["environment"]:
        params["deploy[environment]"] = module.params["environment"]

    if module.params["user"]:
        params["deploy[local_username]"] = module.params["user"]

    if module.params["repo"]:
        params["deploy[repository]"] = module.params["repo"]

    if module.params["revision"]:
        params["deploy[revision]"] = module.params["revision"]

    params["api_key"] = module.params["token"]

    url = module.params.get('url')

    # If we're in check mode, just exit pretending like we succeeded
    if module.check_mode:
        module.exit_json(changed=True)

    try:
        data = urlencode(params)
        response, info = fetch_url(module, url, data=data)
    except Exception as e:
        module.fail_json(msg='Unable to notify Honeybadger: %s' % to_native(e), exception=traceback.format_exc())
    else:
        if info['status'] == 201:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="HTTP result code: %d connecting to %s" % (info['status'], url)) 
Example 65
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: rollbar_deployment.py    MIT License 4 votes vote down vote up
def main():

    module = AnsibleModule(
        argument_spec=dict(
            token=dict(required=True),
            environment=dict(required=True),
            revision=dict(required=True),
            user=dict(required=False),
            rollbar_user=dict(required=False),
            comment=dict(required=False),
            url=dict(
                required=False,
                default='https://api.rollbar.com/api/1/deploy/'
            ),
            validate_certs=dict(default='yes', type='bool'),
        ),
        supports_check_mode=True
    )

    if module.check_mode:
        module.exit_json(changed=True)

    params = dict(
        access_token=module.params['token'],
        environment=module.params['environment'],
        revision=module.params['revision']
    )

    if module.params['user']:
        params['local_username'] = module.params['user']

    if module.params['rollbar_user']:
        params['rollbar_username'] = module.params['rollbar_user']

    if module.params['comment']:
        params['comment'] = module.params['comment']

    url = module.params.get('url')

    try:
        data = urlencode(params)
        response, info = fetch_url(module, url, data=data)
    except Exception as e:
        module.fail_json(msg='Unable to notify Rollbar: %s' % to_native(e), exception=traceback.format_exc())
    else:
        if info['status'] == 200:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg='HTTP result code: %d connecting to %s' % (info['status'], url)) 
Example 66
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: stackdriver.py    MIT License 4 votes vote down vote up
def main():

    module = AnsibleModule(
        argument_spec=dict(
            key=dict(required=True),
            event=dict(required=True, choices=['deploy', 'annotation']),
            msg=dict(),
            revision_id=dict(),
            annotated_by=dict(default='Ansible'),
            level=dict(default='INFO', choices=['INFO', 'WARN', 'ERROR']),
            instance_id=dict(),
            event_epoch=dict(),
            deployed_by=dict(default='Ansible'),
            deployed_to=dict(),
            repository=dict(),
        ),
        supports_check_mode=True
    )

    key = module.params["key"]
    event = module.params["event"]

    # Annotation params
    msg = module.params["msg"]
    annotated_by = module.params["annotated_by"]
    level = module.params["level"]
    instance_id = module.params["instance_id"]
    event_epoch = module.params["event_epoch"]

    # Deploy params
    revision_id = module.params["revision_id"]
    deployed_by = module.params["deployed_by"]
    deployed_to = module.params["deployed_to"]
    repository = module.params["repository"]

    ##################################################################
    # deploy requires revision_id
    # annotation requires msg
    # We verify these manually
    ##################################################################

    if event == 'deploy':
        if not revision_id:
            module.fail_json(msg="revision_id required for deploy events")
        try:
            send_deploy_event(module, key, revision_id, deployed_by, deployed_to, repository)
        except Exception as e:
            module.fail_json(msg="unable to sent deploy event: %s" % to_native(e),
                             exception=traceback.format_exc())

    if event == 'annotation':
        if not msg:
            module.fail_json(msg="msg required for annotation events")
        try:
            send_annotation_event(module, key, msg, annotated_by, level, instance_id, event_epoch)
        except Exception as e:
            module.fail_json(msg="unable to sent annotation event: %s" % to_native(e),
                             exception=traceback.format_exc())

    changed = True
    module.exit_json(changed=changed, deployed_by=deployed_by) 
Example 67
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bigmon_chain.py    MIT License 4 votes vote down vote up
def chain(module):
    try:
        access_token = module.params['access_token'] or os.environ['BIGSWITCH_ACCESS_TOKEN']
    except KeyError as e:
        module.fail_json(msg='Unable to load %s' % e.message, exception=traceback.format_exc())

    name = module.params['name']
    state = module.params['state']
    controller = module.params['controller']

    rest = Rest(module,
                {'content-type': 'application/json', 'Cookie': 'session_cookie=' + access_token},
                'https://' + controller + ':8443/api/v1/data/controller/applications/bigchain')

    if None in (name, state, controller):
        module.fail_json(msg='parameter `name` is missing')

    response = rest.get('chain?config=true', data={})
    if response.status_code != 200:
        module.fail_json(msg="failed to obtain existing chain config: {}".format(response.json['description']))

    config_present = False
    matching = [chain for chain in response.json if chain['name'] == name]
    if matching:
        config_present = True

    if state in ('present') and config_present:
        module.exit_json(changed=False)

    if state in ('absent') and not config_present:
        module.exit_json(changed=False)

    if state in ('present'):
        response = rest.put('chain[name="%s"]' % name, data={'name': name})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error creating chain '{}': {}".format(name, response.json['description']))

    if state in ('absent'):
        response = rest.delete('chain[name="%s"]' % name, data={})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error deleting chain '{}': {}".format(name, response.json['description'])) 
Example 68
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bcf_switch.py    MIT License 4 votes vote down vote up
def switch(module, check_mode):
    try:
        access_token = module.params['access_token'] or os.environ['BIGSWITCH_ACCESS_TOKEN']
    except KeyError as e:
        module.fail_json(msg='Unable to load %s' % e.message, exception=traceback.format_exc())

    name = module.params['name']
    fabric_role = module.params['fabric_role']
    leaf_group = module.params['leaf_group']
    dpid = '00:00:' + module.params['mac']
    state = module.params['state']
    controller = module.params['controller']

    rest = Rest(module,
                {'content-type': 'application/json', 'Cookie': 'session_cookie=' + access_token},
                'https://' + controller + ':8443/api/v1/data/controller/core')

    response = rest.get('switch-config', data={})
    if response.status_code != 200:
        module.fail_json(msg="failed to obtain existing switch config: {}".format(response.json['description']))

    config_present = False
    for switch in response.json:
        if all((switch['name'] == name,
                switch['fabric-role'] == fabric_role,
                switch['dpid'] == dpid)):
            config_present = switch.get('leaf-group', None) == leaf_group
            if config_present:
                break

    if state in ('present') and config_present:
        module.exit_json(changed=False)

    if state in ('absent') and not config_present:
        module.exit_json(changed=False)

    if check_mode:
        module.exit_json(changed=True)

    if state in ('present'):
        data = {'name': name, 'fabric-role': fabric_role, 'leaf-group': leaf_group, 'dpid': dpid}
        response = rest.put('switch-config[name="%s"]' % name, data)
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error configuring switch '{}': {}".format(name, response.json['description']))

    if state in ('absent'):
        response = rest.delete('switch-config[name="%s"]' % name, data={})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error deleting switch '{}': {}".format(name, response.json['description'])) 
Example 69
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ordnance_config.py    MIT License 4 votes vote down vote up
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(
        src=dict(type='path'),

        lines=dict(aliases=['commands'], type='list'),
        parents=dict(type='list'),

        before=dict(type='list'),
        after=dict(type='list'),

        match=dict(default='line', choices=['line', 'strict', 'exact', 'none']),
        replace=dict(default='line', choices=['line', 'block']),
        multiline_delimiter=dict(default='@'),

        config=dict(),
        defaults=dict(type='bool', default=False),

        backup=dict(type='bool', default=False),
        save=dict(default=False, type='bool'),
    )

    mutually_exclusive = [('lines', 'src'),
                          ('parents', 'src')]

    required_if = [('match', 'strict', ['lines']),
                   ('match', 'exact', ['lines']),
                   ('replace', 'block', ['lines'])]

    module = NetworkModule(argument_spec=argument_spec,
                           connect_on_load=False,
                           mutually_exclusive=mutually_exclusive,
                           required_if=required_if,
                           supports_check_mode=True)

    if module.params['force'] is True:
        module.params['match'] = 'none'

    warnings = list()
    check_args(module, warnings)

    result = dict(changed=False, warnings=warnings)

    if module.params['backup']:
        result['__backup__'] = module.config.get_config()

    try:
        run(module, result)
    except NetworkError as e:
        module.disconnect()
        module.fail_json(msg=to_native(e), exception=traceback.format_exc())

    module.disconnect()
    module.exit_json(**result) 
Example 70
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: vertica_facts.py    MIT License 4 votes vote down vote up
def main():

    module = AnsibleModule(
        argument_spec=dict(
            cluster=dict(default='localhost'),
            port=dict(default='5433'),
            db=dict(default=None),
            login_user=dict(default='dbadmin'),
            login_password=dict(default=None, no_log=True),
        ), supports_check_mode=True)

    if not pyodbc_found:
        module.fail_json(msg="The python pyodbc module is required.")

    db = ''
    if module.params['db']:
        db = module.params['db']

    try:
        dsn = (
            "Driver=Vertica;"
            "Server=%s;"
            "Port=%s;"
            "Database=%s;"
            "User=%s;"
            "Password=%s;"
            "ConnectionLoadBalance=%s"
        ) % (module.params['cluster'], module.params['port'], db,
             module.params['login_user'], module.params['login_password'], 'true')
        db_conn = pyodbc.connect(dsn, autocommit=True)
        cursor = db_conn.cursor()
    except Exception as e:
        module.fail_json(msg="Unable to connect to database: %s." % to_native(e), exception=traceback.format_exc())

    try:
        schema_facts = get_schema_facts(cursor)
        user_facts = get_user_facts(cursor)
        role_facts = get_role_facts(cursor)
        configuration_facts = get_configuration_facts(cursor)
        node_facts = get_node_facts(cursor)
        module.exit_json(changed=False,
                         ansible_facts={'vertica_schemas': schema_facts,
                                        'vertica_users': user_facts,
                                        'vertica_roles': role_facts,
                                        'vertica_configuration': configuration_facts,
                                        'vertica_nodes': node_facts})
    except NotSupportedError as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc())
    except SystemExit:
        # avoid catching this on python 2.4
        raise
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc()) 
Example 71
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: vertica_configuration.py    MIT License 4 votes vote down vote up
def main():

    module = AnsibleModule(
        argument_spec=dict(
            parameter=dict(required=True, aliases=['name']),
            value=dict(default=None),
            db=dict(default=None),
            cluster=dict(default='localhost'),
            port=dict(default='5433'),
            login_user=dict(default='dbadmin'),
            login_password=dict(default=None, no_log=True),
        ), supports_check_mode=True)

    if not pyodbc_found:
        module.fail_json(msg="The python pyodbc module is required.")

    parameter_name = module.params['parameter']
    current_value = module.params['value']
    db = ''
    if module.params['db']:
        db = module.params['db']

    changed = False

    try:
        dsn = (
            "Driver=Vertica;"
            "Server={0};"
            "Port={1};"
            "Database={2};"
            "User={3};"
            "Password={4};"
            "ConnectionLoadBalance={5}"
        ).format(module.params['cluster'], module.params['port'], db,
                 module.params['login_user'], module.params['login_password'], 'true')
        db_conn = pyodbc.connect(dsn, autocommit=True)
        cursor = db_conn.cursor()
    except Exception as e:
        module.fail_json(msg="Unable to connect to database: {0}.".format(to_native(e)),
                         exception=traceback.format_exc())

    try:
        configuration_facts = get_configuration_facts(cursor)
        if module.check_mode:
            changed = not check(configuration_facts, parameter_name, current_value)
        else:
            try:
                changed = present(configuration_facts, cursor, parameter_name, current_value)
            except pyodbc.Error as e:
                module.fail_json(msg=to_native(e), exception=traceback.format_exc())
    except NotSupportedError as e:
        module.fail_json(msg=to_native(e), ansible_facts={'vertica_configuration': configuration_facts})
    except CannotDropError as e:
        module.fail_json(msg=to_native(e), ansible_facts={'vertica_configuration': configuration_facts})
    except SystemExit:
        # avoid catching this on python 2.4
        raise
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc())

    module.exit_json(changed=changed, parameter=parameter_name, ansible_facts={'vertica_configuration': configuration_facts}) 
Example 72
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: get_url.py    MIT License 4 votes vote down vote up
def url_get(module, url, dest, use_proxy, last_mod_time, force, timeout=10, headers=None, tmp_dest=''):
    """
    Download data from the url and store in a temporary file.

    Return (tempfile, info about the request)
    """
    if module.check_mode:
        method = 'HEAD'
    else:
        method = 'GET'

    rsp, info = fetch_url(module, url, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, headers=headers, method=method)

    if info['status'] == 304:
        module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', ''))

    # Exceptions in fetch_url may result in a status -1, the ensures a proper error to the user in all cases
    if info['status'] == -1:
        module.fail_json(msg=info['msg'], url=url, dest=dest)

    if info['status'] != 200 and not url.startswith('file:/') and not (url.startswith('ftp:/') and info.get('msg', '').startswith('OK')):
        module.fail_json(msg="Request failed", status_code=info['status'], response=info['msg'], url=url, dest=dest)

    # create a temporary file and copy content to do checksum-based replacement
    if tmp_dest:
        # tmp_dest should be an existing dir
        tmp_dest_is_dir = os.path.isdir(tmp_dest)
        if not tmp_dest_is_dir:
            if os.path.exists(tmp_dest):
                module.fail_json(msg="%s is a file but should be a directory." % tmp_dest)
            else:
                module.fail_json(msg="%s directory does not exist." % tmp_dest)
    else:
        tmp_dest = getattr(module, 'tmpdir', None)

    fd, tempname = tempfile.mkstemp(dir=tmp_dest)

    f = os.fdopen(fd, 'wb')
    try:
        shutil.copyfileobj(rsp, f)
    except Exception as e:
        os.remove(tempname)
        module.fail_json(msg="failed to create temporary content file: %s" % to_native(e), exception=traceback.format_exc())
    f.close()
    rsp.close()
    return tempname, info 
Example 73
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: uri.py    MIT License 4 votes vote down vote up
def write_file(module, url, dest, content):
    # create a tempfile with some test content
    fd, tmpsrc = tempfile.mkstemp(dir=getattr(module, 'tmpdir', None))
    f = open(tmpsrc, 'wb')
    try:
        f.write(content)
    except Exception as e:
        os.remove(tmpsrc)
        module.fail_json(msg="failed to create temporary content file: %s" % to_native(e),
                         exception=traceback.format_exc())
    f.close()

    checksum_src = None
    checksum_dest = None

    # raise an error if there is no tmpsrc file
    if not os.path.exists(tmpsrc):
        os.remove(tmpsrc)
        module.fail_json(msg="Source '%s' does not exist" % tmpsrc)
    if not os.access(tmpsrc, os.R_OK):
        os.remove(tmpsrc)
        module.fail_json(msg="Source '%s' not readable" % tmpsrc)
    checksum_src = module.sha1(tmpsrc)

    # check if there is no dest file
    if os.path.exists(dest):
        # raise an error if copy has no permission on dest
        if not os.access(dest, os.W_OK):
            os.remove(tmpsrc)
            module.fail_json(msg="Destination '%s' not writable" % dest)
        if not os.access(dest, os.R_OK):
            os.remove(tmpsrc)
            module.fail_json(msg="Destination '%s' not readable" % dest)
        checksum_dest = module.sha1(dest)
    else:
        if not os.access(os.path.dirname(dest), os.W_OK):
            os.remove(tmpsrc)
            module.fail_json(msg="Destination dir '%s' not writable" % os.path.dirname(dest))

    if checksum_src != checksum_dest:
        try:
            shutil.copyfile(tmpsrc, dest)
        except Exception as e:
            os.remove(tmpsrc)
            module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, to_native(e)),
                             exception=traceback.format_exc())

    os.remove(tmpsrc) 
Example 74
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: profitbricks_volume.py    MIT License 4 votes vote down vote up
def main():
    module = AnsibleModule(
        argument_spec=dict(
            datacenter=dict(),
            server=dict(),
            name=dict(),
            size=dict(type='int', default=10),
            bus=dict(choices=['VIRTIO', 'IDE'], default='VIRTIO'),
            image=dict(),
            image_password=dict(default=None, no_log=True),
            ssh_keys=dict(type='list', default=[]),
            disk_type=dict(choices=['HDD', 'SSD'], default='HDD'),
            licence_type=dict(default='UNKNOWN'),
            count=dict(type='int', default=1),
            auto_increment=dict(type='bool', default=True),
            instance_ids=dict(type='list', default=[]),
            subscription_user=dict(),
            subscription_password=dict(no_log=True),
            wait=dict(type='bool', default=True),
            wait_timeout=dict(type='int', default=600),
            state=dict(default='present'),
        )
    )

    if not module.params.get('subscription_user'):
        module.fail_json(msg='subscription_user parameter is required')
    if not module.params.get('subscription_password'):
        module.fail_json(msg='subscription_password parameter is required')

    subscription_user = module.params.get('subscription_user')
    subscription_password = module.params.get('subscription_password')

    profitbricks = ProfitBricksService(
        username=subscription_user,
        password=subscription_password)

    state = module.params.get('state')

    if state == 'absent':
        if not module.params.get('datacenter'):
            module.fail_json(msg='datacenter parameter is required for running or stopping machines.')

        try:
            (changed) = delete_volume(module, profitbricks)
            module.exit_json(changed=changed)
        except Exception as e:
            module.fail_json(msg='failed to set volume state: %s' % to_native(e), exception=traceback.format_exc())

    elif state == 'present':
        if not module.params.get('datacenter'):
            module.fail_json(msg='datacenter parameter is required for new instance')
        if not module.params.get('name'):
            module.fail_json(msg='name parameter is required for new instance')

        try:
            (volume_dict_array) = create_volume(module, profitbricks)
            module.exit_json(**volume_dict_array)
        except Exception as e:
            module.fail_json(msg='failed to set volume state: %s' % to_native(e), exception=traceback.format_exc()) 
Example 75
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_datacenter.py    MIT License 4 votes vote down vote up
def main():
    argument_spec = ovirt_full_argument_spec(
        state=dict(
            choices=['present', 'absent'],
            default='present',
        ),
        name=dict(default=None, required=True),
        description=dict(default=None),
        local=dict(type='bool'),
        compatibility_version=dict(default=None),
        quota_mode=dict(choices=['disabled', 'audit', 'enabled']),
        comment=dict(default=None),
        mac_pool=dict(default=None),
        force=dict(default=None, type='bool'),
    )
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    if module._name == 'ovirt_datacenters':
        module.deprecate("The 'ovirt_datacenters' module is being renamed 'ovirt_datacenter'", version=2.8)

    check_sdk(module)
    check_params(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        data_centers_service = connection.system_service().data_centers_service()
        clusters_module = DatacentersModule(
            connection=connection,
            module=module,
            service=data_centers_service,
        )

        state = module.params['state']
        if state == 'present':
            ret = clusters_module.create()
        elif state == 'absent':
            ret = clusters_module.remove(force=module.params['force'])

        module.exit_json(**ret)
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 76
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: _ovirt_affinity_labels_facts.py    MIT License 4 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        name=dict(default=None),
        host=dict(default=None),
        vm=dict(default=None),
    )
    module = AnsibleModule(argument_spec)

    if module._name == 'ovirt_affinity_labels_facts':
        module.deprecate("The 'ovirt_affinity_labels_facts' module is being renamed 'ovirt_affinity_label_facts'", version=2.8)

    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        affinity_labels_service = connection.system_service().affinity_labels_service()
        labels = []
        all_labels = affinity_labels_service.list()
        if module.params['name']:
            labels.extend([
                l for l in all_labels
                if fnmatch.fnmatch(l.name, module.params['name'])
            ])
        if module.params['host']:
            hosts_service = connection.system_service().hosts_service()
            labels.extend([
                label
                for label in all_labels
                for host in connection.follow_link(label.hosts)
                if fnmatch.fnmatch(hosts_service.service(host.id).get().name, module.params['host'])
            ])
        if module.params['vm']:
            vms_service = connection.system_service().vms_service()
            labels.extend([
                label
                for label in all_labels
                for vm in connection.follow_link(label.vms)
                if fnmatch.fnmatch(vms_service.service(vm.id).get().name, module.params['vm'])
            ])

        if not (module.params['vm'] or module.params['host'] or module.params['name']):
            labels = all_labels

        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_affinity_labels=[
                    get_dict_of_struct(
                        struct=l,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for l in labels
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 77
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_affinity_label_facts.py    MIT License 4 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        name=dict(default=None),
        host=dict(default=None),
        vm=dict(default=None),
    )
    module = AnsibleModule(argument_spec)

    if module._name == 'ovirt_affinity_labels_facts':
        module.deprecate("The 'ovirt_affinity_labels_facts' module is being renamed 'ovirt_affinity_label_facts'", version=2.8)

    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        affinity_labels_service = connection.system_service().affinity_labels_service()
        labels = []
        all_labels = affinity_labels_service.list()
        if module.params['name']:
            labels.extend([
                l for l in all_labels
                if fnmatch.fnmatch(l.name, module.params['name'])
            ])
        if module.params['host']:
            hosts_service = connection.system_service().hosts_service()
            labels.extend([
                label
                for label in all_labels
                for host in connection.follow_link(label.hosts)
                if fnmatch.fnmatch(hosts_service.service(host.id).get().name, module.params['host'])
            ])
        if module.params['vm']:
            vms_service = connection.system_service().vms_service()
            labels.extend([
                label
                for label in all_labels
                for vm in connection.follow_link(label.vms)
                if fnmatch.fnmatch(vms_service.service(vm.id).get().name, module.params['vm'])
            ])

        if not (module.params['vm'] or module.params['host'] or module.params['name']):
            labels = all_labels

        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_affinity_labels=[
                    get_dict_of_struct(
                        struct=l,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for l in labels
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 78
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: _ovirt_affinity_labels.py    MIT License 4 votes vote down vote up
def main():
    argument_spec = ovirt_full_argument_spec(
        state=dict(
            choices=['present', 'absent'],
            default='present',
        ),
        cluster=dict(default=None),
        name=dict(default=None, required=True),
        vms=dict(default=None, type='list'),
        hosts=dict(default=None, type='list'),
    )
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ('state', 'present', ['cluster']),
        ],
    )

    if module._name == 'ovirt_affinity_labels':
        module.deprecate("The 'ovirt_affinity_labels' module is being renamed 'ovirt_affinity_label'", version=2.8)

    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        affinity_labels_service = connection.system_service().affinity_labels_service()
        affinity_labels_module = AffinityLabelsModule(
            connection=connection,
            module=module,
            service=affinity_labels_service,
        )

        state = module.params['state']
        if state == 'present':
            ret = affinity_labels_module.create()
        elif state == 'absent':
            ret = affinity_labels_module.remove()

        module.exit_json(**ret)
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 79
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_quotas_facts.py    MIT License 4 votes vote down vote up
def main():
    argument_spec = ovirt_facts_full_argument_spec(
        data_center=dict(required=True),
        name=dict(default=None),
    )
    module = AnsibleModule(argument_spec)
    check_sdk(module)

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        datacenters_service = connection.system_service().data_centers_service()
        dc_name = module.params['data_center']
        dc = search_by_name(datacenters_service, dc_name)
        if dc is None:
            raise Exception("Datacenter '%s' was not found." % dc_name)

        quotas_service = datacenters_service.service(dc.id).quotas_service()
        if module.params['name']:
            quotas = [
                e for e in quotas_service.list()
                if fnmatch.fnmatch(e.name, module.params['name'])
            ]
        else:
            quotas = quotas_service.list()

        module.exit_json(
            changed=False,
            ansible_facts=dict(
                ovirt_quotas=[
                    get_dict_of_struct(
                        struct=c,
                        connection=connection,
                        fetch_nested=module.params.get('fetch_nested'),
                        attributes=module.params.get('nested_attributes'),
                    ) for c in quotas
                ],
            ),
        )
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None) 
Example 80
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ovirt_permissions.py    MIT License 4 votes vote down vote up
def main():
    argument_spec = ovirt_full_argument_spec(
        state=dict(type='str', default='present', choices=['absent', 'present']),
        role=dict(type='str', default='UserRole'),
        object_type=dict(type='str', default='vm',
                         choices=[
                             'cluster',
                             'cpu_profile',
                             'data_center',
                             'disk',
                             'disk_profile',
                             'host',
                             'network',
                             'storage_domain',
                             'system',
                             'template',
                             'vm',
                             'vm_pool',
                             'vnic_profile',
                         ]),
        authz_name=dict(type='str', required=True, aliases=['domain']),
        object_id=dict(type='str'),
        object_name=dict(type='str'),
        user_name=dict(type='str'),
        group_name=dict(type='str'),
        namespace=dict(type='str'),
    )
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    check_sdk(module)

    if (module.params['object_name'] is None and module.params['object_id'] is None) and module.params['object_type'] != 'system':
        module.fail_json(msg='"object_name" or "object_id" is required')

    if module.params['user_name'] is None and module.params['group_name'] is None:
        module.fail_json(msg='"user_name" or "group_name" is required')

    try:
        auth = module.params.pop('auth')
        connection = create_connection(auth)
        permissions_service = _object_service(connection, module).permissions_service()
        permissions_module = PermissionsModule(
            connection=connection,
            module=module,
            service=permissions_service,
        )

        permission = _permission(module, permissions_service, connection)
        state = module.params['state']
        if state == 'present':
            ret = permissions_module.create(entity=permission)
        elif state == 'absent':
            ret = permissions_module.remove(entity=permission)

        module.exit_json(**ret)
    except Exception as e:
        module.fail_json(msg=str(e), exception=traceback.format_exc())
    finally:
        connection.close(logout=auth.get('token') is None)