為了方便在公司利用ANSIBLE操作佈置步驟,在參加今年SA群英會後,參考MAX大的建議,建了一個Alpine Linux 的 VM ,並在VM內裝了Jupyter及Ansible,利用了下面的SCRIPT,當然,下面的SCRIPT也是參考了其他大師的智慧改寫的,主要是改寫自 eipdev/alpine-jupyter-notebook 的Dockerfile 中在Alpine linux安裝Jupyter的步驟,加上了安裝Ansible的程序上去的,預設將Ansible的script放在/opt/notebook/下就可以在啟動Jyuoyter時從頁面讀到了,當然,必需另外編寫ipynb檔案才能用Jupyter來執行。
install_ansible_n_jupyter.sh
#!/bin/sh
echo "@testing http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
apk update && apk upgrade
apk add --no-cache tini python3 libstdc++ openblas@testing freetype wget ca-certificates openssl openssh-client sshpass
apk --update add --virtual build-dependencies python-dev libffi-dev openssl-dev build-base
python3 -m ensurepip && rm -r /usr/lib/python*/ensurepip
pip3 install --upgrade pip setuptools
apk add --no-cache --virtual .build-deps@testing python3-dev make cmake clang clang-dev g++ linux-headers libtbb@testing libtbb-dev@testing openblas-dev@testing freetype-dev
export CC=/usr/bin/clang CXX=/usr/bin/clang++
ln -s /usr/include/locale.h /usr/include/xlocale.h
mkdir -p /opt/tmp && cd /opt/tmp
pip download -d /opt/tmp numpy
unzip -q numpy*.zip
cd numpy* && echo "Building numpy..."
echo -e "[ALL]\nlibrary_dirs = /usr/lib\ninclude_dirs = /usr/include\n[atlas]\natlas_libs = openblas\nlibraries = openblas\n[openblas]\nlibraries = openblas\nlibrary_dirs = /usr/lib\ninclude_dirs = /usr/include\n" > site.cfg
python3 setup.py build -j 4 install &> /dev/null && echo "Successfully installed numpy"
cd /opt/tmp
echo "Downloading opencv" && wget --quiet https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip -q 3.1.0.zip
cd opencv*
mkdir build && cd build && echo "Building opencv..."
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D WITH_FFMPEG=NO \
-D WITH_IPP=NO \
-D WITH_OPENEXR=NO \
-D WITH_WEBP=NO \
-D WITH_TIFF=NO \
-D WITH_JASPER=NO \
-D BUILD_EXAMPLES=OFF \
-D BUILD_PERF_TESTS=NO \
-D BUILD_TESTS=NO .. &> /dev/null
make &> /dev/null && make install &> /dev/null && echo "Successfully installed opencv"
pip3 install --upgrade matplotlib jupyter ipywidgets
jupyter nbextension enable --py widgetsnbextension
pip install --upgrade pip cffi
apk add --no-cache python py-pip
echo "===> Installing Ansible..."
pip install ansible
cd /opt && rm -r /opt/tmp && mkdir -p /opt/notebook
unset CC CXX
apk del .build-deps
rm -rf /var/cache/apk/*
rm -r /root/.cache
find /usr/lib/python3.5/ -type d -name tests -depth -exec rm -rf {} \;
find /usr/lib/python3.5/ -type d -name test -depth -exec rm -rf {} \;
find /usr/lib/python3.5/ -name __pycache__ -depth -exec rm -rf {} \;
mkdir -p /etc/ansible
echo 'localhost' > /etc/ansible/hosts
另外為了讓Jupyter能開機自動啟動,我寫了下面的script並參考這裡的說明放在 /etc/local.d/ 下,再執行
rc-update add local default
就可以自動在開機時啟動了。
/etc/local.d/jupyter.start
#!/bin/sh
/usr/bin/jupyter notebook --port=80 --no-browser --ip=0.0.0.0 --notebook-dir=/opt/notebook &
/etc/local.d/jupyter.stop
#!/bin/sh
/bin/ps axf | /bin/grep jupyter | /bin/grep -v grep | /usr/bin/awk '{print "kill -9 " $1}' | /bin/sh
後記 Alpine Linux 真的很好用,也很小,這整個裝起來才用不到1G的空間,真不錯。