2018年5月3日木曜日

VagrantでJupyter LabとOrientDBをインストールした仮想マシン(Ubuntu16.04)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。

〇Jupyter Labの画面


〇構築方法
以下のVagrantfileで、Jupyter LabとOrientDBをインストールした仮想マシン(Ubuntu16.04)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604jupyterlaborientdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604jupyterlaborientdb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8


# install java
apt-get -y install openjdk-8-jdk

# download and install orientdb community edition
apt-get -y install unzip
wget https://bit.ly/orientdb-ce-2-2-34 -O orientdb-community-2.2.34.zip
unzip orientdb-community-2.2.34.zip
mv orientdb-community-2.2.34 /opt/orientdb

cat << EOF > /etc/systemd/system/orientdb.service
[Unit]
Description=OrientDB

[Service]
Type=simple
ExecStart=/opt/orientdb/bin/server.sh
ExecStop=/opt/orientdb/bin/shutdown.sh
WorkingDirectory=/opt/orientdb
KillMode=process

[Install]
WantedBy=multi-user.target
EOF
sed -i -e 's#<users>#<users><user resources="*" password="root" name="root"/>#" /opt/orientdb/config/orientdb-server-config.xml
systemctl enable orientdb.service
systemctl start orientdb.service

echo 'access http://192.168.55.104:2480'
echo 'user: admin    password: admin'


# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab

# install pyorient 
pip install pyorient


useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8888
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
ExecStartPre=source /opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter

echo 'jupyter -> http://192.168.55.104:8888/?token=jupyter'
SHELL
end

○動作確認用コード
import pyorient
client = pyorient.OrientDB("192.168.1.104", 2424) 
session_id = client.connect("root", "root" )
client.db_create("test", pyorient.DB_TYPE_GRAPH, pyorient.STORAGE_TYPE_MEMORY ) 
client.db_open("test", "admin", "admin" ) 
client.command("create class messages extends V") 
client.command("create property messages.id Integer" ) 
client.command("create property messages.message String" ) 

#insert a row
client.command("insert into messages (id, message) values (10, 'hello world')") 
# execute query
records = client.command("select id, message from messages")
for record in records:
    print(str(record.id) + ":" + record.message)

0 件のコメント:

コメントを投稿