首先介紹下在本文出現的幾個比較重要的概念:node
函數計算(Function Compute):函數計算是一個事件驅動的服務,經過函數計算,用戶無需管理服務器等運行狀況,只需編寫代碼並上傳。函數計算準備計算資源,並以彈性伸縮的方式運行用戶代碼,而用戶只需根據實際代碼運行所消耗的資源進行付費。函數計算更多信息參考。linux
Funcraft:Funcraft 是一個用於支持 Serverless 應用部署的工具,能幫助您便捷地管理函數計算、API 網關、日誌服務等資源。它經過一個資源配置文件(template.yml),協助您進行開發、構建、部署操做。Funcraft 的更多文檔參考。git
LibreOffice:LibreOffice 是由文檔基金會開發的自由及開放源代碼的辦公室套件。LibreOffice套件包含文字處理器、電子表格、演示文稿程序、矢量圖形編輯器和圖表工具、數據庫管理程序及建立和編輯數學公式的應用程序。參見維基百科詞條github
備註: 本文介紹的技巧須要 Funcraft 版本大於等於 3.0.0-beta.7 。docker
本項目是在 MacOS 下開發的,涉及到的工具是平臺無關的,對於 Linux 和 Windows 桌面系統應該也一樣適用。在開始本例以前請確保以下工具已經正確的安裝,更新到最新版本,並進行正確的配置。數據庫
Fun 工具依賴於 docker 來模擬本地環境。npm
對於 MacOS 用戶能夠使用 homebrew 進行安裝:bash
brew cask install docker brew tap vangie/formula brew install fun
Windows 和 Linux 用戶安裝請參考:服務器
安裝好後,記得先執行 fun config 初始化一下配置。less
注意, 若是你已經安裝過了 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 libreoffice-example@1.0.0 No description npm WARN libreoffice-example@1.0.0 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 下載下來。
本文爲雲棲社區原創內容,未經容許不得轉載。