rncher 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/rncher +3 -0
- data/bin/setup +8 -0
- data/lib/active_resource/base_collection.rb +5 -0
- data/lib/active_resource/keyboard_handler.rb +11 -0
- data/lib/active_resource/remote_connection.rb +115 -0
- data/lib/resources/app.rb +3 -0
- data/lib/resources/base.rb +7 -0
- data/lib/resources/container.rb +44 -0
- data/lib/resources/credential.rb +36 -0
- data/lib/resources/service.rb +28 -0
- data/lib/rncher/app.rb +12 -0
- data/lib/rncher/cli.rb +42 -0
- data/lib/rncher/service.rb +129 -0
- data/lib/rncher/version.rb +3 -0
- data/lib/rncher.rb +30 -0
- data/rncher.gemspec +43 -0
- metadata +210 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ded4e49659c56eb69f6e13bf99364da5706f968
|
4
|
+
data.tar.gz: d26cbedce4a33911638942344ecbe11b26d917f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4f4b738fef4e39f407638c4ff6765ffc7928082b01b91bfebe5e26397f2485acfebd1f08ec895754e72ff4736b179472ca3431f2c7b675afa610d9fca0d3d55
|
7
|
+
data.tar.gz: 53934bf0dc8a52195c1bf21000b16be6624bb39454301bc5ec2a485f01f50f75960a4b6682d2849c461459addde26d5260dc940410f65dae74d64dfabb642bea
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sanderson@digaeita.com.br. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 sandsu
|
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,33 @@
|
|
1
|
+
# Rncher
|
2
|
+
|
3
|
+
Rancher CLI to interact with environments using conventions over configurations
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install as:
|
8
|
+
|
9
|
+
$ gem install rncher
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```
|
14
|
+
rncher --help
|
15
|
+
```
|
16
|
+
|
17
|
+
## Development
|
18
|
+
|
19
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
20
|
+
|
21
|
+
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).
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rncher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
26
|
+
|
27
|
+
## License
|
28
|
+
|
29
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
30
|
+
|
31
|
+
## Code of Conduct
|
32
|
+
|
33
|
+
Everyone interacting in the Rncher project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rncher/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rncher"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rncher
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
class RemoteConnection
|
2
|
+
|
3
|
+
def initialize(url, token)
|
4
|
+
@url, @token = url, token
|
5
|
+
end
|
6
|
+
|
7
|
+
def uri
|
8
|
+
"#{@url}?token=#{@token}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def start
|
12
|
+
begin
|
13
|
+
EM.run do
|
14
|
+
ws = Faye::WebSocket::Client.new(uri, nil, {ping: 10 })
|
15
|
+
IO.console.raw!
|
16
|
+
EM.open_keyboard(KeyboardHandler, ws)
|
17
|
+
ws.on :message do |event|
|
18
|
+
content = Base64.decode64(event.data)
|
19
|
+
STDOUT.print content
|
20
|
+
end
|
21
|
+
ws.on :close do |event|
|
22
|
+
EM.stop
|
23
|
+
ws = nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
ensure
|
27
|
+
IO.console.cooked!
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def start_logs(container)
|
32
|
+
begin
|
33
|
+
EM.run do
|
34
|
+
ws = Faye::WebSocket::Client.new(uri, nil, {ping: 10 })
|
35
|
+
pastel = Pastel.new
|
36
|
+
ws.on :message do |event|
|
37
|
+
content = event.data.split(' ')
|
38
|
+
timestamp = Time.rfc3339(content[1])
|
39
|
+
formated_time = timestamp.to_formatted_s(:short)
|
40
|
+
puts pastel.red("#{container.name} | ")+ pastel.green("#{formated_time} | ") + content.last(content.size-2).join(' ')
|
41
|
+
end
|
42
|
+
ws.on :close do |event|
|
43
|
+
EM.stop
|
44
|
+
ws = nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
ensure
|
48
|
+
IO.console.cooked!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def start_status(service)
|
53
|
+
begin
|
54
|
+
items = Hash.new
|
55
|
+
service.instanceIds.each do |id|
|
56
|
+
container = Container.find(id)
|
57
|
+
items[container.externalId] = {
|
58
|
+
id: container.id,
|
59
|
+
name: container.name,
|
60
|
+
state: container.state,
|
61
|
+
cpu: 0
|
62
|
+
}
|
63
|
+
end
|
64
|
+
EM.run do
|
65
|
+
ws = Faye::WebSocket::Client.new("#{uri}&&sockId=5", nil, {ping: 10 })
|
66
|
+
pastel = Pastel.new
|
67
|
+
i = 0
|
68
|
+
ws.on :message do |event|
|
69
|
+
content = JSON.parse(event.data).first
|
70
|
+
table = TTY::Table.new header: ['name','state','memory', 'cpu']
|
71
|
+
|
72
|
+
memory = content['memory']['usage'].to_f
|
73
|
+
memory_limit = content['memLimit'].to_f
|
74
|
+
memory_rate = (memory/memory_limit)
|
75
|
+
formated_memory = ActiveSupport::NumberHelper::number_to_human_size(memory)
|
76
|
+
|
77
|
+
if items[content['id']][:cpu_total]
|
78
|
+
delta_total_usage = (content['cpu']['usage']['total'] - items[content['id']][:cpu_total]).to_f / content['cpu']['usage']['total']
|
79
|
+
delta_system_usage = (content['cpu']['usage']['system'] - items[content['id']][:cpu_system_total]).to_f / content['cpu']['usage']['system']
|
80
|
+
if delta_system_usage>0
|
81
|
+
y = (delta_total_usage / delta_system_usage)*content['cpu']['usage']['per_cpu_usage'].size * 100.0
|
82
|
+
else
|
83
|
+
y = 0
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
items[content['id']][:cpu_total] = content['cpu']['usage']['total']
|
89
|
+
items[content['id']][:cpu_system_total] = content['cpu']['usage']['system']
|
90
|
+
items[content['id']][:cpu] = y
|
91
|
+
|
92
|
+
|
93
|
+
items[content['id']][:memory] = memory
|
94
|
+
items[content['id']][:memory_limit] = memory_limit
|
95
|
+
items[content['id']][:memory_rate] = memory_rate
|
96
|
+
items[content['id']][:formated_memory] = formated_memory
|
97
|
+
items.each_with_index.each do |item, index|
|
98
|
+
table << [item[1][:name], item[1][:state], item[1][:formated_memory], "#{items[content['id']][:cpu].to_f.round(1)}%"]
|
99
|
+
end
|
100
|
+
|
101
|
+
lines = "#{table.render(:ascii, border:{separator: :each_row})}".lines.count
|
102
|
+
print "\r" + ("\e[A\e[J"*(lines-1)) if i>table.rows_size
|
103
|
+
STDOUT.syswrite "#{table.render(:ascii, border:{separator: :each_row})}" if i>table.rows_size-1
|
104
|
+
i+=1
|
105
|
+
end
|
106
|
+
ws.on :close do |event|
|
107
|
+
EM.stop
|
108
|
+
ws = nil
|
109
|
+
end
|
110
|
+
end
|
111
|
+
ensure
|
112
|
+
IO.console.cooked!
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Container < Base
|
2
|
+
belongs_to :service, foreign_key: 'serviceId'
|
3
|
+
|
4
|
+
def remote_token(command=nil)
|
5
|
+
attributes = {
|
6
|
+
'attachStdin': true,
|
7
|
+
'attachStdout': true,
|
8
|
+
'tty': true,
|
9
|
+
'command': parse_command(command)
|
10
|
+
}
|
11
|
+
request_token(:execute, attributes)
|
12
|
+
end
|
13
|
+
|
14
|
+
def logs_token
|
15
|
+
attributes = {
|
16
|
+
'follow': true,
|
17
|
+
'lines': 500
|
18
|
+
}
|
19
|
+
request_token(:logs, attributes)
|
20
|
+
end
|
21
|
+
|
22
|
+
def request_token(action, attributes)
|
23
|
+
response = post(nil, {action: action}, attributes.to_json)
|
24
|
+
json_response = JSON.parse(response.body)
|
25
|
+
{token: json_response['token'], url: json_response['url']}
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_command(command=nil)
|
29
|
+
if !command
|
30
|
+
command = [
|
31
|
+
"/bin/sh",
|
32
|
+
"-c",
|
33
|
+
"TERM=xterm-256color; export TERM; [ -x /bin/bash ] && ([ -x /usr/bin/script ] && /usr/bin/script -q -c \"/bin/bash\" /dev/null || exec /bin/bash) || exec /bin/sh"
|
34
|
+
]
|
35
|
+
else
|
36
|
+
command = [
|
37
|
+
"/bin/sh",
|
38
|
+
"-c",
|
39
|
+
"TERM=xterm-256color; export TERM; [ -x /bin/bash ] && ([ -x /usr/bin/script ] && /usr/bin/script -q -c \"#{command}\" /dev/null || exec #{command}) || /bin/sh #{command}"
|
40
|
+
]
|
41
|
+
end
|
42
|
+
command
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Credential
|
2
|
+
FILE_PATH = "#{ENV['HOME']}/.rncher"
|
3
|
+
|
4
|
+
def self.load_credentials
|
5
|
+
@attributes ||= JSON.parse(File.read(FILE_PATH))
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.exists?
|
9
|
+
begin
|
10
|
+
JSON.parse(File.read(FILE_PATH))
|
11
|
+
true
|
12
|
+
rescue
|
13
|
+
false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.access_key
|
18
|
+
self.exists? ? self.load_credentials['default']['access_key'] : nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.secret_key
|
22
|
+
self.exists? ? self.load_credentials['default']['secret_key'] : nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.host
|
26
|
+
self.exists? ? self.load_credentials['default']['host'] : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.env
|
30
|
+
self.exists? ? self.load_credentials['default']['env'] : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.set_credentials(attributes)
|
34
|
+
File.open(FILE_PATH, 'w') {|f| f.write(attributes.to_json) }
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Service < Base
|
2
|
+
self.element_name = 'service'
|
3
|
+
belongs_to :app, foreign_key: 'stackId'
|
4
|
+
|
5
|
+
|
6
|
+
def containerstats_token
|
7
|
+
attributes = {
|
8
|
+
'follow': true,
|
9
|
+
'lines': 500
|
10
|
+
}
|
11
|
+
request_token(:logs, attributes)
|
12
|
+
end
|
13
|
+
|
14
|
+
def request_token
|
15
|
+
response = get(:containerstats)
|
16
|
+
{token: response['token'], url: response['url']}
|
17
|
+
end
|
18
|
+
|
19
|
+
def upgrade(attributes)
|
20
|
+
response = post(nil, {action: :upgrade}, attributes.to_json)
|
21
|
+
JSON.parse(response.body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def finish_upgrade
|
25
|
+
response = post(nil, {action: :finishupgrade})
|
26
|
+
JSON.parse(response.body)
|
27
|
+
end
|
28
|
+
end
|
data/lib/rncher/app.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
class Rncher::App < Thor
|
2
|
+
package_name "App"
|
3
|
+
|
4
|
+
desc "list", "list apps"
|
5
|
+
def list
|
6
|
+
table = TTY::Table.new header: ['id','name','state']
|
7
|
+
App.where(system: false).each do |app|
|
8
|
+
table << [app.id, app.name, app.state]
|
9
|
+
end
|
10
|
+
puts table.render(:ascii, border:{separator: :each_row})
|
11
|
+
end
|
12
|
+
end
|
data/lib/rncher/cli.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Rncher::Cli < Thor
|
2
|
+
desc 'app', 'interact with apps' # [4]
|
3
|
+
subcommand 'app', Rncher::App
|
4
|
+
|
5
|
+
desc 'service', 'interact with services' # [4]
|
6
|
+
subcommand 'service', Rncher::Service
|
7
|
+
|
8
|
+
desc 'setup', 'setup credentials'
|
9
|
+
method_option :access_key, type: :string, required: true
|
10
|
+
method_option :secret_key, type: :string, required: true
|
11
|
+
method_option :host, type: :string, required: true
|
12
|
+
method_option :env, type: :string, required: true
|
13
|
+
method_option :name, type: :string, required: true
|
14
|
+
method_option :default, type: :boolean, required: false
|
15
|
+
def setup
|
16
|
+
if !Credential.exists?
|
17
|
+
Credential.set_credentials({})
|
18
|
+
end
|
19
|
+
content = Credential.load_credentials
|
20
|
+
content[options[:name].to_sym] = {
|
21
|
+
access_key: options[:access_key],
|
22
|
+
secret_key: options[:secret_key],
|
23
|
+
host: options[:host],
|
24
|
+
env: options[:env]
|
25
|
+
}
|
26
|
+
content[:default] = {
|
27
|
+
access_key: options[:access_key],
|
28
|
+
secret_key: options[:secret_key],
|
29
|
+
host: options[:host],
|
30
|
+
env: options[:env]
|
31
|
+
} if options[:default]==true
|
32
|
+
Credential.set_credentials(content)
|
33
|
+
puts 'Done!'
|
34
|
+
end
|
35
|
+
|
36
|
+
desc 'switch', 'switch environment'
|
37
|
+
def switch name
|
38
|
+
content = Credential.load_credentials
|
39
|
+
content[:default] = content[name]
|
40
|
+
Credential.set_credentials(content)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
class Rncher::Service < Thor
|
2
|
+
package_name 'Service'
|
3
|
+
|
4
|
+
desc 'list', 'list services'
|
5
|
+
method_option :app, type: :string, required: true
|
6
|
+
def list
|
7
|
+
app = App.where({system: false, name: options[:app]}).first
|
8
|
+
if app
|
9
|
+
services = Service.where({system: false, stackId: app.id})
|
10
|
+
table = TTY::Table.new header: ['name','state', 'health', 'scale']
|
11
|
+
services.each do |app|
|
12
|
+
table << [app.name, app.state, app.healthState, app.scale]
|
13
|
+
end
|
14
|
+
puts table.render(:ascii, border:{separator: :each_row})
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'scale', 'scale services'
|
19
|
+
method_option :app, type: :string, required: true
|
20
|
+
method_option :service, type: :string, required: true
|
21
|
+
def scale(num)
|
22
|
+
app = App.where({system: false, name: options[:app]}).first
|
23
|
+
if app
|
24
|
+
service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
|
25
|
+
service.scale = num
|
26
|
+
service.save
|
27
|
+
puts 'Done'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'exec', 'shell services'
|
32
|
+
method_option :app, type: :string, required: true
|
33
|
+
method_option :service, type: :string, required: true
|
34
|
+
method_option :command, type: :string, required: false
|
35
|
+
def exec
|
36
|
+
app = App.where({system: false, name: options[:app]}).first
|
37
|
+
if app
|
38
|
+
service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
|
39
|
+
container_id = service.instanceIds.first
|
40
|
+
container = Container.find(container_id)
|
41
|
+
credential = container.remote_token(options[:command])
|
42
|
+
connection = RemoteConnection.new(credential[:url], credential[:token])
|
43
|
+
connection.start
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
desc 'logs', 'service logs'
|
48
|
+
method_option :app, type: :string, required: true
|
49
|
+
method_option :service, type: :string, required: true
|
50
|
+
def logs
|
51
|
+
app = App.where({system: false, name: options[:app]}).first
|
52
|
+
if app
|
53
|
+
service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
|
54
|
+
EventMachine.run do
|
55
|
+
service.instanceIds.each do |container_id|
|
56
|
+
container = Container.find(container_id)
|
57
|
+
credential = container.logs_token
|
58
|
+
connection = RemoteConnection.new(credential[:url], credential[:token])
|
59
|
+
connection.start_logs(container)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
desc 'status', 'service status'
|
66
|
+
method_option :app, type: :string, required: true
|
67
|
+
method_option :service, type: :string, required: true
|
68
|
+
def status
|
69
|
+
app = App.where({system: false, name: options[:app]}).first
|
70
|
+
if app
|
71
|
+
service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
|
72
|
+
EventMachine.run do
|
73
|
+
credential = service.request_token
|
74
|
+
connection = RemoteConnection.new(credential[:url], credential[:token])
|
75
|
+
connection.start_status(service)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
desc 'upgrade', 'upgrade service'
|
81
|
+
method_option :app, type: :string, required: true
|
82
|
+
method_option :service, type: :string, required: true
|
83
|
+
method_option :image, type: :string, required: false
|
84
|
+
method_option :vars, type: :hash, required: false
|
85
|
+
method_option :remove_vars, type: :array, required: false
|
86
|
+
def upgrade
|
87
|
+
app = App.where({system: false, name: options[:app]}).first
|
88
|
+
if app
|
89
|
+
service = Service.where({system: false, stackId: app.id, name: options[:service]}).first
|
90
|
+
launch_config = service.launchConfig
|
91
|
+
if options[:image]
|
92
|
+
launch_config.imageUuid = "docker:#{options[:image]}"
|
93
|
+
end
|
94
|
+
if options[:vars]
|
95
|
+
new_configs = Hash.new
|
96
|
+
options[:vars].map{|k, v| new_configs[k.upcase] = v}
|
97
|
+
new_attributes = launch_config.environment.attributes.merge(new_configs)
|
98
|
+
launch_config.environment.attributes = new_attributes
|
99
|
+
end
|
100
|
+
if options[:remove_vars]
|
101
|
+
new_configs = Hash.new
|
102
|
+
launch_config.environment.attributes.each do |k, v|
|
103
|
+
new_configs[k.upcase] = v unless options[:remove_vars].collect{|c| c.upcase}.include?(k.upcase)
|
104
|
+
end
|
105
|
+
launch_config.environment.attributes = new_configs
|
106
|
+
end
|
107
|
+
if options[:image] || options[:vars] || options[:remove_vars]
|
108
|
+
attributes = {
|
109
|
+
"inServiceStrategy": {
|
110
|
+
"batchSize": 1,
|
111
|
+
"intervalMillis": 2000,
|
112
|
+
"launchConfig": launch_config,
|
113
|
+
"startFirst": true
|
114
|
+
},
|
115
|
+
"toServiceStrategy": "null"
|
116
|
+
}
|
117
|
+
service.upgrade(attributes)
|
118
|
+
service = Service.find(service.id)
|
119
|
+
state = service.state
|
120
|
+
while(state=='upgrading') do
|
121
|
+
sleep 2
|
122
|
+
service = Service.find(service.id)
|
123
|
+
state = service.state
|
124
|
+
end
|
125
|
+
service.finish_upgrade
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/lib/rncher.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rncher/version'
|
2
|
+
require 'io/console'
|
3
|
+
require 'eventmachine'
|
4
|
+
require 'json'
|
5
|
+
require 'yaml'
|
6
|
+
require 'rest-client'
|
7
|
+
require 'thor'
|
8
|
+
require 'active_support/all'
|
9
|
+
require 'faye/websocket'
|
10
|
+
require 'tty-table'
|
11
|
+
require 'activeresource'
|
12
|
+
require 'pastel'
|
13
|
+
|
14
|
+
require 'resources/credential'
|
15
|
+
require 'active_resource/base_collection'
|
16
|
+
require 'active_resource/keyboard_handler'
|
17
|
+
require 'active_resource/remote_connection'
|
18
|
+
require 'resources/base'
|
19
|
+
require 'resources/app'
|
20
|
+
require 'resources/service'
|
21
|
+
require 'resources/container'
|
22
|
+
|
23
|
+
require 'rncher/app'
|
24
|
+
require 'rncher/service'
|
25
|
+
require 'rncher/cli'
|
26
|
+
|
27
|
+
module Rncher
|
28
|
+
# ActiveResource::Base.logger = Logger.new(STDERR)
|
29
|
+
trap("INT") { puts "\nExiting..."; exit}
|
30
|
+
end
|
data/rncher.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rncher/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rncher"
|
8
|
+
spec.version = Rncher::VERSION
|
9
|
+
spec.authors = ["sandsu"]
|
10
|
+
spec.email = ["sanderson@digaeita.com.br"]
|
11
|
+
|
12
|
+
spec.summary = "summary"
|
13
|
+
spec.description = "description"
|
14
|
+
spec.homepage = "https://digaeita.com.br"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
spec.add_runtime_dependency 'rest-client', '2.0.2'
|
37
|
+
spec.add_runtime_dependency 'thor', '0.20.0'
|
38
|
+
spec.add_runtime_dependency 'activesupport', '5.1.3'
|
39
|
+
spec.add_runtime_dependency 'faye-websocket', '0.10.7'
|
40
|
+
spec.add_runtime_dependency 'tty-table', '0.8.0'
|
41
|
+
spec.add_runtime_dependency 'activeresource', '5.0.0'
|
42
|
+
spec.add_runtime_dependency 'pastel', '0.7.1'
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rncher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sandsu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-20 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rest-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.2
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: thor
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.20.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.20.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 5.1.3
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 5.1.3
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: faye-websocket
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.10.7
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.10.7
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: tty-table
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.8.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.8.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: activeresource
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 5.0.0
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 5.0.0
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pastel
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.7.1
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.7.1
|
153
|
+
description: description
|
154
|
+
email:
|
155
|
+
- sanderson@digaeita.com.br
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- ".rspec"
|
162
|
+
- ".travis.yml"
|
163
|
+
- CODE_OF_CONDUCT.md
|
164
|
+
- Gemfile
|
165
|
+
- LICENSE.txt
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- bin/console
|
169
|
+
- bin/rncher
|
170
|
+
- bin/setup
|
171
|
+
- lib/active_resource/base_collection.rb
|
172
|
+
- lib/active_resource/keyboard_handler.rb
|
173
|
+
- lib/active_resource/remote_connection.rb
|
174
|
+
- lib/resources/app.rb
|
175
|
+
- lib/resources/base.rb
|
176
|
+
- lib/resources/container.rb
|
177
|
+
- lib/resources/credential.rb
|
178
|
+
- lib/resources/service.rb
|
179
|
+
- lib/rncher.rb
|
180
|
+
- lib/rncher/app.rb
|
181
|
+
- lib/rncher/cli.rb
|
182
|
+
- lib/rncher/service.rb
|
183
|
+
- lib/rncher/version.rb
|
184
|
+
- rncher.gemspec
|
185
|
+
homepage: https://digaeita.com.br
|
186
|
+
licenses:
|
187
|
+
- MIT
|
188
|
+
metadata:
|
189
|
+
allowed_push_host: https://rubygems.org
|
190
|
+
post_install_message:
|
191
|
+
rdoc_options: []
|
192
|
+
require_paths:
|
193
|
+
- lib
|
194
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
195
|
+
requirements:
|
196
|
+
- - ">="
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: '0'
|
199
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
200
|
+
requirements:
|
201
|
+
- - ">="
|
202
|
+
- !ruby/object:Gem::Version
|
203
|
+
version: '0'
|
204
|
+
requirements: []
|
205
|
+
rubyforge_project:
|
206
|
+
rubygems_version: 2.6.11
|
207
|
+
signing_key:
|
208
|
+
specification_version: 4
|
209
|
+
summary: summary
|
210
|
+
test_files: []
|