Skip to content

Commit

Permalink
add setup command
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Jul 20, 2013
1 parent ef4280e commit 489dbfe
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 20 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The [travis gem](https://rubygems.org/gems/travis) includes both a command line
* [`open`](#open)
* [`pubkey`](#pubkey)
* [`restart`](#restart)
* [`setup`](#setup)
* [`show`](#show)
* [`status`](#status)
* [Ruby Library](#ruby-library)
Expand Down Expand Up @@ -471,6 +472,33 @@ Or a single job:
$ travis restart 57.1
job #57.1 has been restarted

#### `setup`

Helps you configure Travis addons.

Usage: bin/travis setup service [options]
-h, --help Display help
-i, --[no-]interactive be interactive and colorful
-E, --[no-]explode don't rescue exceptions
--skip-version-check don't check if travis client is up to date
-e, --api-endpoint URL Travis API server to talk to
--pro short-cut for --api-endpoint 'https://api.travis-ci.com/'
--org short-cut for --api-endpoint 'https://api.travis-ci.org/'
--staging talks to staging system
-t, --token [ACCESS_TOKEN] access token to use
--debug show API requests
--adapter ADAPTER Faraday adapter to use for HTTP requests
-r, --repo SLUG repository to use (will try to detect from current git clone)
-f, --force override config section if it already exists

Available services: `heroku`, `nodejitsu`, `openshift` and `sauce_connect`.

Example:

$ travis setup heroku
Deploy only from travis-ci/travis-chat? |yes|
Encrypt API key? |yes|

#### `show`

Displays general infos about the latest build:
Expand Down
1 change: 1 addition & 0 deletions lib/travis/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module CLI
autoload :Raw, 'travis/cli/raw'
autoload :RepoCommand, 'travis/cli/repo_command'
autoload :Restart, 'travis/cli/restart'
autoload :Setup, 'travis/cli/setup'
autoload :Show, 'travis/cli/show'
autoload :Status, 'travis/cli/status'
autoload :Sync, 'travis/cli/sync'
Expand Down
21 changes: 1 addition & 20 deletions lib/travis/cli/encrypt.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# encoding: utf-8
require 'travis/cli'
require 'yaml'

module Travis
module CLI
Expand Down Expand Up @@ -33,7 +32,7 @@ def run(*args)

if config_key
set_config encrypted.map { |e| { 'secure' => e } }
File.write(travis_yaml, travis_config.to_yaml)
save_travis_config
else
list = encrypted.map { |data| format(data.inspect, " secure: %s") }
say(list.join("\n"), template(__FILE__), :none)
Expand All @@ -42,13 +41,6 @@ def run(*args)

private

def travis_config
@travis_config ||= begin
payload = YAML.load_file(travis_yaml)
payload.respond_to?(:to_hash) ? payload.to_hash : {}
end
end

def set_config(result)
parent_config[last_key] = merge_config(result)
end
Expand Down Expand Up @@ -87,17 +79,6 @@ def traverse_config(hash, key = nil, *rest)

traverse_config(hash[key], *rest)
end

def travis_yaml(dir = Dir.pwd)
path = File.expand_path('.travis.yml', dir)
if File.exist? path
path
else
parent = File.expand_path('..', dir)
error "no .travis.yml found" if parent == dir
travis_yaml(parent)
end
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions lib/travis/cli/repo_command.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'travis/cli'
require 'yaml'

module Travis
module CLI
Expand Down Expand Up @@ -68,6 +69,31 @@ def detect_api_endpoint
end
end
end

def travis_config
@travis_config ||= begin
payload = YAML.load_file(travis_yaml)
payload.respond_to?(:to_hash) ? payload.to_hash : {}
end
end

def travis_yaml(dir = Dir.pwd)
path = File.expand_path('.travis.yml', dir)
if File.exist? path
path
else
parent = File.expand_path('..', dir)
error "no .travis.yml found" if parent == dir
travis_yaml(parent)
end
end

def save_travis_config
yaml = travis_config.to_yaml
yaml.gsub! /^(\s+)('on'|true):/, "\\1on:"
yaml.gsub! /\A---\n/, ''
File.write(travis_yaml, yaml)
end
end
end
end
80 changes: 80 additions & 0 deletions lib/travis/cli/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'travis/cli'
require 'json'

module Travis
module CLI
class Setup < RepoCommand
on('-f', '--force', 'override config section if it already exists')

def run(service)
error "unknown service #{service}" unless respond_to? "setup_#{service}"
public_send "setup_#{service}"
end

def setup_heroku
configure 'deploy', 'provider' => 'heroku' do |config|
config['api_key'] = `heroku auth:token 2>/dev/null`.strip
config['api_key'] = ask("Heroku API token: ") { |q| q.echo = "*" }.to_s if config['api_key'].empty?
config['app'] = `heroku apps:info 2>/dev/null`.scan(/^=== (.+)$/).flatten.first
config['app'] = ask("Heroku application name: ") { |q| q.default = repository.name }.to_s if config['app'].nil?
config['on'] = { 'repo' => repository.slug } if agree("Deploy only from #{repository.slug}? ") { |q| q.default = 'yes' }
encrypt(config, 'api_key') if agree("Encrypt API key? ") { |q| q.default = 'yes' }
end
end

def setup_openshift
configure 'deploy', 'provider' => 'openshift' do |config|
config['user'] = ask("OpenShift user: ").to_s
config['password'] = ask("OpenShift password: ") { |q| q.echo = "*" }.to_s
config['app'] = ask("OpenShift application name: ") { |q| q.default = repository.name }.to_s
config['domain'] = ask("OpenShift domain: ").to_s
config['on'] = { 'repo' => repository.slug } if agree("Deploy only from #{repository.slug}? ") { |q| q.default = 'yes' }
encrypt(config, 'password') if agree("Encrypt password? ") { |q| q.default = 'yes' }
end
end

def setup_nodejitsu
configure 'deploy', 'provider' => 'nodejitsu' do |config|
jitsu_file = File.expand_path('.jitsuconf', ENV['HOME'])

if File.exist? jitsu_file
jitsu_conf = JSON.parse(File.read(jitsu_file))
config['user'] = jitsu_conf['username']
config['api_key'] = jitsu_conf['apiToken']
end

config['user'] ||= ask("Nodejitsu user: ").to_s
config['api_key'] ||= ask("Nodejitsu API token: ") { |q| q.echo = "*" }.to_s
config['on'] = { 'repo' => repository.slug } if agree("Deploy only from #{repository.slug}? ") { |q| q.default = 'yes' }
encrypt(config, 'api_key') if agree("Encrypt API key? ") { |q| q.default = 'yes' }
end
end

def setup_sauce_connect
travis_config['addons'] ||= {}
configure 'sauce_connect', {}, travis_config['addons'] do |config|
config['username'] = ask("Sauce Labs user: ").to_s
config['access_key'] = ask("Sauce Labs access key: ") { |q| q.echo = "*" }.to_s
encrypt(config, 'access_key') if agree("Encrypt access key? ") { |q| q.default = 'yes' }
end
end

alias setup_sauce_lab setup_sauce_connect
alias setup_sauce setup_sauce_connect

private

def encrypt(config, key)
encrypted = repository.encrypt(config.fetch(key))
config[key] = { 'secure' => encrypted }
end

def configure(key, value = {}, config = travis_config)
error "#{key} section already exists in .travis.yml, run with --force to override" if config.include? key and not force?
result = yield(config[key] = value)
save_travis_config
result
end
end
end
end
4 changes: 4 additions & 0 deletions lib/travis/client/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def public_key
end
end

def name
slug[/[^\/]+$/]
end

def public_key=(key)
key = Key.new(key) unless key.is_a? Key
set_attribute(:public_key, key)
Expand Down

0 comments on commit 489dbfe

Please sign in to comment.