2021/12/13

Learning Flask 1: Bootstrap with flask

Background

This is a follow up blog post of previous NETCONF blog. Below diagram recalls the application I am going to build.

In the last blog post, I have built a server for running netconfd effectively. In this blog post, I will focus on building the CoreServer (Frontend layer) with flask.
By the way, the full source code of this application is here

Why flask?

I do not have much experiences on python flaks. Actually, I am more familiar with Django instead. Django, however, is not fit in this application since it is sort of overkill in my requirement. I need something small, and easy to code at the first place.
After some research from this website, I pick flask from the list. By the way, Django is classified as full-stack framework while flask is simply a micro framework in this site.

Start the application with a boilerplate

Nowadays, as long as you are using well-known frameworks / libraries etc, usually, there are multiple open sources boilerplates for your framework / libraries. Then, you can save much efforts on building the ground.
Personally, I have picked this boilerplate for my application. This boilerplate provides the docker image, the docker-compose file, the folder structure.

The usage of docker image + docker-compose

They are essential for me to build the application in a docker manner, and they save my time on building the CI/CD pipeline easily

The folder structure

It saves my time for figuring out how to organise files in flaks manner.

Setup CI / CD pipeline

I strongly recommend readers to setup a CI / CD pipeline asap especially on a new project. After having a CI / CD pipeline, code’s quality is kind of guaranteed. For example, coding style, some basic tests etc

  • Below is the yml file for running a CI / CD pipeline in gitlab CI.
# You can override the included template(s) by including variable overrides
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
# Note that environment variables can be set in several places
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
include:
  - template: Security/SAST.gitlab-ci.yml

stages:
  - build
  - test

variables:
  PIPELINE_REGISTRY_DOCKER_IMAGE: "${CI_REGISTRY_IMAGE}:pipeline"
  PIPELINE_RUNTIME_DOCKER_IMAGE: "hands-on-core-server:latest"
  DOCKER_COMPOSE_CI_YML_FILE_NAME: "docker-compose-ci.yml"

buildDockerImage:
  image: docker
  services:
      - docker:dind
  stage: build
  variables:
    DOCKER_BUILDKIT: 0
  script:
      - apk add --no-cache docker-compose
      - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" "$CI_REGISTRY"
      - docker-compose -f "docker/${DOCKER_COMPOSE_CI_YML_FILE_NAME}" build CoreServer
      - docker tag "${PIPELINE_RUNTIME_DOCKER_IMAGE}" "${PIPELINE_REGISTRY_DOCKER_IMAGE}"
      - docker push "${PIPELINE_REGISTRY_DOCKER_IMAGE}"

sast:
  stage: test

runTests:
  image: ${PIPELINE_REGISTRY_DOCKER_IMAGE}
  stage: test
  script:
      - flake8
  • The sast task is provided by gitlab. Just ignore it
  • Pipeline’s contents are trivial. Literally,
    • I compose a docker image which holding all my boilerplates, and dependencies.
    • I setup a stage runTests which runs with the docker image above, and run lint check with flake8 only at this moment
  • Below is the Dockerfile for ci
FROM tiangolo/uwsgi-nginx-flask:python3.8

WORKDIR /workspace
COPY . /workspace

RUN \
    pip install -r requirements-test.txt && \
    pip install -r requirements.txt

Conclusions

Now, I have a good ground to build up my application in flask. There is a CI-CD pipeline to make sure my codes will be in good qualities in some sense. There is a flask-style folder structure for me to organise my codes.
The next step of the development will be writing some trivial tests, and make sure the pipeline will run them on each commit. They will be covered in the next blog

Useful links

沒有留言:

張貼留言