2019年7月28日日曜日

VagrantでNode-RedとRedisをインストールした仮想マシン(Debian Buster/10)を構築する

Node-Redを使用してフロー図による開発を行うことができます。

〇Node-Redの画面


〇構築方法
以下のVagrantfileで、Node-RedとRedisをインストールした仮想マシン(Debian Buster/10)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-10"
  config.vm.hostname = "db10noderedredis"
config.vm.network "public_network", ip:"192.168.1.103", :netmask => "255.255.255.0"
config.vm.network "private_network", ip: "192.168.55.103", :netmask => "255.255.255.0"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db10noderedredis"
     vbox.gui = true
     vbox.cpus = 2
     vbox.memory = 2048
  end
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install task-japanese gawk
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
timedatectl set-timezone Asia/Tokyo


# install redis serrver
apt-get -y install redis-server

# change bind address.
sed -i -e 's/bind 127.0.0.1 ::1/bind 0.0.0.0/' /etc/redis/redis.conf

systemctl restart redis

# wait until port 6379 is opened.
while netstat -lnt | awk '$4 ~ /:6379$/ {exit 1}'; do sleep 10; done
sleep 10


redis-cli -h 192.168.55.103 << EOF
publish mykey 'hello, world!'
EOF


# install nodejs
cd /opt
wget https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.xz
tar xf node-v10.16.0-linux-x64.tar.xz
export PATH=$PATH:/opt/node-v10.16.0-linux-x64/bin
echo 'export PATH=$PATH:/opt/node-v10.16.0-linux-x64/bin/' >> /root/.profile

sudo ln -s /opt/node-v10.16.0-linux-x64/bin/node /usr/bin/node
sudo ln -s /opt/node-v10.16.0-linux-x64/bin/npm /usr/bin/npm
sudo ln -s /opt/node-v10.16.0-linux-x64/bin/bin/npx /usr/bin/npx

# install Node-Red and Node-Red-Dashboard
npm install -g --unsafe-perm node-red
npm install node-red-dashboard
npm install node-red-contrib-redis

mkdir -p /root/.node-red
cat << EOF > /etc/systemd/system/node-red.service
[Unit]
Description=Node-Red
[Service]
Environment="NODE_OPTIONS=--max-old-space-size=128"
Environment="NODE_RED_OPTIONS=-v"
ExecStart=/opt/node-v10.16.0-linux-x64/bin/node $NODE_OPTIONS /opt/node-v10.16.0-linux-x64/lib/node_modules/node-red/red.js $NODE_RED_OPTIONS
WorkingDirectory=/root/.node-red/
User=root
Group=root
Restart=on-failure
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target
EOF
systemctl enable node-red.service
systemctl start node-red.service

echo 'access to http://192.168.1.103:1880 or http://192.168.1.103:1880/ui'

SHELL
end

〇サンプルのワークフロー
指定されたチャネルをsubscribeして、メッセージを表示するサンプルです。
[{"id":"5b0ecbcc.d26de4","type":"redis-in","z":"82f74431.6f13d","server":"ea10aa06.b80e38","command":"subscribe","name":"redis","topic":"mykey","timeout":"0","x":110,"y":220,"wires":[["8e667b99.c15058"]]},{"id":"ea10aa06.b80e38","type":"redis-config","z":"","host":"192.168.1.103","port":"6379","dbase":"0","pass":""}]

0 件のコメント:

コメントを投稿