Skip to content

Instantly share code, notes, and snippets.

View hackerain's full-sized avatar
😀
I may be slow to respond.

Guangyu Suo hackerain

😀
I may be slow to respond.
View GitHub Profile
@hackerain
hackerain / jenkins.sh
Last active December 1, 2016 17:00
jenkins deploy hexo blog
if [ -d hackerain.me ]; then
cd hackerain.me
git pull && git submodule foreach git pull origin master
if [ ! -d node_modules ]; then
npm install --save;
fi
else
git clone git@git.oschina.net:yugsuo/hackerain.me.git --recursive
cd hackerain.me
npm install --save
@hackerain
hackerain / jenkins_dockerfile
Last active August 25, 2018 10:39
jenkins dockerfile integerated hexo and security key
FROM jenkins:latest
USER root
RUN apt-get update
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get install -yq nodejs build-essential
RUN npm install -g hexo-cli
RUN echo "Host *\n\tStrictHostKeyChecking no\n" >> /etc/ssh/ssh_config
@hackerain
hackerain / gist:620f5f71cc25f642b631
Last active August 29, 2015 14:01
控制程序的执行时间
from __future__ import with_statement # Required in 2.5
import signal
from contextlib import contextmanager
class TimeoutException(Exception): pass
@contextmanager
def time_limit(seconds):
def signal_handler(signum, frame):
raise TimeoutException, "Timed out!"
@hackerain
hackerain / gist:fe59367ed6d8b61bd066
Last active August 29, 2015 14:01
快速获得某一个类所在的Module
>>> '.'.join('suo.piao.xxx'.split('.')[:-1])
>>> 'suo.piao'
先按“点”分隔开,得到list,舍去最后一个,再用join将这个list连接起来,用到类中,就是这样的:
>>> '.'.join(self.__module__.split('.')[:-1])
@hackerain
hackerain / gist:6b12f383ab5bc8bbfb97
Created May 15, 2014 13:45
python中的单例模式
class HorizonSite(Site):
_instance = None # 静态私有变量是关键
def __new__(cls, *args, **kwargs):
if not _instance:
cls._instance = super(Site).__new__(cls, *args, **kwargs)
return cls._instance
@hackerain
hackerain / gist:11390974
Last active August 29, 2015 14:00
在某一个计算节点上同时创建多台虚拟机
通过availability_zone参数可以将虚拟机强制调度到指定的计算节点,但是默认的该参数只能由admin使用,若要普通用户使用,在nova的policy.json中,将"compute:create:forced_host": "is_admin:True",这条规则改为"compute:create:forced_host": ""
命令行:
nova boot --image c796de54-25f8-405e-afdd-e03003a9e62a --nic net-id=ba05e057-1eb8-4cb2-af58-9b99a72df5ed --flavor 2 --availability_zone nova:server-76 vm
API:
@hackerain
hackerain / gist:8970225
Created February 13, 2014 05:29
kill screen session
SCREEN_NAME=${SCREEN_NAME:-gring}
SCREEN=$(which screen)
if [[ -n "$SCREEN" ]]; then
SESSION=$(screen -ls | awk '/[0-9].'$SCREEN_NAME'/ { print $1 }')
if [[ -n "$SESSION" ]]; then
screen -X -S $SESSION quit
fi
fi
@hackerain
hackerain / gist:8970215
Created February 13, 2014 05:28
Run services in screen session's multiple windows
SCREEN_NAME=${SCREEN_NAME:-gring}
USE_SCREEN=${USE_SCREEN:-True}
CONFIG_FILE=${CONFIG_FILE:-/etc/gringotts/gringotts.conf}
function screen_it {
echo "staring $1"
if [[ "$USE_SCREEN" = "True" ]]; then
screen -S $SCREEN_NAME -X screen -t $1
@hackerain
hackerain / gist:7780531
Created December 4, 2013 00:57
wsme.types.File example
import wsme
from pecan import rest
from wsmeext.pecan import wsexpose
class RootController(rest.RestController):
@wsexpose(wsme.types.File)
def get(self):
@hackerain
hackerain / gist:7544209
Last active December 28, 2015 18:39
python sched 定时循环任务
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sched
import sys,time
# action function, and add another event to queue.
def event(action_time):
print "action_time:%s" % (action_time)
scheduler.enterabs(action_time + 5, 1, event, (action_time + 5,))