2015年12月29日 星期二

ORM解釋

本文內容主要引用(http://tw.gigacircle.com/2703563-1)

物件關聯對映(Object Relational Mapping,簡稱ORM) 是一種用於實現從物件資料到關聯資料的存儲對映的程式設計技術,將資料庫操作用物件導向的形式來呈現。

ORM支援基本的SQL查詢及附加子句設定,可進行各種關聯式查詢,透過物件間的關聯設定,達成一對一、一對多、多對多的關聯查詢,使查詢更具彈性及明確性。在設定關聯對映時有各種方式,主要須視程式語言類型進行設計,以編譯式程式語言Java的Hibenate為例,Hibenate以XML格式作為配置文件,定義各種物件之間的關聯等資訊。各種 ORM 的技術都有共同的目的,就是能把物件的狀態存到關聯式資料庫。

ORM可以操作和控制數據庫數據的PHP 對象。一旦你定義了關系 ORM 它會允許你從數據庫中讀取任何數據,以你喜歡的方式來控制數據,並把結果保存到數據中而中間不再使用 SQL 語句。許多人都知道ORM的優點來自於隱藏SQL語句及物件呈現上,也可以說是ORM自動產生SQL指令,而因自動產生技術的成熟度及對該資料庫的了解度,因此ORM在某些地方效率低落是必然的。所以,高效率並不是ORM所標榜的優點,維持一定程度效率才是,且自動產生的機制是可以被持續優化的。

ORM多支援Stored Procedure的對應,用這種方式來進行批次更新,是ORM的既定標準手法。採用ORM的優點除了「跨資料庫」、「自動產生SQL」外能夠以一致的方法,把複雜的物件設計對應到關聯式的世界我想才是它的重點。若是拿直接存取 Database與採用 ORM 技術這兩者來做比較,ORM 因為多卡了一層一定會比較慢一些些,但是採用 ORM 技術有很多機會可以調校(Tuning),例如資料快取(Caching)、定義查詢計畫(Query Plan)等,且最重要的是數據庫類的查詢器中的大多數方法都可以在 ORM 模型中使用。

ORM其目的是企圖幫助開發者從物件導向程式開發的角度,單純地思考類別之間的繼承及參照關係,而不需要知道實體的資料是如何分散在不同的資料表,以及物件背後對應哪些資料表,甚至SQL指令的組成,也交由平臺處理。換句話說ORM的目的在於減少SQL指令跟主程式之間的藕合性,採用集中管理或是代理操作機制來讓程式碼與關聯式資料庫間溝通,開發者只要透過原來在使用的開發語言,不必接觸到實際的SQL,就能夠完成增、刪、改、查等跟資料庫之間的操作,不但減少了開發的時間,也降低的以後維護的成本。

歸納後ORM有下列之優點:

  1. 以物件表現列、以屬性表現欄,使用物件導向程式設計的概念來操作資料庫。
  2. 隱藏SQL語法,以達到跨不同的資料庫系統時,不需修改應用程式。
  3. 使用相同的語法,操作各種資料庫系統,減輕開發人員的學習負擔。
  4. 自動產生SQL語法,避免因程式設計師能力不同,寫出效率不彰的應用程式。
  5. 自動產生SQL語法,避免程式設計師對安全性的認知程度不同,寫出不安全的應用程式。



本研究中的框架所提供的ORM,除了具備上述之SQL查詢以外,還提供ORM的延伸應用可針對其他資料儲存型態進行管理,如XML、JSON,可作為其他資料交換等用途。

2015年12月8日 星期二

docker設定 - 暫存資料

docker setting

path : /etc/default/docker"


conf

# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
DOCKER_OPTS="-D --tls=false -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock"
DOCKER_OPTS="$DOCKER_OPTS --insecure-registry=192.168.2.18:5000"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export TMPDIR="/mnt/bigdrive/docker-tmp"



insecure-registry

2015年11月9日 星期一

redis環境設定

ubuntu
====================================

/etc/redis/redis.conf


指定redis來至該IP請求,不設定將接受所有請求
> bind 127.0.0.1

參考網址:

  1. http://blog.csdn.net/ithomer/article/details/9232891(設定檔文件說明)
  2. http://blog.maxkit.com.tw/2015/02/redis.html


---------------------------------------------------------------


2015年10月28日 星期三

暫存supervisor

[unix:http_server]
file=/tmp/appt.sock
chmod=0777
chown=root:golang

[inet_http_server]
port=9900

[program:app]
command=/var/www/golang/src/src
autostart=true
autorestart=true
startretries=10
chown=root:golang
directory=/var/www/golang/src
environment=APP_SETTINGS="/srv/www/yourapp.com/prod.toml"
redirect_stderr=true
stdout_logfile=/var/log/supervisor/golang.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

2015年10月5日 星期一

chroom cache

http://www.howtogeek.com/197804/how-to-clear-the-google-chrome-dns-cache-on-windows/


chrome://net-internals/#dns

2015年10月2日 星期五

golang暫存資料

//重新安裝golang
brew install go --with-cc-all

//編譯檔案於哪個OS下使用
//linux
GOOS=linux     GOARCH=amd64 go build
//mac
GOOS=darwin    GOARCH=amd64 go build

//
http://stackoverflow.com/questions/31586320/go-build-runtime-darwin-amd64-must-be-bootstrapped-using-make-bash

http://stackoverflow.com/questions/12168873/cross-compile-go-on-osx
http://stackoverflow.com/questions/27412601/cross-compiling-go
http://stackoverflow.com/questions/25051623/golang-compile-for-all-platforms-in-windows-7-32-bit
http://www.goinggo.net/2013/10/cross-compile-your-go-programs.html


//編譯檔案OS
https://github.com/mitchellh/gox

2015年9月2日 星期三

swagger安裝

安裝教學:

首先要安裝node


$ git clone https://github.com/swagger-api/swagger-editor.git
$ cd swagger-editor

Start on Mac and Linux:
$ npm start

Start on Windows:
$ npm install -g bower grunt-cli
$ npm install
$ bower install
$ grunt serve


參考網址:

  1. 連結(http://pythonhackers.com/p/sethathi/swagger-editor)


2015年8月31日 星期一

mount將伺服器檔案掛載至本機

Mac
====================================
首先安裝連結(http://osxfuse.github.io/)此網站檔案。
首先先安裝OSXFUSE 與 SSHFS,從前向後安裝。

$ mkdir local_folder

//掛載
$ sshfs NAME@HOST:/path/to/folder local_folder

//卸載
$ umount remote_disk



其他OS安裝方式

  1. http://blogger.gtwang.org/2014/05/sshfs-ssh-linux-windows-mac-os-x.html

2015年8月26日 星期三

server install oh-my-zsh

本文內容引用此網址:https://gist.github.com/tsabat/1498393


$ apt-get install -y zsh
$ apt-get install -y git-core
$ wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh
$ chsh -s `which zsh`
$ sudo shutdown -r 0

2015年8月25日 星期二

git issue

#remote: Permission to w19900227/go_study.git denied to [other user].
fatal: unable to access 'https://github.com/w19900227/go_study/': The requested URL returned error: 403

解決方式:
git remote set-url origin https://w19900227@github.com/w19900227/go_study.git





2015年8月2日 星期日

tcp/ip筆記

網路線

腳位 #1 #2 #3 #4 #5 #6 #7 #8
EIA/TIA 568A 白綠白橙白藍白棕
EIA/TIA 568B 白橙白綠白藍白棕
---------------------------------------------------------------

switch(交換器) 資料連階層、實體層
router(路由器) 網路層、資料連階層、實體層
hub(集線器) 實體層


router(路由器) 可以將不同實體IP做串聯
bridge(橋接器) 不可以將不同實體IP做串聯


straight-through 兩端同時是568A或是568B網路線,用於「電腦<->hub」、「電腦<->switch」
crossover 一端使用568A另一端使用568B,用於「電腦<->電腦」、「電腦<->hub」、「switch<->switch」
---------------------------------------------------------------

IP位址:
分為二位、四位分群
二位
10001100
10001100
11001100
11001100

四位
10001100
10001100
10101100
10101100


#IP class位址說明(classful internet address)
---------------------------------------------------------------

#IP協定特性:
  1. 非連線的傳輸協定(Connectionless)
  2. 非同步通訊(Asynchronous Communication)
  3. 不可靠的傳輸協定(Unreliable)
  4. 較有效率的傳輸協定(Efficient)
---------------------------------------------------------------
#ARP位址解析協定(Address Resolution Protocol):
IP address轉換成Mac address是透過ARP(Address Resolution Protocol)協定,Mac address轉換成IP address則是透過RARP(Reverse Address Resolution Protocol)
只適用於下面兩層,ARP只適用於區域網路(LAN)、無法越過第三層的網路層。或是路由器與路由器間的廣域網路(WAN)之間。

Operation Code 封包操作類型
1 ARP Request
2 ARP Reply
3 RARP Request
4 RARP Reply
來源主機發出「ARP Request」封包
目的主機回應「ARP Reply]封包

舉例流程:
案例:HostA > HostB
流程1:來源電腦(HostA) > DNS > ARP cache > 廣播詢問目標電腦  > 目標電腦回應(HostB),進行雙向通訊(ARP Reply)
流程2:來源電腦(HostA) > DNS > ARP cache > 廣播詢問目標電腦  > 丟棄封包

#網路剪刀手原理就是ARP弱點攻擊
#在區域網路內,真正用來定址傳送的是利用MAC位址。
#ARP主要是透過廣播方式

查詢ARP cache
$ arp -a

新增靜態型態進ARP cache
$ arp -s [IP Address] [Mac address]

刪除ARP cache
$ arp -d [IP address] [Mac address]


---------------------------------------------------------------
#ICMP網際網路控制訊息協定(Internet Control Message Protocol)
ICMP是TCP/IP網路中傳遞一些基本訊息的協定,驗證部分較為薄弱。
主要目的在於輔助IP協定的不足,回傳「錯誤回報」、「偵測錯誤」

查詢
type ICMP訊息類型說明
0 Echo Relpy(回應應答)
8 Echo Request(回應要求)
9 Router Advertisement(路由器回應)
10 Router Solicitation(路由器要求)
13 Timestamp Request(時戳要求)
14 Timestamp Reply(時戳應答)
17 Address Mask Request(位址遮罩要求)
18 Address Mask Reply(位址遮罩應答)
錯誤回報
type ICMP訊息類型說明
3 Destination Unreachable(目的無法到達)
4 Source Quench(抑制來源)
5 Redirect(重新導向)
11 Time Exceeded for a Datagram(時間逾時)
12 Parameter Problem on a Datagram(參數錯誤)

舉例流程:
案例:HostA > HostB
流程1:來源電腦 ICMP Request(HostA) > ARP Request(HostA) > DNS > ARP cache > 廣播詢問目標電腦  > 目標電腦回應(HostB),進行雙向通訊(ARP Reply) > > ICMP Reply(HostB)

#阻絕服務攻擊DoS原理就是ICMP弱點攻擊

#封包追蹤路徑
windows:
tracert [IP address]
linux:
traceroute -P ICMP [IP address]

補充參考連結:

  1. http://idobest.pixnet.net/blog/post/22040166-%5B%E8%BD%89%E8%B2%BC%5D-traceroute-%E7%9A%84%E5%8A%9F%E8%83%BD%E4%BB%8B%E7%B4%B9


---------------------------------------------------------------
#路由協定(Routing Protocol)
主要目的在於幫助來源主機找到目的主機所經過的路徑。
常使用兩種動態路由協定RIP(Routing Information Protocol)、OSPF(Open Shortest Path First)。
距離向量路徑選擇DV Routing(Distance Vector Routing),主要是計算兩者之間距離(稱躍點數),並依據最近躍點數最少為原則進行

外部閘道有EGP:EGP、BGP
內部閘道有IGP:RIP、OSPF

//查看路由
$ ip router

//新增路由
$ ip router add [network destination] via [geteway] metric [metric] dev [interface#]

//修改路由
$ ip router change [network destination] via [geteway] metric [metric] dev [interface#]

//刪除路由
$ ip router del [network destination]
---------------------------------------------------------------
#UTP(User Datagram Protocol)
 著重兩端伺服器之間的溝通協定,忽略中介節點的路由無關,透過「路由協定」尋找最佳路徑。
舉例流程:
案例:HostA > HostB 流程1:來源電腦(HostA) > DNS > ARP cache > 廣播詢問目標電腦 > 目標電腦回應(HostB),進行雙向通訊(ARP Reply) > SNMP request > SNMP response

服務名稱 PORT 傳輸協定
DNS(Domain Name System) 53 UDP
SNMP(Simple Network Management Protocol) 161 UDP
SNMP Trap 162 UDP
RIP(Routing Information Protocol) 520 UDP
FTP data(File Transfer Protocol) 20 TCP
FTP Control(File Transfer Protocol) 21 TCP
SSH(Secure Shell ) 22 TCP
Telnet(Telecommunication network) 23 TCP
SMTP(Simple Mail Transfer Protocol) 25 TCP
HTTP(HyperText Transfer Protocol) 80 TCP
POP3(Post Office Protocol - Version 3) 110 TCP

---------------------------------------------------------------

#TCP(Transmission Control Protocol)
主要在屬於傳輸層
TCP是一個完整的協定,他會經過「建立連線」、「資料傳輸」、「關閉連線」也就是「三向交握」(Three-way handshake)
TCP連線可跳脫「網路層、資料連結層、實體層」,直接進行兩端「傳輸層」連線
TCP是一條全雙工(full-duplex),主要以「正確性」為主的協定
TCP是同步傳輸、可靠的傳輸協定、較無效率傳輸協定、流量控制
TCP對資料處理方式以byte為一個最小基本單位,傳送端要透過TCP傳輸之前,必須先進行編碼成一個一個位元組(byte),這些位元組所組成的集合,稱為「位元組串流」(Byte stream),TCP會將產生的「位元組串流」放入暫存區,稱為「輸出緩衝區」(output buffer)

---------------------------------------------------------------
#IPv6
IPv4傳播模式有,單點傳播(unicast)、群播(multicast)、廣播(broadcast),IPv6傳播方式中,將廣播列為群播的一個特例,所以少了一個「廣播」、多了一個「任意傳播」(Anycast)

---------------------------------------------------------------
#動態主機組態協定DHCP(Dynamic Host Configuration Protocol)

完整網域名稱FQDN(Fully Qualified Domain Name),與IP位置一樣採用「點狀表示法」
網域名稱和作業系統的檔案一樣,分為「絕對路徑」、「相對路徑」
網路資訊中心NIC(Network Information Center)

網址位址轉換NAT(Network Address Translation)標準的訂定,即是處理IP位址短缺現象,也就是由內部網路使用「私人IP位址」再透過NAT伺服器的IP轉換機制,轉換成外部網路的「公開IP位址」
1.網址位址轉換NAT(Network Address Translation):此種轉換方式只負責轉換IP位址
2.NAPT(Network Address Port Translation):此種轉換機制不僅轉換IP位址,也轉換連接port

windows DNS清除方式
#清除
$ ipconfig/flushdns
#查詢cache
$ ipconfig/displaydns
---------------------------------------------------------------
#網路管理與網路安全:
管理範圍有錯誤管理、組態管理、效能管理、帳務管理、安全管理
安全管理有對稱金鑰系統、非對稱金鑰系統、雜湊函數、數位憑證、電子簽章
資訊安全基本項目:資料保密性或隱私性(Data Confidentiality)、資料完整性或正確性(Data Integrity)、鑑別性或認證性(Authentication)、不可否認性(Non-Repudiation)、存取控制(Access Control)、可用性(Availability),通常要達到以上基本項目,都要使用密碼學的技術
密碼學應用基本項目:
1. 演算法(Algorithm):
分為加密演算法(Encryption Algorithm)與解密演算法(Decryption Algorithm)
2. 本文(Text):
分為可閱讀性本文(Readable Text)與不可閱讀性本文(Unreadable Text)
3. 金鑰(key):
分為加密金鑰與解密金鑰

傳統密碼學基本技巧:置換(Transposition)、取代(Substitution)
近代密碼學基本技巧:串流加密法(Stream cipher)、區塊加密法(block cipher)
串流加密主要是處理「影音加密、電話語音加密、無線網路傳輸加密」,並以位元組(Byte)為基本的單位做傳送,所以針對位元組流(Byte Stream)進行加密的方式稱為(串流加密法),主要利用XOR基本運算技術加密
區塊加密法主要針對「電子資料」,將資料切個成大小相同的區塊,再依據不同演算法進行加密,稱為區塊加密法
對稱式金鑰加密

非對稱式金鑰加密(Asymmetric Key Encryption System):對稱式金鑰加密系統,加、解密雙方是共同擁有一把相同的密鑰。公鑰加密必須用私鑰解密、私鑰加密必須用公鑰解密。但是由私鑰加密後傳送出去保密性較低

數位信封(Digital Envelope):將自己產生的「密鑰」加密後的資訊,稱為數位信封,當對方收到「數位信封」後,可以使用自己的「私鑰」將「數位信封」解密,取得內藏的密鑰。
數位憑證(Digital Certificate):主要將申請憑證的擁有者資訊,稱之為「主體識別資訊」(Subject Identification Information),當接受到此憑證的人,亦可取得憑證授權中心的公鑰,進行該憑證身份鑑別,保證憑證內的「公鑰」確實是該主機所擁有。
SSL(Secure Sockets Layer):目前網際網路最普遍被使用的一種安全協定,SSL協定是基於TCP/IP協定組,並位於「傳輸層」之上、「應用層」之下的一種協定,
SSL流程:
1. clinet送出ClientHello訊息給server,表示要求連線
2. server回應ServerHello訊息給client,表示收到要求連線
3. server主動將「數位憑證」傳送給client,此動作主要目的是傳送「數位憑證」中的「公鑰」給client
4. client接收到server「數位憑證」後,從該「數位憑證」取得的伺服器的「公鑰」再利用此「公鑰」自己隨機產生「會議金鑰」加密,形成「數位信封」傳送給伺服器,伺服器可以使用自己的「私鑰」將「數位信封」解密取得「會議金鑰」。之後彼此雙方皆透過「會議金鑰」對傳輸的資料進行加/解密。

---------------------------------------------------------------


專有名詞
  1. 最大傳輸單位(Maximum Transmission Unit MTU)
  2. ISO OSI七層:實體層(Physical)、資料連接層(Data link)、網路層(Network)、傳輸層(Transport)、會議層(Session)、表現層(Presentation)、應用層(Application)
  3. 位移量(Offset)
  4. 來源位址(Source Address)、目的位址(Destination Address)
  5. 位址解析協定ARP(Address Resolution Protocol)、反向位址解析協定RARP(Reverse Address Resolution Protocol)
  6. 網路介面卡NIC(Network Interface Card)
  7. 網域名稱系統DNS(Domain Name System)
  8. 廣播(broadcast)
  9. ARP欺騙攻擊(Address Resolution Protocol Spoofing Attack、ARP Poisoning、ARP Poisoning Routing)
  10. 網際網路協定(Internet Protocol)
  11. 網際網路控制訊息協定ICMP(Internet Control Message Protocol)
  12. 中介節點(intermediate node)、終端節點(end node)
  13. TTL(time to live)
  14. 阻絕服務攻擊DoS(Denial of Service)、分散式阻絕服務攻擊DDoS(Distributed Denial of Service)
  15. RIP(Routing Information Protocol)、OSPF(Open Shortest Path First)
  16. 內部閘道協定IGP(Interior Gateway Protocol)、外部閘道協定(Exterior Gateway Protocol)、EIGRP(Enhanced Interior Gateway Routing Protocol)、BGP(Border Gateway Protocol)
  17. TCP(Transmission Control Protocol)
  18. 全雙工(full-duplex)、半雙工(half-duplex)、多工處理(multi-tasking)
  19. 三向交握(Three-way handshake)
  20. 同步傳輸(Synchronous Transmission)、可靠的傳輸協定(Reliable)、較無效率傳輸協定(Inefficient)、流量控制(Flow Control)
  21. 延遲時間RTT(Round Trip Time)、逾時(timeout)
  22. 輸出緩衝區(Output Buffer)、輸入緩衝區(Input Buffer)
  23. 傳輸控制區塊TCB(Transmission Control Block)
  24. 網際網路地址號碼資源分配管理組織NRO(The Number Resource Organization)
  25. 負載平衡(load balancing)


  26. 簡易網路管理協定SNMP(Simple Network Management Protocol)、簡易閘道監督協定SGMP(Simple Gateway Monitoring Protocol)
  27. 錯誤管理(Fault Management)、組態管理(Configuration Management)、效能管理(Performance Management)、帳務管理(Accounting Management)、安全管理(Security Management)
  28. 網路管理站台NMS(Network Management Station)、管理資訊庫MIB(Management Information Base)
  29. 協定資料單元PDU(Protocol Data Unit)
  30. 資料保密性或隱私性(Data Confidentiality)、資料完整性或正確性(Data Integrity)、鑑別性或認證性(Authentication)、不可否認性(Non-Repudiation)、存取控制(Access Control)、可用性(Availability)
  31. 授權(Authorization)
  32. 加密(Encryption)
  33. 距離向量路徑選擇DV Routing(Distance Vector Routing)
  34. 控制連線(control connection)、資料連線(data connection)
  35. 動態主機組態協定DHCP(Dynamic Host Configuration Protocol)
  36. 網路資訊中心NIC(Network Information Center)
  37. 輪詢RR(Round-Robin)
  38. 網址位址轉換NAT(Network Address Translation)、NAPT(Network Address Port Translation)
  39. 數位信封(Digital Envelope)、數位憑證(Digital Certificate)
  40. 憑證授權中心CA(Certificate Authority)
  41. SSL(Secure Sockets Layer)

Protocol值 名稱 說明
1 ICMP Internet Control Message Protocol
2 IGMP Internet Group Management Protocol
3 GGP Gateway-Gateway Protocol
6 TCP Transmission Control Protocol
8 EGP Exterior Gateway Protocol
9 IGP Interior Gateway Protocol
17 UDP User Datagram Protocol
41 IPv6 Internet Protocol version 6 over IPv4
47 GRE Generic Routing Encapsulation
50 ESP Encapsulating Security Payload
51 AH Authentication Header
58 ICMPv6 Internet Control Message Protocol for IPv6
88 EIGRP Enhanced Interior Gateway Routing Protocol
89 OSPF Open Shortest Path First


推薦書籍
  1. TCP/IP網路通訊協定-TCP/IP Protocol Suite(Second Editor) 陳祥輝



redis-安裝

Ubuntu
====================================
//查版號
$ redis-server --version

$ apt-get update
$ apt-get upgrade
$ apt-get install -y redis-server

$ cp /etc/redis/redis.conf /etc/redis/redis.conf.default

$ redis-cli

參考網址:

  1. https://www.linode.com/docs/databases/redis/redis-on-ubuntu-12-04-precise-pangolin



====================================
====================================
Mac
====================================
//查版號
$ redis-server --version

//安裝
$ brew install redis

//解除安裝
$ brew uninstall redis


----------------------------------------------------------
安裝教學:

This is my setup of redis with OS X 10.9.2 Mavericks and requires brew.
$ brew install redis

Start redis when computer starts:
$ ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents

Start redis:
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Without using launchctl:
$ redis-server /usr/local/etc/redis.conf

Check is redis is correctly running:
$ redis-cli ping
This should reply with PONG if successful.

Configuration 
The configuration file is located in /usr/local/etc/redis.conf.

Uninstall redis:
$ brew uninstall redis; rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
$ rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

參考網址:

  1. http://www.richardsumilang.com/blog/2014/04/04/how-to-install-redis-on-os-x/
  2. https://medium.com/@petehouston/install-and-config-redis-on-mac-os-x-via-homebrew-eb8df9a4f298

================================

python VirtualEnv安裝

Mac
====================================
$ sudo easy_install pip

//virtaulenv建立專案
$ virtualenv somename

//進入virtualenv方式
$ cd somename && source bin/activate

//退出virtualenv方式
$ deactivate

//install package
$ sudo pip install django==1.4.5
or
$ sudo pip install django --upgrade

//uninstall
$ sudo pip uninstall django


//查看目前安裝的package
$ pip freeze

-----------------------------------------------------------------

windows
====================================
$ pip install engineer

$ pip install virtualenv
or
$ easy_install virtualenv

建立一個環境名為test
$ virtualenv test
$ cd test 
$ test\scripts\activate


教學網址:

  1. https://www.youtube.com/watch?v=FshRArXrEcM
  2. pip的相關文件網

mysql安裝

$ export PATH=$PATH:/usr/local/mysql/bin/

python - django

Mac

$ django-admin.py startproject mysite

連線mysql需要安裝以下三個pip
$ pip install mysql-python
$ pip install mysqlclient
$ pip install pymysql

2015年6月9日 星期二

dockerfile

1.FROM
格式為FROM<image>或FROM<image>:<tag>
第一條指令必須為FROM指令。並且,如果在同一個Dockerfile中建立多個image時,可以使用多個FROM指令(每個image一次)

2.MAINTAINER
格式為MAINTAINER<name>,指定維護者資訊。

3.RUN
格式為RUN<command>或RUN["executable", "paraml", "param2"]。
前者是在shell終端中執行命令,即/bin/sh -c;後者則是使用exec執行。指定使用其他shell終端可以透過第二種方式操作,例如RUN["/bin/bash", "-c", "echo hello"]。
每條RUN指令會在目前映像檔狀態下執行指定命令,並commit成新的映像檔。當命令過長時可以使用\來換行。

4.CMD
支援三種格式

  • CMD ["executable","param1","param2"] 使用 exec 執行,推薦使用;
  • CMD command param1 param2 在 /bin/sh 中執行,使用在給需要互動的指令;
  • CMD ["param1","param2"] 提供給 ENTRYPOINT 的預設參數;
如果使用者啟動容器時候指定了運行的命令,則會覆蓋掉 CMD 指定的命令。

指定啟動容器時執行的命令,每個 Dockerfile 只能有一條 CMD 命令。如果指定了多條命令,只有最後一條會被執行。

5.EXPOS
格式為 EXPOSE <port> [<port>...]。
設定 Docker 伺服器容器對外的埠號,供外界使用。在啟動容器時需要透過 -P,Docker 會自動分配一個埠號轉發到指定的埠號。

6.ENV
格式為 ENV <key> <value>。 指定一個環境變數,會被後續 RUN 指令使用,並在容器運行時保持。
ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …

ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH

7.ADD
格式為 ADD <src> <dest>。

該命令將複製指定的 <src> 到容器中的 <dest>。 其中 <src> 可以是 Dockerfile 所在目錄的相對路徑;也可以是一個 URL;還可以是一個 tar 檔案(其複製後會自動解壓縮)。

8.COPY
格式為 COPY <src> <dest>。
複製本地端的 <src>(為 Dockerfile 所在目錄的相對路徑)到容器中的 <dest>。
當使用本地目錄為根目錄時,推薦使用 COPY。

9.ENTRYPOINT
兩種格式:
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2(shell中執行)。
指定容器啟動後執行的命令,並且不會被 docker run 提供的參數覆蓋。

每個 Dockerfile 中只能有一個 ENTRYPOINT,當指定多個時,只有最後一個會生效。

10.VOLUME
格式為 VOLUME ["/data"]。
建立一個可以從本地端或其他容器掛載的掛載點,一般用來存放資料庫和需要保存的資料等。

11.USER
格式為 USER daemon。
指定運行容器時的使用者名稱或 UID,後續的 RUN 也會使用指定使用者。
當服務不需要管理員權限時,可以透過該命令指定運行使用者。並且可以在之前建立所需要的使用者,例如:RUN groupadd -r postgres && useradd -r -g postgres postgres。要臨時取得管理員權限可以使用 gosu,而不推薦 sudo。


12.WORKDIR
格式為 WORKDIR /path/to/workdir。
為後續的 RUN、CMD、ENTRYPOINT 指令指定工作目錄。
可以使用多個 WORKDIR 指令,後續命令如果參數是相對路徑,則會基於之前命令指定的路徑。例如

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

則最終路徑為 /a/b/c。

13.ONBUILD
格式為 ONBUILD [INSTRUCTION]。
指定當建立的映像檔作為其它新建立映像檔的基底映像檔時,所執行的操作指令。
例如,Dockerfile 使用以下的內容建立了映像檔 image-A。

[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]
如果基於 image-A 建立新的映像檔時,新的 Dockerfile 中使用 FROM image-A指定基底映像檔時,會自動執行 ONBUILD 指令內容,等於在後面新增了兩條指令。

FROM image-A

#Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src

使用 ONBUILD 指令的映像檔,推薦在標籤中註明,例如 ruby:1.9-onbuild。


相關網址:

  1. http://philipzheng.gitbooks.io/docker_practice/content/dockerfile/README.html

docker指令

//建置container
$ docker run -d -p
[-p] port
[-name] container name
[--link] {--link name:alias}將container 連結。
name:要連結的container name
alias: 連結container所識別的別名
[-v] [本機路徑]:[放置container路徑]掛載檔案
[-it] 設定不讓外部連結
[-d] or [-id] 啟動背景執行container,離開container時將停止執行
[-td] 啟動背景執行container,離開container時,仍然繼續背景執行

//啟用master-slave模式
//建立master container
$ docker run -d -e REPLICATION_MASTER=true -P --name mysql mysql
//建立slave container
$ docker run -d -e REPLICATION_SLAVE=true -P --link mysql:mysql mysql

//建置docker image
$ docker build -t [container] .

//search container 星評十次以上
$ docker search -s 10 [ubuntu]

//標記tag
$ docker tag [目前擁有的container name] [tag container name]

//查看container版本編號
$ docker run -ti [container name] /bib/bash

//檢視環境變數
$ docker exec [container name] env

//自訂container name
$ docker run -f -P --name [name] image


//變更container name
$ docker inspect -f "[name]" {container id}

//取得詳細資訊
$ docker inspect [image id]

//刪除image
$ docker rmi [repository or image id]

//刪除所有image of container,後再刪除image
$ docker rm
-f, --force=false 強制停止並刪除一個正在執行中的container
-l, --link=false 刪除container的連結,但保留container
-v, --volumes=false 刪除container掛載的volume

//查看本機存在所有container
$ docker ps -a

//映像檔產生的容器來建立
$ docker commit -m "Added a new file" -a "Docker newbee" [image id]
-a, --author=""作者資訊
-m, --message=""加入commit資訊
-p, --pause=true commit時暫停容器運作

//匯入壓縮檔
$ sudo cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu:14.04
or
$ sudo docker import http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz ubuntu:14.04

//載入image
$ docker load --input ubuntu_14.04.tar
or
$ docker load < ubuntu_14.04.tar

//上傳image
$ docker tag [image id] repository:tag
$ docker push [user]/[repository]:tag

//建立container
$ docker create -it ubuntu:latest

//停止container
$ docker stop [image id]

//重啟container
$ docker restart [image id]

//進入container
$ docker run -idt ubuntu
$ docker attach [NAMES]
or
$ docker exec -ti [image id] /bin/bash
or
$ docker exec -ti [image id] bash

//匯出container
$ docker export [image id] > [ubuntu.tar]

//匯入container
$ cat ubuntu.tar | sudo docker import - test/ubuntu:14.04

//登入


//刪除所有container
$ sudo docker ps -a -q | xargs -n 1 -I {} sudo docker rm {}

# Delete all containers
$ docker rm $(docker ps -a -q)
# Delete all images
$ docker rmi $(docker images -q)
// Delete grep container
$ docker rm $(docker ps -a | awk '{ print $1,$2 }' | grep 192.168.12.7:5000/golang-test1:v3 | awk '{print $1 }')

==============================================
docker 短指令安裝
下載 .bashrc_docker,並將內容放到 .bashrc 中。
$ wget -P https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker
$ echo "[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker" >> ~/.bashrc; source ~/.bashrc


參考網站:

  1. https://neiltw.gitbooks.io/book/content/chapter1.html
  2. https://philipzheng.gitbooks.io/docker_practice/content/appendix_command/index.html
  3. http://puremonkey2010.blogspot.tw/2015/05/docker-practice-container.html
  4. 中文介紹https://joshhu.gitbooks.io/dockercommands/content/Containers/IntoContainers.html


//安裝ssh方式
page 96



2015年1月24日 星期六

2015年1月19日 星期一

mysql相關資訊

#select特殊符號 escape

ex:$str = str_replace(['%', '_'], ['\%', '\_'], $str);



#intersection(交集)

可得到有“engine”屬性的值有Cloud & Compution的資料

ex:select * from book where tag=‘Cloud’ intersection tag=‘Computiong'



#調笑SQL查詢效能

資料庫引擎

javascript指令

#把物件或陣列轉成json格式資料
json.stringify()

#把json格式的資料轉成可利用的物件或陣列
json.parser()


#console.time() & console.timeEnd() 可評估特定區域程式碼效能

#js常用功能(proccess、util、)

aws(雲端伺服器) - 服務說明

AWS免費
#EC2:750[機器:小時]
#ELB:750[機器:小時]
#EBS:10GB容量、100萬次的讀寫、1GB快照、1萬次讀取快照、1千次儲存快照
#S3:5GB容量、2萬次讀取、2千次儲存
#網路傳輸:上傳和下載各15GB,總共30GB(搭配cloudWatch網路傳輸量要收費)
#SimpleDB:25個機器小時(200萬次讀取)、1GB儲存量、但是Domain如果6個月為存取,將自動被移除
#SQS:10萬次請求
#SNS:10萬次請求、10萬次http通知、1千次email通知
#CloudWatch:10次警報
#SES:從EC2或是Beanstalk使用SES寄電子郵件,每天2千封
#EC2與S3傳輸不收費

功能
運算(EC2、Elastic MapReduce、AutoScaling、Elastic Beanstalk)
儲存(S3、EBS)
資料庫(SimpleDB、RDS)
網路(CloudFront、ELB、EIP、VPC)
資訊(SQS、SNS、SES)
監控(Amazon CloudWatch)
付費(EPS、DevPay)

-----
RDS收費(低至高)

  1. Small
  2. Large
  3. Extra Large
  4. High-Memory Extra Large
  5. High-Memory Double EXtra Large
  6. High-Memory Quadruple Extra Large


存取控制清單ACL(Access Control List)
LBS(Location-based service)
簡單儲存服務S3(Simple Storage Service)
關聯式資料庫服務RDS(Relational Database Service)
CloudFront屬於(CDN)服務
低備份儲存RRS(Reduced Redundancy Storage)
彈性運算雲EC2(Elastic Computing Cloud)
資料庫安全群組與EC2安全群組一樣
叢集運算虛擬機(cluster compute instance)
社群服務(SNS)
彈性負載平衡ELB(Elastic Load Balancing)
訊息導向中介體MOM(Message oriented middleware)
企業服務匯流排(ESB)
服務導向架構(SOA)
簡單序列服務SQS(Simple Queue Service)
簡單通知服務SNS(Simple Notification Service)
EIP(Elastic IP Addresses)
內容傳遞網路CDN(Content Delivery Network)
EMR(Elastic MapReduce)


AWS解決延展性問題的相關服務
延展性的樣式 相關AWS服務
叢集 EC2、auto scaling
負載平衡 ELB(Elastic Load Balancing)、EIP(Elastic IP Address)
資料分割 S3、CloudFront、SimpleDB
分散式運算 EMR(Elastic MapReduce)
放鬆一致性的限制 SimpleDB、RDS(Relational Database Service)
非同步及批次作業 SQS、SNS

EBS-backed & S3-backed比較表
特性 EBS-backed S3-backed
開機時間 少於1分鐘 少於5分鐘
root device最大限制 1TB 10GB
AMI存放處 EBS S3
資料存續性 EC3虛擬機器失效時,資料仍在EBS volume上。EC2虛擬機器關機時,EBS volume可以選擇繼續留存 EC2虛擬機器失效或關機時,資料不存在。但除了root device以外,可以mount EBS volumes
升級性 虛擬機器處於stop狀態時,可更改虛擬機器類型,kernel,RAM disk 所有屬性不可改
收費 EC2時數,EBS volume儲存費用、EBS snapshot儲存AMI費用 EC2時數、S3儲存AMI的費用
stop狀態 可處於stop狀態、不算EC2時數。存在EBS volume上的資料會保存,但是要注意instance storage上的資料會消失 不能處於stop狀態,不是開就是關

其他服務
延展性的樣式 相關AWS服務
Amazon Beanstalk
Amazon Simple Email Service 簡單郵件服務
AWS CloudFormation
Amazon Flexible Payments Service 彈性付費服務(FPS)
Amazon DevPay 開發者付費服務
Amazon Fulfillment Web Service 墊子商務實現服務(FWS)
Amazon Mechanical Turk 人力外包
AWS import/Export 可直接寄硬碟給AWS將資料抓取下來
AWS Premium Support商業支援
Amazon Route 53 Amazon推出DNS服務可參考http://aws.amazon.com/route53

服務說明
#EBS

  1. EBS建立快照(snapahot),增加備份副本
  2. EBS就像SAN(storage area network)


#S3 & CloudFront
S3 永久儲存
CloudFront 暫時性

#建立RDS副本
點選DB Instance

#ELB
可搭配Apache、CloudWatch可以讓我們更方便往高延展性、高可用性方向前進

#自動延展(auto scaling)
auto scaling不收費,搭配CloudWatch收費

#簡單序列服務SQS
屬於分散式序列

#簡單通知服務SNS
補足SQS不足

#EIP
實現重新導向

#CloudFront
可讀S3「公開」、「私有」URL

#CloudWatch
監視系統狀態


注意
#Key
Access Key ID(不需保密)、Secret Access Key(需保密)

#linux ssh連線
key : test.pem
ex: ssh -i test.pem root@127.0.0.1

#http負載平衡資訊



相關網址
http://aws.amazon.com/free(價格)
http://blog.changyy.org/2009/12/amazon-ec2-windows-server-2003-rdp.html

http://blog.developer.tw/post/740-aws-ec2-free-instance

python



windows安裝
====================================
環境變數path
C:\Python27;C:\Python27\Scripts





#教學

  1. http://swaywang.blogspot.tw/2012/05/pythoncsv.html
  2. http://swaywang.blogspot.tw/2012/05/pythonpythonpython-sorting.html

go安裝

#安裝
====================================
https://golang.org/doc/install

https://golang.org/dl/


#linux安裝
====================================
$ wget https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz
$ tar -C /usr/local -xzf go1.5.1.linux-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin



環境變數設定
http://kalug.linux.org.tw/~lloyd/LLoyd_Hand_Book/book/bash-edit-profile.html

mac指令

Apache
#mac romove apache
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist


2015年1月18日 星期日

mongodb指令

顯示
#列出資料表
> show collections

#列出資料庫清單
> show dbs

#列出當前的資料庫名稱
> db

新增
#insert
> db.[collection].save({index:1, count: 2});

#循環insert data
> for (var i = 1; i <= 5; i++) db.[collection].save({index : i, count : i -1});

修改
#update
> var updatefood = db.others.findOne({index:1});
> updatefood.food = 'fish';
> db.[collection].save(updatefood);

刪除
#remove
> db.[collection].remove({index:2});

查詢
#while
> var cursor = db.[collection].find();
> while ( cursor.hasNext() ) printjson(cursor.next());

#forEach
> db.[collection].find().forEach(printjson);

#array
> var cursor = db.[collection].find();
> printjson(cursor[3]);
-----
> var arr = db.[collection].find().toArray();
> arr[3];

使用
#切換資料庫
> use [db name]

#登入身份
> db.auth('account', 'password') 。

help 顯示協助訊息。
mongo <dbname> --eval "db.dropDatabase()" 移除 database。 或者下面這種方式:


引用網址:

  1. http://andyyou.logdown.com/posts/177570-mongodb-notes

//教學
https://www.youtube.com/watch?v=iT0datgVcfs

2015年1月17日 星期六

mongodb安裝

安裝網址http://www.mongodb.org/downloads

#指定別的路徑儲存資料庫檔案
mongod --dbpath [儲存目錄路徑]

Windows
Step-1:下檔mongodb
Step-2:C:\> mkdir \data
Step-3:C:\> mkdir \data\db
Step-4:執行mongod.exe
Step-5:執行mongo.exe

============================================================

Linux
http://www.cnblogs.com/zhoujinyi/archive/2013/06/02/3113868.html(安裝教學)
http://www.linuxveda.com/2015/01/07/install-mongodb-linux/

/usr/bin/ -> mongodb


# rpm -ql mongodb-org-server
/etc/mongod.conf
/etc/rc.d/init.d/mongod
/etc/sysconfig/mongod
/usr/bin/mongod
/usr/share/man/man1/mongod.1
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
/var/run/mongodb


npm install jade
npm install express@3.4.8

============================================================

nodejs安裝

下載網址:http://nodejs.org/

windows
安裝npm
方法一:
Step-1:NPM下載
Step-2:將壓縮檔放置Nodejs目錄下node/
   C:\ > Program Files\nodejs

方法二:
C:\> npm install express@3.4.8 -g

============================================================

Linux
安裝npm
$ npm install express@3.4.8

將套件安裝在專案裡
$ npm install <package name>

移除全域套件
$ npm uninstall <package name> -g

移除專案裡的套件
$ npm uninstall <package name>

搜尋套件
$ npm search <package name>

列出全域套件
$ npm ls -g

列出全域套件詳細資訊
$ npm ls -gl

列出專案裡的套件
cd /path/to/the/project
npm ls

列出專案裡的套件詳細資訊
$ cd /path/to/the/project
$ npm ls -l

更新全域套件
$ npm update -g

更新案裡的套件
$ cd /path/to/the/project
$ npm update

------------------------------------------------------------------------------------------
用 `package.json` 來管理專案裡的套件

原本
$ cd /path/to/the/project
$ npm install mongoose
$ npm install express
$ npm install jade

package.json 在專案的根目錄就只要
$ cd /path/to/the/project
$ touch package.json (or npm init)

$ vi package.json
{
 "name": "your app name",
 "version": "0.0.1",
 "private": true,
 "dependencies": {
  "express": ">=2.5.0",
  "jade": ">= 0.16.4",
  "mongoose": ">=2.3.10"
 }
}

$ npm install -l (npm install)

============================================================

引用相關網址:

  1. npm 基本指令(http://dreamerslab.com/blog/tw/npm-basic-commands/)

初學者教學網

http://blog.allenchou.cc/nodejs-tuts-2-using-express-framework/

http://blog.allenchou.cc/nodejs-tuts-1-create-a-http-server/

http://www.nodebeginner.org/index-zh-tw.html