engineyard 0.2.7

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.
Files changed (52) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +7 -0
  3. data/bin/ey +14 -0
  4. data/lib/engineyard.rb +44 -0
  5. data/lib/engineyard/account.rb +78 -0
  6. data/lib/engineyard/api.rb +104 -0
  7. data/lib/engineyard/cli.rb +169 -0
  8. data/lib/engineyard/cli/api.rb +42 -0
  9. data/lib/engineyard/cli/error.rb +44 -0
  10. data/lib/engineyard/cli/ui.rb +96 -0
  11. data/lib/engineyard/config.rb +86 -0
  12. data/lib/engineyard/repo.rb +24 -0
  13. data/lib/vendor/thor.rb +244 -0
  14. data/lib/vendor/thor/actions.rb +275 -0
  15. data/lib/vendor/thor/actions/create_file.rb +103 -0
  16. data/lib/vendor/thor/actions/directory.rb +91 -0
  17. data/lib/vendor/thor/actions/empty_directory.rb +134 -0
  18. data/lib/vendor/thor/actions/file_manipulation.rb +223 -0
  19. data/lib/vendor/thor/actions/inject_into_file.rb +104 -0
  20. data/lib/vendor/thor/base.rb +540 -0
  21. data/lib/vendor/thor/core_ext/file_binary_read.rb +9 -0
  22. data/lib/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  23. data/lib/vendor/thor/core_ext/ordered_hash.rb +100 -0
  24. data/lib/vendor/thor/error.rb +30 -0
  25. data/lib/vendor/thor/group.rb +271 -0
  26. data/lib/vendor/thor/invocation.rb +180 -0
  27. data/lib/vendor/thor/parser.rb +4 -0
  28. data/lib/vendor/thor/parser/argument.rb +67 -0
  29. data/lib/vendor/thor/parser/arguments.rb +150 -0
  30. data/lib/vendor/thor/parser/option.rb +128 -0
  31. data/lib/vendor/thor/parser/options.rb +169 -0
  32. data/lib/vendor/thor/rake_compat.rb +66 -0
  33. data/lib/vendor/thor/runner.rb +314 -0
  34. data/lib/vendor/thor/shell.rb +83 -0
  35. data/lib/vendor/thor/shell/basic.rb +239 -0
  36. data/lib/vendor/thor/shell/color.rb +108 -0
  37. data/lib/vendor/thor/task.rb +102 -0
  38. data/lib/vendor/thor/util.rb +230 -0
  39. data/lib/vendor/thor/version.rb +3 -0
  40. data/spec/engineyard/api_spec.rb +56 -0
  41. data/spec/engineyard/cli/api_spec.rb +44 -0
  42. data/spec/engineyard/cli_spec.rb +20 -0
  43. data/spec/engineyard/config_spec.rb +57 -0
  44. data/spec/engineyard/repo_spec.rb +52 -0
  45. data/spec/engineyard_spec.rb +7 -0
  46. data/spec/ey/deploy_spec.rb +65 -0
  47. data/spec/ey/ey_spec.rb +16 -0
  48. data/spec/spec.opts +2 -0
  49. data/spec/spec_helper.rb +40 -0
  50. data/spec/support/bundled_ey +10 -0
  51. data/spec/support/helpers.rb +46 -0
  52. metadata +231 -0
@@ -0,0 +1,230 @@
1
+ require 'rbconfig'
2
+
3
+ class Thor
4
+ module Sandbox #:nodoc:
5
+ end
6
+
7
+ # This module holds several utilities:
8
+ #
9
+ # 1) Methods to convert thor namespaces to constants and vice-versa.
10
+ #
11
+ # Thor::Utils.namespace_from_thor_class(Foo::Bar::Baz) #=> "foo:bar:baz"
12
+ #
13
+ # 2) Loading thor files and sandboxing:
14
+ #
15
+ # Thor::Utils.load_thorfile("~/.thor/foo")
16
+ #
17
+ module Util
18
+
19
+ # Receives a namespace and search for it in the Thor::Base subclasses.
20
+ #
21
+ # ==== Parameters
22
+ # namespace<String>:: The namespace to search for.
23
+ #
24
+ def self.find_by_namespace(namespace)
25
+ namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26
+ Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
27
+ end
28
+
29
+ # Receives a constant and converts it to a Thor namespace. Since Thor tasks
30
+ # can be added to a sandbox, this method is also responsable for removing
31
+ # the sandbox namespace.
32
+ #
33
+ # This method should not be used in general because it's used to deal with
34
+ # older versions of Thor. On current versions, if you need to get the
35
+ # namespace from a class, just call namespace on it.
36
+ #
37
+ # ==== Parameters
38
+ # constant<Object>:: The constant to be converted to the thor path.
39
+ #
40
+ # ==== Returns
41
+ # String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
42
+ #
43
+ def self.namespace_from_thor_class(constant)
44
+ constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
45
+ constant = snake_case(constant).squeeze(":")
46
+ constant
47
+ end
48
+
49
+ # Given the contents, evaluate it inside the sandbox and returns the
50
+ # namespaces defined in the sandbox.
51
+ #
52
+ # ==== Parameters
53
+ # contents<String>
54
+ #
55
+ # ==== Returns
56
+ # Array[Object]
57
+ #
58
+ def self.namespaces_in_content(contents, file=__FILE__)
59
+ old_constants = Thor::Base.subclasses.dup
60
+ Thor::Base.subclasses.clear
61
+
62
+ load_thorfile(file, contents)
63
+
64
+ new_constants = Thor::Base.subclasses.dup
65
+ Thor::Base.subclasses.replace(old_constants)
66
+
67
+ new_constants.map!{ |c| c.namespace }
68
+ new_constants.compact!
69
+ new_constants
70
+ end
71
+
72
+ # Returns the thor classes declared inside the given class.
73
+ #
74
+ def self.thor_classes_in(klass)
75
+ stringfied_constants = klass.constants.map { |c| c.to_s }
76
+ Thor::Base.subclasses.select do |subclass|
77
+ next unless subclass.name
78
+ stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
79
+ end
80
+ end
81
+
82
+ # Receives a string and convert it to snake case. SnakeCase returns snake_case.
83
+ #
84
+ # ==== Parameters
85
+ # String
86
+ #
87
+ # ==== Returns
88
+ # String
89
+ #
90
+ def self.snake_case(str)
91
+ return str.downcase if str =~ /^[A-Z_]+$/
92
+ str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
93
+ return $+.downcase
94
+ end
95
+
96
+ # Receives a string and convert it to camel case. camel_case returns CamelCase.
97
+ #
98
+ # ==== Parameters
99
+ # String
100
+ #
101
+ # ==== Returns
102
+ # String
103
+ #
104
+ def self.camel_case(str)
105
+ return str if str !~ /_/ && str =~ /[A-Z]+.*/
106
+ str.split('_').map { |i| i.capitalize }.join
107
+ end
108
+
109
+ # Receives a namespace and tries to retrieve a Thor or Thor::Group class
110
+ # from it. It first searches for a class using the all the given namespace,
111
+ # if it's not found, removes the highest entry and searches for the class
112
+ # again. If found, returns the highest entry as the class name.
113
+ #
114
+ # ==== Examples
115
+ #
116
+ # class Foo::Bar < Thor
117
+ # def baz
118
+ # end
119
+ # end
120
+ #
121
+ # class Baz::Foo < Thor::Group
122
+ # end
123
+ #
124
+ # Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
125
+ # Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
126
+ # Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
127
+ #
128
+ # ==== Parameters
129
+ # namespace<String>
130
+ #
131
+ def self.find_class_and_task_by_namespace(namespace)
132
+ if namespace.include?(?:)
133
+ pieces = namespace.split(":")
134
+ task = pieces.pop
135
+ klass = Thor::Util.find_by_namespace(pieces.join(":"))
136
+ end
137
+
138
+ unless klass
139
+ klass, task = Thor::Util.find_by_namespace(namespace), nil
140
+ end
141
+
142
+ return klass, task
143
+ end
144
+
145
+ # The same as namespace_to_thor_class_and_task!, but raises an error if a klass
146
+ # could not be found.
147
+ def self.find_class_and_task_by_namespace!(namespace)
148
+ klass, task = find_class_and_task_by_namespace(namespace)
149
+ raise Error, "Could not find namespace or task #{namespace.inspect}." unless klass
150
+ return klass, task
151
+ end
152
+
153
+ # Receives a path and load the thor file in the path. The file is evaluated
154
+ # inside the sandbox to avoid namespacing conflicts.
155
+ #
156
+ def self.load_thorfile(path, content=nil)
157
+ content ||= File.binread(path)
158
+
159
+ begin
160
+ Thor::Sandbox.class_eval(content, path)
161
+ rescue Exception => e
162
+ $stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
163
+ end
164
+ end
165
+
166
+ def self.user_home
167
+ @@user_home ||= if ENV["HOME"]
168
+ ENV["HOME"]
169
+ elsif ENV["USERPROFILE"]
170
+ ENV["USERPROFILE"]
171
+ elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
172
+ File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
173
+ elsif ENV["APPDATA"]
174
+ ENV["APPDATA"]
175
+ else
176
+ begin
177
+ File.expand_path("~")
178
+ rescue
179
+ if File::ALT_SEPARATOR
180
+ "C:/"
181
+ else
182
+ "/"
183
+ end
184
+ end
185
+ end
186
+ end
187
+
188
+ # Returns the root where thor files are located, dependending on the OS.
189
+ #
190
+ def self.thor_root
191
+ File.join(user_home, ".thor").gsub(/\\/, '/')
192
+ end
193
+
194
+ # Returns the files in the thor root. On Windows thor_root will be something
195
+ # like this:
196
+ #
197
+ # C:\Documents and Settings\james\.thor
198
+ #
199
+ # If we don't #gsub the \ character, Dir.glob will fail.
200
+ #
201
+ def self.thor_root_glob
202
+ files = Dir["#{thor_root}/*"]
203
+
204
+ files.map! do |file|
205
+ File.directory?(file) ? File.join(file, "main.thor") : file
206
+ end
207
+ end
208
+
209
+ # Where to look for Thor files.
210
+ #
211
+ def self.globs_for(path)
212
+ ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
213
+ end
214
+
215
+ # Return the path to the ruby interpreter taking into account multiple
216
+ # installations and windows extensions.
217
+ #
218
+ def self.ruby_command
219
+ @ruby_command ||= begin
220
+ ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
221
+ ruby << Config::CONFIG['EXEEXT']
222
+
223
+ # escape string in case path to ruby executable contain spaces.
224
+ ruby.sub!(/.*\s.*/m, '"\&"')
225
+ ruby
226
+ end
227
+ end
228
+
229
+ end
230
+ end
@@ -0,0 +1,3 @@
1
+ class Thor
2
+ VERSION = "0.13.4".freeze
3
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe EY::API do
4
+ it "gets the api token from ~/.eyrc if possible" do
5
+ write_config({"api_token" => "asdf"}, '~/.eyrc')
6
+ EY::API.new.should == EY::API.new("asdf")
7
+ end
8
+
9
+ context "fetching the token from EY cloud" do
10
+ before(:each) do
11
+ FakeWeb.register_uri(:post, "https://cloud.engineyard.com/api/v2/authenticate", :body => %|{"api_token": "asdf"}|)
12
+ @token = EY::API.fetch_token("a@b.com", "foo")
13
+ end
14
+
15
+ it "returns an EY::API" do
16
+ @token.should == "asdf"
17
+ end
18
+
19
+ it "puts the api token into .eyrc" do
20
+ load_config('~/.eyrc')["api_token"].should == "asdf"
21
+ end
22
+ end
23
+
24
+ describe "saving the token" do
25
+ context "without a custom endpoint" do
26
+ it "saves the api token at the root of the data" do
27
+ EY::API.save_token("asdf")
28
+ load_config('~/.eyrc')["api_token"].should == "asdf"
29
+ end
30
+ end
31
+
32
+ context "with a custom endpoint" do
33
+ before do
34
+ write_config({"endpoint" => "http://localhost/"}, 'ey.yml')
35
+ EY::API.save_token("asdf")
36
+ end
37
+
38
+ it "saves the api token" do
39
+ load_config('~/.eyrc').should == {"http://localhost/" => {"api_token" => "asdf"}}
40
+ end
41
+
42
+ it "reads the api token" do
43
+ EY::API.read_token.should == "asdf"
44
+ end
45
+ end
46
+ end
47
+
48
+ it "raises InvalidCredentials when the credentials are invalid" do
49
+ FakeWeb.register_uri(:post, "https://cloud.engineyard.com/api/v2/authenticate", :status => 401)
50
+
51
+ lambda {
52
+ EY::API.fetch_token("a@b.com", "foo")
53
+ }.should raise_error(EY::Error)
54
+ end
55
+
56
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'engineyard/cli'
3
+
4
+ describe EY::CLI::API do
5
+ before(:all) do
6
+ EY.ui = EY::CLI::UI.new
7
+ end
8
+
9
+ after(:all) do
10
+ EY.ui = EY::UI.new
11
+ end
12
+
13
+ it "gets the api token from ~/.eyrc if possible" do
14
+ File.open(File.expand_path("~/.eyrc"), "w") do |fp|
15
+ YAML.dump({"api_token" => "asdf"}, fp)
16
+ end
17
+
18
+ EY::CLI::API.new.should == EY::CLI::API.new("asdf")
19
+ end
20
+
21
+ context "without saved api token" do
22
+ before(:each) do
23
+ FakeWeb.register_uri(:post, "https://cloud.engineyard.com/api/v2/authenticate", :body => %|{"api_token": "asdf"}|)
24
+
25
+ capture_stdout("\n\n") do
26
+ @token = EY::CLI::API.new
27
+ end
28
+ end
29
+
30
+ it "asks you for your credentials" do
31
+ @out.should include("Email:")
32
+ @out.should include("Password:")
33
+ end
34
+
35
+ it "gets the api token" do
36
+ @token.should == EY::CLI::API.new("asdf")
37
+ end
38
+
39
+ it "saves the api token to ~/.eyrc" do
40
+ YAML.load_file(File.expand_path("~/.eyrc")).should == {"api_token" => "asdf"}
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'engineyard/cli'
3
+
4
+ describe EY::CLI do
5
+
6
+ it "sets up EY.ui" do
7
+ EY.ui.should be_an(EY::UI)
8
+ capture_stdout do
9
+ EY::CLI.start(["help"])
10
+ end
11
+ EY.ui.should be_an(EY::CLI::UI)
12
+ end
13
+
14
+ it "provides error classes" do
15
+ EY::CLI::EnvironmentError.should be
16
+ EY::CLI::BranchMismatch.should be
17
+ EY::CLI::DeployArgumentError.should be
18
+ end
19
+
20
+ end # EY::CLI
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ require 'uri'
3
+
4
+ describe EY::Config do
5
+ describe "environments" do
6
+ it "get loaded from the config file" do
7
+ write_config("environments" => {"production" => {"default" => true}})
8
+ EY::Config.new.environments["production"]["default"].should be_true
9
+ end
10
+
11
+ it "are present when the config file has no environments key" do
12
+ write_config("endpoint" => "http://localhost/")
13
+ EY::Config.new.environments.should == {}
14
+ end
15
+ end
16
+
17
+ describe "endpoint" do
18
+ it "defaults to production EY Cloud" do
19
+ EY::Config.new.endpoint.should == EY::Config.new.default_endpoint
20
+ end
21
+
22
+ it "gets loaded from the config file" do
23
+ write_config("endpoint" => "http://localhost/")
24
+ EY::Config.new.endpoint.should == URI.parse("http://localhost/")
25
+ end
26
+
27
+ it "raises on an invalid endpoint" do
28
+ write_config("endpoint" => "non/absolute")
29
+ lambda { EY::Config.new.endpoint }.
30
+ should raise_error(EY::Config::ConfigurationError)
31
+ end
32
+ end
33
+
34
+ it "provides default_endpoint?" do
35
+ write_config("endpoint" => "http://localhost/")
36
+ EY::Config.new.default_endpoint?.should_not be_true
37
+ end
38
+
39
+ describe "files" do
40
+ it "looks for config/ey.yml" do
41
+ write_config({"endpoint" => "http://something/"}, "ey.yml")
42
+ write_config({"endpoint" => "http://localhost/"}, "config/ey.yml")
43
+ EY::Config.new.endpoint.should == URI.parse("http://localhost/")
44
+ end
45
+
46
+ it "looks for ey.yml" do
47
+ write_config({"endpoint" => "http://foo/"}, "ey.yml")
48
+ EY::Config.new.endpoint.should == URI.parse("http://foo/")
49
+ end
50
+
51
+ it "looks for the file given" do
52
+ write_config({"endpoint" => "http://bar/"}, "summat.yml")
53
+ EY::Config.new("summat.yml").endpoint.should == URI.parse("http://bar/")
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe EY::Repo do
4
+ before(:all) do
5
+ @path = "/tmp/ey-test/.git/"
6
+ @r = EY::Repo.new("/tmp/ey-test")
7
+ end
8
+
9
+ describe "current_branch method" do
10
+ it "returns the name of the current branch" do
11
+ set_head "ref: refs/heads/master"
12
+ @r.current_branch.should == "master"
13
+ end
14
+
15
+ it "returns nil if there is no current branch" do
16
+ set_head "20bf478ab6a91ec5771130aa4c8cfd3d150c4146"
17
+ @r.current_branch.should be_nil
18
+ end
19
+
20
+ def set_head(head)
21
+ File.open(@path+"HEAD", "w"){|f| f.write(head) }
22
+ end
23
+ end # current_branch
24
+
25
+ describe "urls method" do
26
+ it "returns the urls of the remotes" do
27
+ origin_url = "git://github.com/engineyard/engineyard.git"
28
+ other_url = "git@github.com:engineyard/engineyard.git"
29
+ set_url origin_url
30
+ set_url other_url, "other"
31
+ @r.urls.should include(origin_url)
32
+ @r.urls.should include(other_url)
33
+ end
34
+
35
+ it "returns nil if there is no origin remote" do
36
+ set_url nil
37
+ @r.urls.should be_empty
38
+ end
39
+
40
+ def set_url(url, remote="origin")
41
+ @config_path = @path+"config"
42
+ # This has to all shell out because FakeFS is enabled
43
+ if url
44
+ system("mkdir -p #{@path} && cd #{@path} && git init -q")
45
+ system("git config -f #{@config_path} remote.#{remote}.url #{url}")
46
+ else
47
+ system("rm -rf #{@config_path}")
48
+ end
49
+ end
50
+ end # url
51
+
52
+ end # EY::Repo