def record_deploy(args):
    import sys

    if len(args) < 2:
        usage("record-deploy")
        sys.exit(1)

    def _args(config_file,
              description,
              revision="Unknown",
              changelog=None,
              user=None,
              *args):
        return config_file, description, revision, changelog, user

    config_file, description, revision, changelog, user = _args(*args)

    settings = global_settings()

    settings.monitor_mode = False

    initialize(config_file)

    host = settings.host

    if host == "collector.newrelic.com":
        host = "api.newrelic.com"
    elif host.startswith("collector.eu"):
        host = "api.eu.newrelic.com"
    elif host == "staging-collector.newrelic.com":
        host = "staging-api.newrelic.com"

    port = settings.port

    server = port and "%s:%d" % (host, port) or host

    app_id = fetch_app_id(settings.app_name, server=server)
    if app_id is None:
        raise RuntimeError(
            "The application named %r was not found in your account. Please "
            "try running the newrelic-admin server-config command to force "
            "the application to register with New Relic." % settings.app_name)

    url = "https://{}/v2/applications/{}/deployments.json".format(
        server, app_id)

    if user is None:
        user = pwd.getpwuid(os.getuid()).pw_gecos

    deployment = {}
    deployment["revision"] = revision

    if description:
        deployment["description"] = description
    if changelog:
        deployment["changelog"] = changelog
    if user:
        deployment["user"] = user

    data = {"deployment": deployment}

    r = requests.post(url, json=data, **api_request_kwargs())

    if r.status_code != 201:
        raise RuntimeError(
            "An unexpected HTTP response of %r was received "
            "for request made to %r. The payload for the request was %r. "
            "The response payload for the request was %r. If this issue "
            "persists then please report this problem to New Relic "
            "support for further investigation." %
            (r.status_code, url, data, r.json()))
Exemple #2
0
def local_config(args):
    import sys

    if len(args) < 2:
        usage('record-deploy')
        sys.exit(1)

    def _args(config_file, description, revision=None, changelog=None,
            user=None, *args):
        return config_file, description, revision, changelog, user

    config_file, description, revision, changelog, user = _args(*args)

    settings = global_settings()

    settings.monitor_mode = False

    initialize(config_file)

    app_name = settings.app_name

    api_key = settings.api_key or 'NO API KEY WAS SET IN AGENT CONFIGURATION'

    host = settings.host

    if host == 'collector.newrelic.com':
        host = 'api.newrelic.com'
    elif host == 'staging-collector.newrelic.com':
        host = 'staging-api.newrelic.com'

    port = settings.port
    ssl = settings.ssl

    url = '%s://%s/deployments.xml'

    scheme = ssl and 'https' or 'http'
    server = port and '%s:%d' % (host, port) or host

    url = url % (scheme, server)

    proxy_host = settings.proxy_host
    proxy_port = settings.proxy_port
    proxy_user = settings.proxy_user
    proxy_pass = settings.proxy_pass

    timeout = settings.agent_limits.data_collector_timeout

    proxies = proxy_details(None, proxy_host, proxy_port, proxy_user,
            proxy_pass)

    if user is None:
        user = pwd.getpwuid(os.getuid()).pw_gecos

    data = {}

    data['deployment[app_name]'] = app_name

    if description is not None:
        data['deployment[description]'] = description
    if revision is not None:
        data['deployment[revision]'] = revision
    if changelog is not None:
        data['deployment[changelog]'] = changelog
    if user is not None:
        data['deployment[user]'] = user

    headers = {}

    headers['X-API-Key'] = api_key

    cert_loc = certs.where()

    r = requests.post(url, proxies=proxies, headers=headers,
            timeout=timeout, data=data, verify=cert_loc)

    if r.status_code != 201:
        raise RuntimeError('An unexpected HTTP response of %r was received '
                'for request made to %r. The API key for the request was '
                '%r. The payload for the request was %r. If this issue '
                'persists then please report this problem to New Relic '
                'support for further investigation.' % (r.status_code,
                url, api_key, data))
Exemple #3
0
def local_config(args):
    import sys

    if len(args) < 2:
        usage("record-deploy")
        sys.exit(1)

    def _args(app_id,
              config_file,
              revision,
              description=None,
              changelog=None,
              user=None,
              timestamp=None,
              *args):
        return (
            app_id,
            config_file,
            revision,
            description,
            changelog,
            user,
            timestamp,
        )

    app_id, config_file, revision, description, changelog, user, timestamp = _args(
        *args)

    settings = global_settings()

    settings.monitor_mode = False

    initialize(config_file)

    api_key = settings.api_key or "NO API KEY WAS SET IN AGENT CONFIGURATION"

    host = settings.host

    if host == "collector.newrelic.com":
        host = "api.newrelic.com"
    elif host.startswith("collector.eu"):
        host = "api.eu.newrelic.com"
    elif host == "staging-collector.newrelic.com":
        host = "staging-api.newrelic.com"

    port = settings.port

    url = "%s://%s/v2/applications/%s/deployments.json"

    scheme = "https"
    server = port and "%s:%d" % (host, port) or host

    url = url % (scheme, server, app_id)

    proxy_scheme = settings.proxy_scheme
    proxy_host = settings.proxy_host
    proxy_port = settings.proxy_port
    proxy_user = settings.proxy_user
    proxy_pass = settings.proxy_pass

    if proxy_scheme is None:
        proxy_scheme = "https"

    timeout = settings.agent_limits.data_collector_timeout

    proxies = proxy_details(proxy_scheme, proxy_host, proxy_port, proxy_user,
                            proxy_pass)

    if user is None:
        user = pwd.getpwuid(os.getuid()).pw_gecos

    data = {}
    data["deployment"] = {}
    data["deployment"]["revision"] = revision

    if description is not None:
        data["deployment"]["description"] = description
    if changelog is not None:
        data["deployment"]["changelog"] = changelog
    if user is not None:
        data["deployment"]["user"] = user
    if timestamp is not None:
        data["deployment"]["timestamp"] = timestamp

    headers = {}
    headers["X-Api-Key"] = api_key
    headers["Content-Type"] = "application/json"

    cert_loc = settings.ca_bundle_path
    if cert_loc is None:
        cert_loc = certs.where()

    if settings.debug.disable_certificate_validation:
        cert_loc = False

    data = json.dumps(data)

    r = requests.post(
        url,
        proxies=proxies,
        headers=headers,
        timeout=timeout,
        data=data,
        verify=cert_loc,
    )

    if r.status_code != 201:
        raise RuntimeError(
            "An unexpected HTTP response of %r was received "
            "for request made to %r. The API key for the request was "
            "%r. The payload for the request was %r. The response "
            "payload for the request was %r. If this issue "
            "persists then please report this problem to New Relic "
            "support for further investigation." %
            (r.status_code, url, api_key, data, r.json()))