前言
首先介绍下在本文出现的几个比较重要的概念:
函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息参考。
Funcraft:Funcraft 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Funcraft 的更多文档参考。
LibreOffice:LibreOffice 是由文档基金会开发的自由及开放源代码的办公室套件。LibreOffice套件包含文字处理器、电子表格、演示文稿程序、矢量图形编辑器和图表工具、数据库管理程序及创建和编辑数学公式的应用程序。参见维基百科词条
备注: 本文介绍的技巧需要 Funcraft 版本大于等于 3.0.0-beta.7 。
依赖工具
本项目是在 MacOS 下开发的,涉及到的工具是平台无关的,对于 Linux 和 Windows 桌面系统应该也同样适用。在开始本例之前请确保如下工具已经正确的安装,更新到最新版本,并进行正确的配置。
- Docker
- Funcraft
Fun 工具依赖于 docker 来模拟本地环境。
对于 MacOS 用户可以使用 homebrew 进行安装:
brew cask install docker
brew tap vangie/formula
brew install fun
Windows 和 Linux 用户安装请参考:
https://github.com/aliyun/fun/blob/master/docs/usage/installation.md
安装好后,记得先执行 fun config 初始化一下配置。
注意, 如果你已经安装过了 funcraft,确保 funcraft 的版本在 3.0.0-beta.7 以上。
$ fun --version
3.0.0-beta.7
初始化
使用 fun init 命令可以快捷地将本模板项目初始化到本地。
$ fun init vangie/libreoffice-example
安装依赖
$ fun install
Installing recursively on fun.yml
skip pulling image aliyunfc/runtime-nodejs8:build-1.6.1...
Task => [UNNAMED]
=> apt-get update (if need)
=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp/install libnss3 --reinstall
=> bash -c
for f in $(ls /code/.fun/tmp/install/archives/*.deb); do
dpkg -x $f /code/.fun/root;
mkdir -p /code/.fun/tmp/install/deb-control/${f%.*};
dpkg -e $f /code/.fun/tmp/install/deb-control/${f%.*};
if [ -f "/code/.fun/tmp/install/deb-control/${f%.*}/postinst" ]; then
FUN_INSTALL_LOCAL=true /code/.fun/tmp/install/deb-control/${f%.*}/postinst configure;
fi;
done;
=> bash -c 'rm -rf /code/.fun/tmp/install/archives'
Task => [UNNAMED]
=> bash -c 'npm install --production && mkdir -p .fun/nas/auto-default/libreoffice/ && mv ./node_modules/fc-libreoffice/bin/lo.tar.br .fun/nas/auto-default/libreoffice/'
info looking for cached prebuild @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz
http request GET https://github.com/MayhemYDG/iltorb/releases/download/v2.4.3/iltorb-v2.4.3-node-v57-linux-x64.tar.gz
http 200 https://github.com/MayhemYDG/iltorb/releases/download/v2.4.3/iltorb-v2.4.3-node-v57-linux-x64.tar.gz
info downloading to @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz.152-11c561798275d.tmp
info renaming to @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz
info unpacking @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz
info unpack resolved to /code/node_modules/iltorb/build/bindings/iltorb.node
info unpack required /code/node_modules/iltorb/build/bindings/iltorb.node successfully
info install Successfully installed iltorb binary!
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
部署
$ fun deploy
using template: template.yml
using region: cn-shanghai
using accountId: ***********4733
using accessKeyId: ***********EUz3
using timeout: 60
Waiting for service libreoffice to be deployed...
make sure role 'aliyunfcgeneratedrole-cn-shanghai-libreoffice' is exist
role 'aliyunfcgeneratedrole-cn-shanghai-libreoffice' is already exist
attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-shanghai-libreoffice
attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-shanghai-libreoffice
using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
vpc already generated, vpcId is: vpc-uf6jphcirs45lekbtw59k
vswitch already generated, vswitchId is: vsw-uf6gmxg7cyl3dlohlck9p
security group already generated, security group is: sg-uf6hkd156qntmzxr51h3
generated auto VpcConfig done: {"vpcId":"vpc-uf6jphcirs45lekbtw59k","vswitchIds":["vsw-uf6gmxg7cyl3dlohlck9p"],"securityGroupId":"sg-uf6hkd156qntmzxr51h3"}
using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
nas file system already generated, fileSystemId is: 3f95748ba7
nas file system mount target is already created, mountTargetDomain is: 3f95748ba7-ips53.cn-shanghai.nas.aliyuncs.com
generated auto NasConfig done: {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"3f95748ba7-ips53.cn-shanghai.nas.aliyuncs.com:/libreoffice","MountDir":"/mnt/auto"}]}
Checking if nas directories /libreoffice exists, if not, it will be created automatically
Checking nas directories done ["/libreoffice"]
Waiting for function word2pdf to be deployed...
Waiting for packaging function word2pdf code...
The function word2pdf has been packaged. A total of 1019 files files were compressed and the final size was 6.36 MB
function word2pdf deploy success
service libreoffice deploy success
执行
$ fun invoke word2pdf
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 20e7c0e4-d674-4be0-854f-bd876dc00043
load code for handler:index.handler
Error: source file could not be loaded
2019-09-17T13:57:35.716Z 20e7c0e4-d674-4be0-854f-bd876dc00043 [verbose] convert success.
FC Invoke End RequestId: 20e7c0e4-d674-4be0-854f-bd876dc00043
Duration: 4741.22 ms, Billed Duration: 4800 ms, Memory Size: 640 MB, Max Memory Used: 578.54 MB
========= FC invoke Logs end =========
FC Invoke Result:
pdf save to /tmp/example.pdf
PDF 文件会写入到 /tmp 目录,如果希望能看到结果,可以把文件上传到 OSS,然后从 OSS 下载下来。
参考阅读
- 五分钟上线——函数计算 Word 转 PDF 云服务
- https://github.com/awesome-fc/fc-libreoffice
- 模板项目
阅读原文
本文为云栖社区原创内容,未经允许不得转载。