Month: 1月 2018


最近工作中经常用到的一些组合命令,本来想提交到 上,但是忘记了密码,怎么也登录不上去了,记到这里吧

  • 脚本所在的目录
dirname $0
  • 文件夹下面按照占用空间大小排序
du -sh `ls` | sort -rh
  • 返回上一个目录
cd -
  • 显示所有的日志的最后几行
tail *
  • set
set -x # 显示每个执行的命令
set -e # 当有程序返回非 0 值时直接退出,相当于抛出异常
  • here doc
cat << EOF > /tmp/yourfilehere
These contents will be written to the file.
        This line is indented.
  • 删除包含某个关键字的所有行
fd -t f -0 | xargs -0 sed -i /KeyWord/d
  • 在 shell 中,所有字符串都要用引号包围

Always quote strings in bash. If you string is empty and you are testing it with == or !=, then there will be a “== is not uniary operator” error

  • 替换一个文件夹下的所有文件
fd . -t file -0 | xargs -0 sed -i -e "s/make_redis_client/create_redis_client/g"


搭建 git 服务器(中心仓库)

使用 gitea

手工搭建 git 服务器

首先,添加一个叫做git的用户adduser git

然后如果不存在的话, 为这个用户新建一个主目录mkdir /home/git,然后把这个目录设为git所有chown git ~git

ssh-copy-id [email protected]



  1. 在服务器上新建仓库

首先,在服务器新建一个项目, 其中 new_project 是你的项目的名字.

ssh [email protected] "mkdir <new_project>.git &amp;&amp; cd <new_project>.git &amp;&amp; git init -bare"

当然这么一长串实在是太烦了, 我们可以把它写成一行脚本

ssh [email protected] "mkdir $1\.git &amp;&amp; cd $1\.git &amp;&amp; git init --bare"
echo "New git repo [email protected]:$1.git"

然后 chmod +x
以后就可以这样调用了 ./new foobar 就可以了

  1. 和本地建立连接


然后 clone 到本地就好了:

git clone [email protected]:new_project.git


如果你已经在本地有了项目, 并且初始化了 git 仓库, 不是采用clone,而是直接设置上游服务器,那就推送到服务器上

git remote add origin [email protected]:new_project.git
git push -u origin master


~~建议使用 gitlab 搭建,只需要一个命令就可以了~~
强烈不建议使用 gitlab,实在是太卡了。

docker run --detach \
    --hostname \
    --publish 443:443 --publish 80:80 --publish 22:22 \
    --name gitlab \
    --restart always \
    --volume /srv/gitlab/config:/etc/gitlab \
    --volume /srv/gitlab/logs:/var/log/gitlab \
    --volume /srv/gitlab/data:/var/opt/gitlab \


一些 vim 小技巧



check if mapping is replaced

:verbose map <Key>

using map may cause infinite recursion!

force set syntax

# vim: set filetype=javascript


使用 Ctrl-C 而不是esc

window management

use ctrl-w r to swap pane


{selection}zf or zf{motion} for manual folding


Ctrl-O to go back to files
Ctrl-I to go to new files


delete blank lines


run python from vim

:w !python



不要使用chardet来检测过长的网页,可以检测前一千个字符 chardet.detect(text[:1000])


:%!awk ‘{print $2, $1}’

:set fileencoding=utf8
:w myfilename

add utf-8 BOM

:set bomb # add BOMB

django auth and user


django 自带的 auth 模块需要收先创建数据库才能够使用:

python migrate auth
python migrate

request.user.is_anonymous 检查用户是否是匿名用户

django comes with login/logout forms and views

from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_views
urlpatterns = [
    url(r'^login/$', auth_views.login, name='login'),
    url(r'^logout/$', auth_views.logout, name='logout'),

By default, the django.contrib.auth.views.login view will try to render the registration/login.html template.  and will redirct to the /accouts/profile page

{% extends 'base.html' %}
 {% block title %}Login{% endblock %} 
{% block content %}
<form method="post"> 
{% csrf_token %} 
{{ form.as_p }}
 <button type="submit">Login</button>
{% endblock %}

You can change what django renders

url(r'^login/$', auth_views.login, {'template_name': 'core/login.html'}, name='login'),

You can change where django redirects

LOGIN_REDIRECT_URL = 'home' # in


by default, renders the registration/logged_out.html

url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),

url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),

Note the difference, you don’t have to do anything when visiting /logout, the system will just log you out and send you to another page or render a logged out page

~~Side notes: why changing the redirect url is different with login? find it out later~~


Yifei’s Notes

Tiller SUCKS

The only good part of tiller is that it makes a docker image can read environment variables and generate config files based on predefined templates, this functionality should easily be achieved by python and jinja2


tiller runs program such as nginx inside docker instead of bare-bone nginx. tiller dynamically generates config files for different environment such as dev and QA.

tiller is only useful because your program reads config files only instead of environment variables

you define variables and pass the variables to tiller, tiller generate config files and the start corresponding program.

Before: Docker -> nginx
After: Docker -> tiller -> nginx

you copy the config file template when building image
and run the container with you env vars


We have to pre-define several environments in the image, then we choose which to use when starting the container.
It’s fine to use if we have limited envs, but what if we want to change the environment, we have to repack the image