把一切脚本化之设计篇

诸多生活和工作中的琐事,都可以用命令行和脚本化来搞定, 我们常用的 Shell, Python, ruby, groovy 甚至 JavaScript 都是很好的胶水代码,以及其他curl,fabric ,scons,以及 scapy 等工具都可以加以利用

在写文档,画图和设计方面也是如此, 列举如下:

用 Markdown 来写文档

这个不多说, 就支持 markdown, 这篇小文也是用 markdown 写的

比如我在学 docker 的时候,打印了一个速查手册,就是用如下命令生成文档的

$ echo "| command| description | example |" > docker_manual.md
$ echo "|---|---|---|" >> docker_manual.md
$ docker help|grep -E "^[ ]+.+[ ]+.+$" | awk '{ a = $1; $1 = ""; print "| " a " | " $0 " |  |"}' >> docker_manual.md ​​​​

马上就生成了如下的表格, 我填了一个常用命令的例子, 一个速查手册就搞定了

command description example
checkpoint Manage checkpoints
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

用 plantuml, websequence 来画图

plantuml

用 plantuml 可以画大多数的 UML 图

  • 它依赖于 graphviz, 所以先安装依赖包
brew install libtool
brew install graphviz
  • 再从 http://plantuml.com 下载 plantuml.jar
  • 然后写一个python 文件封装一下使用方法
    • drawuml.py
import sys,os

def draw_uml(script_file):
    print "draw %s" % script_file
    cmd = "java -jar plantuml.jar %s" % script_file
    print cmd
    os.system(cmd)  


if __name__ == "__main__":
    argc = len(sys.argv)
    if( argc > 1):
        script_file = sys.argv[1]
        draw_uml(script_file)
    else:
        print "Usage: python {0} ".format(sys.argv[0])

    
  • 再写一个 uml 脚本来生成一个活动图
    • activity_example.txt
@startuml
start
:host_start_meeting;
:attendee_join_meeting;
:host_mute_attendee;
:check_attendee_client_screen;

if (Is there "Your audio has been muted"?) then (yes)
  :assert pass;
else (no)
  :assert error;
endif

:host_end_meeting;
stop
@enduml

  • 使用方法
$ python drawuml.py activity_example.txt
draw activity_example.txt
java -jar plantuml.jar activity_example.txt

$ open activity_example.png

把一切脚本化之设计篇_第1张图片
activity example

Websequence

https://www.websequencediagrams.com/ 这个网站几乎搞定了我所有的序列图

把一切脚本化之设计篇_第2张图片
image.png

脚本如下:

title WebRTC and SIP Interaction

participant Browser as browser
participant WebServer as server
participant SIP Proxy as proxy
participant SIP UA as ua

autonumber

browser->server: HTTPS GET
server-->browser: 200 OK
browser->server: WebSocket (SDP offer)
server->proxy: SIP Invite(SDP offer)

proxy->ua: SIP Invite (SDP offer)
ua-->proxy: SIP 200(SDP answer)
proxy-->server: SIP 200 (SDP answer)
server-->browser: WebSocket (SDP answer)
server->proxy: SIP ACK
proxy->ua: SIP ACK
note over browser,ua: ICE, SRTP
ua->proxy:SIP BYE
proxy->server: SIP BYE
server<->browser: WebSocket(close) 
server-->proxy: SIP 200

其它

软件开发中的编译,部署,测试工作几乎都可以通过脚本和命令行完成, 这是不做赘述

  • Make
  • cmake
  • scons
  • maven

部署

  • Ansible
  • Puppet
  • fabric

测试与诊断

  • cUrl
  • Jmeter
  • SIPp

你可能感兴趣的:(把一切脚本化之设计篇)