天天看點

Huginn及環境搭建

部落格搬遷至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 的安裝主要包括以下元件:

  1. Packages / Dependencies
  2. Ruby
  3. System Users
  4. Database
  5. Huginn
  6. 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
           

完成。

參考

  1. https://github.com/huginn/huginn
  2. https://github.com/huginn/huginn/blob/master/doc/manual/installation.md

繼續閱讀