Class: Etcds
- Inherits:
-
Object
- Object
- Etcds
- Defined in:
- lib/etcds.rb,
lib/etcds/version.rb
Constant Summary collapse
- LABEL_BASE =
'com.s21g.etcds'
- H =
{}
- VERSION =
"0.1.2"
Instance Method Summary collapse
- #ctl(n, *args) ⇒ Object
- #health ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ Etcds
constructor
A new instance of Etcds.
- #install(*hosts) ⇒ Object
- #ls ⇒ Object
- #member ⇒ Object
- #ps(*args) ⇒ Object
- #rm(*names) ⇒ Object
- #stop(*names) ⇒ Object
- #up(*names) ⇒ Object
Constructor Details
#initialize ⇒ Etcds
Returns a new instance of Etcds.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/etcds.rb', line 8 def initialize config = './etcds.yml' case ARGV.first when '-f'; _, config = ARGV.slice! 0, 2 when '-v'; puts "etcds version #{VERSION}"; exit when '-h', nil system "#{$0} -v" system "etcd-ca -v" puts "config: #{config}" ms = (public_methods.sort - Object.methods) mlen = ms.map(&:length).max puts "Available sub commands:" ms.each{|m| puts " %-#{mlen+2}s#{H[m.intern]}" % m} exit end @nodes = YAML.load_file config end |
Instance Method Details
#ctl(n, *args) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/etcds.rb', line 139 def ctl(n, *args) node = @nodes[n] ip = node['ip'] system "etcdctl -C https://#{ip}:2379" + " --cert-file ./certs/client.crt" + " --key-file ./certs/client.key.insecure" + " --ca-file ./certs/#{n}.ca.crt " + args*' ' end |
#health ⇒ Object
133 |
# File 'lib/etcds.rb', line 133 def health; for_all_run{|n| ctl n, 'cluster-health'} end |
#init ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/etcds.rb', line 40 def init etcd_ca "init --passphrase ''" @nodes.each do |k, v| ip = v['ip'] name = k etcd_ca "new-cert --passphrase '' --ip #{ip} #{name}" etcd_ca "sign --passphrase '' #{name}" etcd_ca "export --insecure --passphrase '' #{name}" + " | tar -C ./certs -xvf -" etcd_ca "chain #{name} > ./certs/#{name}.ca.crt" end etcd_ca "new-cert --passphrase '' client" etcd_ca "sign --passphrase '' client" etcd_ca "export --insecure --passphrase '' client | tar -C ./certs -xvf -" end |
#install(*hosts) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/etcds.rb', line 57 def install(*hosts) hosts.each do |to| %w[ca.crt crt key.insecure].each do |what| name = "#{to}.#{what}" scp "./certs/#{name} #{to}:/tmp/" ssh to, "mv /tmp/#{name} /etc/docker/certs.d" ssh to, "chown root:root /etc/docker/certs.d/#{name}" end end end |
#ls ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/etcds.rb', line 27 def ls @nodes.each do |k,v| ip = v['ip'] state = case when run?(k); 'running' when exist?(k); 'stopped' else 'not exist' end puts "#{k}: ip=#{ip} #{state}" end end |
#member ⇒ Object
136 |
# File 'lib/etcds.rb', line 136 def member; for_all_run{|n| ctl n, 'member list'} end |
#ps(*args) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/etcds.rb', line 69 def ps(*args) @nodes.keys.each do |n| puts "Node #{n}:" puts docker(n, "ps -f label=#{LABEL_BASE}.name #{args*' '}") + "\n" end end |
#rm(*names) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/etcds.rb', line 91 def rm(*names) names.each do |n| cid = docker n, "ps -q -f status=exited -f label=#{LABEL_BASE}.name=#{n}" if cid.empty? STDERR.puts "etcd is not stopped or existing at #{n}" next end docker n, "rm #{cid}" puts "etcd is removed at #{n}" end end |
#stop(*names) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/etcds.rb', line 77 def stop(*names) names = @nodes.keys if names.empty? names.each do |n| cid = docker n, "ps -qf label=#{LABEL_BASE}.name=#{n}" if cid.empty? STDERR.puts "etcd is not running at #{n}" next end docker n, "stop #{cid}" puts "etcd is stopped at #{n}" end end |
#up(*names) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/etcds.rb', line 104 def up(*names) names.each do |n| node = @nodes[n] ip = node['ip'] stop n if run? n rm n if exist? n docker n, "run -d -p 2379:2379 -p 2380:2380 --name etcd" + " -e ETCD_TRUSTED_CA_FILE=/certs/#{n}.ca.crt" + " -e ETCD_CERT_FILE=/certs/#{n}.crt" + " -e ETCD_KEY_FILE=/certs/#{n}.key.insecure" + " -e ETCD_CLIENT_CERT_AUTH=1" + " -e ETCD_PEER_TRUSTED_CA_FILE=/certs/#{n}.ca.crt" + " -e ETCD_PEER_CERT_FILE=/certs/#{n}.crt" + " -e ETCD_PEER_KEY_FILE=/certs/#{n}.key.insecure" + " -e ETCD_PEER_CLIENT_CERT_AUTH=1" + " -e ETCD_HEARTBEAT_INTERVAL=100" + " -e ETCD_ELECTION_TIMEOUT=2500" + " -v /etc/docker/certs.d:/certs" + " -l #{LABEL_BASE}.name=#{n}" + " quay.io/coreos/etcd" + " -name #{n}" + " -listen-client-urls https://0.0.0.0:2379" + " -listen-peer-urls https://0.0.0.0:2380" + " -advertise-client-urls https://#{ip}:2379" puts "etcd is started at #{n}" end end |