etcds 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c61c8cdb9d510b1947401ef2d5bc9bda5662dd0
4
+ data.tar.gz: dda92c98c5f0e677c9c2fcf9cc84bd7982574256
5
+ SHA512:
6
+ metadata.gz: c297809e94cb0f60b4bbd30644d7b9630b505cc9f69cc029617330d43975b5a1da9940d3ab43e69af79c624449d0f3a8763920dcda0cffbc0640efa86e42873a
7
+ data.tar.gz: d2e1d9a41f1ae3c19dd9d700d6c98bb77d25a89d4778e09fd70869e7bdfa09c3ba5f89225008d90f915de33016a0375a9a9cf3adf20a8e21dc1a3df78dbdbcae
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in etcds.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Etcds
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/etcds`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'etcds'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install etcds
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/etcds. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/etcds ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.require
5
+ Etcds.new.send *ARGV
data/etcds.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'etcds/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "etcds"
8
+ spec.version = Etcds::VERSION
9
+ spec.authors = ["Genki Takiuchi"]
10
+ spec.email = ["genki@s21g.com"]
11
+
12
+ spec.summary = %q{etcd cluster manager}
13
+ spec.homepage = "https://github.com/genki/etcds"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
data/etcds.yml ADDED
@@ -0,0 +1,3 @@
1
+ # sample config file
2
+ default:
3
+ ip: 127.0.0.1
@@ -0,0 +1,3 @@
1
+ class Etcds
2
+ VERSION = "0.1.0"
3
+ end
data/lib/etcds.rb ADDED
@@ -0,0 +1,172 @@
1
+ require "etcds/version"
2
+ require "yaml"
3
+
4
+ class Etcds
5
+ LABEL_BASE = 'com.s21g.etcds'
6
+ H = {}
7
+
8
+ def initialize
9
+ config = './etcds.yml'
10
+ case ARGV.first
11
+ when '-f'; _, config = ARGV.slice! 0, 2
12
+ when '-v'; puts "etcds version 0.1.0"; exit
13
+ when '-h', nil
14
+ system "#{$0} -v"
15
+ system "etcd-ca -v"
16
+ puts "config: #{config}"
17
+ ms = (public_methods.sort - Object.methods)
18
+ mlen = ms.map(&:length).max
19
+ puts "Available sub commands:"
20
+ ms.each{|m| puts " %-#{mlen+2}s#{H[m.intern]}" % m}
21
+ exit
22
+ end
23
+ @nodes = YAML.load_file config
24
+ end
25
+
26
+ H[:ls] = 'list up nodes'
27
+ def ls
28
+ @nodes.each do |k,v|
29
+ ip = v['ip']
30
+ state = case
31
+ when run?(k); 'running'
32
+ when exist?(k); 'stopped'
33
+ else 'not exist'
34
+ end
35
+ puts "#{k}: ip=#{ip} #{state}"
36
+ end
37
+ end
38
+
39
+ H[:init] = 'prepare ca files for all nodes'
40
+ def init
41
+ etcd_ca "init --passphrase ''"
42
+ @nodes.each do |k, v|
43
+ ip = v['ip']
44
+ name = k
45
+ etcd_ca "new-cert --passphrase '' --ip #{ip} #{name}"
46
+ etcd_ca "sign --passphrase '' #{name}"
47
+ etcd_ca "export --insecure --passphrase '' #{name}" +
48
+ " | tar -C ./certs -xvf -"
49
+ etcd_ca "chain #{name} > ./certs/#{name}.ca.crt"
50
+ end
51
+ etcd_ca "new-cert --passphrase '' client"
52
+ etcd_ca "sign --passphrase '' client"
53
+ etcd_ca "export --insecure --passphrase '' client | tar -C ./certs -xvf -"
54
+ end
55
+
56
+ H[:install] = "[names...]\tinstall ca files to the host"
57
+ def install(*hosts)
58
+ hosts.each do |to|
59
+ %w[ca.crt crt key.insecure].each do |what|
60
+ name = "#{to}.#{what}"
61
+ scp "./certs/#{name} #{to}:/tmp/"
62
+ ssh to, "mv /tmp/#{name} /etc/docker/certs.d"
63
+ ssh to, "chown root:root /etc/docker/certs.d/#{name}"
64
+ end
65
+ end
66
+ end
67
+
68
+ H[:ps] = 'list up etcd containers'
69
+ def ps(*args)
70
+ @nodes.keys.each do |n|
71
+ puts "Node #{n}:"
72
+ puts docker(n, "ps -f label=#{LABEL_BASE}.name #{args*' '}") + "\n"
73
+ end
74
+ end
75
+
76
+ H[:stop] = "[names...]\tstop nodes"
77
+ def stop(*names)
78
+ names = @nodes.keys if names.empty?
79
+ names.each do |n|
80
+ cid = docker n, "ps -qf label=#{LABEL_BASE}.name=#{n}"
81
+ if cid.empty?
82
+ STDERR.puts "etcd is not running at #{n}"
83
+ next
84
+ end
85
+ docker n, "stop #{cid}"
86
+ puts "etcd is stopped at #{n}"
87
+ end
88
+ end
89
+
90
+ H[:rm] = "[names...]\tremove stopped nodes"
91
+ def rm(*names)
92
+ names.each do |n|
93
+ cid = docker n, "ps -q -f status=exited -f label=#{LABEL_BASE}.name=#{n}"
94
+ if cid.empty?
95
+ STDERR.puts "etcd is not stopped or existing at #{n}"
96
+ next
97
+ end
98
+ docker n, "rm #{cid}"
99
+ puts "etcd is removed at #{n}"
100
+ end
101
+ end
102
+
103
+ H[:up] = "[names...]\tprepare and activate etcd"
104
+ def up(*names)
105
+ names.each do |n|
106
+ node = @nodes[n]
107
+ ip = node['ip']
108
+ stop n if run? n
109
+ rm n if exist? n
110
+ docker n, "run -d -p 2379:2379 -p 2380:2380 --name etcd" +
111
+ " -e ETCD_TRUSTED_CA_FILE=/certs/#{n}.ca.crt" +
112
+ " -e ETCD_CERT_FILE=/certs/#{n}.crt" +
113
+ " -e ETCD_KEY_FILE=/certs/#{n}.key.insecure" +
114
+ " -e ETCD_CLIENT_CERT_AUTH=1" +
115
+ " -e ETCD_PEER_TRUSTED_CA_FILE=/certs/#{n}.ca.crt" +
116
+ " -e ETCD_PEER_CERT_FILE=/certs/#{n}.crt" +
117
+ " -e ETCD_PEER_KEY_FILE=/certs/#{n}.key.insecure" +
118
+ " -e ETCD_PEER_CLIENT_CERT_AUTH=1" +
119
+ " -e ETCD_HEARTBEAT_INTERVAL=100" +
120
+ " -e ETCD_ELECTION_TIMEOUT=2500" +
121
+ " -v /etc/docker/certs.d:/certs" +
122
+ " -l #{LABEL_BASE}.name=#{n}" +
123
+ " quay.io/coreos/etcd" +
124
+ " -name #{n}" +
125
+ " -listen-client-urls https://0.0.0.0:2379" +
126
+ " -listen-peer-urls https://0.0.0.0:2380" +
127
+ " -advertise-client-urls https://#{ip}:2379"
128
+ puts "etcd is started at #{n}"
129
+ end
130
+ end
131
+
132
+ H[:health] = 'show cluster health for all nodes'
133
+ def health; for_all_run{|n| ctl n, 'cluster-health'} end
134
+
135
+ H[:member] = 'show member list for all nodes'
136
+ def member; for_all_run{|n| ctl n, 'member list'} end
137
+
138
+ H[:ctl] = "[name] commands\tpass commands to etcdctl"
139
+ def ctl(n, *args)
140
+ node = @nodes[n]
141
+ ip = node['ip']
142
+ system "etcdctl -C https://#{ip}:2379" +
143
+ " --cert-file ./certs/client.crt" +
144
+ " --key-file ./certs/client.key.insecure" +
145
+ " --ca-file ./certs/#{n}.ca.crt " + args*' '
146
+ end
147
+
148
+ private
149
+ def etcd_ca(cmd) system "etcd-ca --depot-path ./certs #{cmd}" end
150
+ def dm(cmd) system "docker-machine #{cmd}" end
151
+ def scp(cmd) dm "scp #{cmd}" end
152
+ def ssh(name, cmd) dm "ssh #{name} \"sudo -u root #{escape cmd}\"" end
153
+ def escape(cmd) cmd.gsub /"/, '\"' end
154
+ def run?(n) !docker(n, "ps -qf label=#{LABEL_BASE}.name=#{n}").empty? end
155
+ def exist?(n) !docker(n, "ps -qaf label=#{LABEL_BASE}.name=#{n}").empty? end
156
+
157
+ def docker(name, cmd)
158
+ ip = @nodes[name]['ip']
159
+ IO.popen({'DOCKER_HOST' => "tcp://#{ip}:2376"}, "docker #{cmd}").read
160
+ end
161
+
162
+ def for_all_run(&block)
163
+ @nodes.keys.each do |n|
164
+ if run?(n)
165
+ puts "#{n}:"
166
+ block[n]
167
+ else
168
+ puts "#{n}: not running"
169
+ end
170
+ end
171
+ end
172
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: etcds
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Takiuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - genki@s21g.com
44
+ executables:
45
+ - etcds
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/etcds
57
+ - etcds.gemspec
58
+ - etcds.yml
59
+ - lib/etcds.rb
60
+ - lib/etcds/version.rb
61
+ homepage: https://github.com/genki/etcds
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.8
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: etcd cluster manager
85
+ test_files: []