简体   繁体   English

将Node.js和Node.js应用程序部署到Raspberry Pi

[英]Deploying Node.js and Node.js application to Raspberry Pi

I have a Node.js application that I want to run on a Raspberry Pi. 我有一个Node.js应用程序,我想在Raspberry Pi上运行。

And, I'd like to be able to deploy new version of my application as well as new versions of Node.js to that Raspberry Pi remotely. 而且,我希望能够远程部署我的应用程序的新版本以及新版本的Node.js到Raspberry Pi。

Basically, something such as: 基本上,如:

$ pi-update 192.168.0.37 node@0.11.4
$ pi-update 192.168.0.37 my-app@latest

I don't have any preferences on how to transfer my app to the Pi, may it be pushing or pulling. 我对如何将我的应用程序转移到Pi没有任何偏好,可能是推或拉。 I don't care (although I should add that the code for the application is available from a private GitHub repository). 我不在乎(虽然我应该补充说,应用程序的代码可以从私有GitHub存储库获得)。

Additionally, once Node.js and / or my app were deployed, I want the potentially running Node.js app to restart. 此外,一旦部署了Node.js和/或我的应用程序,我希望可能正在运行的Node.js应用程序重新启动。

How could I do this? 我怎么能这样做? Which software should I look into? 我应该研究哪种软件? Is this something that can easily be done using tools from Raspbian, or should I look for 3rd party software (devops tools, such as Chef & co.), or ...? 这是否可以使用Raspbian的工具轻松完成,或者我应该寻找第三方软件(devop工具,如Chef&co。),还是......?

Any help is greatly appreciated :-) 任何帮助是极大的赞赏 :-)

a) For running the script continuously, you can use tools like forever or pm2 , otherwise you can also make the app a debian daemon on raspian you can run with sudo <servicename> start (if you're running Arch Linux, this is handled differently I guess). a)为了连续运行脚本,你可以使用像foreverpm2这样的工具,否则你也可以在应用程序上使用sudo <servicename> start运行应用程序作为raspian的debian守护进程 (如果你正在运行Arch Linux,这是处理的不同的我猜)。

b) If your Raspberry is reachable from the internet, you can use a GitHub hook ( API Documentation ) to run every time you push a change to your repository. b)如果可以从Internet访问您的Raspberry,您可以使用GitHub挂钩API文档 )在每次将更改推送到存储库时运行。 This hook is basically a URL endpoint on your Pi that runs a little shell script locally. 这个钩子基本上是你的Pi上的URL端点,它在本地运行一个小的shell脚本。

This script should shutdown you app gracefully, do a git pull for your repository and start the app/service again. 这个脚本应该优雅地关闭你的app,为你的存储库做一个git pull并再次启动app / service。 You could also trigger this shell script over SSH from your local machine, eg ssh pi@192.168.0.37 /path/to/your/script 您也可以从本地计算机通过SSH触发此shell脚本,例如ssh pi@192.168.0.37 /path/to/your/script

A update script could look like this: 更新脚本可能如下所示:

# change the 'service' command to your script runner of choice
service <yourapp> stop
cd /path/to/your/app
git pull
service <yourapp> start

c) The problem with remote updating Node itself is, that the official binary builds for Raspberry Pi appear only very irregularly, otherwise it would be easy to just download/update the binaries with wget or curl. c)远程更新Node本身的问题是,Raspberry Pi的官方二进制构建只是非常不规则,否则很容易用wget或curl下载/更新二进制文件。 So most of the time you either need to cross compile Node on your own machine or spend about two hours to recompile it on your Pi. 因此,大多数情况下,您需要在自己的计算机上交叉编译Node,或者花费大约两个小时在Pi上重新编译它。 If you want to go with the unofficial builds on GitHub , you can install them with curl -# -L https://gist.github.com/raw/3245130/v0.10.17/node-v0.10.17-linux-arm-armv6j-vfp-hard.tar.gz | tar xzvf - --strip-components=1 -C /usr/local 如果你想在GitHub上使用非官方版本 ,可以用curl -# -L https://gist.github.com/raw/3245130/v0.10.17/node-v0.10.17-linux-arm-armv6j-vfp-hard.tar.gz | tar xzvf - --strip-components=1 -C /usr/local安装它们curl -# -L https://gist.github.com/raw/3245130/v0.10.17/node-v0.10.17-linux-arm-armv6j-vfp-hard.tar.gz | tar xzvf - --strip-components=1 -C /usr/local curl -# -L https://gist.github.com/raw/3245130/v0.10.17/node-v0.10.17-linux-arm-armv6j-vfp-hard.tar.gz | tar xzvf - --strip-components=1 -C /usr/local but you need to check the file name for every release. curl -# -L https://gist.github.com/raw/3245130/v0.10.17/node-v0.10.17-linux-arm-armv6j-vfp-hard.tar.gz | tar xzvf - --strip-components=1 -C /usr/local但是你需要检查每个版本的文件名。

Look no further than resin.io All you need to is flush your rpi with their image and then git push your project. 看看resin.io只需要用你的图像刷新你的rpi,然后git推送你的项目。 resin.io will compile its code and dependencies for your device's architecture and send the result to your device(s) (in a docker file). resin.io将为您的设备架构编译其代码和依赖项,并将结果发送到您的设备(在docker文件中)。

You can create a very simple continuous integration scheme using supervisor , which does two things: 您可以使用supervisor创建一个非常简单的持续集成方案,它可以做两件事:

  • keeps your process running even if it fails, 即使失败,您的进程也会继续运行
  • and restarts your process if any of the files changes. 如果任何文件发生更改,请重新启动您的进程。

It becomes a simple issue to keep your app updated: you just have to run the commands git pull; npm install 保持应用程序更新成为一个简单的问题:你只需运行命令git pull; npm install git pull; npm install : when code is downloaded (or even node modules change) supervisor will will restart the app automatically for you. git pull; npm install :当下载代码(甚至更改节点模块)时,supervisor将自动为您重启应用程序。

If the Raspberry Pi is visible from the internet you can use a GitHub webhook , pointing it to a very simple page that runs the commands git pull; npm install 如果从互联网上可以看到Raspberry Pi,你可以使用GitHub webhook ,将它指向一个运行命令git pull; npm install的非常简单的页面git pull; npm install git pull; npm install using child_process.exec() . 使用child_process.exec() git pull; npm install (One important note: use a non-trivial URL (with a code or something) so that people don't run into it by mistake.) Otherwise just run those commands from the crontab every hour or so, for instance. (一个重要的注意事项:使用一个非平凡的URL(带有代码或其他东西),以便人们不会错误地遇到它。)否则,只需每小时左右从crontab运行这些命令,例如。

As for updating node.js itself, I would use the official Debian package , either from testing or getting it from unstable . 至于更新node.js本身,我会使用官方的Debian软件包 ,无论是测试还是从unstable获取。 Otherwise you would have to create a private repo to host your own packages, which probably is not worth the hassle; 否则你将不得不创建一个私人仓库来托管你自己的包,这可能不值得麻烦; but is doable. 但是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM