ridoku 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ #
2
+ # Command: service
3
+ #
4
+
5
+ require 'ridoku/base'
6
+
7
+
8
+ module Ridoku
9
+ module Services
10
+ @service_list = []
11
+ def self.add_service(serv)
12
+ @service_list << serv.to_s
13
+ end
14
+
15
+ def self.list
16
+ @service_list
17
+ end
18
+ end
19
+ end
20
+
21
+ require_rel 'services/'
22
+
23
+ module Ridoku
24
+ register :service
25
+
26
+ class Service < Base
27
+ attr_accessor :services
28
+
29
+ # "services":[{
30
+ # "name": "rabbitmq",
31
+ # "layers": [
32
+ # "worker"
33
+ # ]
34
+ # },...]
35
+
36
+ # Service name should be the same used to define configurations, ideally.
37
+
38
+ def run(command = nil)
39
+ command ||= Base.config[:command]
40
+ command.shift
41
+ sub_command = command.shift
42
+
43
+ environment = load_environment
44
+ case sub_command
45
+ when 'list'
46
+ list
47
+ when 'set', 'add'
48
+ add
49
+ when 'delete', 'remove', 'rm'
50
+ delete
51
+ when 'config', nil
52
+ config(ARGV)
53
+ else
54
+ print_service_help
55
+ end
56
+ end
57
+
58
+ protected
59
+
60
+ def load_environment
61
+ Base.fetch_stack
62
+ self.services = (Base.custom_json['services'] ||= {})
63
+ end
64
+
65
+ def print_service_help
66
+ $stderr.puts <<-EOF
67
+ Command: service
68
+
69
+ List/Modify the current app's associated workers.
70
+ service[:list] lists the services and what layer they are assigned
71
+ service:add service:layer, e.g., rabbitmq:workers
72
+ service:delete delete service information from stack and layer
73
+ service:config service:list for description of service configuration layer
74
+
75
+ Example 'service:list' output:
76
+ Services for MyStack:
77
+ [layer] service
78
+
79
+ examples:
80
+ $ services
81
+ No services specified!
82
+ $ services:add rabbitmq:workers
83
+ $ services:add nagios:workers # not yet implemented
84
+ $ services:list
85
+ Services for MyStack:
86
+ [layer] service
87
+ [workers] rabbitmq
88
+ [workers] nagios
89
+ $ services:delete nagios
90
+ Services for MyStack:
91
+ [layer] service
92
+ [workers] rabbitmq
93
+ $ services:config rabbitmq:help
94
+ ...
95
+ EOF
96
+ end
97
+
98
+ def list
99
+ if services.length == 0
100
+ $stdout.puts 'No services specified!'
101
+ else
102
+ $stdout.puts "Services for #{$stdout.colorize(Base.config[:stack], [:bold, :green])}:"
103
+ services.each do |service|
104
+ if service['layers'].is_a?(Array)
105
+ $stdout.puts " [#{$stdout.colorize(service['layers'].join(','), :bold)}] #{service['name']}"
106
+ else
107
+ $stdout.puts " [#{$stdout.colorize('No Layers Specified', [:bold, :red])}] #{service['name']}"
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ def add
114
+ end
115
+
116
+ def delete
117
+ end
118
+
119
+ def config(argv)
120
+ return list unless argv.length > 0
121
+ klass, cmd = argv.shift.split(/:/)
122
+
123
+ if klass.nil?
124
+ $stdout.puts 'Available services:'
125
+ Ridoku::Services.list.each { |service| $stdout.puts " #{service}" }
126
+ return
127
+ end
128
+
129
+ begin
130
+ command = Ridoku::Services.const_get(
131
+ klass.capitalize
132
+ ).new
133
+ rescue => e
134
+ $stderr.puts "Invalid service:config command specified: #{klass}"
135
+ puts e.to_s if Ridoku::Base.config[:debug]
136
+ print_help
137
+ exit 1
138
+ end
139
+
140
+ begin
141
+ command.run cmd, argv
142
+ rescue Ridoku::InvalidConfig => e
143
+ $stderr.puts "#{e.error.to_s.capitalize} #{e.type.to_s} specified."
144
+ $stderr.puts 'Use the `list` command to see relavent info.'
145
+ print_help
146
+ exit 1
147
+ rescue Ridoku::NoSshAccess
148
+ $stderr.puts 'Your user does not have access to ssh on the specified stack.'
149
+ print_help
150
+ exit 1
151
+ rescue ArgumentError => e
152
+ raise e if Ridoku::Base.config[:debug]
153
+ $stderr.puts e.to_s
154
+ end
155
+
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+
3
+ module Ridoku
4
+ module Services
5
+ add_service :postgres
6
+
7
+ class Postgres
8
+ attr_accessor :postgresql
9
+
10
+ def run(cmd, args)
11
+ sub_command = args.shift
12
+
13
+ case cmd
14
+ when 'config'
15
+ config(sub_command)
16
+ when 'describe', 'list', 'show', nil
17
+ show(sub_command)
18
+ else
19
+ print_help
20
+ end
21
+ end
22
+
23
+ def print_help
24
+ $stderr.puts <<-EOF
25
+ Command: service postgres
26
+
27
+ List/Modify the current app's associated workers.
28
+ postgres[:config] lists configuration
29
+
30
+ EOF
31
+ end
32
+
33
+ def setup
34
+ Ridoku::Base.fetch_stack
35
+ self.postgresql = (Base.custom_json['postgresql'] ||= {})
36
+ end
37
+
38
+ def config(sub)
39
+ $stderr.puts ''
40
+ end
41
+
42
+ def show_url()
43
+ Ridoku::Base.fetch_app
44
+ Ridoku::Base.fetch_instance('postgresql', force: true)
45
+
46
+ app = Ridoku::Base.app[:shortname]
47
+ dbase = postgresql['databases'].select do |db|
48
+ db['app'] == app
49
+ end.first
50
+
51
+ unless dbase
52
+ $stderr.puts "Application #{$stderr.colorize('app', :red)} has no "\
53
+ "databases configured."
54
+ return
55
+ end
56
+
57
+ dbase['adapter'] = 'postgres'
58
+ dbase['password'] = dbase['user_password']
59
+ dbase['port'] = postgresql['config']['port']
60
+ dbase['host'] = Ridoku::Base.instances.first[:public_ip]
61
+
62
+ $stdout.puts $stdout.colorize(Ridoku::Db.gen_dbase_url(dbase), :bold)
63
+ end
64
+
65
+ def show(sub)
66
+ setup
67
+
68
+ if sub == 'url'
69
+ return show_url
70
+ end
71
+
72
+ $stdout.puts 'Postgresql configuration:'
73
+ puts JSON.pretty_generate(postgresql)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ module Ridoku
4
+ module Services
5
+ add_service :rabbitmq
6
+
7
+ class Rabbitmq
8
+ attr_accessor :mqconfig
9
+
10
+ def run(cmd, args)
11
+ sub_command = args.shift
12
+
13
+ case cmd
14
+ when 'config', nil
15
+ config(sub_command)
16
+ when 'describe', 'list', 'show'
17
+ show(sub_command)
18
+ else
19
+ print_help
20
+ end
21
+ end
22
+
23
+ def print_help
24
+ $stderr.puts <<-EOF
25
+ Command: service:change rabbitmq
26
+
27
+ List/Modify the current app's associated workers.
28
+ rabbitmq[:config] lists configuration
29
+
30
+ EOF
31
+ end
32
+
33
+ def setup
34
+ Ridoku::Base.fetch_stack
35
+ self.mqconfig = (Base.custom_json['rabbitmq'] ||= {})
36
+ end
37
+
38
+ def config(sub)
39
+ $stderr.puts 'RabbitMQ Config'
40
+ end
41
+
42
+ def show(sub)
43
+ setup
44
+ puts JSON.pretty_generate(mqconfig)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module Ridoku
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,138 @@
1
+ #
2
+ # Command: worker
3
+ #
4
+
5
+ require 'ridoku/base'
6
+
7
+ module Ridoku
8
+ register :worker
9
+
10
+ class Worker < Base
11
+ attr_accessor :workers
12
+
13
+ def run
14
+ command = Base.config[:command]
15
+ sub_command = (command.length > 0 && command[1]) || nil
16
+
17
+ environment = load_environment
18
+
19
+ case sub_command
20
+ when 'list', nil
21
+ list
22
+ when 'set', 'add'
23
+ add
24
+ when 'delete', 'remove', 'rm'
25
+ delete
26
+ when 'push', 'reconfigure'
27
+ reconfigure
28
+ else
29
+ print_worker_help
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ def load_environment
36
+ Base.fetch_stack
37
+ self.workers = (Base.custom_json['deploy'][Base.config[:app].downcase]['workers'] ||= {})
38
+ end
39
+
40
+ def print_worker_help
41
+ $stderr.puts <<-EOF
42
+ Command: worker
43
+
44
+ List/Modify the current app's associated workers.
45
+ worker[:list] lists the worker type and separate process queues
46
+ worker:add worker, e.g., delayed_job:queue_name
47
+ worker:delete delete all keys associated with a worker
48
+ worker:push reconfigure the worker server.
49
+ Updates environment and DB changes.
50
+
51
+ Example 'worker:list' output:
52
+ delayed_job: ["mail", "sms"]
53
+
54
+ The above list output indicates a DelayedJob worker and two separate
55
+ delayed_job processes. One processing 'mail'; the other processing 'sms'.
56
+
57
+ examples:
58
+ $ worker
59
+ No workers specified!
60
+ $ worker:add delayed_job=mail
61
+ $ worker:add delayed_job=sms
62
+ $ worker:add sneakers=Workers::MailWorker
63
+ $ worker:list
64
+ Workers for MyCurrentApp:
65
+ delayed_job: ["mail", "sms"]
66
+ $ worker:delete delayed_job
67
+ No workers specified!
68
+ EOF
69
+ end
70
+
71
+ def list
72
+ if workers.length == 0
73
+ $stdout.puts 'No workers specified!'
74
+ else
75
+ $stdout.puts "Workers for #{Base.config[:app]}:"
76
+ workers.each do |type, queues|
77
+ $stdout.puts " #{$stdout.colorize(type.to_s, :bold)}: #{queues.to_s}"
78
+ end
79
+ end
80
+ end
81
+
82
+ def reconfigure
83
+ if Base.config[:wait]
84
+ $stdout.puts 'Disabling command wait (2 commands to issue)...'
85
+ Base.config[:wait] = false
86
+ end
87
+
88
+ Base.fetch_app
89
+
90
+ cjson = {
91
+ deploy: {
92
+ Base.app[:shortname] => {
93
+ application_type: 'rails'
94
+ }
95
+ }
96
+ }
97
+
98
+ begin
99
+ $stdout.puts 'This operation will '\
100
+ "#{$stdout.colorize('RESTART', :red)} (not soft reload) "\
101
+ 'your application workers.'
102
+ sleep 2
103
+ $stdout.puts 'Hold your seats: worker reconfigure and force restart in... (Press CTRL-C to Stop)'
104
+ 5.times { |t| $stdout.print "#{$stdout.colorize(5-t, :red)} "; sleep 1 }
105
+ $stdout.puts "\nSilence is acceptance..."
106
+ rescue Interrupt
107
+ $stdout.puts $stdout.colorize("\nCommand canceled successfully!", :green)
108
+ exit 1
109
+ end
110
+
111
+ Base.config[:layers] = ['workers']
112
+ Ridoku::Cook.cook_recipe(['rails::configure', 'workers::configure'], cjson)
113
+ end
114
+
115
+ def add
116
+ ARGV.each do |worker|
117
+ new_workers = worker.split('=')
118
+ workers[new_workers[0]] ||= []
119
+ workers[new_workers[0]] << new_workers[1]
120
+ workers[new_workers[0]].flatten!
121
+ $stdout.puts "Adding #{worker}."
122
+ end
123
+
124
+ list
125
+ Base.save_stack
126
+ end
127
+
128
+ def delete
129
+ ARGV.each do |worker|
130
+ workers.delete(worker)
131
+ $stdout.puts "Deleting worker: #{worker}"
132
+ end
133
+
134
+ list
135
+ Base.save_stack
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ridoku/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ridoku"
8
+ spec.version = Ridoku::VERSION
9
+ spec.authors = ["Terry Meacham","Joel Clay"]
10
+ spec.email = ["zv1n.fire@gmail.com","ra3don92@gmail.com"]
11
+ spec.description = %q{Ridoku: OpsWork management CLI.}
12
+ spec.summary = %q{Ridoku: OpsWork management CLI.}
13
+ spec.homepage = "http://ridoku-cli.blogspot.com"
14
+ spec.license = "BSD 3-clause"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = ['ridoku', 'rid']
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'aws-sdk', '~> 1.50'
22
+ spec.add_runtime_dependency 'rugged', '~> 0.21'
23
+ spec.add_runtime_dependency 'json', '~> 1.8'
24
+ spec.add_runtime_dependency 'rest-client', '~> 1.7'
25
+ spec.add_runtime_dependency 'require_all', '~> 1.3'
26
+ spec.add_runtime_dependency 'activesupport', '~> 4.1'
27
+ spec.add_runtime_dependency 'activesupport-inflector', '~> 0'
28
+
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.3"
31
+ spec.add_development_dependency "rake", '~> 0'
32
+ end