Skip to content

Commit

Permalink
Small changes to the resources scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
frankcorneliusmartin committed Oct 9, 2024
1 parent 5c7ad50 commit 25b3ab5
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 22 deletions.
151 changes: 132 additions & 19 deletions instructors/files/infrastructure/create-resources.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"source": [
"from vantage6.client import Client\n",
"\n",
"client = Client(url, port, api_path, log_level='debug')\n",
"client = Client(url, port, api_path, log_level='info')\n",
"\n",
"client.authenticate(username, password)\n",
"client.setup_encryption(None)"
Expand Down Expand Up @@ -86,23 +86,32 @@
"### read participants as list of dictionaries\n",
"import csv\n",
"\n",
"participants_file = 'workshop_resources/participants_try_outs.csv'\n",
"participants_file = 'workshop_resources/participants_real.csv'\n",
"participants = []\n",
"with open(participants_file, mode='r') as f:\n",
" reader = csv.DictReader(f)\n",
" for row in reader:\n",
" participants.append(row)\n",
"\n",
"print(len(participants))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cfdec69",
"metadata": {},
"outputs": [],
"source": [
"i = 0\n",
"while len(participants) % 6 != 0:\n",
" participants.append({\n",
" 'first_name': f'firt-filler-{i}',\n",
" 'last_name': f'last-name-filler-{i}',\n",
" 'email': f'filler-{i}@nowhere.something'\n",
" 'first_name': f'John-{i}',\n",
" 'last_name': f'Doe-{i}',\n",
" 'email': f'john-doe-{i}@dummy.co'\n",
" })\n",
" i += 1\n",
"\n",
"print(f\"Creating {len(participants)} participants with {i} dummies\")\n"
"print(f\"Creating {len(participants)} participants of which {i} dummies\")"
]
},
{
Expand All @@ -122,7 +131,24 @@
"source": [
"org_data = []\n",
"for org in organizations:\n",
" org_data.append(client.organization.create(**org, address2=''))"
" org_data.append(client.organization.create(**org, address2=''))\n",
" print(f\"Created organization {org['name']}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6870461a",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import string\n",
"\n",
"def generate_password(length=8):\n",
" characters = string.ascii_letters + string.digits\n",
" password = ''.join(random.choices(characters, k=length))\n",
" return password + '1aA!'"
]
},
{
Expand All @@ -132,21 +158,41 @@
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# create users\n",
"researcher_role = 5\n",
"for i, participant in enumerate(participants[1:]):\n",
"user_data = []\n",
"for i, participant in enumerate(participants):\n",
" username = participant['first_name'][0].lower() + participant['last_name'].lower()\n",
" print(f\"--> Creating user {username}\")\n",
" password = generate_password(8)\n",
" data = {\n",
" \"firstname\": participant['first_name'],\n",
" \"lastname\": participant['last_name'],\n",
" \"email\": participant['email'],\n",
" # username of John Doe is jdoe\n",
" \"username\": participant['first_name'][0].lower() + participant['last_name'].lower(),\n",
" \"password\": \"Password123!\",\n",
" \"username\": username,\n",
" \"password\": password,\n",
" \"roles\": [researcher_role],\n",
" \"rules\": [],\n",
" \"organization\": org_data[i]['id']\n",
" }\n",
" client.user.create(**data)"
" client.user.create(**data)\n",
" user_data.append(data)\n",
"\n",
"df = pd.DataFrame(user_data)\n",
"df.to_csv('workshop_resources/credentials.csv', index=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a07209a",
"metadata": {},
"outputs": [],
"source": [
"SIZE_COLLABS = 5"
]
},
{
Expand All @@ -162,7 +208,6 @@
"# first 35 and 1 from the last 7 (offline) organizations.\n",
"collaboration_data = []\n",
"collaboration_responses = []\n",
"SIZE_COLLABS = 5 # TODO: if you have less than 6 participants this need to be set to that number\n",
"for idx in range(0, int(len(participants) / SIZE_COLLABS)):\n",
" data = {\n",
" \"name\": collaboration_names[idx],\n",
Expand Down Expand Up @@ -209,10 +254,38 @@
" cwd = Path.cwd()\n",
" output_path = Path(cwd / \"output\" / \"node_configs\" / f\"{node_name}.yaml\")\n",
" output_path.parent.mkdir(parents=True, exist_ok=True)\n",
" with open(output_path, \"x\") as f:\n",
" with open(output_path, \"w\") as f:\n",
" f.write(node_config)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac779a7e",
"metadata": {},
"outputs": [],
"source": [
"# collaboration_responses = client.collaboration.list(scope=\"global\")[\"data\"]\n",
"# collaboration_data = []\n",
"# for collab in collaboration_responses:\n",
"# collaboration_data.append({\n",
"# \"name\": collab['name'],\n",
"# \"organizations\": [org[\"id\"] for org in client.organization.list(collaboration=collab['id'], per_page=999)[\"data\"]],\n",
"# \"encrypted\": False\n",
"# })\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f7b8d38",
"metadata": {},
"outputs": [],
"source": [
"# org_data = client.organization.list(per_page=999)[\"data\"]\n",
"# org_data"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -234,7 +307,8 @@
" # for all nodes except the last one, create a node config file\n",
" if org_idx != SIZE_COLLABS:\n",
" create_node_config_file(response['api_key'], data[\"name\"], org_idx + 1)\n",
" node_responses.append(response)"
" node_responses.append(response)\n",
" print(f\"Created node {data['name']}\")"
]
},
{
Expand All @@ -250,8 +324,8 @@
" # TODO we are not sure here that all the collaboration_names are created as we are\n",
" # not sure that we have sufficient participants. However it will raise an error,\n",
" # this is not a problem as the studies for the necessary collaborations are created\n",
" collab = client.collaboration.list(scope=\"global\", name=collab_name)['data'][0]\n",
" orgs = client.organization.list(collaboration=collab['id'])['data']\n",
" collab = client.collaboration.list(scope=\"global\", name=collab_name, per_page=999)['data'][0]\n",
" orgs = client.organization.list(collaboration=collab['id'], per_page=999)['data']\n",
" org_ids = [org['id'] for org in orgs]\n",
" # remove the org with the highest id, which is the offline org\n",
"\n",
Expand Down Expand Up @@ -319,6 +393,16 @@
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41cacd35",
"metadata": {},
"outputs": [],
"source": [
"len(participants)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -348,6 +432,35 @@
" org_data[abbrev_name] = partipant_orgs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4b02494",
"metadata": {},
"outputs": [],
"source": [
"# Read credentials from CSV file\n",
"credentials_file = 'workshop_resources/credentials.csv'\n",
"credentials = []\n",
"with open(credentials_file, mode='r') as f:\n",
" reader = csv.DictReader(f)\n",
" for row in reader:\n",
" credentials.append(row)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bbf37a33",
"metadata": {},
"outputs": [],
"source": [
"for participant in participants:\n",
" for credential in credentials:\n",
" if participant['email'] == credential['email']:\n",
" participant[\"password\"] = credential[\"password\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -366,7 +479,7 @@
" \"email\": participant['email']+ \"2\",\n",
" # username of John Doe is jdoe_admin\n",
" \"username\": abbrev_name + \"_admin\",\n",
" \"password\": \"Password123!\",\n",
" \"password\": participant['password'],\n",
" \"roles\": [workshopadmin_role],\n",
" \"rules\": [],\n",
" \"organization\": org_id\n",
Expand Down Expand Up @@ -417,7 +530,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
54 changes: 51 additions & 3 deletions instructors/files/infrastructure/delete-resources.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"### read participants as list of dictionaries\n",
"import csv\n",
"\n",
"participants_file = 'workshop_resources/participants_try_outs.csv'\n",
"participants_file = 'workshop_resources/participants_real.csv'\n",
"participants = []\n",
"with open(participants_file, mode='r') as f:\n",
" reader = csv.DictReader(f)\n",
Expand All @@ -78,8 +78,9 @@
"source": [
"# clear users\n",
"for participant in participants:\n",
" print(f\"Deleting user {participant['first_name']} {participant['last_name']}\")\n",
" response = client.user.list(firstname=participant['first_name'], lastname=participant['last_name'])\n",
" username = participant['first_name'][0].lower() + participant['last_name'].lower()\n",
" print(f\"Deleting user {participant['first_name']} {participant['last_name']} ({username})\")\n",
" response = client.user.list(username=username)\n",
" if len(response['data']) > 0:\n",
" user = response['data'][0]\n",
" response = client.user.delete(user['id'])\n",
Expand Down Expand Up @@ -167,6 +168,53 @@
" except Exception as e:\n",
" print(\"Failed to delete collaboration\", e)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Delete all dummies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for org in client.organization.list(name=\"jdoe%\", per_page=999)[\"data\"]:\n",
" print(f\"Deleting organization {org}\")\n",
" client.organization.delete(org[\"id\"], delete_dependents=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for col in client.collaboration.list(name=\"jdoe%\", scope=\"global\", per_page=999)[\"data\"]:\n",
" print(f\"Deleting collaboration {col}\")\n",
" client.collaboration.delete(col[\"id\"], delete_dependents=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for user in client.user.list(username=\"jdoe%\")[\"data\"]:\n",
" print(f\"Deleting user {user['username']}\")\n",
" client.user.delete(user[\"id\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 25b3ab5

Please sign in to comment.