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 / Round up the value to 50 integer time
Last active December 17, 2015 00:19
Round up the value to 50 integer time
a = (a+50-int(a)%50);
For example:
before after
a=1 --> a=50
a=49 --> a=50
a=51 --> a=100
@hackerain
hackerain / C语言产生随机数
Created May 5, 2013 09:54
C语言产生随机数
C语言产生随机数需要调用stdlib.h头文件中的两个函数:
int rand(void): 产生一个0到RAND_MAX之间的随机整数。(RAND_MAX定义在stdlib.h, 其值为2147483647)
void srand(int seed): 用于初始化种子,便于每次产生不同的随机数。
/*
如果要产生其他范围内的整数,可以使用取余运算实现。以下代码为产生0~100之间的随机数:
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@hackerain
hackerain / gist:5525906
Last active December 17, 2015 01:09
顺序分配
public void bindCloudletsToVmsSimple(){
int vmNum = vmList.size();
int cloudletNum = cloudletList.size();
int idx = 0;
for (int i=0;i<cloudletNum;i++){
cloudletList.get(i).setVmId(vmList.get(idx).getId());
idx = (idx+1)%vmNum;
}
suo@ustack:~/devstack$ curl -H "content-type: application/json" -H "x-auth-token:$ADMIN" http://localhost:35357/v3/credentials -d '{"credential": {"user_id": "91f2f916a5714101866ae325fd6d5aec", "project_id" : "473c507eb7e645fda4d7dabb6c7eac03", "blob": {"AccessKey": "AKIAJ5AAFSPWGWH25P3A", "SecertKey": "PzJGRqDLN8uET9ZzhQZ4wy5gTIGFxE1S3FhghwS8"}, "type": "AccessKey"}}' | ./jsontool.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 611 100 372 100 239 6223 3998 --:--:-- --:--:-- --:--:-- 6200
{
"credential": {
"user_id": "91f2f916a5714101866ae325fd6d5aec",
"links": {
"self": "http://localhost:5000/v3/credentials/c04f153f34134fb68699c9efb70c50ec"
},
@hackerain
hackerain / .tmux.conf
Last active December 25, 2015 06:39
my localhost tmux.conf
#set -g prefix C-x
#unbind C-b
#bind-key C-x send-prefix
set-option -g status-keys vi
set-window-option -g mode-keys vi
setw -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
@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,))
@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: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: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: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: