以下のVagrantfileで、Jupyter LabとMongoDBをインストールした仮想マシン(Ubuntu16.04)を構築できます。
Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "bento/ubuntu-16.04"
config.vm.hostname = "ub1604jupyterlabmongodb36"
config.vm.provider :virtualbox do |vbox|
vbox.name = "ub1604jupyterlabmongodb36"
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
# 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 mongodb
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" >> /etc/apt/sources.list.d/mongodb-org-3.6.list
apt-get update
apt-get -y install mongodb-org
systemctl enable mongod
systemctl start mongod
# wait until mongod starts listening.
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done
cat << EOF > /tmp/addadmin.js
var db = db.getSiblingDB('admin');
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
EOF
mongo /tmp/addadmin.js
echo 'security:' >> /etc/mongod.conf
echo ' authorization: enabled' >> /etc/mongod.conf
systemctl restart mongod
while netstat -lnt | awk '$4 ~ /:27017$/ {exit 1}'; do sleep 10; done
# create a test database and a test user.
cat << EOF > /tmp/createtest.js
var db = db.getSiblingDB('test');
db.createUser({user:"test",pwd:"test",roles:[{role:"readWrite",db:"test"}]});
EOF
mongo -u "admin" -p "admin" --authenticationDatabase "admin" /tmp/createtest.js
# create sample data
mongo -u "test" -p "test" --authenticationDatabase "test" << EOF > /tmp/output.js
var db = db.getSiblingDB('test');
db.products.insert( { item: "chair", qty: 15 } );
db.products.insert( { item: "table", qty: 3 } );
db.products.find().pretty();
EOF
cat /tmp/output.js
# 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 pymongo
pip install pymongo
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:8080/?token=jupyter'
SHELL
end
〇動作確認用コード
import pymongo
# connect to mongodb
client = pymongo.MongoClient('mongodb://test:test@localhost/test')
db = client.test
products = db.products
for data in products.find():
print(data)
〇Jupyter Labの画面
0 件のコメント:
コメントを投稿