├── LICENSE ├── README.md ├── main.py └── s2d3.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Yash Indane 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://img.shields.io/badge/python-3-lightgrey) 2 | 3 | # scan2deploy 4 | 5 | Just write down your docker file , take a snap and deploy the app in Kubernetes! 6 | 7 | ![s2d3](https://user-images.githubusercontent.com/53041219/207027869-bf690f8b-ba7a-4d4f-ab9c-8d4a6468d1bd.png) 8 | 9 | [Demo Link](https://www.linkedin.com/posts/yash-indane-aa6534179_docker-kubernetes-python-activity-6790679076320382976-OrMT) 10 | 11 | ## Requirements 12 | 13 | Install OpenCV by 14 | 15 | ``` 16 | $ pip install opencv-python 17 | ``` 18 | 19 | Do this if your Kubernetes cluster is running in aws -> 20 | 21 | Install boto3 (calls APIs of AWS services) by 22 | 23 | ``` 24 | $ pip install boto3 25 | ``` 26 | 27 | Install aws command line tool by 28 | 29 | ``` 30 | $ pip install awscli 31 | ``` 32 | 33 | Since I am running the code on Windows machine , Docker Desktop is required to get the docker enviornment for building and pushing images 34 | Download Docker Desktop from -> [link](https://docs.docker.com/docker-for-windows/install/) 35 | 36 | 37 | ## Enviornment settings 38 | 39 | Docker Desktop- 40 | 41 | After opening Docker Desktop, provide your Docker Hub credentials so that pushing images happens smoothly. 42 | 43 | AWS- 44 | 45 | configure AWS-CLI by - 46 | ``` 47 | $ aws configure 48 | ``` 49 | 50 | Create a bucket in S3 (here the snap will be uploaded) 51 | 52 | ``` 53 | $ aws s3api create-bucket --bucket --region --create-bucket-configuration LocationConstraint= 54 | ``` 55 | 56 | Have the Kubernetes master key (.pem file) in the working directory, so that deployment can be made using ssh 57 | 58 | ## Working and Usage 59 | 60 | The image of your page is uploaded in the S3 bucket. This image is then processed by ```AWS Textract``` service, which extracts the text from it line by line. 61 | More on AWS Textract -> [link](https://aws.amazon.com/textract/) 62 | 63 | ```py 64 | textract = boto3.client('textract') 65 | 66 | response = textract.detect_document_text( 67 | Document={ 68 | 69 | 'S3Object': { 70 | 'Bucket': bucket_name, 71 | 'Name': file_name, 72 | 73 | } 74 | } 75 | ) 76 | ``` 77 | 78 | the lines are written in the Dockerfile, which is then used to build the ```container image``` and push it to Docker Hub. 79 | 80 | After pushing the image to Docker Hub, the code runs the command to create a deployment using the image pushed (using ssh) and expose it to create a service. 81 | 82 | To get the port number where the service is launched, go to the master node and run -> 83 | 84 | ``` 85 | $ kubectl get svc 86 | ``` 87 | 88 | To get the IP of node on which the pod is launched run this -> 89 | 90 | ``` 91 | $ kubectl describe pod 92 | ``` 93 | 94 | The app can be accessed by ```http://:``` 95 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import boto3 3 | import subprocess 4 | 5 | #click photo 6 | cap = cv2.VideoCapture(0) 7 | 8 | cap.set(3 , 1920) 9 | cap.set(4 , 1080) 10 | 11 | ret , photo = cap.read() 12 | 13 | #name of captured image file 14 | file_name = "" 15 | 16 | cv2.imwrite(file_name,photo) 17 | 18 | print(ret) 19 | cap.release() 20 | 21 | #upload photo in s3 22 | 23 | #enter s3 bucket name 24 | bucket_name = "" 25 | 26 | s3 = boto3.resource("s3") 27 | s3.Bucket(bucket_name).upload_file(file_name , file_name) 28 | 29 | #Call Amazon Textract 30 | textract = boto3.client('textract') 31 | 32 | response = textract.detect_document_text( 33 | Document={ 34 | 35 | 'S3Object': { 36 | 'Bucket': bucket_name, 37 | 'Name': file_name, 38 | 39 | } 40 | } 41 | ) 42 | 43 | #Writing commands to the docker file from Textract response 44 | with open('Dockerfile' , 'a' ) as dockerfile: 45 | 46 | for blocks in response['Blocks']: 47 | if blocks['BlockType'] == 'LINE': 48 | 49 | dockerfile.write(blocks['Text'] + ' \n') 50 | 51 | elif blocks['BlockType'] == 'WORD': 52 | break 53 | 54 | dockerfile.close() 55 | 56 | #build image 57 | 58 | #container image name (username/repo:version) 59 | image_name = '' 60 | 61 | build_image_status = subprocess.getstatusoutput(f'docker build -t {image_name} .') 62 | print('image build!' if build_image_status[0] == 0 else 'error building image') 63 | 64 | #push image 65 | push_image = subprocess.getstatusoutput(f'docker push {image_name}') 66 | print('image pushed to DockerHub' if push_image[0] == 0 else 'error pushing image') 67 | 68 | #creating deployment 69 | 70 | #Kubernetes master DNS 71 | master_DNS = '' 72 | 73 | #Kubernetes master key path (.pem file) 74 | key = '' 75 | 76 | #Kubernetes deployment name 77 | deployment_name = '' 78 | 79 | deployment_status = subprocess.getstatusoutput( 80 | f'ssh -i "{key}" {master_DNS} sudo kubectl create deployment {deployment_name} --image {image_name}' 81 | ) 82 | 83 | #exposing the deployment 84 | 85 | #port at which the service inside container is running 86 | port_no= 87 | 88 | #type of service 89 | TYPE='' 90 | 91 | service_status = subprocess.getstatusoutput( 92 | f'ssh -i "{key}" {master_DNS} sudo kubectl expose deployment {deployment_name} --port={port_no} --type={TYPE}' 93 | ) 94 | 95 | print( 96 | 'Deployment successful!' 97 | if deployment_status[0] == 0 else 98 | 'error' 99 | ) 100 | -------------------------------------------------------------------------------- /s2d3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YashIndane/scan2deploy/f27685baeb4bc46ad0e6276696d81d778feb12fd/s2d3.png --------------------------------------------------------------------------------