diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 7a3156c..31cdc3d 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -21,6 +21,7 @@ jobs: python -m pip install --upgrade pip pip install setuptools wheel twine pip install requests + - name: fc-mysql-python env: publish_token: ${{ secrets.alibaba_registry_publish_token }} @@ -29,4 +30,34 @@ jobs: wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py ls python hub-publish.py + cd .. + + - name: fc-mongodb-python + env: + publish_token: ${{ secrets.alibaba_registry_publish_token }} + run: | + cd mongodb + wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py + ls + python hub-publish.py + cd .. + + - name: fc-sql-server-python + env: + publish_token: ${{ secrets.alibaba_registry_publish_token }} + run: | + cd sql_server + wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py + ls + python hub-publish.py + cd .. + + - name: fc-redis-python + env: + publish_token: ${{ secrets.alibaba_registry_publish_token }} + run: | + cd redis + wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py + ls + python hub-publish.py cd .. \ No newline at end of file diff --git a/mongodb/publish.yaml b/mongodb/publish.yaml new file mode 100644 index 0000000..e9108b7 --- /dev/null +++ b/mongodb/publish.yaml @@ -0,0 +1,20 @@ +Type: Application +Name: start-fc-mongodb-python +Version: 0.0.2 +Provider: + - 阿里云 +Description: 快速部署一个 Python 访问 mongodb 的函数到阿里云函数计算。 +HomePage: https://github.com/devsapp/start-fc-db-python +Tags: + - 函数计算 + - 你好世界 + - 新手入门 +Category: 新手入门 +Service: + 函数计算: + Authorities: + - 创建函数 + - 删除函数 + - 创建服务 + - 删除服务 + Runtime: Python \ No newline at end of file diff --git a/mongodb/readme.md b/mongodb/readme.md new file mode 100644 index 0000000..0259f05 --- /dev/null +++ b/mongodb/readme.md @@ -0,0 +1,12 @@ +# 阿里云函数计算 Python 访问 mongodb 数据库 + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mongodb 数据库 + +- 初始化项目:`s init start-fc-mongodb-python -d start-fc-mongodb-python` +- 进入项目:`cd start-fc-mongodb-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 mongodb`案例的初始化、部署整个流程。 diff --git a/mongodb/src/code/index.py b/mongodb/src/code/index.py new file mode 100644 index 0000000..a94037c --- /dev/null +++ b/mongodb/src/code/index.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +import os +from pymongo import MongoClient + + +def handler(event, context): + dbName = os.environ['MONGO_DATABASE'] + url = os.environ['MONGO_URL'] + + client = MongoClient(url) + + col = client[dbName]['fc_col'] + col.insert_one(dict(DEMO="FC", MSG="Hello FunctionCompute For MongoDB")) + doc = col.find_one(dict(DEMO="FC")) + print('find documents:' + str(doc)) + return str(doc) diff --git a/mongodb/src/code/requirements.txt b/mongodb/src/code/requirements.txt new file mode 100644 index 0000000..8c7d698 --- /dev/null +++ b/mongodb/src/code/requirements.txt @@ -0,0 +1 @@ +pymongo \ No newline at end of file diff --git a/mongodb/src/readme.md b/mongodb/src/readme.md new file mode 100644 index 0000000..0259f05 --- /dev/null +++ b/mongodb/src/readme.md @@ -0,0 +1,12 @@ +# 阿里云函数计算 Python 访问 mongodb 数据库 + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mongodb 数据库 + +- 初始化项目:`s init start-fc-mongodb-python -d start-fc-mongodb-python` +- 进入项目:`cd start-fc-mongodb-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 mongodb`案例的初始化、部署整个流程。 diff --git a/mongodb/src/s.yaml b/mongodb/src/s.yaml new file mode 100644 index 0000000..e181201 --- /dev/null +++ b/mongodb/src/s.yaml @@ -0,0 +1,25 @@ +edition: 1.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范 +name: fcDeployApp # 项目名称 +access: "{{ access }}" # 秘钥别名 + +services: + fc-db-mongodb-python: # 服务名称 + component: devsapp/fc # 组件名称 + props: # 组件的属性值 + region: cn-hangzhou + service: + name: fc-db-demo + description: 'demo for fc visit db' + internetAccess: true + function: + name: mongodb + description: visit mongodb + runtime: python3 + codeUri: ./code + handler: index.handler + memorySize: 256 + timeout: 60 + environmentVariables: + MONGO_DATABASE: + MONGO_URL: +# 函数计算FC组件文档参考地址:https://github.com/devsapp/fc \ No newline at end of file diff --git a/mongodb/version.md b/mongodb/version.md new file mode 100644 index 0000000..b51e0aa --- /dev/null +++ b/mongodb/version.md @@ -0,0 +1 @@ +- 新版本支持 \ No newline at end of file diff --git a/mysql/publish.yaml b/mysql/publish.yaml index 0936d1f..dbe7028 100644 --- a/mysql/publish.yaml +++ b/mysql/publish.yaml @@ -1,6 +1,6 @@ Type: Application Name: start-fc-mysql-python -Version: 0.0.1 +Version: 0.0.2 Provider: - 阿里云 Description: 快速部署一个 Python 访问 mysql 的函数到阿里云函数计算。 diff --git a/mysql/src/code/index.py b/mysql/src/code/index.py index a699420..c8bf51a 100644 --- a/mysql/src/code/index.py +++ b/mysql/src/code/index.py @@ -2,7 +2,6 @@ import logging import pymysql import os -import sys logger = logging.getLogger() @@ -21,15 +20,13 @@ def getConnection(): logger.error(e) logger.error( "ERROR: Unexpected error: Could not connect to MySql instance.") - sys.exit() + raise Exception(str(e)) def handler(event, context): + conn = getConnection() try: - conn = getConnection() with conn.cursor() as cursor: - sql = "REPLACE INTO users (id, name) VALUES(%s, %s)" - cursor.execute(sql, ('2', 'wan')) cursor.execute("SELECT * FROM users") result = cursor.fetchone() logger.info(result) diff --git a/readme.md b/readme.md index 04fd1ba..f18de49 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,45 @@ 即可实现`Python 访问 mysql`案例的初始化、部署整个流程。 +### redis + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 redis 数据库 + +- 初始化项目:`s init start-fc-redis-python -d start-fc-redis-python` +- 进入项目:`cd start-fc-redis-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 redis`案例的初始化、部署整个流程。 + +### mongodb + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mongodb 数据库 + +- 初始化项目:`s init start-fc-mongodb-python -d start-fc-mongodb-python` +- 进入项目:`cd start-fc-mongodb-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 mongodb`案例的初始化、部署整个流程。 + +### sql-server + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mysql 数据库 + +- 初始化项目:`s init start-fc-sql-server-python -d start-fc-sql-server-python` +- 进入项目:`cd start-fc-sql-server-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 sql server`案例的初始化、部署整个流程。 + ## 参考 > - Serverless Devs 项目:https://www.github.com/serverless-devs/serverless-devs diff --git a/redis/publish.yaml b/redis/publish.yaml new file mode 100644 index 0000000..421b986 --- /dev/null +++ b/redis/publish.yaml @@ -0,0 +1,20 @@ +Type: Application +Name: start-fc-redis-python +Version: 0.0.2 +Provider: + - 阿里云 +Description: 快速部署一个 Python 访问 redis 的函数到阿里云函数计算。 +HomePage: https://github.com/devsapp/start-fc-db-python +Tags: + - 函数计算 + - 你好世界 + - 新手入门 +Category: 新手入门 +Service: + 函数计算: + Authorities: + - 创建函数 + - 删除函数 + - 创建服务 + - 删除服务 + Runtime: Python \ No newline at end of file diff --git a/redis/readme.md b/redis/readme.md new file mode 100644 index 0000000..f4a4bde --- /dev/null +++ b/redis/readme.md @@ -0,0 +1,12 @@ +# 阿里云函数计算 Python 访问 redis 数据库 + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 redis 数据库 + +- 初始化项目:`s init start-fc-redis-python -d start-fc-redis-python` +- 进入项目:`cd start-fc-redis-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 redis`案例的初始化、部署整个流程。 diff --git a/redis/src/code/index.py b/redis/src/code/index.py new file mode 100644 index 0000000..b32239c --- /dev/null +++ b/redis/src/code/index.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +import os +import redis + +conn_pool = None + + +def initializer(context): + global conn_pool + conn_pool = redis.ConnectionPool( + host=os.environ['REDIS_HOST'], password=os.environ['REDIS_PASSWORD'], + port=os.environ['REDIS_PORT'], db=1, decode_responses=True) + + +def handler(event, context): + global conn_pool + r = redis.Redis(connection_pool=conn_pool) + + counter = r.get('counter') + + if counter is None: + counter = 0 + else: + counter = int(counter) + + print('counter: ' + str(counter)) + + r.set('counter', str(counter + 1)) + return counter diff --git a/redis/src/code/requirements.txt b/redis/src/code/requirements.txt new file mode 100644 index 0000000..74b362f --- /dev/null +++ b/redis/src/code/requirements.txt @@ -0,0 +1 @@ +redis \ No newline at end of file diff --git a/redis/src/readme.md b/redis/src/readme.md new file mode 100644 index 0000000..f4a4bde --- /dev/null +++ b/redis/src/readme.md @@ -0,0 +1,12 @@ +# 阿里云函数计算 Python 访问 redis 数据库 + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 redis 数据库 + +- 初始化项目:`s init start-fc-redis-python -d start-fc-redis-python` +- 进入项目:`cd start-fc-redis-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 redis`案例的初始化、部署整个流程。 diff --git a/redis/src/s.yaml b/redis/src/s.yaml new file mode 100644 index 0000000..624a4c1 --- /dev/null +++ b/redis/src/s.yaml @@ -0,0 +1,28 @@ +edition: 1.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范 +name: fcDeployApp # 项目名称 +access: "{{ access }}" # 秘钥别名 + +services: + fc-db-redis-python: # 服务名称 + component: devsapp/fc # 组件名称 + props: # 组件的属性值 + region: cn-hangzhou + service: + name: fc-db-demo + description: 'demo for fc visit db' + internetAccess: true + function: + name: redis + description: visit redis + runtime: python3 + codeUri: ./code + handler: index.handler + memorySize: 256 + timeout: 30 + initializationTimeout: 60 + initializer: index.initializer + environmentVariables: + REDIS_HOST: + REDIS_PASSWORD: + REDIS_PORT: +# 函数计算FC组件文档参考地址:https://github.com/devsapp/fc \ No newline at end of file diff --git a/redis/version.md b/redis/version.md new file mode 100644 index 0000000..b51e0aa --- /dev/null +++ b/redis/version.md @@ -0,0 +1 @@ +- 新版本支持 \ No newline at end of file diff --git a/sql_server/publish.yaml b/sql_server/publish.yaml new file mode 100644 index 0000000..b408470 --- /dev/null +++ b/sql_server/publish.yaml @@ -0,0 +1,20 @@ +Type: Application +Name: start-fc-sql-server-python +Version: 0.0.2 +Provider: + - 阿里云 +Description: 快速部署一个 Python 访问 sql-server 的函数到阿里云函数计算。 +HomePage: https://github.com/devsapp/start-fc-db-python +Tags: + - 函数计算 + - 你好世界 + - 新手入门 +Category: 新手入门 +Service: + 函数计算: + Authorities: + - 创建函数 + - 删除函数 + - 创建服务 + - 删除服务 + Runtime: Python \ No newline at end of file diff --git a/sql_server/readme.md b/sql_server/readme.md new file mode 100644 index 0000000..52797d6 --- /dev/null +++ b/sql_server/readme.md @@ -0,0 +1,12 @@ +# 阿里云函数计算 Python 访问 sql server 数据库 + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mysql 数据库 + +- 初始化项目:`s init start-fc-sql-server-python -d start-fc-sql-server-python` +- 进入项目:`cd start-fc-sql-server-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 sql server`案例的初始化、部署整个流程。 diff --git a/sql_server/src/code/index.py b/sql_server/src/code/index.py new file mode 100644 index 0000000..295bb34 --- /dev/null +++ b/sql_server/src/code/index.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +import logging +import pymssql +import os + +logger = logging.getLogger() + + +def getConnection(): + try: + conn = pymssql.connect( + host=os.environ['MSSQL_SERVER'], + port=int(os.environ['MSSQL_PORT']), + user=os.environ['MSSQL_USER'], + password=os.environ['MSSQL_PASSWORD'], + database=os.environ['MSSQL_DATABASE'], + charset='utf8') + return conn + except Exception as e: + logger.error(e) + logger.error( + "ERROR: Unexpected error: Could not connect to SQL Server instance.") + raise Exception(str(e)) + + +def handler(event, context): + conn = getConnection() + try: + with conn.cursor() as cursor: + cursor.execute("SELECT * FROM users") + result = cursor.fetchone() + logger.info(result) + return result + finally: + conn.close() diff --git a/sql_server/src/code/requirements.txt b/sql_server/src/code/requirements.txt new file mode 100644 index 0000000..0c8c84d --- /dev/null +++ b/sql_server/src/code/requirements.txt @@ -0,0 +1 @@ +pymssql \ No newline at end of file diff --git a/sql_server/src/readme.md b/sql_server/src/readme.md new file mode 100644 index 0000000..52797d6 --- /dev/null +++ b/sql_server/src/readme.md @@ -0,0 +1,12 @@ +# 阿里云函数计算 Python 访问 sql server 数据库 + +只需几步就可以快速在阿里云函数计算服务上体验 Python 访问 mysql 数据库 + +- 初始化项目:`s init start-fc-sql-server-python -d start-fc-sql-server-python` +- 进入项目:`cd start-fc-sql-server-python` +- 将 s.yaml 中的环境变量修改成您自己的值, 并将数据库操作代码改成您需要的 +- 构建项目:`s build --use-docker` +- 部署项目:`s deploy` +- 触发项目:`s invoke` + +即可实现`Python 访问 sql server`案例的初始化、部署整个流程。 diff --git a/sql_server/src/s.yaml b/sql_server/src/s.yaml new file mode 100644 index 0000000..43952a3 --- /dev/null +++ b/sql_server/src/s.yaml @@ -0,0 +1,28 @@ +edition: 1.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范 +name: fcDeployApp # 项目名称 +access: "{{ access }}" # 秘钥别名 + +services: + fc-db-sql-server-python: # 服务名称 + component: devsapp/fc # 组件名称 + props: # 组件的属性值 + region: cn-hangzhou + service: + name: fc-db-demo + description: 'demo for fc visit db' + internetAccess: true + function: + name: sql_server + description: visit sql server + runtime: python3 + codeUri: ./code + handler: index.handler + memorySize: 256 + timeout: 60 + environmentVariables: + MSSQL_SERVER: + MSSQL_PORT: + MSSQL_USER: + MSSQL_PASSWORD: + MSSQL_DATABASE: +# 函数计算FC组件文档参考地址:https://github.com/devsapp/fc \ No newline at end of file diff --git a/sql_server/version.md b/sql_server/version.md new file mode 100644 index 0000000..b51e0aa --- /dev/null +++ b/sql_server/version.md @@ -0,0 +1 @@ +- 新版本支持 \ No newline at end of file