Month: 一月 2018

命令行的一些小技巧

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

* 脚本所在的目录

“`
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.
EOF
“`

* 删除包含某个关键字的所有行

“`
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’
“`
from: https://stackoverflow.com/questions/6758963/find-and-replace-with-sed-in-directory-and-sub-directories

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

使用 gitea

手工搭建 git 服务器

假设服务器的名字是 git.example.com.
首先,添加一个叫做git的用户adduser git

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

再在本地把你的ssh公钥拷贝到服务器上
ssh-copy-id git@git.example.com

服务器就这样搞好了

使用

  1. 在服务器上新建仓库

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

ssh git@git.example.com "mkdir <new_project>.git && cd <new_project>.git && git init -bare"

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

#!/bin/bash
ssh git@git.example "mkdir $1\.git && cd $1\.git && git init --bare"
echo "New git repo git@git.example.com:$1.git"

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

  1. 和本地建立连接

如果是新项目

然后 clone 到本地就好了:

git clone git@git.example.com:new_project.git

如果是老项目

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

git remote add origin git@git.example.com:new_project.git
git push -u origin master

gitlab

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

docker run --detach \
    --hostname gitlab.example.com \
    --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 \
    gitlab/gitlab-ce:latest

source: https://docs.gitlab.com/omnibus/docker/README.html

一些 vim 小技巧

### 重新对齐文本到固定长度

“`
gq
“`

### check if mapping is replaced

`:verbose map `

using map may cause infinite recursion!

### force set syntax

`# vim: set filetype=javascript`

### exit

使用 Ctrl-C 而不是esc
使用ZZ而不是:wq

### window management

use ctrl-w r to swap pane

### folding

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

### history

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

:%s/pattern//gn

### delete blank lines

:g/^$/d

### run python from vim

:w !python

### nerdtree的使用

使用i和s分别在split中打开文件

不要使用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 manage.py migrate auth
python manage.py 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’),
url(r’^admin/’, admin.site.urls),
]
“`

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 %}

Login

{% csrf_token %}
{{ form.as_p }}

{% 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 settings.py`

logout

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~~

tiller for docker

# 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

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

# Downsides

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

读《数学之美》

1. 文字只是信息的载体,而非信息本身
2. 翻译之所以能够进行,是因为人类的语言本质上都是等价的
3. 信息的冗余是信息安全的保障
4. 自然语言是上下文相关文法,而人工语言往往是上下文无关文法。