aptible-cli 0.2.0

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: a3d74fcf955ded96e824f00538e04210e31412c9
4
+ data.tar.gz: d96da04e7f720d58f77eecb6371979ec0b7485a3
5
+ SHA512:
6
+ metadata.gz: 319a2859143709591fa1abf2ec3e77b68ecde13d268887ce5016d8a986e9947ea6fc182c832757a4eede36753abd8c6ebc65b105dda29ac5051773eeb86de1a1
7
+ data.tar.gz: 3547c769910209356ab04d11f52f6ecc993cdc0e71735f541ad62c15a30804e1d2ea5cc5fbb817df60e99b2484e301c253db9f50fffec5aa613230242d4c8809
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aptible-cli.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Aptible, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # ![](https://raw.github.com/aptible/straptible/master/lib/straptible/rails/templates/public.api/icon-60px.png) Aptible CLI
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/aptible-cli.png)](https://rubygems.org/gems/aptible-cli)
4
+ [![Build Status](https://travis-ci.org/aptible/aptible-cli.png?branch=master)](https://travis-ci.org/aptible/aptible-cli)
5
+ [![Dependency Status](https://gemnasium.com/aptible/aptible-cli.png)](https://gemnasium.com/aptible/aptible-cli)
6
+
7
+ Command-line interface for Aptible services.
8
+
9
+ ## Installation
10
+
11
+ Install the command line tool using Rubygems:
12
+
13
+ gem install aptible-cli
14
+
15
+ ## Usage
16
+
17
+ TODO: Add usage notes.
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork the project.
22
+ 1. Commit your changes, with specs.
23
+ 1. Ensure that your code passes specs (`rake spec`) and meets Aptible's Ruby style guide (`rake rubocop`).
24
+ 1. Create a new pull request on GitHub.
25
+
26
+ ## Copyright and License
27
+
28
+ MIT License, see [LICENSE](LICENSE.md) for details.
29
+
30
+ Copyright (c) 2013 [Aptible](https://www.aptible.com), Frank Macreery, and contributors.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'aptible/tasks'
4
+ Aptible::Tasks.load_tasks
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'English'
6
+ require 'aptible/cli/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'aptible-cli'
10
+ spec.version = Aptible::CLI::VERSION
11
+ spec.authors = ['Frank Macreery']
12
+ spec.email = ['frank@macreery.com']
13
+ spec.description = %q(Aptible CLI)
14
+ spec.summary = %q(Command-line interface for Aptible services)
15
+ spec.homepage = 'https://github.com/aptible/aptible-cli'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files`.split($RS)
19
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(/^spec\//)
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'thor'
24
+ spec.add_dependency 'aptible-auth', '>= 0.4.2'
25
+ spec.add_dependency 'aptible-api', '>= 0.4.4'
26
+ spec.add_dependency 'git'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.3'
29
+ spec.add_development_dependency 'aptible-tasks', '>= 0.2.0'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'rspec', '~> 2.0'
32
+ spec.add_development_dependency 'pry'
33
+ end
data/bin/aptible ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'aptible/cli'
5
+ rescue
6
+ require 'rubygems'
7
+ require 'aptible/cli'
8
+ end
9
+
10
+ begin
11
+ Aptible::CLI::Agent.start
12
+ rescue HyperResource::ClientError => e
13
+ if e.body['error'] == 'invalid_token'
14
+ STDERR.puts 'API authentication error: please run aptible login'
15
+ exit 1
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'aptible/cli/version'
2
+ require 'aptible/cli/agent'
3
+
4
+ module Aptible
5
+ module CLI
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require 'thor'
2
+ require 'json'
3
+ require 'aptible/auth'
4
+
5
+ require_relative 'helpers/token'
6
+ require_relative 'helpers/operation'
7
+ require_relative 'helpers/app'
8
+
9
+ require_relative 'subcommands/config'
10
+
11
+ module Aptible
12
+ module CLI
13
+ class Agent < Thor
14
+ include Thor::Actions
15
+
16
+ include Helpers::Token
17
+ include Subcommands::Config
18
+
19
+ desc 'version', 'Print Aptible CLI version'
20
+ def version
21
+ puts "aptible-cli v#{Aptible::CLI::VERSION}"
22
+ end
23
+
24
+ desc 'login', 'Log in to Aptible'
25
+ def login
26
+ email = ask('Email: ')
27
+ password = ask('Password: ', echo: false)
28
+ puts ''
29
+
30
+ begin
31
+ token = Aptible::Auth::Token.create(email: email, password: password)
32
+ rescue OAuth2::Error
33
+ raise Thor::Error, 'Could not authenticate with given credentials'
34
+ end
35
+
36
+ save_token(token.access_token)
37
+ puts "Token written to #{token_file}"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require 'aptible/api'
2
+ require 'git'
3
+
4
+ module Aptible
5
+ module CLI
6
+ module Helpers
7
+ module App
8
+ include Helpers::Token
9
+
10
+ def ensure_app(options = {})
11
+ handle = options[:app] || ensure_default_handle
12
+ app = app_from_handle(handle)
13
+ return app if app
14
+ fail Thor::Error, "Could not find app #{handle}"
15
+ end
16
+
17
+ def app_from_handle(handle)
18
+ Aptible::Api::App.all(token: fetch_token).find do |a|
19
+ a.handle == handle
20
+ end
21
+ end
22
+
23
+ def ensure_default_handle
24
+ return default_handle if default_handle
25
+ fail Thor::Error, <<-ERR.gsub(/\s+/, ' ').strip
26
+ Could not find app in current working directory, please specify
27
+ with --app
28
+ ERR
29
+ end
30
+
31
+ def default_handle
32
+ git = Git.open(Dir.pwd)
33
+ aptible_remote = git.remote(:aptible).url || ''
34
+ aptible_remote[/:(?<name>.+)\.git/, :name]
35
+ rescue
36
+ nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,26 @@
1
+ require 'aptible/api'
2
+
3
+ module Aptible
4
+ module CLI
5
+ module Helpers
6
+ module Operation
7
+ POLL_INTERVAL = 1
8
+
9
+ def poll_for_success(operation)
10
+ puts 'Updating configuration and restarting app...'
11
+ wait_for_completion operation
12
+ unless operation.status == 'succeeded'
13
+ fail Thor::Error, 'Operation failed: please check logs'
14
+ end
15
+ end
16
+
17
+ def wait_for_completion(operation)
18
+ while %w(queued running).include? operation.status
19
+ sleep 1
20
+ operation.get
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ require 'aptible/auth'
2
+
3
+ module Aptible
4
+ module CLI
5
+ module Helpers
6
+ module Token
7
+ def fetch_token
8
+ @token ||= current_token_hash[Aptible::Auth.configuration.root_url]
9
+ return @token if @token
10
+ fail Thor::Error, 'Could not read token: please run aptible login'
11
+ end
12
+
13
+ # rubocop:disable MethodLength
14
+ def save_token(token)
15
+ hash = current_token_hash.merge(
16
+ Aptible::Auth.configuration.root_url => token
17
+ )
18
+
19
+ FileUtils.mkdir_p(File.dirname(token_file))
20
+ File.open(token_file, 'w') do |file|
21
+ file.puts hash.to_json
22
+ end
23
+ rescue
24
+ raise Thor::Error, <<-ERR.gsub(/\s+/, ' ').strip
25
+ Could not write token to #{token_file}, please check filesystem
26
+ permissions
27
+ ERR
28
+ end
29
+ # rubocop:enable MethodLength
30
+
31
+ def current_token_hash
32
+ JSON.parse(File.read(token_file))
33
+ rescue
34
+ {}
35
+ end
36
+
37
+ def token_file
38
+ File.join ENV['HOME'], '.aptible', 'tokens.json'
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,55 @@
1
+ require 'shellwords'
2
+
3
+ module Aptible
4
+ module CLI
5
+ module Subcommands
6
+ module Config
7
+ # rubocop:disable MethodLength
8
+ # rubocop:disable CyclomaticComplexity
9
+ def self.included(thor)
10
+ thor.class_eval do
11
+ include Helpers::Operation
12
+ include Helpers::App
13
+
14
+ desc 'config', "Print an app's current configuration"
15
+ option :app
16
+ def config
17
+ app = ensure_app(options)
18
+ config = app.current_configuration
19
+ env = config ? config.env : nil
20
+ puts formatted_config(env || {})
21
+ end
22
+
23
+ desc 'config:add', 'Add an ENV variable to an app'
24
+ option :app
25
+ define_method 'config:add' do |*args|
26
+ # FIXME: define_method - ?! Seriously, WTF Thor.
27
+ app = ensure_app(options)
28
+ env = Hash[args.map { |arg| arg.split('=', 2) }]
29
+ operation = app.create_operation(type: 'configure', env: env)
30
+ poll_for_success(operation)
31
+ end
32
+
33
+ desc 'config:rm', 'Remove an ENV variable from an app'
34
+ option :app
35
+ define_method 'config:rm' do |*args|
36
+ # FIXME: define_method - ?! Seriously, WTF Thor.
37
+ app = ensure_app(options)
38
+ env = Hash[args.map { |arg| [arg, ''] }]
39
+ operation = app.create_operation(type: 'configure', env: env)
40
+ poll_for_success(operation)
41
+ end
42
+
43
+ private
44
+
45
+ def formatted_config(env)
46
+ env.map { |k, v| "#{k}=#{Shellwords.escape(v)}" }.join("\n")
47
+ end
48
+ end
49
+ end
50
+ # rubocop:enable CyclomaticComplexity
51
+ # rubocop:enable MethodLength
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module Aptible
2
+ module CLI
3
+ VERSION = '0.2.0'
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aptible::CLI::Agent do
4
+ before { subject.stub(:ask) }
5
+
6
+ describe '#version' do
7
+ it 'should print the version' do
8
+ version = Aptible::CLI::VERSION
9
+ expect(STDOUT).to receive(:puts).with "aptible-cli v#{version}"
10
+ subject.version
11
+ end
12
+ end
13
+
14
+ describe '#login' do
15
+ let(:token) { double('Aptible::Auth::Token') }
16
+
17
+ before { OAuth2::Error.send :define_method, :initialize, -> {} }
18
+ before { token.stub(:access_token) { 'access_token' } }
19
+
20
+ it 'should save a token to ~/.aptible/tokens' do
21
+ Aptible::Auth::Token.stub(:create) { token }
22
+ expect(subject).to receive(:save_token).with('access_token')
23
+ subject.login
24
+ end
25
+
26
+ it 'should raise an error if authentication fails' do
27
+ Aptible::Auth::Token.stub(:create).and_raise OAuth2::Error
28
+ expect do
29
+ subject.login
30
+ end.to raise_error 'Could not authenticate with given credentials'
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ # Load shared spec files
5
+ Dir["#{File.dirname(__FILE__)}/shared/**/*.rb"].each do |file|
6
+ require file
7
+ end
8
+
9
+ # Require library up front
10
+ require 'aptible/cli'
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aptible-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Frank Macreery
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aptible-auth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: aptible-api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: git
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: aptible-tasks
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '2.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '2.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Aptible CLI
140
+ email:
141
+ - frank@macreery.com
142
+ executables:
143
+ - aptible
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - .gitignore
148
+ - .rspec
149
+ - .travis.yml
150
+ - Gemfile
151
+ - LICENSE.md
152
+ - README.md
153
+ - Rakefile
154
+ - aptible-cli.gemspec
155
+ - bin/aptible
156
+ - lib/aptible/cli.rb
157
+ - lib/aptible/cli/agent.rb
158
+ - lib/aptible/cli/helpers/app.rb
159
+ - lib/aptible/cli/helpers/operation.rb
160
+ - lib/aptible/cli/helpers/token.rb
161
+ - lib/aptible/cli/subcommands/config.rb
162
+ - lib/aptible/cli/version.rb
163
+ - spec/aptible/cli/agent_spec.rb
164
+ - spec/spec_helper.rb
165
+ homepage: https://github.com/aptible/aptible-cli
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - '>='
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.2.1
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Command-line interface for Aptible services
189
+ test_files:
190
+ - spec/aptible/cli/agent_spec.rb
191
+ - spec/spec_helper.rb