unidad 0.0.1.beta1 → 0.0.1.beta2

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
  SHA1:
3
- metadata.gz: 18e092529015146ccf806193891a80d5330270fa
4
- data.tar.gz: 51d016dcda7be99a9e3f091ab8a9cd250199302c
3
+ metadata.gz: 5a8d6bb8bfb697da6937982cf1b572c55dd23b13
4
+ data.tar.gz: 74cf84eea25316cb288b6fc68168f7927f08410d
5
5
  SHA512:
6
- metadata.gz: fe54afed6364b9a9fc4a551f7448fceb92dcab4aaa496f724aca41701c9284356fa75e623752c5916e31af5f7de8b2fe521c3cd09875a048ac4d38255cbd892f
7
- data.tar.gz: e4d47298cce8a43440eabe55ed4716c5ce945eb9709b56ee10d3c96a9a925241beb08d67f6cc859bad124056f1922dd8b681d12e082ded47cd2f2b97e468aebe
6
+ metadata.gz: 32f1404897c68c1a15e1c0f0c97091786d584be707f4be3cd1dc13d1e7f9f3579633bde1706608b493937b6c9dfdb96531aa9e5091c2eff0662b4e6336dbcbdb
7
+ data.tar.gz: cb4819b82900e6dadfe5e175bf1c5f5eda68bbaeab8c2c7cabefda933db208b42796869f93fee3f5fd882616da7f6f221603a8fea44ed4dfd9c5dbd848fbff72
data/config/unidad.yml CHANGED
@@ -1,12 +1,23 @@
1
- commands:
2
- - "MIDDLEMAN_ENV=%{environment} middleman build"
3
- - "s3_website push --config-dir ./config/deploy/%{environment}/"
4
- messages:
5
- deploy:
6
- before: "*%{username}* has started deploying branch `%{branch}` of `%{repository}` to *%{environment}*."
7
- after: "*%{username}* has finished deploying branch `%{branch}` of `%{repository}` to *%{environment}*."
8
- failure: "*[ERROR]* *%{username}* failed to deploy branch `%{branch}` of `%{repository}` to *%{environment}*."
9
- options:
10
- deploy:
1
+ unidad:
2
+ default_options: &default_options
11
3
  slack_username: crushbot
12
4
  slack_icon: http://www.gravatar.com/avatar/39293a628644a33d2373c9c329330d49.png
5
+ deploy:
6
+ commands:
7
+ - "MIDDLEMAN_ENV=%{environment} middleman build"
8
+ - "s3_website push --config-dir ./config/deploy/%{environment}/"
9
+ messages:
10
+ before: "*%{username}* has started deploying branch `%{branch}` of `%{repository}` to *%{environment}*."
11
+ after: "*%{username}* has finished deploying branch `%{branch}` of `%{repository}` to *%{environment}*."
12
+ failure: "*[ERROR]* *%{username}* failed to deploy branch `%{branch}` of `%{repository}` to *%{environment}*."
13
+ options:
14
+ <<: *default_options
15
+ playbook:
16
+ commands:
17
+ - "ansible-playbook -i %{hosts_file} %{playbook} --vault-password-file %{vault_password_file}"
18
+ messages:
19
+ before: "*%{username}* has started running playbook `%{playbook}` from `%{branch}` of `%{repository}`"
20
+ after: "*%{username}* has completed running playbook `%{playbook}` from `%{branch}` of `%{repository}`"
21
+ failure: "*[ERROR]* *%{username}* failed to complete running playbook `%{playbook}` from `%{branch}` of `%{repository}`"
22
+ options:
23
+ <<: *default_options
@@ -0,0 +1,77 @@
1
+ module Unidad
2
+ module Cli
3
+ module Ansible
4
+ class Playbook < Thor::Group
5
+ include Thor::Actions
6
+
7
+ argument :hosts_file, :desc => 'The hosts file to use when running the playbook',
8
+ :type => :string,
9
+ :required => true
10
+ argument :playbook, :desc => 'The playbook to run',
11
+ :type => :string,
12
+ :required => true
13
+
14
+ class_option :ansible_path, :default => 'ansible',
15
+ :type => :string,
16
+ :desc => 'The path to your ansible playbooks. (optional, defaults to ./ansible)',
17
+ :banner => '--ansible_path my_playbooks'
18
+
19
+ class_option :vault_password, :type => :string,
20
+ :desc => 'The Ansible vault password required to run your playbook. (optional)',
21
+ :banner => '--vault_password S3CR3T!!!'
22
+ def before
23
+ messenger.on(:before)
24
+ end
25
+
26
+ def ask_for_vault_password
27
+ unless password_file_exists?
28
+ vault_password = options.fetch(:vault_password, ask('What is your Ansible vault password?', :echo => false))
29
+ puts
30
+ create_file password_file_path, vault_password
31
+ end
32
+ end
33
+
34
+ def run_playbook
35
+ Unidad::CommandBuilder.all(:playbook, command_options).each do |command|
36
+ run command
37
+ end
38
+ end
39
+
40
+ def after
41
+ messenger.on(:after)
42
+ end
43
+
44
+ private
45
+
46
+ def messenger
47
+ @messenger ||= Unidad::Messenger.new(:playbook).tap do |m|
48
+ m.playbook = playbook
49
+ m.username = ENV['CIRCLE_USERNAME'] || ENV['USER'] || ENV['USERNAME']
50
+ m.repository = File.basename(Dir.pwd)
51
+ m.branch = `git rev-parse --abbrev-ref HEAD`.strip
52
+ end
53
+ end
54
+
55
+ def password_file_path
56
+ Unidad.root.join('tmp/ansible_vault.txt')
57
+ end
58
+
59
+ def password_file_exists?
60
+ File.exists?(password_file_path)
61
+ end
62
+
63
+ def ansible_path
64
+ Unidad.root.join(options[:ansible_path])
65
+ end
66
+
67
+ def command_options
68
+ {
69
+ :hosts_file => ansible_path.join(hosts_file),
70
+ :playbook => ansible_path.join(playbook),
71
+ :vault_password_file => password_file_path.to_s
72
+ }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -12,7 +12,7 @@ module Unidad
12
12
  end
13
13
 
14
14
  def deploy
15
- Unidad::CommandBuilder.all(environment).each do |command|
15
+ Unidad::CommandBuilder.all(:deploy, :environment => environment).each do |command|
16
16
  run command
17
17
  end
18
18
  end
@@ -5,11 +5,13 @@ require 'thor/group'
5
5
  Dotenv.load
6
6
 
7
7
  require 'unidad/cli/deploy'
8
+ require 'unidad/cli/ansible/playbook'
8
9
 
9
10
  module Unidad
10
11
  module Cli
11
12
  class Root < Thor
12
13
  register Unidad::Cli::Deploy, 'deploy', 'deploy [ENVIRONMENT]', 'Deploy your application to the specified environment.'
14
+ register Unidad::Cli::Ansible::Playbook, 'playbook', 'playbook [HOSTS_FILE] [PLAYBOOK]', 'Run an Ansible playbook.'
13
15
  end
14
16
  end
15
17
  end
@@ -1,18 +1,18 @@
1
1
  module Unidad
2
2
  class CommandBuilder
3
- attr_reader :command, :environment
3
+ attr_reader :command, :options
4
4
 
5
- def self.all(environment)
6
- Unidad.config['commands'].collect { |command| new(command, environment).to_s }
5
+ def self.all(namespace, options = {})
6
+ Unidad.config[namespace]['commands'].collect { |command| new(command, options).to_s }
7
7
  end
8
8
 
9
- def initialize(command, environment)
9
+ def initialize(command, options = {})
10
10
  @command = command
11
- @environment = environment
11
+ @options = options
12
12
  end
13
13
 
14
14
  def to_s
15
- command % { :environment => environment }
15
+ command % options
16
16
  end
17
17
  end
18
18
  end
@@ -3,11 +3,19 @@ require 'multi_json'
3
3
 
4
4
  module Unidad
5
5
  class Messenger
6
+ extend MatchMethodMacros
7
+
6
8
  attr_reader :command
7
- attr_accessor :callback, :username, :repository, :branch, :environment
9
+ attr_accessor :callback, :options
10
+
11
+ match_method(/\A(.*)=/) do |name, *args|
12
+ option = name.to_s.gsub('=', '').to_sym
13
+ self.options[option] = args[0]
14
+ end
8
15
 
9
16
  def initialize(command)
10
17
  @command = command
18
+ @options = {}
11
19
  end
12
20
 
13
21
  def on(callback)
@@ -17,18 +25,22 @@ module Unidad
17
25
 
18
26
  protected
19
27
 
28
+ def config
29
+ Unidad.config[command].messages
30
+ end
31
+
20
32
  def deliver_message
21
33
  uri = URI(ENV['SLACK_WEBHOOK_URL'])
22
34
  Net::HTTP.post_form(uri, build_params)
23
35
  rescue => e
24
- puts 'There was an error notifying Slack.'
36
+ puts 'There was an error notifying Slack. Please check that SLACK_WEBHOOK_URL is set in your environment.'
25
37
  puts e.inspect
26
38
  end
27
39
 
28
40
  def build_params
29
41
  payload = {
30
- :username => options['slack_username'],
31
- :icon_url => options['slack_icon'],
42
+ :username => message_options['slack_username'],
43
+ :icon_url => message_options['slack_icon'],
32
44
  :text => message
33
45
  }
34
46
 
@@ -36,16 +48,11 @@ module Unidad
36
48
  end
37
49
 
38
50
  def message
39
- Unidad.config.messages[command][callback] % options
51
+ config[callback] % message_options
40
52
  end
41
53
 
42
- def options
43
- @options ||= {
44
- :username => username,
45
- :repository => repository,
46
- :branch => branch,
47
- :environment => environment
48
- }.merge(Unidad.config.options[command])
54
+ def message_options
55
+ @message_options ||= options.merge(config.options)
49
56
  end
50
57
  end
51
58
  end
@@ -1,3 +1,3 @@
1
1
  module Unidad
2
- VERSION = '0.0.1.beta1'
2
+ VERSION = '0.0.1.beta2'
3
3
  end
data/lib/unidad.rb CHANGED
@@ -2,17 +2,22 @@ require 'yaml'
2
2
  require 'erb'
3
3
  require 'hashie'
4
4
 
5
+ require 'util/match_method_macros'
5
6
  require 'unidad/version'
6
7
  require 'unidad/command_builder'
7
8
  require 'unidad/messenger'
8
9
  require 'unidad/cli'
9
10
 
10
11
  module Unidad
12
+ def self.root
13
+ Pathname.new(Dir.pwd)
14
+ end
15
+
11
16
  def self.config_path
12
- Pathname.new(Dir.pwd).join('config/unidad.yml')
17
+ root.join('config/unidad.yml')
13
18
  end
14
19
 
15
20
  def self.config
16
- Hashie::Mash.new(YAML.load(ERB.new(File.new(config_path).read).result))
21
+ Hashie::Mash.new(YAML.load(ERB.new(File.new(config_path).read).result)).unidad
17
22
  end
18
23
  end
@@ -0,0 +1,21 @@
1
+ module MatchMethodMacros
2
+ def match_method(matcher, &method_body)
3
+ mod = Module.new do
4
+ define_method(:method_missing) do |method_name, *args|
5
+ if matcher === method_name.to_s
6
+ instance_exec(method_name, *args, &method_body)
7
+ else
8
+ super(method_name, *args)
9
+ end
10
+ end
11
+
12
+ define_method(:respond_to_missing?) do |method_name, include_private|
13
+ # Even though this is in the #respond_to_missing? hook we
14
+ # still need to call 'super' in case there are other included
15
+ # modules which also define #respond_to_missing?
16
+ (matcher === method_name) || super(method_name, include_private)
17
+ end
18
+ end
19
+ include mod
20
+ end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unidad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta1
4
+ version: 0.0.1.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - PJ Kelly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -215,11 +215,13 @@ files:
215
215
  - config/unidad.yml
216
216
  - lib/unidad.rb
217
217
  - lib/unidad/cli.rb
218
+ - lib/unidad/cli/ansible/playbook.rb
218
219
  - lib/unidad/cli/deploy.rb
219
220
  - lib/unidad/cli/root.rb
220
221
  - lib/unidad/command_builder.rb
221
222
  - lib/unidad/messenger.rb
222
223
  - lib/unidad/version.rb
224
+ - lib/util/match_method_macros.rb
223
225
  - unidad.gemspec
224
226
  homepage: https://github.com/crushlovely/unidad
225
227
  licenses: