mescal-cli 0.0.1

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: d221c5cb1ace9322bb0dc3b922852de1afdcd829
4
+ data.tar.gz: 3bcd6fffa6e2b983e5bfb598ae6a5be9c898d0ca
5
+ SHA512:
6
+ metadata.gz: e46747263ae7bf5880be5deec6ca69a42c63792f3a525df83c5743c6be18645849ffcf5c8054e40aa2ef045edade6bd70574c3a57f389218ce990102ac9b077d
7
+ data.tar.gz: e6115b916eac2ccc46398cd98f1e1fd4c4c11d73a09c19a78df05b07199102888d6cfae3b25c52f5b2647e71f2cdf4e9ead3ef683927bd1252f94fb687030117
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .mescal.json
2
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mescal-cli (0.0.1)
5
+ multi_json (~> 1.11, >= 1.11.0)
6
+ rest-client (~> 1.8, >= 1.8.0)
7
+ yajl-ruby (~> 1.2, >= 1.2.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ domain_name (0.5.24)
13
+ unf (>= 0.0.5, < 1.0.0)
14
+ http-cookie (1.0.2)
15
+ domain_name (~> 0.5)
16
+ mime-types (2.4.3)
17
+ multi_json (1.11.0)
18
+ netrc (0.10.3)
19
+ rake (10.4.2)
20
+ rest-client (1.8.0)
21
+ http-cookie (>= 1.0.2, < 2.0)
22
+ mime-types (>= 1.16, < 3.0)
23
+ netrc (~> 0.7)
24
+ unf (0.1.4)
25
+ unf_ext
26
+ unf_ext (0.0.7.1)
27
+ yajl-ruby (1.2.1)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.7)
34
+ mescal-cli!
35
+ rake (~> 10.0)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chris Kite
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # mescal-cli
data/bin/mescal ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'mescal-cli'
5
+
6
+ config = MescalCli::Config.load('.mescal.json')
7
+ cli = MescalCli::Cli.new(config)
8
+ cli.run!
data/example.conf ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "mescal": "127.0.0.1",
3
+ "image": "ubuntu:14.04",
4
+ "cmd": "/bin/sleep 10"
5
+ }
@@ -0,0 +1,72 @@
1
+ require 'etc'
2
+ require 'thread'
3
+
4
+ module MescalCli
5
+ class Cli
6
+ def initialize(config)
7
+ @mode = ARGV.first
8
+ usage and System.exit(1) unless !!@mode
9
+ @config = config
10
+ @client = Client.new(config['mescal'])
11
+ @image = config['image']
12
+ @cmd = ARGV[1] || config['cmd']
13
+ @user = Etc.getlogin
14
+ @sshCmd = config['sshCmd']
15
+ end
16
+
17
+ def usage
18
+ puts "mescal [run|ssh]"
19
+ end
20
+
21
+ def run!
22
+ case @mode
23
+ when "run" then run
24
+ when "ssh" then ssh
25
+ end
26
+ end
27
+
28
+ def run
29
+ puts "Sending task to Mescal..."
30
+ task = Task.create(@client, @image, @cmd, @user)
31
+ run = true
32
+ threads = []
33
+ pailer_started = false
34
+ while(run) do
35
+ sleep(2)
36
+ state = task.state
37
+ task.update!
38
+ if state != task.state
39
+ puts "State: #{task.state}"
40
+ if task.state != "TASK_PENDING" && !pailer_started
41
+ pailer_started = true
42
+ ["stdout", "stderr"].each do |std|
43
+ threads << Thread.new { Pailer.new(task, @config['mescal'], std).run! }
44
+ end
45
+ end
46
+ end
47
+
48
+ run = !task.done?
49
+ end
50
+ sleep(10)
51
+ threads.each { |t| t.kill }
52
+ end
53
+
54
+ def ssh
55
+ task = Task.create(@client, @image, @sshCmd, @user)
56
+ run = true
57
+ while(run) do
58
+ sleep(2)
59
+ state = task.state
60
+ task.update!
61
+ if state != task.state
62
+ if task.state != "TASK_PENDING"
63
+ Ssh.new(task).run!
64
+ end
65
+ end
66
+
67
+ run = !task.done?
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,39 @@
1
+ module MescalCli
2
+ class Client
3
+ def initialize(host)
4
+ @base_url = "http://#{host}/"
5
+ end
6
+
7
+ def task
8
+ TaskClient.new(@base_url)
9
+ end
10
+
11
+ def slave
12
+ SlaveClient.new(@base_url)
13
+ end
14
+ end
15
+
16
+ class SlaveClient
17
+ def initialize(base_url)
18
+ @base_url = base_url + "slaves"
19
+ end
20
+
21
+ def get(id)
22
+ RestClient.get "#{@base_url}/#{id}"
23
+ end
24
+ end
25
+
26
+ class TaskClient
27
+ def initialize(base_url)
28
+ @base_url = base_url + "tasks"
29
+ end
30
+
31
+ def create(image, cmd, user)
32
+ RestClient.post @base_url, image: image, cmd: cmd, port: 22
33
+ end
34
+
35
+ def get(id)
36
+ RestClient.get "#{@base_url}/#{id}"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,22 @@
1
+ require 'delegate'
2
+
3
+ module MescalCli
4
+ class Config < Delegator
5
+ def self.load(file)
6
+ Config.new(MultiJson.load(open(file).read))
7
+ end
8
+
9
+ def initialize(obj)
10
+ super
11
+ @delegate_obj = obj
12
+ end
13
+
14
+ def __getobj__
15
+ @delegate_obj
16
+ end
17
+
18
+ def __setobj__(obj)
19
+ @delegate_obj = obj
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module MescalCli
5
+ class Pailer
6
+ def initialize(task, host, std)
7
+ @task, @host, @std = task, host, std
8
+ end
9
+
10
+ def run!
11
+ uri = URI("http://#{@host}/#{@std}/#{@task.id}")
12
+ STDOUT.flush
13
+ Net::HTTP.start(uri.host, uri.port) do |http|
14
+ http.request Net::HTTP::Get.new(uri) do |response|
15
+ response.read_body do |chunk|
16
+ print chunk
17
+ STDOUT.flush
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module MescalCli
2
+ class Ssh
3
+ def initialize(task)
4
+ @task = task
5
+ end
6
+
7
+ def run!
8
+ ip = @task.slave_ip
9
+ port = @task.ssh_port
10
+ exec "ssh -o LogLevel=quiet -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@#{ip} -p #{port}"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ module MescalCli
2
+ class Task
3
+ attr_reader :id, :state, :slave_id
4
+
5
+ def self.create(client, image, cmd, user)
6
+ resp = client.task.create(image, cmd, user)
7
+ obj = MultiJson.load(resp)
8
+ Task.new(client, obj['id'], image, cmd, user)
9
+ end
10
+
11
+ def initialize(client, id, image, cmd, user)
12
+ @client, @id, @image, @cmd, @user = client, id, image, cmd, user
13
+ end
14
+
15
+ def update!
16
+ resp = @client.task.get(@id)
17
+ obj = MultiJson.load(resp)
18
+ @state = obj['state']
19
+ @slave_id = obj['slaveId']
20
+ @ports = obj['ports']
21
+ end
22
+
23
+ def done?
24
+ ['TASK_FINISHED', 'TASK_LOST', 'TASK_FAILED'].include?(@state)
25
+ end
26
+
27
+ def slave_ip
28
+ resp = @client.slave.get(@slave_id)
29
+ obj = MultiJson.load(resp)
30
+ obj['hostname']
31
+ end
32
+
33
+ def ssh_port
34
+ @ports.find { |p| p[0] == 22 }[1]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module MescalCli
2
+ VERSION = "0.0.1"
3
+ end
data/lib/mescal-cli.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+ require 'mescal-cli/config'
5
+ require 'mescal-cli/task'
6
+ require 'mescal-cli/pailer'
7
+ require 'mescal-cli/ssh'
8
+ require 'mescal-cli/client'
9
+ require 'mescal-cli/cli'
10
+
11
+ module MescalCli
12
+
13
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mescal-cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mescal-cli"
8
+ spec.version = MescalCli::VERSION
9
+ spec.authors = ["Chris Kite"]
10
+ spec.email = ["chris@chriskite.com"]
11
+ spec.summary = %q{Mescal CLI}
12
+ spec.homepage = "http://github.com/chriskite/mescal-cli"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_runtime_dependency 'rest-client', '~> 1.8', '>= 1.8.0'
24
+ spec.add_runtime_dependency 'yajl-ruby', '~> 1.2', '>= 1.2.1'
25
+ spec.add_runtime_dependency 'multi_json', '~> 1.11', '>= 1.11.0'
26
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mescal-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Kite
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-08 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 1.8.0
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '1.8'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.8.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: yajl-ruby
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.2'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.2.1
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.2'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.2.1
81
+ - !ruby/object:Gem::Dependency
82
+ name: multi_json
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.11'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.11.0
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.11'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 1.11.0
101
+ description:
102
+ email:
103
+ - chris@chriskite.com
104
+ executables:
105
+ - mescal
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - ".gitignore"
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE
113
+ - README.md
114
+ - bin/mescal
115
+ - example.conf
116
+ - lib/mescal-cli.rb
117
+ - lib/mescal-cli/cli.rb
118
+ - lib/mescal-cli/client.rb
119
+ - lib/mescal-cli/config.rb
120
+ - lib/mescal-cli/pailer.rb
121
+ - lib/mescal-cli/ssh.rb
122
+ - lib/mescal-cli/task.rb
123
+ - lib/mescal-cli/version.rb
124
+ - mescal-cli.gemspec
125
+ homepage: http://github.com/chriskite/mescal-cli
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.5
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Mescal CLI
149
+ test_files: []