Nurtch-DevOps-Demo.ipynb (163 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Please store any variables (credentials, endpoint etc.) required to operate infrastructure as [Gitlab project variables](https://gitlab.com/help/ci/variables/README#variables). We use gitlab [python client](https://python-gitlab.readthedocs.io/en/stable/index.html) to retrieve the variables. Feel free to supply credentials directly for demo purposes but we don't recommend storing them in the Notebook.\n",
"\n",
"To retrieve variables we need to know which project they are stored in (project_id) and your [personal access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#personal-access-tokens)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use Gitlab python client to retrieve variables (credentials, endpoint etc.) required to operate infrastructure\n",
"\n",
"import gitlab, os\n",
"\n",
"PRIVATE_TOKEN = '<YOUR_PRIVATE_TOKEN>' # see: https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html\n",
"PROJECT_ID = '<YOUR_PROJECT_ID>' # Available on your GitLab project's home page\n",
"\n",
"gl = gitlab.Gitlab('https://gitlab.com', private_token=PRIVATE_TOKEN)\n",
"project = gl.projects.get(PROJECT_ID)\n",
"\n",
"VARIABLE_VALUE = project.variables.get('VARIABLE_NAME').value"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run SQL queries in Notebook\n",
"\n",
"Documentation: http://docs.nurtch.com/en/latest/nurtch-platform/index.html#run-sql-queries-in-notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# hit play button above to execute the query\n",
"\n",
"%env DB_USER={project.variables.get('DB_USER').value}\n",
"%env DB_PASSWORD={project.variables.get('DB_PASSWORD').value}\n",
"%env DB_ENDPOINT={project.variables.get('DB_ENDPOINT').value}\n",
"%env DB_NAME={project.variables.get('DB_NAME').value}\n",
"\n",
"%load_ext sql\n",
"%sql postgres://$DB_USER:$DB_PASSWORD@$DB_ENDPOINT:5432/$DB_NAME\n",
"\n",
"%sql SELECT pid, query_start, query FROM pg_stat_activity ORDER BY query_start; "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot Cloudwatch Metrics\n",
"\n",
"Documentation: http://docs.nurtch.com/en/latest/rubix-library/aws/cloudwatch.html#plot_metric"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from rubix.aws.cloudwatch import plot_metric\n",
"\n",
"%env AWS_ACCESS_KEY_ID={project.variables.get('AWS_ACCESS_KEY_ID').value}\n",
"%env AWS_SECRET_ACCESS_KEY={project.variables.get('AWS_SECRET_ACCESS_KEY').value}\n",
"%env AWS_REGION=us-east-1\n",
"\n",
"# Maximum CPU Utilization for a given EC2 instance\n",
"# Hit play button above to see the graph\n",
"plot_metric(namespace='AWS/EC2',\n",
" metric_name='CPUUtilization',\n",
" dimensions=[{'Name':'InstanceId', 'Value': 'i-09381ae2a6228a1c7'}],\n",
" statistics='Maximum')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## AWS ECS - Get Latest Deployment Time\n",
"\n",
"Documentation: http://docs.nurtch.com/en/latest/rubix-library/aws/ecs.html#get_latest_deployment_status"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from rubix.aws.ecs import get_latest_deployment_status\n",
"\n",
"%env AWS_ACCESS_KEY_ID={project.variables.get('AWS_ACCESS_KEY_ID').value}\n",
"%env AWS_SECRET_ACCESS_KEY={project.variables.get('AWS_SECRET_ACCESS_KEY').value}\n",
"%env AWS_REGION=us-east-1\n",
"\n",
"# hit play button above to see the output\n",
"get_latest_deployment_status(service='hello-world-service', cluster='prod-cluster')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## AWS ECS - Rollback deployment\n",
"\n",
"Documentation: http://docs.nurtch.com/en/latest/rubix-library/aws/ecs.html#rollback_deployment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from rubix.aws.ecs import rollback_deployment\n",
"\n",
"%env AWS_ACCESS_KEY_ID={project.variables.get('AWS_ACCESS_KEY_ID').value}\n",
"%env AWS_SECRET_ACCESS_KEY={project.variables.get('AWS_SECRET_ACCESS_KEY').value}\n",
"%env AWS_REGION=us-east-1\n",
"\n",
"# currently there is no machine in the cluster so rollback will not succeed \n",
"# but you can hit play to see the interaction flow\n",
"rollback_deployment(service='hello-world-service', cluster='prod-cluster')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}