forked from MichaelCade/90DaysOfDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request MichaelCade#331 from rishabkumar7/main
- Loading branch information
Showing
3 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Day 46 - Web development in Python | ||
|
||
Python is quite capable when it comes to web development, and there are a variety of frameworks and modules are available for it. These can be used to create web applications. | ||
Flask, Django, and Pyramid are a few well-known frameworks. The choice of framework will rely on the project's requirements. Each of these frameworks has advantages and disadvantages of its own. | ||
|
||
## Creating a basic web app using Flask: | ||
|
||
Creating a basic web application using Flask: Flask is a micro web framework for Python that is easy to learn and use. It provides a simple way to create web applications and APIs using Python. Here are some examples of Flask code for creating a basic web application: | ||
|
||
``` python | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello, World!' | ||
``` | ||
This code creates a Flask application and defines a single route for the root URL (/). When the user visits the URL, the hello_world() function is called and returns the string 'Hello, World!'. | ||
|
||
## Working with databases: | ||
|
||
The majority of online applications need some sort of permanent storage, and Python offers a number of modules and frameworks for doing so. Popular options include Django ORM, Peewee, and SQLAlchemy. Here is an illustration of how to work with a SQLite database using SQLAlchemy: | ||
|
||
``` python | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
app = Flask(__name__) | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' | ||
db = SQLAlchemy(app) | ||
|
||
class User(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.String(50)) | ||
|
||
@app.route('/') | ||
def index(): | ||
users = User.query.all() | ||
return render_template('index.html', users=users) | ||
``` | ||
This code creates a SQLite database and a User table using SQLAlchemy. The index() function queries the database for all users and passes them to the template for rendering. | ||
|
||
Having a good understanding of how these web apps work, will help you with automation and deployment when it comes to practicing DevOps. | ||
You can dive deeper into how you can build APIs using Python and serverless technologies like AWS Lambda, Azure Functions etc. | ||
|
||
I have a demo on [how I built a serverless resume API](https://github.com/rishabkumar7/AzureResumeAPI). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Day 47 - Automation with Python | ||
|
||
Using Python for infrastructure management involves automating the management of IT infrastructure, such as servers, databases, and networking equipment. This can include tasks like provisioning, configuration, and orchestration. | ||
Python is a popular language for infrastructure management, and there are several tools and libraries available to help with this. Some popular tools for infrastructure management that use Python include: | ||
|
||
- Fabric | ||
- PyWinRM | ||
- Pulumi | ||
|
||
## Fabric | ||
|
||
Fabric is a Python library that can be used for streamlining SSH commands and remote execution of scripts, which can be used to automate server configuration and deployment. | ||
Here is an example in which we will be using the Fabric library to connect to a remote server using SSH, and then run the `ls -l` command on the remote server. The output of the command will be printed to the console. | ||
|
||
``` python | ||
from fabric import Connection | ||
|
||
# Connect to the remote server | ||
c = Connection('[email protected]') | ||
|
||
# Run a command on the remote server | ||
result = c.run('ls -l') | ||
|
||
# Print the output of the command | ||
print(result.stdout) | ||
``` | ||
|
||
|
||
## PyWinRM | ||
|
||
A Python library that can be used to automate Windows Remote Management tasks, which can be used to automate Windows server configuration and management. | ||
|
||
## Pulumi | ||
|
||
Pulumi is a cloud infrastructure as code (CIaC) tool that lets you define and manage cloud resources in a variety of programming languages, including Python. | ||
|
||
You can use Pulumi to write Python code to describe your infrastructure as code, and then use the Pulumi CLI to deploy and manage it. Here is an example: | ||
|
||
``` python | ||
import pulumi | ||
from pulumi_aws import ec2 | ||
|
||
# Define an EC2 instance | ||
server = ec2.Instance('server', | ||
instance_type='t2.micro', | ||
ami='ami-0c55b159cbfafe1', | ||
tags={ | ||
'Name': 'cloud-server', | ||
}, | ||
) | ||
|
||
# Export the server's IP address | ||
pulumi.export('ip_address', server.public_ip) | ||
``` | ||
|
||
In this example, we're using the Pulumi Python SDK to define an EC2 instance on AWS. We specify the instance type, the AMI ID, and some tags for the instance, and then export the instance's public IP address. ou can then use the Pulumi CLI to deploy this infrastructure, which will create the EC2 instance on AWS. You can also use the Pulumi CLI to manage your infrastructure over time, making changes and updates as needed. | ||
|
||
## Resources: | ||
|
||
- [Learn more about Fabric](https://docs.fabfile.org/en/stable/index.html) | ||
- [PyWinRM](https://github.com/diyan/pywinrm) | ||
- [Pulumi - IaC Tool](https://www.pulumi.com/docs/reference/pkg/python/pulumi/) |