#287 常见 Web 缓存服务

2019-01-06

Apache,Nginx 都有缓存功能,再加上 Redis 做动态数据的缓存,再加上 CDN,所以我还没有用过专门的缓存服务。
但是这些服务真是大名鼎鼎,即便不用,我也可以先列出来做个简单的了解。

#286 Git Submodule

2019-01-03

命令

usage: git submodule [--quiet] [--cached]
   or: git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
   or: git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
   or: git submodule [--quiet] init [--] [<path>...]
   or: git submodule [--quiet] deinit [-f|--force] (--all| [--] <path>...)
   or: git submodule [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
   or: git submodule [--quiet] set-branch (--default|--branch <branch>) [--] <path>
   or: git submodule [--quiet] set-url [--] <path> <newurl>
   or: git submodule [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
   or: git submodule [--quiet] foreach [--recursive] <command>
   or: git submodule [--quiet] sync [--recursive] [--] [<path>...]
   or: git submodule [--quiet] absorbgitdirs [--] [<path>...]

相关文件

  1. .gitmodules

    [submodule "<moduleName>"]
        path = <moduleDir>
        url = <repoAddr>
    
  2. .git/config 中有相近的 section:

    [submodule "<moduleName>"]
        active = true
        url = <repoAddr>
    
  3. .git 目录在 .git/modules/<moduleName>

克隆

参考:https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules

git clone --recurse-submodules -j8 github.com:shouce/shouce.git # 2.13+
git clone --recursive -j8 github.com:shouce/shouce.git # 1.6.5+

-j 表示子模块并发操作,每次 n 个子模块。

针对更老的版本或者以存在的库:

git clone github.com:shouce/shouce.git
cd shouce
git submodule update --init --recursive

发现一个 clone 参数 --[no-]shallow-submodules,可以使每个子模块仓库的克隆 deepth 为 1,应该是用得上的。

已存在的项目

可能是克隆的时候没有克隆子仓库,也可能是后面添加进来的子仓库。

$ git submodule init
Submodule '<moduleName>' (<repoAddr>) registered for path '<moduleDir>'

$ git submodule update
Cloning into '<moduleFullPath>'...
Submodule path '<moduleDir>': checked out '<commitID>'

或者二合一:

git submodule update --init --recursive

后面有更新就进入子模块 git pull。

添加子模块

git submodule add -b dev --name devtools gitee.com:catroll/devtools tools/dev
# [submodule "devtools"]
#      path = tools/dev
#      url = gitee.com:catroll/devtools
#      branch = dev

删除子模块

$ git submodule deinit <moduleDir>
Cleared directory '<moduleDir>'
Submodule '<moduleName>' (<remoteAddr>) unregistered for path '<moduleDir>'

作用:

  1. 清空子模块目录下的所有文件
  2. 去掉 .git/config 子模块配置

这个操作之后:git status 没有任何变化,.gitmodule 还保留着。

然后:

  1. 修改 .gitmodules
  2. 删除子模块目录
  3. 提交变更
  4. 推送到远程仓库

#284 Python 定时任务的简单部署

2018-12-20

部署

# curl: (35) SSL connect error
# StackOverflow: You are using a very old version of curl.
# yum upgrade curl

# 安装 pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

# 环境变量
PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

# 可用版本列表
pyenv install -l
# 使用国内 Python 镜像,相当于给 pyenv 加速
wget https://mirrors.sohu.com/python/3.6.7/Python-3.6.7.tar.xz -P ~/.pyenv/cache/
# 安装(优先使用缓存目录中的文件,会检查缓存校验码是否正确)
pyenv install 3.6.7

运行方式

假定:

  • 项目路径:/path/to/project/
  • 定时任务命令:python main.py

crontab 配置

早上 01:15 执行某某定时任务:

15 1 * * * /bin/bash /path/to/project/cron.sh

脚本 cron.sh

pyenv + pip 模式

#!/bin/bash

# 环境变量
PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

cd /path/to/project/

# 创建并激活虚拟环境,安装依赖
# $PYENV_ROOT/versions/3.6.7/envs/tasks/
pyenv virtualenv 3.6.7 tasks

if [ $? -ne 0 ]; then
    pyenv activate tasks
    pip install -r requirements.txt
else
    pyenv activate tasks
fi

# 运行脚本
python main.py

pyenv + pipenv 模式

#!/bin/bash

# 环境变量
PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

cd /path/to/project/

# 选择 Python 版本
pyenv local 3.6.7

pip show pipenv > /dev/null

if [ $? -ne 0 ]; then
    # 安装 pipenv
    pip install pipenv
    # 创建虚拟环境,安装依赖
    pipenv install
fi

# 运行脚本
pipenv run python main.py

#279 VirtualBox 虚拟机磁盘扩容

2018-12-13

之前只有一个 50G 存储,后来磁盘空间总不够,就把占空间比较大的 Projects 目录(用户主目录下)移出来,弄了一个独立的 50G 盘。
过了段时间,又隔三差五提醒我空间不够了,baobab 清理了,可以删除的东西一删,多出几 GB 来。
但是看结果,还是扩容一下,一了百了。