How to setup SSL for flask web server


In this blog post, I'll walk you through the process of adding an SSL certificate to your Flask web server using a certificate created with Let's Encrypt and Nginx.

Step 1: Install Certbot

First, install Certbot using your package manager. For example, if you're using yum, you can run:

sudo yum install certbot

Make sure to adapt the installation command to your specific OS.

Step 2: Create a Standalone Certificate

To create a standalone certificate, use the following command:

sudo certbot certonly --agree-tos --email emaiid@gmail.com -d example.com -n --standalone

This command generates your SSL certificate. You will find two important files in the /etc/letsencrypt/live/example.com directory: fullchain.pem and privkey.pem.

Let's Encrypt provides certificates that are valid for 90 days. To renew the certificate when it expires, simply run:

sudo certbot renew

Step 3: Implement SSL

Now that we have our SSL certificate, there are two ways to implement it. You can either add it to your Flask configuration or use Nginx. In this example, we'll use Nginx.

Edit your Nginx server configuration and add the following lines:


server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/fullchain.pem;  
    ssl_certificate_key /etc/letsencrypt/privkey.pem;  

    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
    

Replace example.com with your domain name, and make sure the paths to your SSL certificate and private key match the locations where Certbot stored them.

And that's it! You've successfully set up SSL for your Flask web server using Let's Encrypt and Nginx.

Happy Learning!

Adding Images Using the Resource Option in PyQt6: A Step-by-Step Guide


Have you ever faced difficulties adding images to your PyQt6 application using the resource option? I certainly have, but after some tinkering, I've figured it out! In this guide, I'll walk you through the steps to seamlessly incorporate images into your PyQt6 project.

Step 1: Update the resource.qrc File

Start by adding your images to the resource.qrc file. This file serves as a resource container for your application. Simply include the image files you want to use here.

Step 2: Adding the Icon Image

Now, let's add an icon image to your application. Go ahead and select your desired icon from the resource file and save it. This icon will represent your application visually.

Step 3: Convert the UI File

After you've added your images to resource.qrc, you may notice that the images are not immediately visible when converting your .ui file to a Python file. To convert the .ui file to Python, use the following command:

pyuic6 -x form.ui -o test.py

Step 4: Generate the Resource Python File

Here's where the magic happens. To ensure that your images become accessible in your Python code, you need to create a resource Python file using the pyside6-rcc command. Execute the following commands:

pyside6-rcc resource.qrc -o rc.py

This command generates a Python file (rc.py) that contains references to your resources.

Step 5: Import the Resource Python File

Finally, import the resource Python file (rc.py) into your main test.py file. This step allows your application to access the resources you added earlier in the resource.qrc file.

That's it! You've successfully added images to your PyQt6 application using the resource option. Now, you can use these images to enhance the visual appeal of your application. Happy coding!

How to run the vs code server

What is Code-Server? Code-Server Logo

What is Code-Server?

Code-server, also known as Visual Studio Code Server, is an open-source project that brings the power of Microsoft's Visual Studio Code (VS Code) editor to your web browser. It allows you to code, develop, and edit software remotely through a user-friendly web-based interface. This innovative tool is a game-changer for remote development, collaborative coding, and ensuring a consistent coding environment across various devices and platforms.

Setting up Code-Server on a Cloud Linux Machine

If you're using a cloud-based Linux machine, setting up Code-Server is a breeze. Here's a step-by-step guide to get you started:

  1. Download the Package:
  2. Begin by downloading the Code-Server package from the official GitHub release page. Ensure you choose the appropriate package for your system.

  3. Installation on RHEL-based Systems:
  4. If you're working with a Red Hat Enterprise Linux (RHEL) or a related distribution, you can install Code-Server using the following command:

    
          sudo yum install code-server-xx.rpm
        

    Replace xx with the specific version you've downloaded.

  5. Start the Code-Server Service:
  6. Launch the Code-Server service and set it to start automatically with your user:

    
          sudo systemctl enable --now code-server@$USER
        
  7. Access Code-Server:
  8. You can now access Code-Server by opening a web browser and navigating to http://127.0.0.1:8080. Your password for authentication is stored in ~/.config/code-server/config.yaml.

  9. Expose the Port:
  10. To make Code-Server accessible beyond your local machine, you can expose port 8080 in your cloud console. This step allows you to access your development environment from anywhere with an internet connection.

Conclusion

Code-Server is a versatile and powerful tool that brings the flexibility of VS Code to your web browser. Whether you're working remotely, collaborating with team members, or seeking a consistent development environment, Code-Server has you covered. Try it out on your cloud Linux machine and streamline your coding experience today!

Github Actions - How to use?

 What is GitHub Actions? 

                GitHub Actions is an automated workflow and continuous integration/continuous deployment (CI/CD) platform provided by GitHub. It allows developers to define and automate various tasks such as building, testing, and deploying code directly within their GitHub repositories. Users can create custom workflows using YAML configuration files to streamline their software development processes and improve collaboration among development teams.

How this help me ?

            My scalper app, written in Python and PyQt6, requires a Windows virtual machine to build a Windows executable. However, I encountered an issue yesterday when my virtual machine ran out of space and became unresponsive. Unable to create another virtual machine, I began exploring alternatives and discovered that GitHub Actions could provide a solution.


            With GitHub Actions, I set up a workflow that automates the build process. When I push changes to the main branch, the workflow initiates, runs the necessary job, and then uploads the artifact seamlessly. It's a straightforward and efficient solution to my build needs.

Steps & Example: 

  • Github repo create a workflow build file 
         .github/workflows/build.yml    

        











Name: Build Windows Executable - This is the name of your GitHub Actions workflow.

on: This section specifies when the workflow should be triggered.

push: The workflow will be triggered when there is a push event to the repository.
branches: It specifies the branches on which the workflow should run. In this case, it's set to main.
jobs: The workflow consists of a single job named build.

runs-on: ${{ matrix.os }}: This sets the operating system for the job based on the matrix defined later. In this case, it's set to run on the latest Windows.

strategy: This section defines the build matrix for the job. It allows you to run the job on different configurations, but in this case, it's just targeting Windows.

steps: These are the individual tasks that the workflow will perform:

actions/checkout@v2: This step checks out the code from your repository so that subsequent steps can access it.

actions/setup-python@v2: This step sets up a Python environment. It specifies that Python version 3.11 should be used.

run: pip install -r requirement.txt pyinstaller: This step installs the Python dependencies listed in requirement.txt and PyInstaller.

run: pyinstaller --noconsole RichDotinScalperApp.py: This step uses PyInstaller to create a Windows executable for your Python application. The --noconsole flag indicates that it should be a GUI application without a console window.

run: ./dist/RichDotinScalperApp/RichDotinScalperApp: This step optionally attempts to run the newly created executable to verify that it works, assuming it doesn't require user interaction.

run: |: This is a multi-line script that copies some files into the directory where the executable was created. It copies sn_expiry_v23.db and config.ini into the dist/RichDotinScalperApp directory.

actions/upload-artifact@v2: Finally, this step uploads the contents of the dist/RichDotinScalperApp directory as an artifact with the name RichDotinScalperApp-win. This makes the executable and associated files available for later stages or for download as part of the workflow run.


  • I just made the small changes and pushed it, you can see the workflow job created and in queue. 

  • We can also see the job logs and details where it failed like that details.



  • Once the job is complete you can see the executable uploaded as artefact we can download the same and distribute or release. 





Thanks for reading, 

SN







    




Docker Container Learning

What is Docker ?

Docker is basically a container engine which uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system and automates application deployment on the container. Docker uses Copy-on-write union file system for its backend storage.

What is container ?

A Docker container is an open source software development platform. Its main benefit is to package applications in containers, allowing them to be portable to any system running a Linux or Windows operating system (OS). A Windows machine can run Linux containers by using a virtual machine (VM).

How to Docker?

Here are some basic things like how to install docker and how to we can pull images,build custom image, run containers,remove containers and see the logs, attach volume,inspect the config, etc…

Install Docker on fedora 31

Add Docker repository:

sudo dnf config-manager --add-repo=https://download.docker.com/linux/fedora/docker-ce.repo

Install docker

sudo dnf install docker-ce

Enable and Start Docker deamon.

sudo systemctl enable docker
sudo systemctl start docker

Search for Docker images

Search images from registry

[snandago@sureshn ~]$ docker search http
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                The Apache HTTP Server Project                  2913                [OK]                
haproxy                              HAProxy - The Reliable, High Performance TCP…   1383                [OK]                
steveltn/https-portal                A fully automated HTTPS server powered by Ng…   88                                      [OK]
kennethreitz/httpbin                 A simple HTTP service.                          50                                      [OK]
centos/httpd-24-centos7              Platform for running Apache httpd 2.4 or bui…   31                                      
hashicorp/http-echo                  http-echo is an in-memory web server that ec…   26                                      
geldim/https-redirect                Very small http to https redirector based on…   17                                      [OK]
citizenstig/httpbin                  Docker container for httpbin: HTTP Request &…   14                                      [OK]
clue/httpie                          HTTPie is a cURL-like tool for humans. Usefu…   13                                      [OK]
alpine/httpie                        Auto-trigger docker build for `httpie` when …   10                                      [OK]
mendhak/http-https-echo              Listens on http/https, echoes details about …   8                                       [OK]
mainflux/http                        HTTP adapter service for Mainflux IoT platfo…   3                                       

Pull docker image from registry

using docker pull command we can pull the images locally

[snandago@sureshn ~]$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
68ced04f60ab: Pull complete 
35d35f1e0dc9: Pull complete 
8a918bf0ae55: Pull complete 
d7b9f2dbc195: Pull complete 
d56c468bde81: Pull complete 
Digest: sha256:946c54069130dbf136903fe658fe7d113bd8db8004de31282e20b262a3e106fb
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
[snandago@sureshn ~]$

List locally Downloaded Docker images

docker images command shows the locally builded/downloaed images.

[snandago@sureshn ~]$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myutil/httpd        latest              c8811902f987        57 minutes ago      381MB
httpd               latest              c5a012f9cf45        3 weeks ago         165MB
centos              latest              470671670cac        2 months ago        237MB
centos              latest              49f7960eb7e4        21 months ago       200MB
fedora/firefox      latest              7fa9c2fade45        3 years ago         880MB
[snandago@sureshn ~]$ 

Remove the Docker images

Remove docker images from local cab be done by docker rmi imagename

[snandago@sureshn ~]$ docker rmi httpd
Untagged: httpd:latest
Untagged: httpd@sha256:946c54069130dbf136903fe658fe7d113bd8db8004de31282e20b262a3e106fb
Deleted: sha256:c5a012f9cf45ce0634f5686cfb91009113199589bd39b683242952f82cf1cec1
Deleted: sha256:0f29a08770415263e178a4fd0114fe05e6dcc7d0c7922d5ee5430ad29dde9aef
Deleted: sha256:7e07c23416eb19df1444ae11062dc553d9e8eb8fd91f866b2ad2aa22954597b9
Deleted: sha256:997f97a68088ee2a31925e6deefcc690d8b45f2d795a5ce540e4d540d838fca7
Deleted: sha256:c61f156b49aa9f766f67b79ee6d7df6e83a4a2a0bda8da0c5ff19b3ea480cbd3
Deleted: sha256:f2cb0ecef392f2a630fa1205b874ab2e2aedf96de04d0b8838e4e728e28142da
[snandago@sureshn ~]$ 

Run container

Run container by docker run commands with Arguments required.

[snandago@sureshn ~]$ docker run --name test_http -d -p 9090:80 httpd
cc80d4595d4c566af398b0ca145f7e9f82661e1751e397b59bdca3fd4a2b499e
[snandago@sureshn ~]$ 

Above docker command ran the test_http apcahe container with argument -d -p where -d make sure its run on backgroud and -p to expose port 80,we can verify it using curl localhost:9090 and Good news is it works! 

[snandago@sureshn ~]$ curl 127.0.0.1:9090
<html><body><h1>It works!</h1></body></html>
[snandago@sureshn ~]$ 

Copy the file to running container

docker cp command can be used to copy the file to running container.

this below example show command to copy the index.html to running container path /var/www/html

[snandago@sureshn my_utils]$ docker cp index.html test-myutil:/var/www/html

List Running & Stopped Containers

Running containers can be listed using docker ps
docker ps only so the running container we can use -a argument to see all the containers [running|stopped]

[snandago@sureshn ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
cc80d4595d4c        httpd               "httpd-foreground"       6 minutes ago       Up 6 minutes        0.0.0.0:9090->80/tcp   test_http
2e8264b4050c        myutil/httpd        "/usr/sbin/httpd -D …"   27 minutes ago      Up 27 minutes       0.0.0.0:8081->80/tcp   test-myutil
[snandago@sureshn ~]$ 

[snandago@sureshn ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS                  NAMES
1da62939c4a4        httpd               "httpd-foreground"       15 seconds ago       Exited (0) 8 seconds ago                          stopped
6664c9d1bcf7        httpd               "httpd-foreground"       About a minute ago   Created                                           test_http2
cc80d4595d4c        httpd               "httpd-foreground"       9 minutes ago        Up 9 minutes               0.0.0.0:9090->80/tcp   test_http
2e8264b4050c        myutil/httpd        "/usr/sbin/httpd -D …"   30 minutes ago       Up 30 minutes              0.0.0.0:8081->80/tcp   test-myutil
[snandago@sureshn ~]$ 

Kill the Running Containers

Kill the running container using docker kill command

[snandago@sureshn ~]$ docker kill test_http
test_http
[snandago@sureshn ~]$ 

Remove the Containers

docker rm command can be used to remove container

If you would like to remove multiple containers at on shot just run docker rm $(docker ps -a -q)

[snandago@sureshn ~]$ docker rm test_http
test_http
[snandago@sureshn ~]$ 

Run commands on Running Container

Using docker exec command we can Run a command in a running container

[snandago@sureshn ~]$ docker exec httpd_test ls /var/log
alternatives.log
apt
btmp
dpkg.log
faillog
lastlog
wtmp
[snandago@sureshn ~]$ docker exec httpd_test date
Fri Mar 20 12:42:06 UTC 2020
[snandago@sureshn ~]$
[snandago@sureshn ~]$ docker exec httpd_test env
PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=622b2f999cd2
HTTPD_PREFIX=/usr/local/apache2
HTTPD_VERSION=2.4.41
HTTPD_SHA256=133d48298fe5315ae9366a0ec66282fa4040efa5d566174481077ade7d18ea40
HTTPD_PATCHES=
HOME=/root
[snandago@sureshn ~]$

ssh container

Using docker exec -it and /bin/bash conainer name we can ssh into container.


[snandago@sureshn search_index]$ docker exec -it grafana /bin/bash 
bash-5.0$ grafana-cli -v
Grafana CLI version 6.7.1
bash-5.0$ 

Add storage to the Container

by adding VOLUME in docker file we can attach the storage

docker -v path:containerpath will attach the persistance storage to the container.

[snandago@sureshn docker]$ docker run --name con_volume_test -v /home/snandago/learning/docker:/var/www/html -d -p 9010:80 centos/httpd
dd126bb53c2f5f427b92997a6a51181f638d0aa05bf0d76c6fd9a3d1a47911ba
[snandago@sureshn docker]$ docker exec con_volume_test df
Filesystem                                            1K-blocks      Used Available Use% Mounted on
overlay                                               103077688  52877616  44940812  55% /
tmpfs                                                     65536         0     65536   0% /dev
shm                                                       65536         0     65536   0% /dev/shm
/dev/mapper/luks-66ddcfec-d738-46c9-ab1d-3954bf254999 103077688  52877616  44940812  55% /etc/hosts
/dev/mapper/luks-841e521e-de10-4ae5-9e7d-d76dac326fa7 367219400 204250500 144292064  59% /var/www/html
tmpfs                                                   9943936         0   9943936   0% /proc/asound
tmpfs                                                   9943936         0   9943936   0% /proc/acpi
tmpfs                                                   9943936         0   9943936   0% /proc/scsi
tmpfs                                                   9943936         0   9943936   0% /sys/firmware
[snandago@sureshn docker]$ docker exec con_volume_test ls -l /var/www/html
total 4
-rw-rw-r--. 1 1000 1000 84 Mar 20 12:57 index.html
[snandago@sureshn docker]$ curl 127.0.0.1:9010
<html> 

<body>

<b> Test file volume attached to container. </b>

</body>

</html>
[snandago@sureshn docker]$ 

Inspect the Container configs

docker inspect command show provides detailed information on constructs controlled by Docker.

[snandago@sureshn ~]$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' test-myutil
172.17.0.2
[snandago@sureshn ~]$ 

[snandago@sureshn ~]$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}'  test-myutil
02:42:ac:11:00:02
[snandago@sureshn ~]$ 


Above docker inspect filter show container ip address and next filter show mac address. We can can filter multiple informations.

Build Custom image

by writing the Dockerfile we can build the custome image by our wish.

below is the sample Dockerfile

[snandago@sureshn docker]$ cat Dockerfile 
#sample Dockerfile
FROM centos
 
MAINTAINER sureshcbe5@gmail.com

RUN yum install -y httpd; yum clean all

COPY index.html /var/www/html

EXPOSE 80

CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
[snandago@sureshn docker]$ 


Small explaination on Dockerfile writing. How this file wrting is done.

Dockerfile file must start with FROM instruction we can add the base image name there.

The MAINTAINER instruction allows you to set the Author field of the generated images.

RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)

COPY Copies new files or directories from <src> and adds them to the filesystem of the image at the path <dest>.

EXPOSE Informs Docker that the container listens on the specified network port(s) at runtime.

CMD The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

create a Dockerfile and run docker build command to build the custom image.

[snandago@sureshn docker]$ docker build -t custombuild_image .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM centos
 ---> 470671670cac
Step 2/6 : MAINTAINER sureshcbe5@gmail.com
 ---> Running in dfb3416849c6
Removing intermediate container dfb3416849c6
 ---> 03af3fb70aad
Step 3/6 : RUN yum install -y httpd; yum clean all
 ---> Running in ae9595d0dede
CentOS-8 - AppStream                            1.9 MB/s | 6.5 MB     00:03    
CentOS-8 - Base                                 2.6 MB/s | 5.0 MB     00:01    
CentOS-8 - Extras                               5.7 kB/s | 4.2 kB     00:00    
Dependencies resolved.
================================================================================
 Package           Arch   Version                               Repo       Size
================================================================================
Installing:
 httpd             x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 M
Installing dependencies:
 apr               x86_64 1.6.3-9.el8                           AppStream 125 k
 apr-util          x86_64 1.6.1-6.el8                           AppStream 105 k
 centos-logos-httpd
                   noarch 80.5-2.el8                            AppStream  24 k
 httpd-filesystem  noarch 2.4.37-16.module_el8.1.0+256+ae790463 AppStream  35 k
 httpd-tools       x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 103 k
 mod_http2         x86_64 1.11.3-3.module_el8.1.0+213+acce2796  AppStream 158 k
 brotli            x86_64 1.0.6-1.el8                           BaseOS    323 k
 mailcap           noarch 2.1.48-3.el8                          BaseOS     39 k
Installing weak dependencies:
 apr-util-bdb      x86_64 1.6.1-6.el8                           AppStream  25 k
 apr-util-openssl  x86_64 1.6.1-6.el8                           AppStream  27 k
Enabling module streams:
 httpd                    2.4                                                  

Transaction Summary
================================================================================
Install  11 Packages

________trimmed some content to make it smaller-----------------------------
Complete!
20 files removed
Removing intermediate container ae9595d0dede
 ---> 1e797d9d4344
Step 4/6 : COPY index.html /var/www/html
 ---> f6b8f7331d0e
Step 5/6 : EXPOSE 80
 ---> Running in 1c0f40e48936
Removing intermediate container 1c0f40e48936
 ---> 878e8361d5ce
Step 6/6 : CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
 ---> Running in 5d8785e85068
Removing intermediate container 5d8785e85068
 ---> d0ee1706467b
Successfully built d0ee1706467b
Successfully tagged custombuild_image:latest
[snandago@sureshn docker]$ 

docker images show up the custom buid image. Now we can try running this container.

[snandago@sureshn docker]$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
custombuild_image   latest              d0ee1706467b        5 minutes ago       259MB
myutil/httpd        latest              c8811902f987        2 hours ago         381MB
centos              latest              470671670cac        2 months ago        237MB
centos/httpd        latest              2cc07fbb5000        14 months ago       258MB
centos              latest              49f7960eb7e4        21 months ago       200MB
fedora/firefox      latest              7fa9c2fade45        3 years ago         880MB
[snandago@sureshn docker]$ docker run --name custom_build -d -p 9098:80 custombuild_image
39c1ac7a24067e5030a214c96c0cc58cca0ca11dfd22646b78d29e2daabb1dce
[snandago@sureshn docker]$ curl 127.0.0.1:9098
Cutom build tested!
[snandago@sureshn docker]$ 

yes! we can see the container running and the index.html file we copied while build shows up. :+1:

Running mysql container

Run the mysql container mysql-57-centos7 docker registry image.

docker run -d --name mysql \
-v /mydata/data:/var/lib/mysql/init \
-e MYSQL_DATABASE=lab \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=password \
-e MYSQL_ROOT_PASSWORD=password! \
centos/mysql-57-centos7

find the network details of the container and connect the db and verify.

docker inspect -f '{{ .NetworkSettings.IPAddress }}' mysql

[snandago@sureshn data]$ mysql -u root -p -h 172.17.0.2
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lab                |
| mysql              |
| new_schema         |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.002 sec)

MySQL [(none)]> use lab;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [lab]> show tables;
+---------------+
| Tables_in_lab |
+---------------+
| data          |
| lab_inventory |
+---------------+
2 rows in set (0.001 sec)

MySQL [lab]> 


tags: container docker Learning