2019年7月21日日曜日

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

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

〇Node-Redの画面


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

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-10"
  config.vm.hostname = "db10nodered"
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 = "db10nodered"
     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 postgresql
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt-get update
apt-get -y install postgresql-11

echo "listen_addresses='*'" >> /etc/postgresql/11/main/postgresql.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/11/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/11/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/11/main/pg_hba.conf

systemctl restart postgresql.service
su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 test
psql -c "
alter user postgres with password 'postgres';
create user test with password 'test';
grant all privileges on database test to test;
"
EOF
export PGPASSWORD=test
psql -h localhost -d test -U test -c "
create table messages (message_id integer not null, message varchar(100));
insert into messages values (1, 'hello world.');
insert into messages values (2, 'test message.');
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service

# 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-postgres-multi

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

〇サンプルのワークフロー
インジェクションボタンをクリックするとPostgreSQLにクエリを実行し、デバッグ表示するサンプルです。
[{"id":"f3632482.cf9818","type":"tab","label":"フロー 1","disabled":false,"info":""},{"id":"b1f066e5.dcbc6","type":"inject","z":"f3632482.cf9818","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":100,"wires":[["64e4c46f.7a3ebc"]]},{"id":"5d339df4.131e94","type":"postgres","z":"f3632482.cf9818","postgresdb":"220b0389.c2391c","name":"testdb","output":true,"outputs":1,"x":210,"y":260,"wires":[["a4ae247f.488c9"]]},{"id":"64e4c46f.7a3ebc","type":"function","z":"f3632482.cf9818","name":"sample-query","func":"msg.payload = [\n    {\n        query: 'select message from messages where message_id=1',\n        output: true,\n    },\n];\nreturn msg;","outputs":1,"noerr":0,"x":150,"y":180,"wires":[["5d339df4.131e94"]]},{"id":"a4ae247f.488c9","type":"debug","z":"f3632482.cf9818","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0]","targetType":"jsonata","x":320,"y":340,"wires":[]},{"id":"220b0389.c2391c","type":"postgresdb","z":"","hostname":"localhost","port":"5432","db":"test","ssl":false}]

0 件のコメント:

コメントを投稿