2019年7月27日土曜日

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

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

〇Node-Redの画面


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

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-10"
  config.vm.hostname = "db10noderedmariadb"
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 = "db10noderedmariadb"
     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 mariadb
echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -proot -e "CREATE DATABASE test DEFAULT CHARACTER SET utf8;"
mysql -uroot -proot -e "CREATE USER test@localhost IDENTIFIED BY 'test';"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON test.* TO 'test'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"

mysql -utest -ptest -e "CREATE TABLE message (message_id INTEGER NOT NULL, message_text varchar(100)); INSERT INTO message VALUES (1, 'Hello, World!');" test



# 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-node-mysql

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

〇サンプルのワークフロー
インジェクションボタンをクリックするとMariaDBにクエリを実行し、デバッグ表示するサンプルです。
[{"id":"93fe20b2.f6a328","type":"tab","label":"フロー 1","disabled":false,"info":""},{"id":"11991ce9.f4ee83","type":"mysql","z":"93fe20b2.f6a328","mydb":"c12f3242.9d8cb8","name":"testdb","x":170,"y":220,"wires":[["ec768b10.fb5e68"]]},{"id":"5bcb36f5.48bb78","type":"inject","z":"93fe20b2.f6a328","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":80,"wires":[["f1ea45eb.dc289"]]},{"id":"ec768b10.fb5e68","type":"debug","z":"93fe20b2.f6a328","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":220,"y":340,"wires":[]},{"id":"f1ea45eb.dc289","type":"function","z":"93fe20b2.f6a328","name":"","func":"msg.topic = \"SELECT message_text FROM message where message_id=1;\"\nreturn msg;","outputs":1,"noerr":0,"x":120,"y":160,"wires":[["11991ce9.f4ee83"]]},{"id":"c12f3242.9d8cb8","type":"MySQLdatabase","z":"","host":"127.0.0.1","port":"3306","db":"test","tz":""}]

0 件のコメント:

コメントを投稿