2018年4月22日日曜日

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

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

〇Jupyter Labの画面


〇構築方法
以下のVagrantfileで、Jupyter LabとFoundationDBをインストールした仮想マシン(Debian Stretch/9.3)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604jupyterlabfoundationdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604jupyterlabfoundationdb"
     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 FoundationDB
wget https://www.foundationdb.org/downloads/5.1.5/ubuntu/installers/foundationdb-clients_5.1.5-1_amd64.deb
wget https://www.foundationdb.org/downloads/5.1.5/ubuntu/installers/foundationdb-server_5.1.5-1_amd64.deb

dpkg -i foundationdb-clients_5.1.5-1_amd64.deb
dpkg -i foundationdb-server_5.1.5-1_amd64.deb

systemctl enable foundationdb
systemctl start foundationdb


# 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 python binding
wget https://www.foundationdb.org/downloads/5.1.5/bindings/python/foundationdb-5.1.5.tar.gz
tar xvfz foundationdb-5.1.5.tar.gz
cd foundationdb-5.1.5
python setup.py install

cd /home/vagrant
cat << EOF > test.py
import fdb
fdb.api_version(510)
db = fdb.open()
test = fdb.directory.create_or_open(db, ('test',))
db[test['foo']] = 'hello world!'.encode(encoding='utf-8')
print(db[test['foo']].decode(encoding='utf-8'))
EOF
python test.py

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 fdb
fdb.api_version(510)
db = fdb.open()
test = fdb.directory.create_or_open(db, ('test',))
print(db[test['foo']].decode(encoding='utf-8'))

0 件のコメント:

コメントを投稿