2018年3月26日月曜日

VagrantでJupyter LabとCouchDBをインストールした仮想マシン(Debian Stretch/9.3)を構築する

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。
以下のVagrantfileで、Jupyter LabとCouchDBをインストールした仮想マシン(Debian Stretch/9.3)を構築できます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.3"
  config.vm.hostname = "db93jupyterlabcouchdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db93jupyterlabcouchdb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.101", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
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
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# install couchdb
echo "deb http://apache.bintray.com/couchdb-deb stretch main" >> /etc/apt/sources.list
curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc | apt-key add -
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get -y install couchdb

sed -i -e 's/bind_address = 127.0.0.1/bind_address = 0.0.0.0/' /etc/couchdb/default.ini
systemctl restart couchdb.service


# 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 couchdb driver
pip install couchdb


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 = 8080
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 'access : http://192.168.55.101:5984/_utils/index.html'
echo ''
echo 'url for jupyter lab -> http://192.168.55.101:8080/?token=jupyter'

SHELL
end

〇動作確認用コード
import couchdb

# connect to couchdb
couchserver = couchdb.Server("http://localhost:5984/")

# create a db
dbname = "testdb"
if dbname in couchserver:
  db = couchserver[dbname]
else:
  db = couchserver.create(dbname)

# store a document
doc_id, doc_rev = db.save({'message_id': 'hello world!'})

# get a document by id
print(db.get(doc_id))

〇Jupyter Labの画面


○関連情報
・CouchDBに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿