博客搬遷至https://blog.wangjiegulu.com
RSS訂閱:https://blog.wangjiegulu.com/feed.xml
Huginn 及環境搭建
什么是 Huginn ?
Huginn 是一個可以通過構建 agents 來幫你實現在線自動化任務的系統。它們可以理解 web,監聽事件,按你所需地去執行一些行為。Huginn 的 agents 創建和消費事件,通過有向圖表來進行轉播。你可以把它當作部署在你自己的服務器上的破解版本的 IFTTT 或 Zapier。
你可以用 Huginn 做什么?
- 追蹤天氣並在明天下雨(雪)的時候郵件通知給你(明天不要忘記帶傘)。
- 列出你關心的條目,並在 Twitter 發生改變的時候電子郵件通知給你。(例如,想知道在機器學習領域發生了什么有趣的事情嗎?Huginn 將在 Twitter 上觀察“machine learning”這個詞,並告訴你什么時候討論會高高峰。)
- 幫你觀察旅游機票和購物優惠信息。
- 抓取任意網站並在發生變化時電子郵件通知你。
- 連接到 Adioso, HipChat, Basecamp, Growl, FTP, IMAP, Jabber, JIRA, MQTT, nextbus, Pushbullet, Pushover, RSS, Bash, Slack, StubHub, translation APIs, Twilio, Twitter, Wunderground, and 微博等第三方.
- 發送和接收 WebHooks。
- 其它很多很多你能想到的。
環境搭建
以 Debian
服務器為例,進行環境搭建(大家可以選擇購買VPS)。
概述
Huginn 的安裝主要包括以下組件:
- Packages / Dependencies
- Ruby
- System Users
- Database
- Huginn
- Nginx
1. Packages / Dependencies
Debian 中 sudo
並沒有默認安裝。確保你的系統是最新的,然后安裝它。
# run as root!
apt-get update -y
apt-get upgrade -y
apt-get install sudo -y
注意:在安裝過程中,需要手動編輯一些文件。如果你熟悉 vim,請使用下面的命令將其設置為默認編輯器。如果你對 vim 不熟悉,請跳過此操作並繼續使用默認編輯器。
# Install vim and set as default editor
sudo apt-get install -y vim
sudo update-alternatives --set editor /usr/bin/vim.basic
導入 node.js 庫 (如果是 Ubuntu 或者 Debian Jessie 的話可以跳過):
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
安裝需要的 packages:
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
Debian Stretch
由於 Debian Stretch 的 runit 不會自動啟動,但是這會被init系統處理。另外,Ruby需要 OpenSSL 1.0 開發包而不是 1.1的。對於默認安裝使用這些包:
sudo apt-get install -y runit-systemd libssl1.0-dev
2. Ruby
在生產中使用帶有 Huginn 的 Ruby 版本管理器(如 RVM,rbenv 或 chruby)會頻繁導致難以診斷的問題。版本管理器不受支持,我們強烈建議所有人按照以下說明使用系統 Ruby。
如果存在的話,刪除舊版本的 Ruby:
sudo apt-get remove -y ruby1.8 ruby1.9
下載 Ruby,然后編譯:
mkdir /tmp/ruby && cd /tmp/ruby
curl -L --progress http://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.bz2 | tar xj
cd ruby-2.4.2
./configure --disable-install-rdoc
make -j`nproc`
sudo make install
安裝 bundler 和 foreman:
sudo gem install rake bundler foreman --no-ri --no-rdoc
3. System Users
為 Huginn 創建一個用戶:
sudo adduser --disabled-login --gecos 'Huginn' huginn
4. Database
安裝數據庫
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
# 選擇一個 MySQL root 密碼 (可以任意), 輸入並按回車,
# 重復輸入 MySQL root 密碼 然后按回車
對於 Debian Stretch, 替換 libmysqlclient-dev 為 default-libmysqlclient-dev。
檢查你安裝的 MySQL 版本:
mysql --version
sudo mysql_secure_installation
登錄 MySQL:
mysql -u root -p
# 輸入 MySQL root 密碼
為 Huginn 創建一個用戶,替換 命令中的 $password 為你真實的密碼:
mysql> CREATE USER 'huginn'@'localhost' IDENTIFIED BY '$password';
支持 long indexes,你需要確保可以使用 InnoDB engine:
mysql> SET default_storage_engine=INNODB;
# 如果失敗,檢查你的 MySQL 配置文件 (e.g. `/etc/mysql/*.cnf`, `/etc/mysql/conf.d/*`)
# 設置 "innodb = off"
給予 Huginn 用戶必要的數據庫相關權限:
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES ON `huginn_production`.* TO 'huginn'@'localhost';
退出 database 會話:
mysql> \q
嘗試使用新的用戶鏈接到新的數據庫
sudo -u huginn -H mysql -u huginn -p -D huginn_production
# Type the password you replaced $password with earlier
你應該回看到 ERROR 1049 (42000): Unknown database 'huginn_production'
,這是正常的,因為我們會稍后創建數據庫。
5. Huginn
Clone 源代碼
# We'll install Huginn into the home directory of the user "huginn"
cd /home/huginn
# Clone Huginn repository
sudo -u huginn -H git clone https://github.com/huginn/huginn.git -b master huginn
# Go to Huginn installation folder
cd /home/huginn/huginn
# Copy the example Huginn config
sudo -u huginn -H cp .env.example .env
# Create the log/, tmp/pids/ and tmp/sockets/ directories
sudo -u huginn mkdir -p log tmp/pids tmp/sockets
# Make sure Huginn can write to the log/ and tmp/ directories
sudo chown -R huginn log/ tmp/
sudo chmod -R u+rwX,go-w log/ tmp/
# Make sure permissions are set correctly
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/
sudo -u huginn -H chmod o-rwx .env
# Copy the example Unicorn config
sudo -u huginn -H cp config/unicorn.rb.example config/unicorn.rb
配置它
# Update Huginn config file and follow the instructions
sudo -u huginn -H editor .env
DATABASE_ADAPTER=mysql2
DATABASE_RECONNECT=true
DATABASE_NAME=huginn_production
DATABASE_POOL=20
DATABASE_USERNAME=huginn
DATABASE_PASSWORD='$password'
#DATABASE_HOST=your-domain-here.com
#DATABASE_PORT=3306
#DATABASE_SOCKET=/tmp/mysql.sock
DATABASE_ENCODING=utf8
# MySQL only: If you are running a MySQL server >=5.5.3, you should
# set DATABASE_ENCODING to utf8mb4 instead of utf8 so that the
# database can hold 4-byte UTF-8 characters like emoji.
#DATABASE_ENCODING=utf8mb4
重要: 取消注釋 RAILS_ENV 設置,以便於在生產環節運行 Huginn。
RAILS_ENV=production
如果需要改變 Unicorn 配置:
# Increase the amount of workers if you expect to have a high load instance.
# 2 are enough for most use cases, if the server has less then 2GB of RAM
# decrease the worker amount to 1
sudo -u huginn -H editor config/unicorn.rb
重要:確保 .env
和 unicorn.rb
都符合你的配置。
安裝 Gems
sudo -u huginn -H bundle install --deployment --without development test
初始化 Database
# Create the database
sudo -u huginn -H bundle exec rake db:create RAILS_ENV=production
# Migrate to the latest version
sudo -u huginn -H bundle exec rake db:migrate RAILS_ENV=production
# Create admin user and example agents using the default admin/password login
sudo -u huginn -H bundle exec rake db:seed RAILS_ENV=production SEED_USERNAME=admin SEED_PASSWORD=password
編譯 Assets
sudo -u huginn -H bundle exec rake assets:precompile RAILS_ENV=production
安裝初始腳本
Huginn 使用 foreman 來生成基於 Procfile
的初始化腳本。
編輯 Procfile
來針對生產選擇一個推薦的版本。
sudo -u huginn -H editor 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
Export 初始化 scripts:
sudo bundle exec rake production:export
注意:每次你修改了
.env
或者你的 procfile 文件,你都必須要重新 export 出事 script。
設置 Logrotate
sudo cp deployment/logrotate/huginn /etc/logrotate.d/huginn
確保你的 Huginn 實例正在運行
sudo bundle exec rake production:status
6. Nginx
注意:Nginx 是 Huginn 官方支持的 web 服務器。如果你不會或者不想使用 Nginx 作為你的 web 服務器,參考 wiki 的文章來使用配置 apache
安裝
sudo apt-get install -y nginx
網站配置
復制示例網站配置:
sudo cp deployment/nginx/huginn /etc/nginx/sites-available/huginn
sudo ln -s /etc/nginx/sites-available/huginn /etc/nginx/sites-enabled/huginn
確保編輯配置文件以匹配你的設置,如果你正在運行多個nginx站點,請從 listen
指令中刪除 default_server
參數:
# Change YOUR_SERVER_FQDN to the fully-qualified
# domain name of your host serving Huginn.
sudo editor /etc/nginx/sites-available/huginn
如果 huginn 是唯一可用的 nginx 網站,刪除默認的 nginx 網站:
sudo rm /etc/nginx/sites-enabled/default
Restart
sudo service nginx restart
完成。