ona 0.4.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2ebee33a8dd4f8e079ec27110ef2ddfd45b2ab845774c0bcf1047ded01834cb
4
- data.tar.gz: d0f12db18353a9304123342fc0e52398a1fdcaa5006abfdd65c2344630efc085
3
+ metadata.gz: 00cac1d1f46370554c113bd8046a84cd79da23f74531424140ffd1445c927374
4
+ data.tar.gz: db80fffc3243a7ad43363d292f27b7d7e855693cd19d1cfca9fe201a236cf251
5
5
  SHA512:
6
- metadata.gz: '04902e951aef11a85ee492f583719b5c2146f6f2913f24362d42f40ea47b83ee64874851fb6bbfaa0b2173c13df7907858acbf9211d101ebc5adb782ee38fc3e'
7
- data.tar.gz: 588c8ec69e18acfe8df482880efde255eb3cb6f233548ec2d178f1bef0b6c2389f382edddbd403baa89f9a1ee52a2f5d0a9b97549a74800e169a03c9dfc2bbec
6
+ metadata.gz: 5e59d548206b8e69028e9bb5c337f4cd3043f46e00e2a21a79b5aae8c9cbee64cd2c8cc4068bfcfdf735d407cef5388398bef5fd073249ad1104b84b598778d9
7
+ data.tar.gz: '0604507116913163d44be4983b2efa1e09175c965c17c593694b4061799824b8f972344fcc7e591de2f9def4cd9f99725fc3f039e8b784c9371420ef48699b92'
data/bin/ona CHANGED
@@ -8,15 +8,7 @@ unless File.exists? 'Onafile'
8
8
  abort 'Please create a file named:[Onafile] to start the shell.'
9
9
  end
10
10
 
11
- stack = Ona::Stack.new
12
11
  eval File.read 'Onafile'
13
- cli = Ona::Cli.new(stack)
14
12
 
15
- loop do
16
- line = Readline.readline("Ona> ", true)
17
- if (line == 'quit') || (line == 'exit')
18
- puts 'bye.'
19
- break
20
- end
21
- cli.parse(line)
22
- end
13
+ Ona.main
14
+
data/lib/ona.rb CHANGED
@@ -1,4 +1,164 @@
1
- require 'ona_cli'
2
- require 'ona_stack'
3
- require 'ona_server'
4
- require 'ona_instructions'
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ostruct'
4
+ require 'readline'
5
+ require 'isna'
6
+
7
+ class Ona
8
+ def self.resource(name, attributes)
9
+ @resources ||= {}
10
+ @resources[name] ||= {}
11
+ @resources[name][:class] = Struct.new(*attributes)
12
+ @resources[name][:entries] = []
13
+ @resources[name][:handlers] = []
14
+ @resources[name][:attributes] = attributes
15
+ _create_to_s_method(@resources[name])
16
+ nil
17
+ end
18
+ def self._create_to_s_method(resource)
19
+ attribute_sizes = resource[:attributes].map do |attribute|
20
+ attribute.to_s.size
21
+ end
22
+ resource[:class].send(:define_method, :to_s) do
23
+ max_attribute_length = attribute_sizes.sort.last + 4
24
+ resource[:attributes].sort.map do |attribute|
25
+ key = attribute.to_s.rjust(max_attribute_length, ' ')
26
+ "#{key.to_ansi.green.to_s} : #{send(attribute)}"
27
+ end * "\n"
28
+ end
29
+ end
30
+ def self.register(name, &block)
31
+ object = @resources[name][:class].new
32
+ block.call(object)
33
+ @resources[name][:entries].push(object)
34
+ nil
35
+ end
36
+ def self.action(hash, &block)
37
+ unless hash[:find]
38
+ _generic_action(hash, &block)
39
+ return
40
+ end
41
+ unless hash[:token]
42
+ $stderr.puts "No token for: #{hash.inspect}"
43
+ exit 1
44
+ end
45
+ _single_action(hash, &block)
46
+ end
47
+ def self._generic_action(hash, &block)
48
+ hash[:block] = block
49
+ @resources[hash[:resource]][:handlers].push(hash)
50
+ nil
51
+ end
52
+ def self._single_action(hash, &block)
53
+ Ona._generic_action(hash) do |resources, command, regex|
54
+ wanted_id = hash[:token].call(command.scan(regex))
55
+ wanted_id = wanted_id.to_i
56
+ resources.each_with_index do |array, id|
57
+ next if id != wanted_id
58
+ block.call(array, id)
59
+ end
60
+ end
61
+ end
62
+ def self.run(command)
63
+ @resources.each do |name, resource|
64
+ resource[:handlers].each do |handler|
65
+ next unless handler[:regex].match(command)
66
+ handler[:block].call(resource[:entries], command, handler[:regex])
67
+ return
68
+ end
69
+ end
70
+ nil
71
+ end
72
+ def self.prompt=(string)
73
+ @prompt = string
74
+ end
75
+ def self.prompt
76
+ @prompt
77
+ end
78
+ def self.main(use_defaults = true)
79
+ defaults if use_defaults
80
+ _prompt = (prompt || 'Ona').to_ansi.cyan.to_s + '> '
81
+ while input = Readline.readline(_prompt, true)
82
+ Ona.run(input)
83
+ end
84
+ puts ''
85
+ end
86
+ def self._get_max_regex_length
87
+ max = 0
88
+ @resources.each do |name, resource|
89
+ resource[:handlers].each do |handler|
90
+ if max < handler[:regex].inspect.size
91
+ max = handler[:regex].inspect.size
92
+ end
93
+ end
94
+ end
95
+ max
96
+ end
97
+ def self._get_max_example_length
98
+ max = 0
99
+ @resources.each do |name, resource|
100
+ resource[:handlers].each do |handler|
101
+ if max < handler[:example].size
102
+ max = handler[:example].size
103
+ end
104
+ end
105
+ end
106
+ max
107
+ end
108
+ def self._get_max_name_length
109
+ max = 0
110
+ @resources.each do |name, resource|
111
+ if max < name.size
112
+ max = name.size
113
+ end
114
+ end
115
+ max
116
+ end
117
+ def self.help
118
+ @resources.each do |name, resource|
119
+ pretty_name = "[#{name}]".to_s.rjust(_get_max_name_length + 2, ' ')
120
+ pretty_name = pretty_name.to_ansi.cyan.to_s
121
+ resource[:handlers].sort do |a, b|
122
+ a[:example] <=> b[:example]
123
+ end.each do |handler|
124
+ pretty_regex = handler[:regex].inspect.ljust(_get_max_regex_length, ' ')
125
+ pretty_regex = pretty_regex.to_s.to_ansi.pink.to_s
126
+ pretty_example = handler[:example].ljust(_get_max_example_length, ' ')
127
+ pretty_example = pretty_example.to_s.to_ansi.yellow.to_s
128
+ puts "#{pretty_name} #{pretty_example} #{handler[:text]}"
129
+ end
130
+ end
131
+ end
132
+ def self.defaults
133
+ Ona.resource(:any, [:empty])
134
+
135
+ Ona.action(
136
+ :regex => /(^)(quit|exit)($)/,
137
+ :resource => :any,
138
+ :text => 'Exit this program.',
139
+ :example => 'quit'
140
+ ) do |resource, command, regex|
141
+ puts 'bye.'
142
+ exit 0
143
+ end
144
+
145
+ Ona.action(
146
+ :regex => /^help$/,
147
+ :resource => :any,
148
+ :text => 'Print help.',
149
+ :example => 'help'
150
+ ) do |resource, command, regex|
151
+ Ona.help
152
+ end
153
+ end
154
+ def self.confirm(text, expected)
155
+ puts ''
156
+ puts "# " + " #{text} ".to_ansi.white.red_background.to_s
157
+ puts "# " + '=' * 78
158
+ puts "Type [#{expected.to_ansi.green.to_s}] to continue. or anything else to skip."
159
+ print 'What to do? >'
160
+ input = gets.chomp
161
+ input == expected
162
+ end
163
+ end
164
+
data/ona.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ona"
8
- s.version = "0.4.1"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["kazuyoshi tlacaelel"]
@@ -27,10 +27,6 @@ Gem::Specification.new do |s|
27
27
  "VERSION",
28
28
  "bin/ona",
29
29
  "lib/ona.rb",
30
- "lib/ona_cli.rb",
31
- "lib/ona_instructions.rb",
32
- "lib/ona_server.rb",
33
- "lib/ona_stack.rb",
34
30
  "ona.gemspec",
35
31
  "test/helper.rb",
36
32
  "test/test_ona.rb"
@@ -42,6 +38,20 @@ Gem::Specification.new do |s|
42
38
 
43
39
  if s.respond_to? :specification_version then
44
40
  s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<rake>, ["= 0.8.7"])
44
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
45
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
46
+ else
47
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
48
+ s.add_dependency(%q<jeweler>, [">= 0"])
49
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
45
55
  end
46
56
  end
47
57
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ona
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuyoshi tlacaelel
@@ -9,7 +9,49 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2013-12-14 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thoughtbot-shoulda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  description: Simple shell for deployment.
14
56
  email: kazu.dev@gmail.com
15
57
  executables:
@@ -28,10 +70,6 @@ files:
28
70
  - VERSION
29
71
  - bin/ona
30
72
  - lib/ona.rb
31
- - lib/ona_cli.rb
32
- - lib/ona_instructions.rb
33
- - lib/ona_server.rb
34
- - lib/ona_stack.rb
35
73
  - ona.gemspec
36
74
  - test/helper.rb
37
75
  - test/test_ona.rb
@@ -53,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
91
  - !ruby/object:Gem::Version
54
92
  version: '0'
55
93
  requirements: []
56
- rubygems_version: 3.0.6
94
+ rubygems_version: 3.0.3
57
95
  signing_key:
58
96
  specification_version: 3
59
97
  summary: Ona -- Deployment symplified.
data/lib/ona_cli.rb DELETED
@@ -1,192 +0,0 @@
1
- module Ona
2
-
3
- class Cli
4
-
5
- TRANSLATION = {
6
- 'show' => { :method => :show, :id_required => true },
7
- 'setup' => { :method => :setup, :id_required => true },
8
- 'deploy' => { :method => :deploy, :id_required => true },
9
- 'ssh#' => { :method => :ssh_root, :id_required => true },
10
- 'ssh' => { :method => :ssh_deploy,:id_required => true },
11
- 'go' => { :method => :go_deploy, :id_required => true },
12
- 'go#' => { :method => :go_root, :id_required => true },
13
- 'http' => { :method => :http, :id_required => true },
14
- 'key' => { :method => :key, :id_required => true },
15
- 'keys' => { :method => :keys, :id_required => true },
16
- 'rake' => { :method => :rake, :id_required => true },
17
- 'ls' => { :method => :ls, :id_required => false },
18
- 'help' => { :method => :help, :id_required => false },
19
- }
20
-
21
- def initialize(stack)
22
- @stack = stack
23
- @instructions = Ona::Instructions.new(/[a-z]+[#]*/, /\d+/)
24
- end
25
-
26
- def parse command_string
27
- # Handle this separately for now.
28
- return run_instruction('ls', 0) if command_string == 'ls'
29
- return run_instruction('help', 0) if command_string == 'help'
30
-
31
- @instructions.define_command_string(command_string)
32
- group = @instructions.group_targets_by_instruction
33
- copy = group.dup
34
- copy.pop
35
-
36
- # Only last group has ids?
37
- if copy.all? { |x| x.values == [[]] }
38
- @instructions.instructions.each do |instruction|
39
- @instructions.targets.each do |target|
40
- run_instruction instruction, target
41
- end
42
- end
43
- return
44
- end
45
-
46
- # Run by group.
47
- group.each do |hash|
48
- instruction, targets = hash.to_a[0][0], hash.to_a[0][1]
49
- targets.each do |target|
50
- run_instruction instruction, target
51
- end
52
- end
53
- end
54
-
55
- def run_instruction instruction, target
56
- return unless TRANSLATION.keys.include?(instruction)
57
- hash = TRANSLATION[instruction]
58
- cmd = "#{TRANSLATION[instruction][:method]}"
59
- if (hash[:id_required])
60
- cmd << "(#{target.to_i})"
61
- end
62
- eval cmd
63
- end
64
-
65
- def help
66
- puts "
67
- Ona -- Deployment simplified.
68
-
69
- deploy 1 # Deploy a specific server
70
- exit # Same as *quit*
71
- go # Go to server without opening a new window.
72
- go# # Same as go but do it as root.
73
- help # Show this help
74
- http 1 # Open the server in default browser.
75
- key 1 # Uploads my public ssh-key to remote server (root)
76
- keys # Uploads my public ssh-key to all servers (root)
77
- ls # Short list for available servers
78
- quit # Termintes the ona shell.
79
- rake # Shows rake tasks.
80
- setup 1 # Upload ssh-keys and bootstrap server
81
- show 1 # Detailed info for a specific server
82
- ssh 1 # Open a ssh session as *deploy* on remote server (new window)
83
- ssh# 1 # Open a ssh session as *root* to a remote server (new window)
84
-
85
- --
86
-
87
- Note: 1 is a server id, you can use many ids!
88
-
89
- show 1 2 3 # Will display info for three servers.
90
-
91
- "
92
- end
93
-
94
- def http server_id
95
- @stack.find_all(server_id).each do |server|
96
- system server.to_http
97
- end
98
- end
99
-
100
- def key server_id
101
- @stack.find_all(server_id).each do |server|
102
- puts 'Setting up key for root'
103
- system server.setup_ssh
104
- end
105
- end
106
-
107
- def show server_id
108
- @stack.find_all(server_id).each do |server|
109
- puts server.to_s
110
- end
111
- end
112
-
113
- def ls
114
- @stack.to_a.each do |server|
115
- puts server.to_short_s
116
- end
117
- end
118
-
119
- def ssh_deploy server_id
120
- @stack.find_all(server_id).each do |server|
121
- system server.to_ssh 'deploy'
122
- end
123
- end
124
-
125
- def ssh_root server_id
126
- @stack.find_all(server_id).each do |server|
127
- system server.to_ssh 'root'
128
- end
129
- end
130
-
131
- def go_deploy server_id
132
- @stack.find_all(server_id).each do |server|
133
- system server.to_go 'deploy'
134
- end
135
- end
136
-
137
- def go_root server_id
138
- @stack.find_all(server_id).each do |server|
139
- system server.to_go 'root'
140
- end
141
- end
142
-
143
- def deploy server_id
144
- @stack.find_all(server_id).each do |server|
145
- prompt_for_continuation server
146
- system server.say_sure_to_deploy
147
- line = gets
148
- next unless line.chomp == 'yes'
149
- system server.deploy
150
- system server.say_deployed
151
- end
152
- end
153
-
154
- def setup server_id
155
- key server_id
156
- @stack.find_all(server_id).each do |server|
157
- prompt_for_continuation server
158
- system server.say_sure_to_setup
159
- line = gets
160
- next unless line.chomp == 'yes'
161
- system server.bootstrap
162
- system server.say_finished_setup
163
- end
164
- end
165
-
166
- def rake server_id
167
- @stack.find_all(server_id).each do |server|
168
- server.to_s
169
- server.rake.each do |task|
170
- puts task
171
- end
172
- end
173
- end
174
-
175
- def keys
176
- @stack.to_a.each do |server|
177
- puts 'Setting up key for root'
178
- system server.setup_ssh
179
- end
180
- end
181
-
182
- protected
183
-
184
- def prompt_for_continuation server
185
- puts server.to_s
186
- puts 'Type [yes] to continue. or anything else to skip.'
187
- print 'What to do? >'
188
- end
189
-
190
- end
191
-
192
- end
@@ -1,43 +0,0 @@
1
- module Ona
2
-
3
- class Instructions
4
-
5
- def initialize instruction_matcher, target_matcher
6
- define_instruction_matcher instruction_matcher
7
- define_target_matcher target_matcher
8
- end
9
-
10
- def define_command_string command_string
11
- @command_string = command_string
12
- end
13
-
14
- def define_instruction_matcher matcher_regex
15
- @instruction_matcher = matcher_regex
16
- end
17
-
18
- def define_target_matcher matcher_regex
19
- @target_matcher = matcher_regex
20
- end
21
-
22
- def targets
23
- @command_string.scan(@target_matcher)
24
- end
25
-
26
- def instructions
27
- @command_string.scan(@instruction_matcher)
28
- end
29
-
30
- def group_targets_by_instruction
31
- group = []
32
- chunks = (@command_string.split(@instruction_matcher) - [""])
33
- return group if chunks.size != instructions.size
34
- chunks.each_with_index do |instruction_targets, index|
35
- instruction_targets = (instruction_targets.scan(@target_matcher) - [])
36
- group << { instructions[index] => instruction_targets }
37
- end
38
- group
39
- end
40
-
41
- end
42
-
43
- end
data/lib/ona_server.rb DELETED
@@ -1,79 +0,0 @@
1
- module Ona
2
-
3
- class Server
4
-
5
- attr_accessor :id, :role, :ip, :pass, :desc, :dna
6
-
7
- def load_block id, &block
8
- @id = id
9
- yield(self)
10
- end
11
-
12
- def setup_ssh
13
- "rake upload_ssh_key server=root@#{ip} pass=#{pass} key=#{local_key}"
14
- end
15
-
16
- def say_deployed
17
- "say #{desc} deployed"
18
- end
19
-
20
- def say_finished_setup
21
- "say #{desc} setup terminated"
22
- end
23
-
24
- def say_sure_to_setup
25
- "say 'Are you sure to setup #{desc}'"
26
- end
27
-
28
- def say_sure_to_deploy
29
- "say 'Are you sure to deploy #{desc}'"
30
- end
31
-
32
- def bootstrap
33
- "rake bootstrap server=root@#{ip}"
34
- end
35
-
36
- def deploy
37
- "rake cook server=root@#{ip} dna=#{dna} instance_role=#{role}"
38
- end
39
-
40
- def to_ssh user
41
- "open ssh://#{user}@#{ip}"
42
- end
43
-
44
- def to_go user
45
- "ssh #{user}@#{ip}"
46
- end
47
-
48
- def to_http
49
- "open http://#{ip}"
50
- end
51
-
52
- def to_short_s
53
- "#{id}".ljust(5) + "#{desc}"
54
- end
55
-
56
- def to_s
57
- "
58
- #{id} - #{desc}
59
-
60
- Id #{id}
61
- Ip #{ip}
62
- Dna #{dna}
63
- Role #{role}
64
- Password #{pass}
65
-
66
- "
67
- end
68
-
69
- def rake
70
- [setup_ssh, bootstrap, deploy]
71
- end
72
-
73
- def local_key
74
- File.join(ENV['HOME'], '.ssh/id_rsa.pub')
75
- end
76
-
77
- end
78
-
79
- end
data/lib/ona_stack.rb DELETED
@@ -1,39 +0,0 @@
1
- module Ona
2
-
3
- class Stack
4
-
5
- def initialize
6
- @list = []
7
- @id = 0
8
- end
9
-
10
- def append &block
11
- @id += 1
12
- server = Server.new
13
- server.load_block(@id, &block)
14
- @list << server
15
- end
16
-
17
- def find num
18
- @list.each do |server|
19
- return server if server.id == num
20
- end
21
- nil
22
- end
23
-
24
- def find_all *args
25
- args.map { |id| find id }.compact
26
- end
27
-
28
- def exists? num
29
- return false if find(num).nil?
30
- true
31
- end
32
-
33
- def to_a
34
- @list
35
- end
36
-
37
- end
38
-
39
- end