ubuntu 16.04执行letsencrypt的时候报告错误“ImportError: No module named datetime”

网站一直使用letsencrypt提供的HTTPS证书,这个证书的问题在于每隔三个月就必须更新一次,本次更新证书的时候,提示如下错误:

Error: couldn't get currently installed version for /root/.local/share/letsencrypt/bin/letsencrypt:
Traceback (most recent call last):
  File "/root/.local/share/letsencrypt/bin/letsencrypt", line 7, in <module>
    from certbot.main import main
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/certbot/main.py", line 9, in 

<module>
    from acme import jose
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/acme/jose/__init__.py", line 

37, in <module>
    from acme.jose.interfaces import JSONDeSerializable
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/acme/jose/interfaces.py", line 

9, in <module>
    from acme.jose import util
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/acme/jose/util.py", line 4, in 

<module>
    from cryptography.hazmat.primitives.asymmetric import rsa
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-

packages/cryptography/hazmat/primitives/asymmetric/rsa.py", line 14, in <module>
    from cryptography.hazmat.backends.interfaces import RSABackend
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-

packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
    import pkg_resources
  File "/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/pkg_resources/__init__.py", 

line 36, in <module>
    import plistlib
  File "/usr/lib/python2.7/plistlib.py", line 62, in <module>
    import datetime
ImportError: No module named datetime

错误发生的原因在于letsencrypt自己构建了一个Python的虚拟环境来隔离,但是早期建立的虚拟环境中是缺少部分软件包的,而自身的BUG导致也没有重新更新虚拟环境,导致出现异常。

解决方法就是删除letsencrypt自己构建的Python的虚拟环境,然后继续执行脚本让他重建即可。

$ rm -rf ~/.local/share/letsencrypt

参考链接


Ubuntu 16.04.3/14.04.5系统上修改Docker镜像的存储路径

最近在Ubuntu 16.04.3/14.04.5系统上使用Docker结果由于默认的镜像存储路径在系统分区上,而系统分区又不足够大,导致整个系统都不能正常工作了。

因此我们需要把Docker的镜像存储目录移动到数据分区。

执行如下命令查询默认的存储路径

$ sudo docker info | grep "Docker Root Dir"

我们看到如下输出

Docker Root Dir: /var/lib/docker

比较简单的方法是通过软链接的方式来实现,具体命令如下:

$ sudo service docker stop

#我的系统是用户分区足够大
$ sudo mv /var/lib/docker ~/.docker

$ sudo ln -s ~/.docker /var/lib/docker

$ sudo service docker start

参考链接


Ubuntu 14.04系统上安装部署OpenWhisk(本地CouchDB版本)

1.安装配置CouchDB

$ sudo apt-get install -V couchdb

校验CouchDB是否工作正常

$ curl –k http://127.0.0.1:5984

看到如下输出即可

{"couchdb":"Welcome","uuid":"87586fee02b1149fa06dda7148450d4a","version":"1.5.0","vendor":{"name":"Ubuntu","version":"14.04"}}

2.编译配置OpenWhisk

下载OpenWhisk的源代码

$ sudo apt-get install git

$ cd  ~

$ git clone https://github.com/apache/incubator-openwhisk.git

配置OpenWhisk编译需要的环境

$ cd incubator-openwhisk

# Install all required software
$ cd tools/ubuntu-setup

$ sudo ./all.sh

执行编译

#返会代码根目录
$ cd ../..

$ sudo ./gradlew distDocker

3.发布并运行OpenWhisk服务

配置运行环境

$ cd ~

$ cd incubator-openwhisk

$ cd ansible

#只在第一次初始化数据库的时候执行这句话
$ sudo ansible-playbook -i environments/local/ setup.yml

$ sudo ansible-playbook -i environments/local/ openwhisk.yml

安装必备软件

#系统自带的版本太低,但是还是需要安装一下,用来解决依赖问题
$ cd ~
$ sudo apt-get install vagrant

$ wget https://releases.hashicorp.com/vagrant/1.9.7/vagrant_1.9.7_x86_64.deb

$ sudo dpkg -i vagrant*.deb

$ sudo apt-get install virtualbox

运行测试Demo

$ cd ~/incubator-openwhisk/tools/vagrant

$ ./hello

如过想查看刚刚通过vagrant部署完成的镜像系统里面的内容,则执行如下命令:

$ cd ~/incubator-openwhisk/tools/vagrant

$ sudo vagrant ssh

如果成功部署了OpenWhisk,那么可以在Docker机器中执行如下代码验证是否可以正常调用

$ cd ~/incubator-openwhisk/tools/vagrant

$ sudo vagrant ssh

$ wsk action invoke /whisk.system/utils/echo -p message hello --result
#输出结果
{
    "message": "hello"
}

$ wsk package list /whisk.system
#输出结果
packages
/whisk.system/watson-textToSpeech                                      shared
/whisk.system/weather                                                  shared
/whisk.system/watson-translator                                        shared
/whisk.system/combinators                                              shared
/whisk.system/samples                                                  shared
/whisk.system/github                                                   shared
/whisk.system/utils                                                    shared
/whisk.system/websocket                                                shared
/whisk.system/slack                                                    shared
/whisk.system/watson-speechToText                                      shared

外部的机器访问则执行如下命令来验证

$ cd ~/incubator-openwhisk/bin/

#注意参数 -i  --apihost 里指定的地址在部署镜像的时候vagrant脚本中指定的
$ ./wsk -i property set --apihost 192.168.33.13 --namespace guest --auth `cat ~/incubator-openwhisk/ansible/files/auth.guest`

$ ./wsk -i action invoke /whisk.system/utils/echo -p message hello --result
#输出结果 
{ 
"message": "hello" 
}

4.遇到的问题

最大的问题是在编译发布的过程中系统分区被消耗干净,导致编译发布失败。
解决方法如下
1.参照Ubuntu 16.04系统上修改Docker镜像的存储路径介绍的方法调整Docker镜像的存储路径到合适的分区
2.如果使用sudo的方式执行,那么可能会在/root目录下生成虚拟机的镜像文件,同样导致系统分区被消耗,解决方法也是通过软链接的方式把/root目录链接到合适的分区目录

如过执行中遇到如下错误,则增加-i参数即可

$ ./wsk action invoke /whisk.system/utils/echo -p message hello --result
error: Unable to invoke action 'utils/echo': Post https://192.168.33.13/api/v1/namespaces/whisk.system/actions/utils/echo?blocking=true&result=true: x509: cannot validate certificate for 192.168.33.13 because it doesn't contain any IP SANs
Run 'wsk --help' for usage.

参考链接


Ubuntu分析磁盘使用情况

Ubuntu系统上,由于某些原因导致磁盘空间不足,需要分析一下磁盘使用情况,看看是哪个目录或者应用占据了磁盘。

Ubuntu系统默认自带的磁盘分析工具是baobab,如果没有自动安装,可以执行如下命令来手动安装:

$ sudo apt-get install baobab

这个工具的问题在于,如果直接点击启动,会由于权限问题,无法完整的分析磁盘。

因此,需要在命令行下面用root权限去执行:

$ sudo baobab

参考链接


获得ubuntu的版本与Codename

有时我们需要获取ubuntu系统的版本信息,尤其是Codename部分,某些包安装的时候会根据Codename来区分。

打印ubuntu系统的版本信息的命令如下:

$ lsb_release -a

命令输出的结果如下:

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.2 LTS
Release:	16.04
Codename:	xenial

提取Codename部分的字段的命令如下:

$ lsb_release -a 2>&1| grep Codename | awk '{print $2}'

Ubuntu 16.04上查看dd命令的进度信息

Ubuntu 16.04系统上执行如下命令

$ dd if=/dev/zero of=/tmp/zero.img bs=10M count=100000

的时候,可能会非常耗时,这个时候,如果让dd命令输出执行进度信息,会非常有用。

重新打开一个Shell,然后执行如下命令即可每秒输出一次进度信息

$ watch -n 1 pkill -USR1 -x dd

参考链接


Ubuntu 16.04系统上Clang与GCC之间切换

在编译C++代码的时候,我们有时需要比较一下不同编译器之间优化性能的差异,因此需要在ClangGCC之间进行切换,用来比较最后的实际效果。

Ubuntu 16.04系统上使用如下命令进行切换

$ sudo apt-get install clang
$ sudo update-alternatives --config c++

参考链接


Switching between GCC and Clang/LLVM using CMake

Ubuntu 16.04上使用TCMalloc

最近在研究如何提高C++程序的性能,Google开源的TCMalloc,在C++小对象频繁创建销毁的处理上拥有非常大的优势。

Ubuntu 16.04上使用如下命令安装最新的TCMalloc

$ sudo apt-get install google-perftools

对于已经编译好的程序,可以使用如下命令,让程序加载TCMalloc来大致测试一下程序性能的提升

$ LD_PRELOAD="/usr/lib/libtcmalloc.so.4"

参考链接


Ubuntu 16.04上Linux C++程序性能分析工具perf使用入门

目前,perfLinux系统上最全面最方便的一个性能检测工具。由Linux内核携带并且同步更新。

Ubuntu 16.04系统上需要执行如下命令安装:

# 常规内核使用
#sudo apt-get install linux-tools-generic
# 低延时内核使用
#sudo apt-get install linux-tools-lowlatency

# 因此我们根据内核自动选择安装
$ sudo apt-get install linux-tools-`uname -r | cut -d- -f1-2`-`uname -r | cut -d- -f3`

$ sudo apt-get install linux-tools-common

#解决报错"Kernel address maps (/proc/{kallsyms,modules}) were restricted. Check /proc/sys/kernel/kptr_restrict before running 'perf record'".

$ sudo sh -c " echo 0 > /proc/sys/kernel/kptr_restrict"

使用方法如下(gcc编译时最好使用-g参数,生成符号,方便调试):

#生成性能日志文件,默认生成 perf.data
$ sudo perf record -e cpu-clock -g ./hello

#解析性能日志
$ perf report -g -i perf.data

参考链接