foreplay 0.9.13 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,126 +2,77 @@ require 'yaml'
2
2
  require 'string'
3
3
  require 'hash'
4
4
 
5
- class Foreplay::Engine
6
- include Foreplay
7
- attr_reader :mode, :environment, :filters
8
-
9
- DEFAULT_CONFIG_FILE = "#{Dir.getwd}/config/foreplay.yml"
10
- DEFAULTS_KEY = 'defaults'
11
-
12
- def initialize(e, f)
13
- @environment = e
14
- @filters = f
15
- end
16
-
17
- def deploy
18
- @mode = :deploy
19
- execute
20
- end
21
-
22
- def check
23
- @mode = :check
24
- execute
25
- end
26
-
27
- def execute
28
- puts "#{mode.capitalize}ing #{environment.dup.yellow} environment, "\
29
- "#{explanatory_text(filters, 'role')}, #{explanatory_text(filters, 'server')}"
30
-
31
- actionable_roles.map { |role, instructions| threads(role, instructions) }.flatten.each(&:join)
32
-
33
- puts mode == :deploy ? 'Finished deployment' : 'Deployment configuration check was successful'
34
- end
35
-
36
- private
5
+ require 'foreplay/engine/defaults'
6
+ require 'foreplay/engine/logger'
7
+ require 'foreplay/engine/port'
8
+ require 'foreplay/engine/remote'
9
+ require 'foreplay/engine/role'
10
+ require 'foreplay/engine/secrets'
11
+ require 'foreplay/engine/server'
12
+ require 'foreplay/engine/step'
13
+
14
+ module Foreplay
15
+ class Engine
16
+ include Foreplay::Engine::Defaults
17
+ attr_reader :mode, :environment, :filters
18
+
19
+ def initialize(e, f)
20
+ @environment = e
21
+ @filters = f
22
+ end
37
23
 
38
- def actionable_roles
39
- roles.select { |role, _i| role != DEFAULTS_KEY && role != filters['role'] }
40
- end
24
+ [:deploy, :check].each { |m| define_method(m) { execute m } }
41
25
 
42
- def threads(role, instructions)
43
- Foreplay::Engine::Role.new(
44
- environment,
45
- mode,
46
- build_instructions(role, instructions)
47
- ).threads
48
- end
26
+ def execute(m)
27
+ @mode = m
28
+ puts "#{mode.capitalize}ing #{environment.dup.yellow} environment, "\
29
+ "#{explanatory_text(filters, 'role')}, #{explanatory_text(filters, 'server')}"
49
30
 
50
- def explanatory_text(hsh, key)
51
- hsh.key?(key) ? "#{hsh[key].dup.yellow} #{key}" : "all #{key}s"
52
- end
31
+ actionable_roles.map { |role, instructions| threads(role, instructions) }.flatten.each(&:join)
53
32
 
54
- def build_instructions(role, additional_instructions)
55
- instructions = defaults.supermerge(additional_instructions)
56
- instructions['role'] = role
57
- instructions['verbose'] = verbose
58
- required_keys = %w(name environment role servers path repository)
59
-
60
- required_keys.each do |key|
61
- next if instructions.key? key
62
- terminate("Required key #{key} not found in instructions for #{environment} environment.\nCheck #{config_file}")
33
+ puts mode == :deploy ? 'Finished deployment' : 'Deployment configuration check was successful'
63
34
  end
64
35
 
65
- # Apply server filter
66
- instructions['servers'] &= server_filter if server_filter
67
- instructions
68
- end
69
-
70
- def server_filter
71
- @server_filter ||= filters['server'].split(',') if filters.key?('server')
72
- end
73
-
74
- def config_file
75
- @config_file ||= (filters['config_file'] || DEFAULT_CONFIG_FILE)
76
- end
36
+ private
77
37
 
78
- def defaults
79
- return @defaults if @defaults
80
-
81
- # Establish defaults
82
- # First the default defaults
83
- @defaults = {
84
- 'name' => File.basename(Dir.getwd),
85
- 'environment' => environment,
86
- 'env' => { 'RAILS_ENV' => environment },
87
- 'port' => 50_000
88
- }
89
-
90
- @defaults['env'].merge! secrets
91
- @defaults['application'] = secrets
92
- @defaults = @defaults.supermerge(roles_all[DEFAULTS_KEY]) if roles_all.key? DEFAULTS_KEY
93
- @defaults = @defaults.supermerge(roles[DEFAULTS_KEY]) if roles.key? DEFAULTS_KEY
94
- @defaults
95
- end
38
+ def actionable_roles
39
+ roles.select { |role, _i| role != DEFAULTS_KEY && (filters.key?('role') ? role == filters['role'] : true) }
40
+ end
96
41
 
97
- # Secret environment variables
98
- def secrets
99
- @secrets ||= Foreplay::Engine::Secrets.new(environment, roles_all['secrets']).fetch || {}
100
- end
42
+ def threads(role, instructions)
43
+ Foreplay::Engine::Role.new(
44
+ environment,
45
+ mode,
46
+ build_instructions(role, instructions)
47
+ ).threads
48
+ end
101
49
 
102
- def verbose
103
- @verbose ||= filters.key?('verbose')
104
- end
50
+ def explanatory_text(hsh, key)
51
+ hsh.key?(key) ? "#{hsh[key].dup.yellow} #{key}" : "all #{key}s"
52
+ end
105
53
 
106
- def roles
107
- @roles ||= roles_all[environment]
108
- end
54
+ def build_instructions(role, additional_instructions)
55
+ instructions = defaults.supermerge(additional_instructions)
56
+ instructions['role'] = role
57
+ instructions['verbose'] = verbose
58
+ required_keys = %w(name environment role servers path repository)
109
59
 
110
- def roles_all
111
- return @roles_all if @roles_all
60
+ required_keys.each do |key|
61
+ next if instructions.key? key
62
+ terminate("Required key #{key} not found in instructions for #{environment} environment.\nCheck #{config_file}")
63
+ end
112
64
 
113
- @roles_all = YAML.load(File.read(config_file))
65
+ # Apply server filter
66
+ instructions['servers'] &= server_filter if server_filter
67
+ instructions
68
+ end
114
69
 
115
- # This environment
116
- unless @roles_all.key? environment
117
- terminate("No deployment configuration defined for #{environment} environment.\nCheck #{config_file}")
70
+ def server_filter
71
+ @server_filter ||= filters['server'].split(',') if filters.key?('server')
118
72
  end
119
73
 
120
- @roles_all
121
- rescue Errno::ENOENT
122
- terminate "Can't find configuration file #{config_file}.\nPlease run foreplay setup or create the file manually."
123
- rescue Psych::SyntaxError
124
- terminate "I don't understand the configuration file #{config_file}.\n"\
125
- 'Please run foreplay setup or edit the file manually.'
74
+ def verbose
75
+ @verbose ||= filters.key?('verbose')
76
+ end
126
77
  end
127
78
  end
@@ -1,13 +1,15 @@
1
1
  require 'thor/group'
2
2
 
3
- class Foreplay::Launcher < Thor::Group
4
- include Thor::Actions
3
+ module Foreplay
4
+ class Launcher < Thor::Group
5
+ include Thor::Actions
5
6
 
6
- argument :mode, type: :string, required: true
7
- argument :environment, type: :string, required: true
8
- argument :filters, type: :hash, required: false
7
+ argument :mode, type: :string, required: true
8
+ argument :environment, type: :string, required: true
9
+ argument :filters, type: :hash, required: false
9
10
 
10
- def parse
11
- Foreplay::Engine.new(environment, filters).__send__ mode
11
+ def parse
12
+ Foreplay::Engine.new(environment, filters).__send__ mode
13
+ end
12
14
  end
13
15
  end
@@ -1,31 +1,33 @@
1
1
  require 'thor/group'
2
2
 
3
- class Foreplay::Setup < Thor::Group
4
- include Thor::Actions
3
+ module Foreplay
4
+ class Setup < Thor::Group
5
+ include Thor::Actions
5
6
 
6
- class_option :name, aliases: '-n', default: File.basename(Dir.getwd)
7
- class_option :repository, aliases: '-r'
8
- class_option :user, aliases: '-u'
9
- class_option :password
10
- class_option :keyfile
11
- class_option :private_key, aliases: '-k'
12
- class_option :port, aliases: '-p', default: 50_000
13
- class_option :path, aliases: '-f'
14
- class_option :servers, aliases: '-s', type: :array
15
- class_option :db_adapter, aliases: '-a', default: 'postgresql'
16
- class_option :db_encoding, aliases: '-e', default: 'utf8'
17
- class_option :db_pool, default: 5
18
- class_option :db_name, aliases: '-d'
19
- class_option :db_host, aliases: '-h'
20
- class_option :db_user
21
- class_option :db_password
22
- class_option :resque_redis
7
+ class_option :name, aliases: '-n', default: File.basename(Dir.getwd)
8
+ class_option :repository, aliases: '-r'
9
+ class_option :user, aliases: '-u'
10
+ class_option :password
11
+ class_option :keyfile
12
+ class_option :private_key, aliases: '-k'
13
+ class_option :port, aliases: '-p', default: 50_000
14
+ class_option :path, aliases: '-f'
15
+ class_option :servers, aliases: '-s', type: :array
16
+ class_option :db_adapter, aliases: '-a', default: 'postgresql'
17
+ class_option :db_encoding, aliases: '-e', default: 'utf8'
18
+ class_option :db_pool, default: 5
19
+ class_option :db_name, aliases: '-d'
20
+ class_option :db_host, aliases: '-h'
21
+ class_option :db_user
22
+ class_option :db_password
23
+ class_option :resque_redis
23
24
 
24
- def self.source_root
25
- File.dirname(__FILE__)
26
- end
25
+ def self.source_root
26
+ File.dirname(__FILE__)
27
+ end
27
28
 
28
- def config
29
- template('setup/foreplay.yml', 'config/foreplay.yml')
29
+ def config
30
+ template('setup/foreplay.yml', 'config/foreplay.yml')
31
+ end
30
32
  end
31
33
  end
@@ -1,3 +1,3 @@
1
1
  module Foreplay
2
- VERSION = '0.9.13'
2
+ VERSION = '0.10.1'
3
3
  end
data/lib/foreplay.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  require 'foreplay/version'
2
- require 'foreplay/foreplay'
3
- require 'foreplay/launcher'
4
2
  require 'foreplay/engine'
5
- require 'foreplay/engine/remote'
6
- require 'foreplay/engine/remote/check'
7
- require 'foreplay/engine/remote/step'
8
- require 'foreplay/engine/logger'
9
- require 'foreplay/engine/port'
10
- require 'foreplay/engine/role'
11
- require 'foreplay/engine/secrets'
12
- require 'foreplay/engine/server'
13
- require 'foreplay/engine/step'
3
+ require 'foreplay/launcher'
4
+
5
+ module Foreplay
6
+ DEFAULT_PORT = 50_000
7
+ PORT_GAP = 1_000
8
+
9
+ def log(message, options = {})
10
+ Foreplay::Engine::Logger.new(message, options)
11
+ end
12
+
13
+ def terminate(message)
14
+ fail message
15
+ end
16
+ end
@@ -137,9 +137,13 @@ describe Foreplay::Launcher do
137
137
  'rvm rvmrc warning ignore 50000',
138
138
  'gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys D39DC0E3',
139
139
  'cd 50000 && mkdir -p tmp doc log config',
140
- 'rvm info',
140
+ 'rvm rvmrc load && rvm info',
141
141
  'if [ -f .ruby-version ] ; then rvm install `cat .ruby-version` ; else echo "No .ruby-version file found" ; fi',
142
- 'echo "RAILS_ENV=production" > .env',
142
+ 'echo "HOME=$HOME" > .env',
143
+ 'echo "SHELL=$SHELL" >> .env',
144
+ 'echo "PATH=$PATH:`which bundle`" >> .env',
145
+ 'echo "GEM_HOME=$HOME/.rvm/gems/`rvm tools identifier`" >> .env',
146
+ 'echo "RAILS_ENV=production" >> .env',
143
147
  'echo "concurrency: web=1,worker=0,scheduler=0" > .foreman',
144
148
  'echo "app: foreplay-50000" >> .foreman',
145
149
  'echo "port: 50000" >> .foreman',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreplay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.13
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xenapto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-08 00:00:00.000000000 Z
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foreman
@@ -218,10 +218,10 @@ files:
218
218
  - features/setup.feature
219
219
  - features/support/env.rb
220
220
  - foreplay.gemspec
221
- - foreplay.rake
222
221
  - lib/foreplay.rb
223
222
  - lib/foreplay/cli.rb
224
223
  - lib/foreplay/engine.rb
224
+ - lib/foreplay/engine/defaults.rb
225
225
  - lib/foreplay/engine/logger.rb
226
226
  - lib/foreplay/engine/port.rb
227
227
  - lib/foreplay/engine/remote.rb
@@ -232,7 +232,6 @@ files:
232
232
  - lib/foreplay/engine/server.rb
233
233
  - lib/foreplay/engine/step.rb
234
234
  - lib/foreplay/engine/steps.yml
235
- - lib/foreplay/foreplay.rb
236
235
  - lib/foreplay/launcher.rb
237
236
  - lib/foreplay/setup.rb
238
237
  - lib/foreplay/setup/foreplay.yml
data/foreplay.rake DELETED
@@ -1,28 +0,0 @@
1
- # Syntax:
2
- #
3
- # rake ENV=production [ROLE=web] foreplay:command
4
- #
5
- # You can set the environment variables ENV and ROLE elsewhere if you wish.
6
- # If ROLE is not defined then we deploy all roles.
7
- #
8
- # Dependencies:
9
- #
10
- # gem 'net-ssh-shell'
11
- #
12
- # You can constrain this to whatever group you use for initiating deployments, e.g.
13
- #
14
- # group :development do
15
- # gem 'net-ssh-shell'
16
- # end
17
-
18
- namespace :foreplay do
19
- desc 'Push app to deployment targets'
20
- task push: :environment do
21
- Foreplay.push
22
- end
23
-
24
- desc 'Check deployment configuration'
25
- task check: :environment do
26
- Foreplay.push true
27
- end
28
- end
@@ -1,9 +0,0 @@
1
- module Foreplay
2
- def log(message, options = {})
3
- Foreplay::Engine::Logger.new(message, options)
4
- end
5
-
6
- def terminate(message)
7
- fail message
8
- end
9
- end