Author: jpuyy

  • 今天学习ruby sinatra slim

    ruby语言支持

    sinatra是web支持

    sinatra是通过Application.run!来启动服务器的

    slim是模板支持

    slim引入外部css和js             == stylesheet_link_tag “xxx”

    开发还是在vps上搞,方便

    http://kxh.github.com/Bootstrap_doc_in_chinese/scaffolding.html#layouts

    确定官方的文档有70%以上会了再去其他地方弄,弄来弄去还要去查官方的文档

    一个小入口上要付出很多努力,10次以上的试验

    http://blogs.ejb.cc/archives/2690/first-met-sinatra

  • ansible中的变量

    用ansible也有一段时间了,在使用模板和写playbook的时候变量很重要,整理一些常用的变量。

    #写到hosts文件中的主机名

    inventory_hostname

    ansible自带的setup模块检测出来的变量都可以直接使用,写几个常用的

    #系统发行分支

    ansible_distribution

    # 主机名

    ansible_hostname

    # 所有的cpu

    ansible_processor

    # 硬件型号

    ansible_product_name

    # 系统时间,会是一组不同方式表示的值

    ansible_date_time

    主机的fqdn,即 hostname -f ,得到的方法是

    ansible_fqdn
    python -c "import socket; print socket.gethostname()"
    python -c "import socket; print socket.gethostbyaddr('whatever_we_got_from_last_command')"

    主机名即 hostname

    ansible_nodename
    python -c "import platform; print platform.node()"

    item变量,配合with_items使用

    - name: install rpms
    yum: name={{ item }} disable_gpg_check=yes state=present
    
    with_items:
    - gcc
    - zlib
    

    #eth0的值

     ansible_eth0.ipv4.address
  • nodejs-forever

    node.js的forever可保证进程挂掉之后重启,保证高可用

    Monitors the script specified in the current process or as a daemon

    [Daemon]                                                                                                                                                                     

      The forever process will run as a daemon which will make the target process start

      in the background. This is extremely useful for remote starting simple node.js scripts

      without using nohup. It is recommended to run start with -o -l, & -e.

      ex. forever start -l forever.log -o out.log -e err.log my-daemon.js

    常用动作有

    start Start SCRIPT as a daemon
    stop Stop the daemon SCRIPT
    stopall Stop all running forever scripts
    restart Restart the daemon SCRIPT
    restartall Restart all running forever scripts
    list List all running forever scripts
    config Lists all forever user configuration
    set Sets the specified forever config
    clear Clears the specified forever config
    logs Lists log files for all forever processes
    logs <script|index> Tails the logs for <script|index>
    columns add
    Adds the specified column to the output in `forever list`
    columns rm
    Removed the specified column from the output in `forever list`
    columns set Set all columns for the output in `forever list`
    columns reset Resets all columns to defaults for the output in `forever list`
    cleanlogs [CAREFUL] Deletes all historical forever log files

    例子:

    查看被forever启动的进程

    forever list

    重启具体某一个脚本,如果不知道绝对路径,forever会自动匹配重启

    forever restart myscript.js

    –sourceDir指定js脚本位置

    forever stop --sourceDir=/data/nodejs/ start.js

    -l 指定log所在位置, -e 指定错误日志,-o指console的输出,-a 追加的方式,平时使用时最好把-l, -e, -o全用上。

    forever start -l /var/log/nodejs/log -e /var/log/nodejs/error -o /var/log/nodejs/out.log -a --sourceDir=/data/nodejs start.js
  • rsyslog以及logger命令

    配置 rsyslog

    cat /etc/rsyslog.d/custom_logging.conf

    # 定义模板
    $template CUSTOM_LOGS,"/var/log/%programname%.log"
    if ($programname == 'my_custom_app') then ?CUSTOM_LOGS
    # 匹配到后停止后续匹配
    &~
    
    if ($programname startswith 'm') then ?CUSTOM_LOGS
    &~
    

    使用 logger 测试上面的配置

    logger -t my_custom_app "==================>my_custom_app" 会记录到 /var/log/my_custom_app.log 
    logger -t my_custom_app_1 "==================>my_custom_app_1" 会记录到 /var/log/my_custom_app_1.log
    

    logger命令是syslog的shell接口

    测试上面的配置

    把文件记录到syslog里

    logger -f filename

    后面直接跟要记录的文本

    logger jpuyy.com

    可以看到这样一条记录

    Sep 13 16:55:51 iZ28bz7jrdyZ root: jpuyy.com

    参考:

    http://help.papertrailapp.com/discussions/questions/96-how-to-log-a-message-from-the-linux-command-line.html

  • python encode decode编码

    utf8编码的涵义

    UTF-8 is one of the most commonly used encodings. UTF stands for “Unicode Transformation Format”, and the ‘8’ means that 8-bit numbers are used in the encoding.

    早在1968年,ASCII代码发表了,代表了0到127的字母数字,但仍表示不了其他国家的字母,1980s之后,处理器变为8-bit,变为了0-255,后来为为了16-bit,说明2^16 = 65,536。之后utf-8出现了。

    可以用type或isinstance来判断变量是什么类型

    >>> s = '杨'
    >>> type(s)
    <type 'str'>
    >>> isinstance(s, str)
    True
    >>> isinstance(s, unicode)
    False
    

    如果前面加一个u符号指定用unicode编码

    >>> a = u'杨'
    >>> type(a)
    <type 'unicode'>
    >>> isinstance(a, str)
    False
    >>> isinstance(a, unicode)
    True
    

    python encode unicode编码

    >>> str = "上海"
     >>> print str
     上海
     >>> print data.encode("unicode_escape")
     \\u4ea4\\u6362\\u673a
     >>> print data.encode("raw_unicode_escape")
     \u4ea4\u6362\u673a

    python decode unicode编码

    >>> data = "\u4ea4\u6362\u673a"
    >>> type(data)
    <type 'str'>
     >>> print data.decode('unicode_escape')
     交换机
     当字符串本身有\时,使用
     >>> print data.decode('raw_unicode_escape')
     交换机

    参考文档:

    https://docs.python.org/2/howto/unicode.html

  • python str字符串处理

    几个实际的例子

    string = “abc”

    字符串的长度
    len(string)

    转换为小写
    string.lower()

    转换为大写
    string.upper()

    统计字符串里一个字母a出现的次数

    string.count(‘a’)

    查找子串在一个字符串出现的位置,从0开始算,如果没找到返回-1

    >>> string = ‘i love you’
    >>> string.find(‘love’)
    2

    转换int to str

    >>> a = 2

    >>> type(a)

    <type ‘int’>

    >>> str(a)

    ‘2’

    将字符串中的替换

    .replace(‘:’,’’)

    sum([bin(int(x)).count(‘1’) for x in ‘255.255.255.224’.split(‘.’)])

    strip和split

    strip() #removes from both ends

    lstrip() #removes leading characters (Left-strip)

    rstrip() #removes trailing characters (Right-strip)

    >>> s = ‘ i love you ‘
    >>> print s.strip()
    i love you
    >>> print s.lstrip()
    i love you
    >>> print s.rstrip()
    i love you
    >>> help(” abc”.strip)
    >>> help(“abc”.split)