Exemplo n.º 1
0
def remove_service(input, service_id):
    service = get_service(service_id)
    try:
        service.Remove()
    except:
        return "Invalid or missing service %s" % service_id

    return None
Exemplo n.º 2
0
def remove_service(input, service_id):
    service = get_service(service_id)
    try:
        service.Remove()
    except:
        return "Invalid or missing service %s" % service_id

    return None
Exemplo n.º 3
0
def connect_psk(input, service_id, agent):
    (from_agent, to_agent, agent_stderr) = (agent.stdout, agent.stdin,
                                            agent.stderr)
    sleep(1)
    print >> to_agent, "Passphrase=%s\n" % input.passphrase
    to_agent.flush()
    sleep(1)
    service = get_service(service_id)
    try:
        service.Connect()
    except dbus.DBusException, error:
        #print "%s: %s" % (error._dbus_error_name, error.message)

        if error._dbus_error_name != "net.connman.Error.InProgress":
            agent.terminate()
            agent.wait()
            return "Invalid or missing service %s (%s: %s)" % \
                (service_id, error._dbus_error_name, error.message)
Exemplo n.º 4
0
def connect_psk(input, service_id, agent):
    (from_agent, to_agent, agent_stderr) = (agent.stdout, agent.stdin,
                                            agent.stderr)
    sleep(1)
    print >> to_agent, "Passphrase=%s\n" % input.passphrase
    to_agent.flush()
    sleep(1)
    service = get_service(service_id)
    try:
        service.Connect()
    except dbus.DBusException, error:
        #print "%s: %s" % (error._dbus_error_name, error.message)

        if error._dbus_error_name != "net.connman.Error.InProgress":
            agent.terminate()
            agent.wait()
            return "Invalid or missing service %s (%s: %s)" % \
                (service_id, error._dbus_error_name, error.message)
Exemplo n.º 5
0
  Args:
    output_dir: str containing the path to the directory you want to save to.
    query_id: str containing the Id of the query that generated the report.
    report_url: str containing the url to the generated report.
  """
    # Create formatter for output file path.
    if not os.path.isabs(output_dir):
        output_dir = os.path.expanduser(output_dir)
    output_fmt = output_dir + '/%s.csv'
    with (open(output_fmt % query_id, 'wb')) as handle:
        handle.write(urllib.request.urlopen(report_url).read())


if __name__ == '__main__':
    args = parser.parse_args()
    # Retrieve the query id of the report we're downloading, or set to 0.
    QUERY_ID = args.query_id
    if not QUERY_ID:
        try:
            QUERY_ID = int(
                input('Enter the query id or press enter to '
                      'list queries: '))
        except ValueError:
            QUERY_ID = 0

    main(
        util.get_service(client_id=args.client_id,
                         client_secret=args.client_secret),
        args.output_directory, QUERY_ID, args.report_window)
Exemplo n.º 6
0
        # Then write the contents of the response to a CSV file.
        handler.write(request.execute()['lineItems'].encode('utf-8'))
        print('Download complete.')


if __name__ == '__main__':
    request_body = {}
    valid_filter_types = [
        'ADVERTISER_ID', 'INSERTION_ORDER_ID', 'LINE_ITEM_ID'
    ]

    # If your download requests time out, you may need to filter to reduce the
    # number of items returned. Below we parse optional arguments to add these
    # filters to the body of the request.
    args = parser.parse_args()

    path = args.file_path
    if not os.path.isabs(path):
        FILE_PATH = os.path.expanduser(path)
    if args.filter_ids:
        request_body['filterIds'] = args.filter_ids.split(',')
    # if args.filter_type in valid_filter_types:
    #   request_body['filterType'] = args.filter_type
    # else:
    #     raise ValueError('Invalid filterType. Acceptable values: %s' %
    #                      valid_filter_types)

    main(
        util.get_service(client_id=args.client_id,
                         client_secret=args.client_secret), path, request_body)
Exemplo n.º 7
0
def update_service(input, service_id):
    properties = get_properties(service_id)
    if not properties.keys():
        return "No properties for service %s" % service_id

    service = get_service(service_id)

    try:
        if input.autoconnect == "Yes":
            autoconnect = True
        else:
            autoconnect = False
        (value_changed, error) = change(service, properties, "AutoConnect",
                                        autoconnect)
        if not value_changed:
            return service_not_found(service_id, error, "AutoConnect")
    except:
        pass

    try:
        (value_changed, error) = change(service, properties,
                                        "Domains.Configuration",
                                        input.domains,
                                        dbus.Array([input.domains],
                                                signature=dbus.Signature('s')))
        if not value_changed:
            return service_not_found(service_id, error, "Domains")
    except:
        pass

    try:
        (value_changed, error) = change(service, properties,
                                        "Nameservers.Configuration",
                                        input.nameservers,
                                        dbus.Array([input.nameservers],
                                               signature=dbus.Signature('s')))
        if not value_changed:
            return service_not_found(service_id, error, "Nameservers")
    except:
        pass

    try:
        (value_changed, error) = change(service, properties,
                                        "Timeservers.Configuration",
                                        input.timeservers,
                                        dbus.Array([input.timeservers],
                                               signature=dbus.Signature('s')))
        if not value_changed:
            return service_not_found(service_id, error, "Timeservers")
    except:
        pass

    try:
        if input.proxymethod == "auto":
            if changed_dict(properties, "Proxy.Configuration", "URL",
                            input.proxyurl):
                proxy = { "Method": make_variant("auto") }
                proxy["URL"] = make_variant(input.proxyurl)
                try:
                    service.SetProperty("Proxy.Configuration", proxy)
                except dbus.DBusException, error:
                    return service_not_found(service_id, error,
                                             "Proxy URL setting failed")

        elif input.proxymethod == "manual":
            set_property = False
            proxy = { "Method": make_variant("manual") }
            if changed_dict(properties, "Proxy.Configuration", "Servers",
                            input.proxyservers):
                proxy["Servers"] = split_string(input.proxyservers)
                set_property = True

            if changed_dict(properties, "Proxy.Configuration", "Excludes",
                        input.proxyexcludes):
                proxy["Excludes"] = split_string(input.proxyexcludes)
                set_property = True

            if set_property:
                try:
                    service.SetProperty("Proxy.Configuration",
                                        dbus.Dictionary(proxy, signature='sv'))
                except dbus.DBusException, error:
                    return service_not_found(service_id, error,
                                             "Manual Proxy setting failed")
def main(doubleclick_bid_manager, body):
  # Construct the request.
  request = doubleclick_bid_manager.lineitems().uploadlineitems(body=body)
  response = request.execute()

  if 'uploadStatus' in response and 'errors' in response['uploadStatus']:
    for error in response['uploadStatus']['errors']:
      print error
  else:
    print 'Upload Successful.'


if __name__ == '__main__':
  args = parser.parse_args()

  file_path = args.file_path
  if not os.path.isabs(file_path):
    file_path = os.path.expanduser(file_path)

  with open(file_path, 'rb') as handle:
    line_items = handle.read()

  BODY = {
      'dryRun': args.dry_run,
      'lineItems': line_items
  }

  main(util.get_service(
      client_id=args.client_id, client_secret=args.client_secret), BODY)

Exemplo n.º 9
0
def test_remote_auth_hub(image, swarm, network, make_service):
    """Test that logging in as a new user creates a new docker service."""
    test_logger.info("Start of service testing")
    make_service(remote_hub_service)
    client = docker.from_env()
    # jupyterhub service should be running at this point
    services_before_spawn = client.services.list()
    test_logger.info("Pre test services: {}".format(services_before_spawn))

    username = "******"
    # Auth header
    test_logger.info("Authenticating with user: {}".format(username))
    headers = {"Remote-User": username}
    assert wait_for_site(JHUB_URL, valid_status_code=401) is True

    with requests.Session() as s:
        # Login
        login_response = s.post(JHUB_URL + "/hub/login", headers=headers)
        test_logger.info("Login response message: {}".format(
            login_response.text))
        assert login_response.status_code == 200

        # Spawn a notebook
        spawn_form_resp = s.get(JHUB_URL + "/hub/spawn")
        test_logger.info("Spawn page message: {}".format(spawn_form_resp.text))
        assert spawn_form_resp.status_code == 200
        assert "Select a notebook image" in spawn_form_resp.text
        payload = {"dockerimage": "nielsbohr/base-notebook:latest"}
        spawn_resp = s.post(JHUB_URL + "/hub/spawn", data=payload)
        test_logger.info("Spawn POST response message: {}".format(
            spawn_resp.text))
        assert spawn_resp.status_code == 200

        # Get spawned service
        target_service_name = "{}-".format("jupyter")
        spawned_service = get_service(client, target_service_name)
        assert spawned_service is not None

        # Get the service api url
        service_url = get_service_url(spawned_service)
        # If failed the service might not be running
        if not service_url:
            test_logger.info("Properly failed to start the service correctly")
        assert service_url is not None

        # Combine with the base jhub URL
        jhub_service_api = urljoin(JHUB_URL, service_url)

        # Write to user home
        new_file = "write_test.ipynb"
        data = json.dumps({"name": new_file})
        test_logger.info("Looking for xsrf in: {}".format(s.cookies))

        # Refresh csrf token
        assert wait_for_session(s, jhub_service_api, require_xsrf=True)
        assert "_xsrf" in s.cookies
        xsrf_token = s.cookies["_xsrf"]
        service_api_url = get_service_api_url(spawned_service,
                                              postfix_url="contents/")
        jhub_service_content = urljoin(JHUB_URL, service_api_url)

        # Write to home
        xsrf_headers = {"X-XSRFToken": xsrf_token}
        resp = s.put(
            "".join([jhub_service_content, new_file]),
            data=data,
            headers=xsrf_headers,
        )
        assert resp.status_code == 201

        # Remove via the web interface
        refresh_csrf(s, jhub_service_api)
        assert "_xsrf" in s.cookies
        delete_headers = {
            "Referer": urljoin(JHUB_URL, "/hub/home"),
            "Origin": JHUB_URL,
        }

        jhub_user = get_service_user(spawned_service)
        delete_url = urljoin(JHUB_URL,
                             "/hub/api/users/{}/server".format(jhub_user))

        # Wait for the server to finish deleting
        deleted = delete(s, delete_url, headers=delete_headers)
        assert deleted

        deleted_service = get_service(client, target_service_name)
        assert deleted_service is None
Exemplo n.º 10
0
def test_image_selection(image, swarm, network, make_service):
    """Test that the spawner allows for dynamic image selection"""
    test_logger.info("Start of the image selection test")
    make_service(hub_service)
    client = docker.from_env()
    # jupyterhub service should be running at this point
    services_before_spawn = client.services.list()
    test_logger.info("Pre test services: {}".format(services_before_spawn))

    username = "******"
    password = "******"
    test_logger.info("Authenticating with user: {}".format(username))
    assert wait_for_site(JHUB_URL) is True

    with requests.Session() as s:
        # login
        test_logger.info("Authenticating with user: {}".format(username))
        login_response = s.post(
            urljoin(JHUB_URL, "/hub/login"),
            data={
                "username": username,
                "password": password
            },
        )
        test_logger.info("Login response message: {}".format(
            login_response.text))
        assert login_response.status_code == 200

        # Spawn a notebook
        spawn_form_resp = s.get(JHUB_URL + "/hub/spawn")
        test_logger.info("Spawn page message: {}".format(spawn_form_resp.text))

        assert spawn_form_resp.status_code == 200
        assert "Select a notebook image" in spawn_form_resp.text

        user_image = "nielsbohr/base-notebook:latest"
        user_image_name = "Basic Python Notebook"

        payload = {"name": user_image_name, "image": user_image}
        json_payload = json.dumps(payload)
        spawn_resp = s.post(
            JHUB_URL + "/hub/spawn/{}".format(username),
            files={"select_image": (
                None,
                json_payload,
            )},
        )

        test_logger.info("Spawn POST response message: {}".format(
            spawn_resp.text))
        assert spawn_resp.status_code == 200

        target_service_name = "{}-{}-{}".format("jupyter", username, "1")
        spawned_service = get_service(client, target_service_name)
        assert spawned_service is not None

        # Verify that a task is succesfully running
        running_task = wait_for_service_task(
            client, spawned_service, filters={"desired-state": "running"})
        assert running_task

        # Verify that the image is correct
        service_image = get_task_image(running_task)
        assert service_image == user_image

        service_labels = get_service_labels(spawned_service)
        assert service_labels is not None
        assert service_labels["image_name"] == user_image_name

        # Delete the spawned service
        delete_headers = {
            "Referer": urljoin(JHUB_URL, "/hub/home"),
            "Origin": JHUB_URL
        }

        jhub_user = get_service_user(spawned_service)
        delete_url = urljoin(JHUB_URL,
                             "/hub/api/users/{}/server".format(jhub_user))

        deleted = delete(s, delete_url, headers=delete_headers)
        assert deleted

        deleted_service = get_service(client, target_service_name)
        assert deleted_service is None

        # Spawn a second service with a different name but the same image ##
        # Spawn a notebook
        second_spawn_form_resp = s.get(JHUB_URL + "/hub/spawn")
        test_logger.info("Spawn page message: {}".format(
            second_spawn_form_resp.text))

        assert second_spawn_form_resp.status_code == 200
        assert "Select a notebook image" in second_spawn_form_resp.text

        second_image_name = "Basic Python Notebook 2"
        selection_payload = {"name": second_image_name, "image": user_image}
        json_second_payload = json.dumps(selection_payload)

        spawn_resp = s.post(
            JHUB_URL + "/hub/spawn/{}".format(username),
            files={"select_image": (
                None,
                json_second_payload,
            )},
        )
        test_logger.info("Spawn POST response message: {}".format(
            spawn_resp.text))
        assert spawn_resp.status_code == 200

        second_target_service_name = "{}-{}-{}".format("jupyter", username,
                                                       "1")
        second_spawned_service = get_service(client,
                                             second_target_service_name)
        assert second_spawned_service is not None

        # Verify that a task is succesfully running
        second_running_task = wait_for_service_task(
            client,
            second_spawned_service,
            filters={"desired-state": "running"})
        assert second_running_task

        # Verify that the image is correct
        second_service_image = get_task_image(second_running_task)
        assert second_service_image == user_image

        second_service_labels = get_service_labels(second_spawned_service)
        assert second_service_labels is not None
        assert second_service_labels["image_name"] == second_image_name

        # Delete the second spawned service
        deleted = delete(s, delete_url, headers=delete_headers)
        assert deleted

        deleted_service = get_service(client, second_target_service_name)
        assert deleted_service is None
Exemplo n.º 11
0
def disconnect_service(input, service_id):
    service = get_service(service_id)
    try:
        service.Disconnect()
    except dbus.DBusException, error:
        return error
Exemplo n.º 12
0
def service(input, service_id):
    service = get_service(service_id)
    try:
        service.Connect()
    except dbus.DBusException, error:
        print "%s: %s" % (error._dbus_error_name, error.message)
Exemplo n.º 13
0
def disconnect_service(input, service_id):
    service = get_service(service_id)
    try:
        service.Disconnect()
    except dbus.DBusException, error:
        return error
Exemplo n.º 14
0
def service(input, service_id):
    service = get_service(service_id)
    try:
        service.Connect()
    except dbus.DBusException, error:
        print "%s: %s" % (error._dbus_error_name, error.message)
Exemplo n.º 15
0
def post(post_id):
	res = util.get_service('/collab/post/', post_id)
	return util.toJSON(res)
Exemplo n.º 16
0
def test_creates_service(image, swarm, network, make_service):
    """Test that logging in as a new user creates a new docker service."""
    test_logger.info("Start of service testing")
    make_service(hub_service)
    client = docker.from_env()
    # jupyterhub service should be running at this point
    services_before_spawn = client.services.list()
    test_logger.info("Pre test services: {}".format(services_before_spawn))

    username = "******"
    password = "******"
    test_logger.info("Authenticating with user: {}".format(username))
    assert wait_for_site(JHUB_URL) is True

    with requests.Session() as s:
        # login
        test_logger.info("Authenticating with user: {}".format(username))
        login_response = s.post(
            JHUB_URL + "/hub/login?next=",
            data={
                "username": username,
                "password": password
            },
        )
        test_logger.info("Login response message: {}".format(
            login_response.text))
        assert login_response.status_code == 200
        # Spawn a notebook
        spawn_form_resp = s.get(JHUB_URL + "/hub/spawn")
        test_logger.info("Spawn page message: {}".format(spawn_form_resp.text))

        assert spawn_form_resp.status_code == 200
        assert "Select a notebook image" in spawn_form_resp.text

        payload = {"dockerimage": "nielsbohr/base-notebook:latest"}
        spawn_resp = s.post(JHUB_URL + "/hub/spawn", data=payload)
        test_logger.info("Spawn POST response message: {}".format(
            spawn_resp.text))
        assert spawn_resp.status_code == 200

        services = client.services.list()
        test_logger.info("Post spawn services: {}".format(services))

        target_service_name = "{}-{}-{}".format("jupyter", username, "1")
        spawned_service = get_service(client, target_service_name)
        assert spawned_service is not None

        # Verify that a task is succesfully running
        running_task = wait_for_service_task(
            client, spawned_service, filters={"desired-state": "running"})
        assert running_task

        # wait for user home
        home_resp = s.get(JHUB_URL + "/user/{}/tree?".format(username))
        assert home_resp.status_code == 200
        # Remove via the web interface
        delete_headers = {
            "Referer": urljoin(JHUB_URL, "/hub/home"),
            "Origin": JHUB_URL
        }

        jhub_user = get_service_user(spawned_service)
        delete_url = urljoin(JHUB_URL,
                             "/hub/api/users/{}/server".format(jhub_user))

        pending = True
        num_wait, max_wait = 0, 15
        while pending or num_wait > max_wait:
            num_wait += 1
            resp = s.delete(delete_url, headers=delete_headers)
            test_logger.info(
                "Response from removing the user server: {}".format(resp.text))
            if resp.status_code == 204:
                pending = False
            time.sleep(1)

        assert resp.status_code == 204

        # double check it is gone
        deleted_service = get_service(client, target_service_name)
        assert deleted_service is None
        test_logger.info("End of test service")
Exemplo n.º 17
0
def security_get_session():
	res = util.get_service('/security/session/')
	return util.toJSON(res)
Exemplo n.º 18
0
def update_service(input, service_id):
    properties = get_properties(service_id)
    if not properties.keys():
        return "No properties for service %s" % service_id

    service = get_service(service_id)

    try:
        if input.autoconnect == "Yes":
            autoconnect = True
        else:
            autoconnect = False
        (value_changed, error) = change(service, properties, "AutoConnect",
                                        autoconnect)
        if not value_changed:
            return service_not_found(service_id, error, "AutoConnect")
    except:
        pass

    try:
        (value_changed, error) = change(
            service, properties, "Domains.Configuration", input.domains,
            dbus.Array([input.domains], signature=dbus.Signature('s')))
        if not value_changed:
            return service_not_found(service_id, error, "Domains")
    except:
        pass

    try:
        (value_changed, error) = change(
            service, properties, "Nameservers.Configuration",
            input.nameservers,
            dbus.Array([input.nameservers], signature=dbus.Signature('s')))
        if not value_changed:
            return service_not_found(service_id, error, "Nameservers")
    except:
        pass

    try:
        (value_changed, error) = change(
            service, properties, "Timeservers.Configuration",
            input.timeservers,
            dbus.Array([input.timeservers], signature=dbus.Signature('s')))
        if not value_changed:
            return service_not_found(service_id, error, "Timeservers")
    except:
        pass

    try:
        if input.proxymethod == "auto":
            if changed_dict(properties, "Proxy.Configuration", "URL",
                            input.proxyurl):
                proxy = {"Method": make_variant("auto")}
                proxy["URL"] = make_variant(input.proxyurl)
                try:
                    service.SetProperty("Proxy.Configuration", proxy)
                except dbus.DBusException, error:
                    return service_not_found(service_id, error,
                                             "Proxy URL setting failed")

        elif input.proxymethod == "manual":
            set_property = False
            proxy = {"Method": make_variant("manual")}
            if changed_dict(properties, "Proxy.Configuration", "Servers",
                            input.proxyservers):
                proxy["Servers"] = split_string(input.proxyservers)
                set_property = True

            if changed_dict(properties, "Proxy.Configuration", "Excludes",
                            input.proxyexcludes):
                proxy["Excludes"] = split_string(input.proxyexcludes)
                set_property = True

            if set_property:
                try:
                    service.SetProperty("Proxy.Configuration",
                                        dbus.Dictionary(proxy, signature='sv'))
                except dbus.DBusException, error:
                    return service_not_found(service_id, error,
                                             "Manual Proxy setting failed")