omc 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4dd1d6445040ba01d433ef7420ee17e6fbd8947b
4
- data.tar.gz: ab68bfcbb40518930199073d3b518513c3aa23d1
3
+ metadata.gz: b2361d70e5037256d91560c0942293a8131dcb4c
4
+ data.tar.gz: 313d68f01a3ad87f012a02244635d4f8540ecb9e
5
5
  SHA512:
6
- metadata.gz: 7b04216dce3e21212fa1728de63dd6af4a8caad9c01021750f361a3fd33f8507ae7e7e10049582e59dfffe7fb240faad188cc4447c9d141ebb22e30ea869e1b2
7
- data.tar.gz: a0496b2bd1f001603451759a707ae86098618fee1715c6d59ef5acccdb8e67a69b0a6195dfbb664191971c5d8cd464867a521991cc11d5c29e08d357c1529d96
6
+ metadata.gz: 6282b605730b8267d82c0c33d440827fd712e3b36e11ea29d325e0feeed8cfa9e3809fc9c4150713b794f81ed9a14228bf8d8e1cb5e4f363d36d2e9420b74ad2
7
+ data.tar.gz: c734612a25bf5b68f36da62fc30403ec347e05ceab0610ee3138dbddecca0d2d9ec9814ad6489303832e25a84332b4bcec20cbf11e2668017442c845ed0cfd10
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Omc - Opsworks Missing Console
1
+ # OMC - Opsworks' Missing Console
2
2
 
3
3
  Useful commands for dealing with AWS and opsworks across multiple IAM accounts.
4
4
 
@@ -9,9 +9,9 @@ Useful commands for dealing with AWS and opsworks across multiple IAM accounts.
9
9
  gem install omc
10
10
  ```
11
11
 
12
- ### Aws Cred Vault
12
+ ### AWS Cred Vault
13
13
 
14
- omc works off of [aws_cred_vault](www.github.com/cbrunsdon/aws_cred_vault) and requires a ~/.aws_cred_vault file to exist. This file should be populated with your different IAM accounts and users.
14
+ omc works off of [aws_cred_vault](http://www.github.com/cbrunsdon/aws_cred_vault) and requires an ~/.aws_cred_vault file to exist. This file should be populated with your different IAM accounts and users.
15
15
 
16
16
  Below is an example of a valid ~/.aws_cred_vault
17
17
 
data/Rakefile CHANGED
@@ -1 +1,13 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ desc 'Open an pry (or irb) session'
4
+ task :console do
5
+ begin
6
+ require 'pry'
7
+ sh 'pry -Ilib -romc'
8
+ rescue LoadError => _
9
+ sh 'irb -rubygems -Ilib -romc'
10
+ end
11
+ end
12
+
13
+ task c: :console
data/lib/omc.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "omc/version"
2
+ require "omc/account"
2
3
  require "omc/cli"
3
4
 
4
5
  module Omc
@@ -0,0 +1,17 @@
1
+ require 'omc/stack'
2
+
3
+ module Omc
4
+ class Account
5
+ attr_accessor :client
6
+
7
+ def initialize(credentials)
8
+ @client = ::AWS::OpsWorks::Client.new(credentials)
9
+ end
10
+
11
+ def stacks
12
+ @stacks ||= client.describe_stacks[:stacks].map do |stack|
13
+ ::Omc::Stack.new(self, stack)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module Omc
2
+ class App
3
+ attr_reader :stack, :attributes
4
+
5
+ extend Forwardable
6
+ def_delegators :@attributes, :[]
7
+ def_delegators :stack, :account, :client
8
+
9
+ def initialize stack, attributes
10
+ @stack = stack
11
+ @attributes = attributes
12
+ end
13
+ end
14
+ end
@@ -2,27 +2,52 @@ require 'thor'
2
2
  require 'aws_cred_vault'
3
3
  require 'aws-sdk'
4
4
  require "omc/stack_command"
5
+ require "omc/config"
5
6
 
6
7
  module Omc
7
8
  class Cli < Thor
8
- desc 'ssh', 'Connect to an instance on a stack on an account'
9
- def ssh(account, stack)
10
- command = StackCommand.new(user(account), stack)
9
+ class_option :account, aliases: '-a', optional: true
10
+
11
+ desc 'ssh STACK', 'Connect to an instance on a stack on an account'
12
+ def ssh(stack)
13
+ command = StackCommand.new(user, stack)
11
14
  command.ssh
12
15
  end
13
16
 
14
- desc 'console', 'Run a rails console on the given stack'
15
- def console(account, stack)
16
- command = StackCommand.new(user(account), stack)
17
+ desc 'console STACK', 'Run a rails console on the given stack'
18
+ method_option :app
19
+ def console(stack)
20
+ command = StackCommand.new(user, stack, options[:app])
17
21
  command.console
18
22
  end
19
23
 
24
+ desc 'db STACK', 'Connect and run the database client on the given stack'
25
+ method_option :app
26
+ def db(stack)
27
+ command = StackCommand.new(user, stack, options[:app])
28
+ command.db
29
+ end
30
+
31
+ desc 'status STACK', 'Show the instance status for the given stack'
32
+ def status(stack)
33
+ command = StackCommand.new(user, stack, options[:app])
34
+ command.status(self)
35
+ end
36
+
20
37
  private
21
- def user(account)
38
+ def user
22
39
  iam_account = vault.account(account) || abort("Account #{account.inspect} not configured")
23
40
  iam_account.users.first || abort("No users configured under #{account.inspect}")
24
41
  end
25
42
 
43
+ def account
44
+ options[:account] || config.account || abort("Must specify account")
45
+ end
46
+
47
+ def config
48
+ @config ||= Omc::Config.load
49
+ end
50
+
26
51
  def vault
27
52
  AwsCredVault::Toml.new File.join(ENV['HOME'], '.aws_cred_vault')
28
53
  end
@@ -0,0 +1,31 @@
1
+ require 'toml'
2
+
3
+ module Omc
4
+ class Config
5
+ PATHS = [
6
+ File.join(ENV['HOME'], '.config', 'omcrc'),
7
+ File.join(ENV['HOME'], '.omcrc'),
8
+ '.omcrc'
9
+ ]
10
+ def self.load
11
+ config = new
12
+ PATHS.each do |path|
13
+ config.load_file(path)
14
+ end
15
+ config
16
+ end
17
+
18
+ def load_file path
19
+ return unless File.exists?(path)
20
+ @config.update TOML.load_file(path)
21
+ end
22
+
23
+ def initialize(config={})
24
+ @config = config
25
+ end
26
+
27
+ def account
28
+ @config['account']
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module Omc
2
+ class Instance
3
+ attr_reader :stack, :attributes
4
+
5
+ extend Forwardable
6
+ def_delegators :@attributes, :[]
7
+ def_delegators :stack, :account
8
+ def_delegators :account, :client
9
+
10
+ def initialize stack, attributes
11
+ @stack = stack
12
+ @attributes = attributes
13
+ end
14
+
15
+ def online?
16
+ attributes[:status] == "online"
17
+ end
18
+
19
+ def offline?
20
+ !online?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ require 'omc/instances'
2
+ require 'omc/app'
3
+
4
+ module Omc
5
+ class Stack
6
+ attr_reader :account, :attributes
7
+
8
+ extend Forwardable
9
+ def_delegators :@attributes, :[]
10
+ def_delegators :account, :client
11
+
12
+ def initialize account, attributes
13
+ @account = account
14
+ @attributes = attributes
15
+ end
16
+
17
+ def instances
18
+ @instances ||= client.describe_instances(stack_id: self[:stack_id])[:instances].map do |instance|
19
+ ::Omc::Instance.new(self, instance)
20
+ end
21
+ end
22
+
23
+ def apps
24
+ @apps ||= client.describe_apps(stack_id: self[:stack_id])[:apps].map do |app|
25
+ ::Omc::App.new(self, app)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,10 +1,12 @@
1
1
  require 'aws-sdk'
2
+ require 'omc/account'
2
3
 
3
4
  module Omc
4
5
  class StackCommand
5
- def initialize user, stack_name
6
+ def initialize user, stack_name, app_name=nil
6
7
  @user = user
7
8
  @stack_name = stack_name
9
+ @app_name = app_name
8
10
  end
9
11
 
10
12
  def ssh
@@ -12,31 +14,54 @@ module Omc
12
14
  end
13
15
 
14
16
  def console
15
- app = applications.first
16
- exec 'ssh', '-t', ssh_host, "sudo su deploy -c 'cd /srv/www/#{app[:name]}/current && RAILS_ENV=#{app[:attributes]['RailsEnv']} bundle exec rails c'"
17
+ ssh_and_execute "cd /srv/www/#{app[:name]}/current && RAILS_ENV=#{app[:attributes]['RailsEnv']} bundle exec rails c"
18
+ end
19
+
20
+ def db
21
+ ssh_and_execute "cd /srv/www/#{app[:name]}/current && RAILS_ENV=#{app[:attributes]['RailsEnv']} bundle exec rails db -p"
22
+ end
23
+
24
+ def status(thor)
25
+ details = stack.instances.map do |i|
26
+ [
27
+ i[:hostname],
28
+ i[:instance_type],
29
+ i[:status],
30
+ i[:availability_zone],
31
+ i[:ec2_instance_id],
32
+ i[:public_ip],
33
+ ]
34
+ end
35
+ thor.print_table(details)
17
36
  end
18
37
 
19
38
  private
20
- def applications
21
- ops.describe_apps(stack_id: stack[:stack_id])[:apps]
39
+ def ssh_and_execute(command)
40
+ exec 'ssh', '-t', ssh_host, "sudo su deploy -c '#{command}'"
41
+ end
42
+
43
+ def app
44
+ if @app_name
45
+ get_by_name(stack.apps, @app_name)
46
+ else
47
+ stack.apps.first
48
+ end
22
49
  end
23
50
 
24
51
  def instance
25
- instances = ops.describe_instances(stack_id: stack[:stack_id])[:instances]
26
- instances.reject!{|i| i[:status] != "online" }
27
- instance = instances.first || abort("No running instances")
52
+ stack.instances.detect(&:online?) || abort("No running instances")
28
53
  end
29
54
 
30
55
  def ssh_host
31
56
  "#{@user.name}@#{instance[:public_ip]}"
32
57
  end
33
58
 
34
- def ops
35
- @ops ||= ::AWS::OpsWorks::Client.new @user.credentials
59
+ def account
60
+ @account ||= Omc::Account.new(@user.credentials)
36
61
  end
37
62
 
38
63
  def stack
39
- @stack ||= get_by_name ops.describe_stacks[:stacks], @stack_name
64
+ @stack ||= get_by_name(account.stacks, @stack_name)
40
65
  end
41
66
 
42
67
  def get_by_name collection, name
@@ -1,3 +1,3 @@
1
1
  module Omc
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clarke Brunsdon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-22 00:00:00.000000000 Z
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -95,7 +95,12 @@ files:
95
95
  - Rakefile
96
96
  - bin/omc
97
97
  - lib/omc.rb
98
+ - lib/omc/account.rb
99
+ - lib/omc/app.rb
98
100
  - lib/omc/cli.rb
101
+ - lib/omc/config.rb
102
+ - lib/omc/instances.rb
103
+ - lib/omc/stack.rb
99
104
  - lib/omc/stack_command.rb
100
105
  - lib/omc/version.rb
101
106
  - omc.gemspec