diff --git a/.github/workflows/deploy-app.yaml b/.github/workflows/deploy-app.yaml new file mode 100644 index 0000000..aa0f68b --- /dev/null +++ b/.github/workflows/deploy-app.yaml @@ -0,0 +1,69 @@ +name: App Image Build And Deploy +on: + workflow_dispatch: + inputs: + environment: + type: choice + description: environment to choice + options: + - testnet + - mainnet +env: + AWS_REGION : "ap-southeast-1" + app: hailstone +permissions: + id-token: write + contents: read +run-name: Build Image For Repo +jobs: + build: + runs-on: ubuntu-latest + outputs: + commit_sha: ${{ steps.get-commit-sha.outputs.commit_sha }} + ecr_registry: ${{ steps.login-ecr.outputs.registry }} + steps: + - name: Checkout repo + uses: actions/checkout@v3 + - name: Get Commit Sha + id: get-commit-sha + run: | + echo "::set-output name=commit_sha::$(git rev-parse --short=7 HEAD)" + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v3 + with: + role-to-assume: arn:aws:iam::942583694543:role/github_assume_role + aws-region: ${{ env.AWS_REGION }} + role-session-name: samplerolesession + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + - name: Build Docker Image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + REPO: ${{ env.app }} + #REF: ${{ github.event.inputs.ref }} + COMMIT_SHA: ${{ steps.get-commit-sha.outputs.commit_sha }} + run: | + # aws s3 cp s3://eniac-x-labs-config-center/${{ env.app }}/${{ github.event.inputs.environment }}/acorus.yaml acorus.yaml + IMAGE_URL1=${ECR_REGISTRY}/${REPO}:${COMMIT_SHA} + docker build --pull -t ${IMAGE_URL1} . + docker push ${IMAGE_URL1} + - name: deploy app + run: | + aws eks update-kubeconfig --name savour-layer3-eks + # echo 'KUBE_CONFIG_DATA<> $GITHUB_ENV + # echo $(cat /home/runner/.kube/config | base64) >> $GITHUB_ENV + # echo 'EOF' >> $GITHUB_ENV + HELM_OPTS="${HELM_OPTS} --set image.repository=${{ steps.login-ecr.outputs.registry }}/${{ env.app }}" + HELM_OPTS="${HELM_OPTS} --set image.tag=${{ steps.get-commit-sha.outputs.commit_sha }}" + if [ ${{github.event.inputs.environment }} == "testnet" ]; then + helm template ${{ env.app }} -f values.yaml charts -n testnet-layer3 ${HELM_OPTS} + helm upgrade --install ${{ env.app }} -f values.yaml charts -n testnet-layer3 ${HELM_OPTS} + else + helm template ${{ env.app }} -f values.yaml charts -n mainnet ${HELM_OPTS} + helm upgrade --install ${{ env.app }} -f values.yaml charts -n mainnet ${HELM_OPTS} + fi + + + + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..742ddd7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.8 +WORKDIR /app +ADD . /app +RUN pip install -r requirements.txt +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] diff --git a/airdrop/migrations/0001_initial.py b/airdrop/migrations/0001_initial.py index 4217d5f..688c37a 100644 --- a/airdrop/migrations/0001_initial.py +++ b/airdrop/migrations/0001_initial.py @@ -1,4 +1,5 @@ -# Generated by Django 4.1.1 on 2024-03-13 05:29 +# Generated by Django 4.1.1 on 2024-03-03 03:11 + from django.db import migrations, models import django.db.models.deletion @@ -45,6 +46,7 @@ class Migration(migrations.Migration): ('updated_at', models.DateTimeField(auto_now=True, db_index=True)), ('address', models.CharField(max_length=100, verbose_name='用户地址')), ('type', models.CharField(choices=[('Invite', 'Invite'), ('BridgeTransfer', 'BridgeTransfer'), ('BridgeStaking', 'BridgeStaking')], default='BridgeTransfer', max_length=100, verbose_name='交易类别')), + ('points', models.PositiveIntegerField(default=0, verbose_name='积分数量')), ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='airdrop_user', to='airdrop.airdropuser', verbose_name='收藏的商家')), ], diff --git a/api/airdrop/api.sh b/api/airdrop/api.sh new file mode 100644 index 0000000..72d49bc --- /dev/null +++ b/api/airdrop/api.sh @@ -0,0 +1,4 @@ +curl --location --request POST 'http://127.0.0.1:8000/api/get_points_by_address' --header 'Content-Type: application/json' --data-raw '{"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F0" }' + + +curl --location --request POST 'http://127.0.0.1:8000/api/get_points_record_by_address' --header 'Content-Type: application/json' --data-raw '{"address": "0xe3b4ECd2EC88026F84cF17fef8bABfD9184C94F0" }' \ No newline at end of file diff --git a/api/airdrop/api_v1.py b/api/airdrop/api_v1.py index 3bc77b5..95eb22f 100644 --- a/api/airdrop/api_v1.py +++ b/api/airdrop/api_v1.py @@ -3,6 +3,7 @@ import json import uuid + from common.helpers import ( ok_json, error_json @@ -56,6 +57,7 @@ def submit_invite_info(request): ) return ok_json({}) + # @check_api_token def get_points_by_address(request): params = json.loads(request.body.decode()) @@ -89,3 +91,4 @@ def get_points_record_by_address(request): "points": point_list, } return ok_json(data) + diff --git a/charts/Chart.yaml b/charts/Chart.yaml new file mode 100644 index 0000000..e8523e0 --- /dev/null +++ b/charts/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +description: A Helm chart for Kubernetes + +name: app +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/charts/templates/NOTES.txt b/charts/templates/NOTES.txt new file mode 100644 index 0000000..01a7d7b --- /dev/null +++ b/charts/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "app.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "app.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "app.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "app.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/charts/templates/_helpers.tpl b/charts/templates/_helpers.tpl new file mode 100644 index 0000000..bb8ba9d --- /dev/null +++ b/charts/templates/_helpers.tpl @@ -0,0 +1,64 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "app.name" -}} +{{- default .Release.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} + + +{{- define "app.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "app.chart" -}} +{{- printf "%s-%s" .Release.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "app.labels" -}} +helm.sh/chart: {{ include "app.chart" . }} +{{ include "app.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "app.selectorLabels" -}} +app.kubernetes.io/name: {{ include "app.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "app.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "app.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/templates/deployment.yaml b/charts/templates/deployment.yaml new file mode 100644 index 0000000..d5e3884 --- /dev/null +++ b/charts/templates/deployment.yaml @@ -0,0 +1,72 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "app.fullname" . }} + labels: + {{- include "app.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "app.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "app.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "app.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Release.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + {{- range $index, $val := .Values.ports }} + - name: {{ $val.name }} + containerPort: {{ $val.portNumber }} + protocol: TCP + {{- end }} + {{- with .Values.livenessProbe }} + livenessProbe: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.readinessProbe }} + readinessProbe: + {{- toYaml . | nindent 12 }} + {{- end }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + initContainers: + - name: {{ .Release.Name }}-migrate + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: IfNotPresent + args: + - python manage.py makemigrations; python manage.py migrate; + command: + - "/bin/sh" + - "-c" + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/charts/templates/hpa.yaml b/charts/templates/hpa.yaml new file mode 100644 index 0000000..efa6dae --- /dev/null +++ b/charts/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "app.fullname" . }} + labels: + {{- include "app.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "app.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/charts/templates/ingress.yaml b/charts/templates/ingress.yaml new file mode 100644 index 0000000..ced20f9 --- /dev/null +++ b/charts/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "app.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "app.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/charts/templates/service.yaml b/charts/templates/service.yaml new file mode 100644 index 0000000..d97e420 --- /dev/null +++ b/charts/templates/service.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "app.fullname" . }} + labels: + {{- include "app.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + {{- range $index, $val := .Values.ports }} + - port: {{ $val.portNumber }} + targetPort: {{ $val.portNumber }} + protocol: TCP + name: {{ $val.name }} + {{- end }} + selector: + {{- include "app.selectorLabels" . | nindent 4 }} diff --git a/charts/templates/serviceaccount.yaml b/charts/templates/serviceaccount.yaml new file mode 100644 index 0000000..bfeb176 --- /dev/null +++ b/charts/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "app.serviceAccountName" . }} + labels: + {{- include "app.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/charts/templates/tests/test-connection.yaml b/charts/templates/tests/test-connection.yaml new file mode 100644 index 0000000..61b5ae9 --- /dev/null +++ b/charts/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "app.fullname" . }}-test-connection" + labels: + {{- include "app.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "app.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/hailstone/settings.py b/hailstone/settings.py index 9f47c6c..a17e386 100644 --- a/hailstone/settings.py +++ b/hailstone/settings.py @@ -59,9 +59,9 @@ "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "hailstone", - "USER": "guoshijiang", - "PASSWORD": "", - "HOST": "127.0.0.1", + "USER": "postgres", + "PASSWORD": "vVb9NhLYui", + "HOST": "postgresql", }, } @@ -112,4 +112,4 @@ except ImportError: pass -# usSszPkH \ No newline at end of file +# usSszPkH diff --git a/requirements.txt b/requirements.txt index 4e98c5e..93808d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -68,6 +68,7 @@ qrcode==7.3.1 redis==3.3.10 requests==2.22.0 rsa==4.8 +psycopg2==2.9.9 schedule==1.1.0 semantic-version==2.8.5 service-identity==21.1.0 diff --git a/values.yaml b/values.yaml new file mode 100644 index 0000000..8733f70 --- /dev/null +++ b/values.yaml @@ -0,0 +1,97 @@ +# Default values for app. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + +ports: + - name: http + portNumber: 8000 + # - name: metrics + # portNumber: 8081 +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + + +resources: + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + limits: + cpu: 300m + memory: 450Mi + requests: + cpu: 100m + memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} +livenessProbe: + tcpSocket: + port: 8000 + initialDelaySeconds: 30 + periodSeconds: 10 +readinessProbe: + tcpSocket: + port: 8000 + initialDelaySeconds: 30 + periodSeconds: 10