Huginn 是一个在线运行的自动化机器人,可以根据用户的配置,抓取 rss 订阅、天气、新闻等数据,并进行邮件发送、rss 推送等处理。
目前 Huginn 在 Github 上已经获得了 31K 的 star,可以说是非常厉害了。
Huginn 提供了演示的 网站,可以对 Huginn 有一个基础的认识;如果想要本地看一看效果,最简单的方案就是通过 docker 来部署
docker run -it -p 3000:3000 huginn/huginn
然后在浏览器打开 http://localhost:3000
即可看到效果,默认邀请码为“try-huginn”,或者使用默认账号登录:
admin
password
我们这里以 Ubuntu 为例说明本地部署的方案,使用 Nginx 和 HTTPS,和 官方提供的方案 有一些不同,一方面是 Huginn 提供的部署方案比较死板且存在错误,另一方面在于我懒得了解 ruby 的一些东西了。
如果有大牛看到我文档中的一些不足,或者更好的部署建议,也欢迎来指正、交流。
sudo apt-get update
sudo apt-get install -y runit build-essential git zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake nodejs graphviz jq runit-systemd
目前(2021 年 4 月)Huginn 支持了 Ruby 2.6。Ruby 和 Python 类似,也有一个环境管理的工具 rbenv,我们同时安装这两个工具。
# == 安装 rbenv ==
sudo apt install rbenv
rbenv init
# == 退出命令行,重新登录 ==
echo $PATH # 检查一下环境变量,如果发现 .../.rbenv/shims 已经被加到 PATH 头部,那么就是安装成功了
# == 安装 ruby-install ==
# 直接 apt 安装的 ruby 版本可能有问题,所以通过 ruby-install 脚本来安装更可控
wget -O ruby-install-0.8.1.tar.gz https://github.com/postmodern/ruby-install/archive/v0.8.1.tar.gz
tar -xzvf ruby-install-0.8.1.tar.gz
cd ruby-install-0.8.1/
sudo make install # 完成 ruby-install 的安装
# == 安装 ruby 2.6.6 到 rbenv 的路径下 ==
ruby-install --install-dir ~/.rbenv/versions/2.6.6 ruby 2.6.6
# == 进入希望安装 Huginn 的路径 ==
cd /home/用户名/
# == 从 github 上获取 Huginn ==
git clone https://github.com/huginn/huginn
cd huginn
rbenv local 2.6.6 # 设置该路径下,使用 2.6.6 版本的 ruby
# == 编辑基础配置 ==
cp .env.example .env
cp config/unicorn.rb.example config/unicorn.rb
vi .env
需要编辑的选项包括:
APP_SECRET_TOKEN
的值,请随意填写一个高强度的密码DATABASE_HOST
为 localhost
DATABASE_NAME
为 huginn_production
DATABASE_USERNAME
为 huginn
DATABASE_PASSWORD
为刚刚设置的 mysql 账号的密码INVITATION_CODE
,即平台邀请码,默认是try-huginn
RAILS_ENV=production
这一行的注释USE_GRAPHVIZ_DOT=dot
这一行的注释,因为国内不能使用 google graphSMTP
相关的配置,从而支持邮箱发送进行依赖包的安装、编译
rm Gemfile.lock # 因为 Gemfile.lock 规定的依赖包存在一些问题
vi Gemfile
# 修改 `gem 'mini_racer', '~> 0.2.4'` 为 `gem 'mini_racer', '~> 0.3.1'`
bundle install
bundle exec rake assets:precompile RAILS_ENV=production
对 rails6 再做一个小配置,不然在进行访问的时候会发现域名不被允许访问
vi config/environments/production.rb
# 在文件中增加 config.hosts << "你自己的域名"
这里以 mysql 为例,如果没有安装 mysql,那么需要先
sudo apt install mariadb-server
sudo mysql_secure_installation
创建 Huginn 的用户和数据库
sudo mysql -u root -p
# 输入密码
mysql> CREATE USER 'huginn'@'localhost' IDENTIFIED BY '此处请设置一个用户密码';
mysql> CREATE DATABASE huginn_development character set utf8 collate utf8_bin;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES ON `huginn_production`.* TO 'huginn'@'localhost';
mysql> \q
创建数据库
bundle exec rake db:create RAILS_ENV=production
bundle exec rake db:migrate RAILS_ENV=production
# 这里记得修改 admin 和 password 为你希望的初始用户名、密码
bundle exec rake db:seed RAILS_ENV=production SEED_USERNAME=admin SEED_PASSWORD=password
如果以上步骤没有问题,此时运行 bundle exec foreman start
后服务就会启动,等待片刻后打开浏览器 http://localhost:3000
即可访问,使用刚刚 bundle exec rake db:seed
设置的用户名、密码即可登录
如果没有什么问题,那么可以 Ctrl+C
把服务先暂停。
如果只想本地访问服务,那么可以跳过 Nginx 配置,直接进入下一步
修改 Procfile 文件
vi Procfile
# == 注释这两行 ==
# web: bundle exec rails server -p ${PORT-3000} -b ${IP-0.0.0.0}
# jobs: bundle exec rails runner bin/threaded.rb
# == 去除这两行的注释
web: bundle exec unicorn -c config/unicorn.rb
jobs: bundle exec rails runner bin/threaded.rb
此处省略对于 Nginx 的安装、配置说明、证书生成、服务重启过程,直接给出配置文件,注意有几个地方需要修改:
# vi /etc/nginx/sites-enabled/60-huginn.conf
upstream huginn {
server unix:/home/用户名/huginn/tmp/sockets/unicorn.socket fail_timeout=0;
}
## HTTPS host
server {
listen 端口 ssl;
server_name 你的域名; ## Replace this with something like huginn.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice
root /home/用户/huginn/public;
## Increase this if you want to upload large attachments
## Or if you want to accept large git objects over http
client_max_body_size 20m;
## Strong SSL Security
ssl on;
ssl_certificate /etc/nginx/cert/certificate.crt;
ssl_certificate_key /etc/nginx/cert/private.key;
ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4';
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
## Individual nginx logs for this huginn vhost
access_log /var/log/nginx/huginn_access.log;
error_log /var/log/nginx/huginn_error.log;
location / {
## Serve static files from defined root folder.
## @huginn is a named location for the upstream fallback, see below.
try_files $uri $uri/index.html $uri.html @huginn;
}
## If a file, which is not found in the root folder is requested,
## then the proxy passes the request to the upsteam (huginn unicorn).
location @huginn {
## If you use HTTPS make sure you disable gzip compression
## to be safe against BREACH attack.
gzip off;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Frame-Options SAMEORIGIN;
proxy_pass http://huginn;
}
## Enable gzip compression as per rails guide:
## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
## WARNING: If you are using relative urls remove the block below
## See config/application.rb under "Relative url support" for the list of
## other files that need to be changed for relative url support
location /assets/ {
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
error_page 502 /502.html;
}
配置后,还要把 Huginn 服务开启后才能看到效果
可以使用以下命令来后台运行
bundle exec foreman start &>/dev/null &
disown
开机启动可以使用
# /etc/systemd/system/huginn.service
[Unit]
Description=huginn
After=network.target
[Service]
Type=forking
User=用户名
Group=用户组
WorkingDirectory=/home/用户名/huginn
ExecStart=/home/用户名/.rbenv/versions/2.6.6/bin/bundle exec foreman start &>/dev/null &;disown
[Install]
WantedBy=multi-user.target
虽然 Huginn 文档组织比较乱,部署的过程中依然遇到了不少问题,但是毕竟功能强大,也算值了。