cf_script 0.0.1.beta.1

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 (205) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/CHANGELOG.md +0 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +22 -0
  6. data/README.md +84 -0
  7. data/Rakefile +33 -0
  8. data/cf_script.gemspec +34 -0
  9. data/lib/cf_script/callbacks.rb +43 -0
  10. data/lib/cf_script/command/base.rb +57 -0
  11. data/lib/cf_script/command/cf/apps/app.rb +19 -0
  12. data/lib/cf_script/command/cf/apps/apps.rb +44 -0
  13. data/lib/cf_script/command/cf/apps/env.rb +20 -0
  14. data/lib/cf_script/command/cf/apps/push.rb +47 -0
  15. data/lib/cf_script/command/cf/apps/restage.rb +19 -0
  16. data/lib/cf_script/command/cf/apps/restart.rb +19 -0
  17. data/lib/cf_script/command/cf/apps/set_env.rb +18 -0
  18. data/lib/cf_script/command/cf/apps/start.rb +19 -0
  19. data/lib/cf_script/command/cf/apps/stop.rb +19 -0
  20. data/lib/cf_script/command/cf/apps/unset_env.rb +18 -0
  21. data/lib/cf_script/command/cf/apps.rb +55 -0
  22. data/lib/cf_script/command/cf/general/api.rb +36 -0
  23. data/lib/cf_script/command/cf/general/auth.rb +28 -0
  24. data/lib/cf_script/command/cf/general/login.rb +42 -0
  25. data/lib/cf_script/command/cf/general/logout.rb +15 -0
  26. data/lib/cf_script/command/cf/general/target.rb +64 -0
  27. data/lib/cf_script/command/cf/general.rb +15 -0
  28. data/lib/cf_script/command/cf/routes/check_route.rb +17 -0
  29. data/lib/cf_script/command/cf/routes/create_route.rb +19 -0
  30. data/lib/cf_script/command/cf/routes/delete_route.rb +19 -0
  31. data/lib/cf_script/command/cf/routes/map_route.rb +30 -0
  32. data/lib/cf_script/command/cf/routes/routes.rb +40 -0
  33. data/lib/cf_script/command/cf/routes/unmap_route.rb +30 -0
  34. data/lib/cf_script/command/cf/routes.rb +17 -0
  35. data/lib/cf_script/command/cf/spaces/space.rb +36 -0
  36. data/lib/cf_script/command/cf/spaces/spaces.rb +23 -0
  37. data/lib/cf_script/command/cf/spaces.rb +9 -0
  38. data/lib/cf_script/command/error.rb +5 -0
  39. data/lib/cf_script/command/line.rb +105 -0
  40. data/lib/cf_script/command/registry.rb +50 -0
  41. data/lib/cf_script/command/runner.rb +97 -0
  42. data/lib/cf_script/command.rb +44 -0
  43. data/lib/cf_script/config.rb +60 -0
  44. data/lib/cf_script/executor/non_blocking.rb +107 -0
  45. data/lib/cf_script/executor/recorder.rb +16 -0
  46. data/lib/cf_script/executor/simple.rb +17 -0
  47. data/lib/cf_script/executor.rb +5 -0
  48. data/lib/cf_script/manifest.rb +38 -0
  49. data/lib/cf_script/object/api_endpoint.rb +10 -0
  50. data/lib/cf_script/object/app_info.rb +69 -0
  51. data/lib/cf_script/object/app_list.rb +24 -0
  52. data/lib/cf_script/object/app_spec.rb +55 -0
  53. data/lib/cf_script/object/attribute.rb +18 -0
  54. data/lib/cf_script/object/attribute_list.rb +40 -0
  55. data/lib/cf_script/object/instance_status.rb +29 -0
  56. data/lib/cf_script/object/route_info.rb +14 -0
  57. data/lib/cf_script/object/space.rb +17 -0
  58. data/lib/cf_script/object/target.rb +23 -0
  59. data/lib/cf_script/object.rb +24 -0
  60. data/lib/cf_script/output/buffer.rb +74 -0
  61. data/lib/cf_script/output/parser/attributes.rb +44 -0
  62. data/lib/cf_script/output/parser/section.rb +15 -0
  63. data/lib/cf_script/output/parser/table.rb +46 -0
  64. data/lib/cf_script/output/parser.rb +15 -0
  65. data/lib/cf_script/output/tests.rb +51 -0
  66. data/lib/cf_script/output/utils.rb +13 -0
  67. data/lib/cf_script/output.rb +62 -0
  68. data/lib/cf_script/scope/app/env.rb +15 -0
  69. data/lib/cf_script/scope/app/routes.rb +21 -0
  70. data/lib/cf_script/scope/app/state.rb +33 -0
  71. data/lib/cf_script/scope/app/utils.rb +32 -0
  72. data/lib/cf_script/scope/app.rb +94 -0
  73. data/lib/cf_script/scope/proxy.rb +36 -0
  74. data/lib/cf_script/scope/script.rb +59 -0
  75. data/lib/cf_script/scope/space.rb +9 -0
  76. data/lib/cf_script/scope/target.rb +90 -0
  77. data/lib/cf_script/scope.rb +68 -0
  78. data/lib/cf_script/ui.rb +261 -0
  79. data/lib/cf_script/utils.rb +19 -0
  80. data/lib/cf_script/version.rb +3 -0
  81. data/lib/cf_script.rb +105 -0
  82. data/test/fixtures/commands/apps/app/good.yml +18 -0
  83. data/test/fixtures/commands/apps/app/not_found.yml +8 -0
  84. data/test/fixtures/commands/apps/app/running.yml +19 -0
  85. data/test/fixtures/commands/apps/apps/good.yml +14 -0
  86. data/test/fixtures/commands/apps/apps/no_apps.yml +10 -0
  87. data/test/fixtures/commands/apps/env/empty.yml +37 -0
  88. data/test/fixtures/commands/apps/env/good.yml +41 -0
  89. data/test/fixtures/commands/apps/push/good.yml +60 -0
  90. data/test/fixtures/commands/apps/restage/good.yml +48 -0
  91. data/test/fixtures/commands/apps/restage/not_found.yml +8 -0
  92. data/test/fixtures/commands/apps/restart/good.yml +35 -0
  93. data/test/fixtures/commands/apps/restart/not_found.yml +8 -0
  94. data/test/fixtures/commands/apps/set_env/empty.yml +5 -0
  95. data/test/fixtures/commands/apps/set_env/good.yml +9 -0
  96. data/test/fixtures/commands/apps/set_env/not_found.yml +8 -0
  97. data/test/fixtures/commands/apps/start/good.yml +32 -0
  98. data/test/fixtures/commands/apps/start/not_found.yml +8 -0
  99. data/test/fixtures/commands/apps/start/with_container.yml +98 -0
  100. data/test/fixtures/commands/apps/stop/good.yml +8 -0
  101. data/test/fixtures/commands/apps/stop/not_found.yml +8 -0
  102. data/test/fixtures/commands/apps/unset_env/empty.yml +5 -0
  103. data/test/fixtures/commands/apps/unset_env/good.yml +9 -0
  104. data/test/fixtures/commands/apps/unset_env/not_found.yml +8 -0
  105. data/test/fixtures/commands/common/fail.yml +5 -0
  106. data/test/fixtures/commands/common/no_endpoint.yml +11 -0
  107. data/test/fixtures/commands/common/no_login.yml +8 -0
  108. data/test/fixtures/commands/general/api/bad_argument.yml +9 -0
  109. data/test/fixtures/commands/general/api/bad_endpoint.yml +9 -0
  110. data/test/fixtures/commands/general/api/good.yml +7 -0
  111. data/test/fixtures/commands/general/api/not_set.yml +7 -0
  112. data/test/fixtures/commands/general/api/set.yml +7 -0
  113. data/test/fixtures/commands/general/auth/empty.yml +5 -0
  114. data/test/fixtures/commands/general/auth/good.yml +10 -0
  115. data/test/fixtures/commands/general/auth/rejected.yml +10 -0
  116. data/test/fixtures/commands/general/login/good.yml +8 -0
  117. data/test/fixtures/commands/general/login/rejected.yml +23 -0
  118. data/test/fixtures/commands/general/logout/good.yml +8 -0
  119. data/test/fixtures/commands/general/target/empty.yml +5 -0
  120. data/test/fixtures/commands/general/target/good.yml +11 -0
  121. data/test/fixtures/commands/general/target/no_space.yml +10 -0
  122. data/test/fixtures/commands/general/target/not_found.yml +9 -0
  123. data/test/fixtures/commands/general/target/production.yml +11 -0
  124. data/test/fixtures/commands/general/target/staging.yml +11 -0
  125. data/test/fixtures/commands/general/target/staging_org.yml +11 -0
  126. data/test/fixtures/commands/general/target/test.yml +11 -0
  127. data/test/fixtures/commands/routes/check_route/good.yml +9 -0
  128. data/test/fixtures/commands/routes/check_route/no_domain.yml +9 -0
  129. data/test/fixtures/commands/routes/check_route/no_route.yml +9 -0
  130. data/test/fixtures/commands/routes/create_route/exists.yml +9 -0
  131. data/test/fixtures/commands/routes/create_route/good.yml +8 -0
  132. data/test/fixtures/commands/routes/delete_route/good.yml +8 -0
  133. data/test/fixtures/commands/routes/delete_route/not_exist.yml +8 -0
  134. data/test/fixtures/commands/routes/map_route/good.yml +10 -0
  135. data/test/fixtures/commands/routes/map_route/host_taken.yml +10 -0
  136. data/test/fixtures/commands/routes/map_route/invalid.yml +9 -0
  137. data/test/fixtures/commands/routes/map_route/no_app.yml +8 -0
  138. data/test/fixtures/commands/routes/map_route/no_domain.yml +9 -0
  139. data/test/fixtures/commands/routes/routes/good.yml +14 -0
  140. data/test/fixtures/commands/routes/unmap_route/good.yml +8 -0
  141. data/test/fixtures/commands/routes/unmap_route/no_app.yml +8 -0
  142. data/test/fixtures/commands/routes/unmap_route/no_domain.yml +9 -0
  143. data/test/fixtures/commands/spaces/space/empty.yml +5 -0
  144. data/test/fixtures/commands/spaces/space/good.yml +16 -0
  145. data/test/fixtures/commands/spaces/space/staging.yml +16 -0
  146. data/test/fixtures/commands/spaces/spaces/empty.yml +5 -0
  147. data/test/fixtures/commands/spaces/spaces/good.yml +12 -0
  148. data/test/lib/cf_script/command/base_test.rb +21 -0
  149. data/test/lib/cf_script/command/cf/apps/app_test.rb +56 -0
  150. data/test/lib/cf_script/command/cf/apps/apps_test.rb +81 -0
  151. data/test/lib/cf_script/command/cf/apps/env_test.rb +55 -0
  152. data/test/lib/cf_script/command/cf/apps/push_test.rb +46 -0
  153. data/test/lib/cf_script/command/cf/apps/restage_test.rb +47 -0
  154. data/test/lib/cf_script/command/cf/apps/restart_test.rb +47 -0
  155. data/test/lib/cf_script/command/cf/apps/set_env_test.rb +38 -0
  156. data/test/lib/cf_script/command/cf/apps/start_test.rb +55 -0
  157. data/test/lib/cf_script/command/cf/apps/stop_test.rb +35 -0
  158. data/test/lib/cf_script/command/cf/apps/unset_env_test.rb +38 -0
  159. data/test/lib/cf_script/command/cf/general/api_test.rb +55 -0
  160. data/test/lib/cf_script/command/cf/general/auth_test.rb +76 -0
  161. data/test/lib/cf_script/command/cf/general/login_test.rb +80 -0
  162. data/test/lib/cf_script/command/cf/general/logout_test.rb +34 -0
  163. data/test/lib/cf_script/command/cf/general/target_test.rb +110 -0
  164. data/test/lib/cf_script/command/cf/routes/check_route_test.rb +31 -0
  165. data/test/lib/cf_script/command/cf/routes/create_route_test.rb +19 -0
  166. data/test/lib/cf_script/command/cf/routes/delete_route_test.rb +19 -0
  167. data/test/lib/cf_script/command/cf/routes/map_route_test.rb +56 -0
  168. data/test/lib/cf_script/command/cf/routes/routes_test.rb +24 -0
  169. data/test/lib/cf_script/command/cf/routes/unmap_route_test.rb +36 -0
  170. data/test/lib/cf_script/command/cf/spaces/space_test.rb +55 -0
  171. data/test/lib/cf_script/command/cf/spaces/spaces_test.rb +47 -0
  172. data/test/lib/cf_script/command/line_test.rb +59 -0
  173. data/test/lib/cf_script/object/api_endpoint_test.rb +19 -0
  174. data/test/lib/cf_script/object/app_info_test.rb +127 -0
  175. data/test/lib/cf_script/object/app_list_test.rb +51 -0
  176. data/test/lib/cf_script/object/attribute_list_test.rb +42 -0
  177. data/test/lib/cf_script/object/attribute_test.rb +29 -0
  178. data/test/lib/cf_script/object/instance_status_test.rb +37 -0
  179. data/test/lib/cf_script/object/space_test.rb +31 -0
  180. data/test/lib/cf_script/object/target_test.rb +41 -0
  181. data/test/lib/cf_script/output/buffer_test.rb +73 -0
  182. data/test/lib/cf_script/output/parser/attributes_test.rb +102 -0
  183. data/test/lib/cf_script/output/parser/section_test.rb +60 -0
  184. data/test/lib/cf_script/output/parser/table_test.rb +108 -0
  185. data/test/lib/cf_script/output/tests_test.rb +91 -0
  186. data/test/lib/cf_script/output_test.rb +134 -0
  187. data/test/lib/cf_script/scope/app/state_test.rb +61 -0
  188. data/test/lib/cf_script/scope/app_test.rb +101 -0
  189. data/test/lib/cf_script/scope/script_test.rb +88 -0
  190. data/test/lib/cf_script/scope/space_test.rb +27 -0
  191. data/test/lib/cf_script/scope/target_test.rb +158 -0
  192. data/test/lib/cf_script/scope_test.rb +41 -0
  193. data/test/lib/cf_script/utils_test.rb +20 -0
  194. data/test/lib/cf_script_test.rb +73 -0
  195. data/test/support/assertions.rb +14 -0
  196. data/test/support/helpers/config_context.rb +19 -0
  197. data/test/support/helpers/fixture_executor.rb +79 -0
  198. data/test/support/helpers/mock_execution.rb +58 -0
  199. data/test/support/helpers/object_helpers.rb +29 -0
  200. data/test/support/helpers/output_sink.rb +25 -0
  201. data/test/support/helpers.rb +3 -0
  202. data/test/support/shared_examples/command_object.rb +38 -0
  203. data/test/support/shared_examples.rb +30 -0
  204. data/test/test_helper.rb +34 -0
  205. metadata +321 -0
@@ -0,0 +1,44 @@
1
+ module CfScript
2
+ module Output::Parser::Attributes
3
+ ATTRIBUTE_REGEXP = /^(?<name>[^:]+):\s+(?<value>.+)$/
4
+
5
+ def parse_attribute(line, symbolize_name = false)
6
+ if line =~ ATTRIBUTE_REGEXP
7
+ name = Regexp.last_match[:name].strip
8
+ value = Regexp.last_match[:value].strip
9
+
10
+ CfScript::Attribute.new(
11
+ symbolize_name ? symbolize(name) : name,
12
+ Regexp.last_match['value'].strip
13
+ )
14
+ end
15
+ end
16
+
17
+ def parse_attribute_list(buffer, symbolize_names = true)
18
+ attribute_list = CfScript::AttributeList.new
19
+
20
+ buffer.each_line do |line|
21
+ if attribute = parse_attribute(line, symbolize_names)
22
+ attribute_list << attribute
23
+ end
24
+ end
25
+
26
+ attribute_list
27
+ end
28
+
29
+ def parse_line_attributes(buffer, regexp)
30
+ if matched = buffer.match(regexp)
31
+ attribute_list = CfScript::AttributeList.new
32
+
33
+ matched.names.each do |name|
34
+ attribute_list << CfScript::Attribute.new(
35
+ symbolize(name),
36
+ matched[name].strip
37
+ )
38
+ end
39
+
40
+ attribute_list
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ module CfScript
2
+ module Output::Parser::Section
3
+ def extract_section(buffer, header)
4
+ if match = buffer.match(/^#{header}:?\s*\n(.*?)\n\n/m)
5
+ match[1]
6
+ end
7
+ end
8
+
9
+ def parse_section_attributes(buffer, header)
10
+ if section = extract_section(buffer, header)
11
+ parse_attribute_list(section, false)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ module CfScript
2
+ module Output::Parser::Table
3
+ def parse_columns(line)
4
+ line ? line.split(/\s{2,}/).map(&:strip) : []
5
+ end
6
+
7
+ def parse_row(line, columns)
8
+ row = CfScript::AttributeList.new
9
+ values = line.split(/\s{2,}/, columns.length).map!(&:strip)
10
+
11
+ columns.each_with_index do |column, index|
12
+ # Special case for instance status tables
13
+ name = index == 0 && column == '' ? 'index' : column
14
+
15
+ row << CfScript::Attribute.new(
16
+ symbolize(name),
17
+ (values[index] ? values[index].strip : '')
18
+ )
19
+ end
20
+
21
+ row
22
+ end
23
+
24
+ def parse_rows(lines)
25
+ rows = []
26
+
27
+ if lines.first.strip.empty?
28
+ lines.shift
29
+ end
30
+
31
+ cols = parse_columns(lines.shift)
32
+
33
+ lines.each do |line|
34
+ rows << parse_row(line, cols)
35
+ end
36
+
37
+ rows
38
+ end
39
+
40
+ def parse_table(buffer, headers)
41
+ if m = buffer.match(/^#{headers.join('\s+')}/)
42
+ parse_rows buffer[m.begin(0)..-1].lines
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,15 @@
1
+ CfScript::Output.const_set(:Parser, Module.new)
2
+
3
+ require 'cf_script/output/parser/attributes'
4
+ require 'cf_script/output/parser/section'
5
+ require 'cf_script/output/parser/table'
6
+
7
+ module CfScript
8
+ module Output::Parser
9
+ include CfScript::Utils
10
+
11
+ include CfScript::Output::Parser::Attributes
12
+ include CfScript::Output::Parser::Section
13
+ include CfScript::Output::Parser::Table
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ module CfScript
2
+ module Output::Tests
3
+ def matches?(regexp)
4
+ out.matches?(regexp)
5
+ end
6
+
7
+ def contains?(text)
8
+ out.contains?(text)
9
+ end
10
+
11
+ def ok?
12
+ out.last_line_matches? /^OK$/
13
+ end
14
+
15
+ def authenticated?
16
+ matches? /Authenticating\.\.\.\nOK/m
17
+ end
18
+
19
+ def failed?
20
+ contains? 'FAILED'
21
+ end
22
+
23
+ def no_api_endpoint?
24
+ contains? 'No API endpoint set.'
25
+ end
26
+
27
+ def not_logged_in?
28
+ contains? 'Not logged in'
29
+ end
30
+
31
+ def not_authorized?
32
+ contains? 'You are not authorized'
33
+ end
34
+
35
+ def credentials_rejected?
36
+ contains? 'Credentials were rejected'
37
+ end
38
+
39
+ def not_found?(type, name)
40
+ out.last_line_matches? /#{type} #{name} not found/
41
+ end
42
+
43
+ def is_already?(name, state)
44
+ out.last_line_matches? /#{name} is already #{state}/
45
+ end
46
+
47
+ def already_exists?(type, name)
48
+ out.last_line_matches? /#{type} #{name} already exists/
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ module CfScript
2
+ module Output::Utils
3
+ def dump
4
+ list = []
5
+
6
+ list << 'STATUS:' << status.inspect
7
+ list << 'STDOUT:' << (stdout.size == 0 ? '<empty>' : stdout)
8
+ list << 'STDERR:' << (stderr.size == 0 ? '<empty>' : stderr)
9
+
10
+ CfScript.stderr.puts list.join("\n")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,62 @@
1
+ CfScript.const_set(:Output, Class.new)
2
+
3
+ require 'cf_script/output/buffer'
4
+ require 'cf_script/output/parser'
5
+ require 'cf_script/output/tests'
6
+ require 'cf_script/output/utils'
7
+
8
+ module CfScript
9
+ class Output
10
+ attr_reader :command
11
+ attr_reader :stdout
12
+ attr_reader :stderr
13
+ attr_reader :status
14
+
15
+ include CfScript::Output::Parser
16
+ include CfScript::Output::Tests
17
+ include CfScript::Output::Utils
18
+
19
+ def initialize(command, stdout, stderr, status)
20
+ @command = command
21
+ @stdout = stdout
22
+ @stderr = stderr
23
+ @status = status
24
+ end
25
+
26
+ def good?
27
+ status.success? && status.exitstatus == 0
28
+ end
29
+
30
+ def out
31
+ @sane_stdout ||= Buffer.new(stdout)
32
+ end
33
+
34
+ def err
35
+ @sane_stderr ||= Buffer.new(stderr)
36
+ end
37
+
38
+ def last_line
39
+ out.last_line
40
+ end
41
+
42
+ def attributes
43
+ parse_attribute_list(out)
44
+ end
45
+
46
+ def line_attributes(line_regexp)
47
+ parse_line_attributes(out, line_regexp)
48
+ end
49
+
50
+ def attributes_from(line_start)
51
+ parse_attribute_list(out.from(line_start))
52
+ end
53
+
54
+ def table(headers)
55
+ parse_table(out, headers)
56
+ end
57
+
58
+ def section_attributes(header)
59
+ parse_section_attributes(out, header)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,15 @@
1
+ module CfScript::Scope
2
+ module App::Env
3
+ def env
4
+ CfScript::Command.env name
5
+ end
6
+
7
+ def set_env(var_name, var_value)
8
+ CfScript::Command.set_env name, var_name, var_value
9
+ end
10
+
11
+ def unset_env(var_name)
12
+ CfScript::Command.unset_env name, var_name
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module CfScript::Scope
2
+ module App::Routes
3
+ def has_route?(domain, host = nil)
4
+ route = [host, domain].compact.join('.')
5
+
6
+ urls.split(', ').include?(route)
7
+ end
8
+
9
+ def map_route(domain, host = nil)
10
+ CfScript::Command.map_route name, domain, host do |mapped|
11
+ cf_self; has_route?(domain, host)
12
+ end
13
+ end
14
+
15
+ def unmap_route(domain, host = nil)
16
+ CfScript::Command.unmap_route name, domain, host do |unmapped|
17
+ cf_self; not has_route?(domain, host)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ module CfScript::Scope
2
+ module App::State
3
+ def start
4
+ CfScript::Command.start name do |app_info|
5
+ @app_info = app_info if app_info
6
+ end
7
+ end
8
+
9
+ def stop
10
+ CfScript::Command.stop name do |stopped|
11
+ cf_self
12
+ end
13
+ end
14
+
15
+ def restart
16
+ CfScript::Command.restart name do |app_info|
17
+ @app_info = app_info if app_info
18
+ end
19
+ end
20
+
21
+ def push(options = {})
22
+ CfScript::Command.push name, options do |app_info|
23
+ @app_info = app_info if app_info
24
+ end
25
+ end
26
+
27
+ def restage
28
+ CfScript::Command.restage name do |app_info|
29
+ @app_info = app_info if app_info
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module CfScript::Scope
2
+ module App::Utils
3
+ def dump
4
+ title name
5
+
6
+ app_info.instance_variables.each do |var|
7
+ next if var == :@name or var == :@instance_status
8
+ detail "#{var.to_s.gsub('@', '')}: #{app_info.instance_variable_get(var)}"
9
+ end
10
+
11
+ if app_info.instance_status
12
+ title 'Instances', 2
13
+
14
+ app_info.instance_status.each do |inst|
15
+ str = inst.show(:index, :state, :cpu, :memory, :disk, :since)
16
+
17
+ detail str, 3
18
+ end
19
+ end
20
+ end
21
+
22
+ def show(*attrs)
23
+ line = []
24
+
25
+ attrs.each do |attr|
26
+ line << "#{attr}: #{self.send(attr)}"
27
+ end
28
+
29
+ info line.join(', ')
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,94 @@
1
+ require 'forwardable'
2
+
3
+ module CfScript::Scope
4
+ class App < Base
5
+ extend Forwardable
6
+
7
+ attr_reader :name
8
+ attr_reader :app_info
9
+ #attr_reader :app_spec
10
+ attr_reader :current_target
11
+
12
+ #include CfScript::Callbacks
13
+ include CfScript::UI
14
+ include CfScript::UI::NameTag
15
+
16
+ def initialize(name_or_info, current_target)
17
+ @current_target = current_target
18
+
19
+ case name_or_info
20
+ when String, Symbol
21
+ @name = name_or_info
22
+ @app_info = nil
23
+ #@app_spec = spec_for(name)
24
+ when CfScript::AppInfo
25
+ @name = name_or_info.name
26
+ @app_info = name_or_info
27
+ #@app_spec = spec_for(name)
28
+ else
29
+ raise "App accepts a name (String or Symbol) or an AppInfo object, " +
30
+ "but a #{name_or_info.class.name} was given"
31
+ end
32
+
33
+ cf_self
34
+ end
35
+
36
+ def_delegators :@app_info, :requested_state, :instances, :urls,
37
+ :usage, :last_uploaded, :stack, :buildpack, :memory, :disk,
38
+ :state, :started?, :hot?, :stopped?, :cold?
39
+
40
+ def current_space
41
+ current_target.space
42
+ end
43
+
44
+ def tag_color(type = nil)
45
+ started? ? :active : :inactive
46
+ end
47
+
48
+ def name_tag
49
+ "#{current_target.space}:#{name}"
50
+ end
51
+
52
+ #def run_callbacks(stage, command)
53
+ # return unless app_spec
54
+
55
+ # app_spec.callbacks_for(stage, command).each do |callback|
56
+ # self.instance_eval(&callback)
57
+ # end
58
+ #end
59
+
60
+ #def callbacks_before(command)
61
+ # run_callbacks(:before, command)
62
+ #end
63
+
64
+ #def callbacks_after(command)
65
+ # run_callbacks(:after, command)
66
+ #end
67
+
68
+ #def callbacks_around(command, &block)
69
+ # run_callbacks(:before, command)
70
+ # self.instance_eval(&block)
71
+ # run_callbacks(:after, command)
72
+ #end
73
+
74
+ private
75
+
76
+ def cf_self
77
+ CfScript::Command.app name do |app_info|
78
+ @app_info = app_info if app_info
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ require 'cf_script/scope/app/state'
85
+ require 'cf_script/scope/app/env'
86
+ require 'cf_script/scope/app/routes'
87
+ require 'cf_script/scope/app/utils'
88
+
89
+ class CfScript::Scope::App
90
+ include State
91
+ include Env
92
+ include Routes
93
+ include Utils
94
+ end
@@ -0,0 +1,36 @@
1
+ require 'set'
2
+
3
+ module CfScript
4
+ class Scope::Proxy
5
+ BASIC_OBJECT_METHODS = BasicObject.instance_methods.to_set
6
+ PROXY_OBJECT_METHODS = BASIC_OBJECT_METHODS | Set[
7
+ :object_id,
8
+ :instance_variables,
9
+ :instance_variable_get,
10
+ :instance_variable_set
11
+ ]
12
+
13
+ PROXY_OBJECT_INSTANCE_VARIABLES = Set[:@__inner__, :@__outer__]
14
+
15
+ instance_methods.each do |m|
16
+ undef_method(m) unless PROXY_OBJECT_METHODS.include?(m)
17
+ end
18
+
19
+ def initialize(inner, outer)
20
+ @__inner__ = inner
21
+ @__outer__ = outer
22
+ end
23
+
24
+ def instance_variables
25
+ super.select { |v| !PROXY_OBJECT_INSTANCE_VARIABLES.include?(v.to_sym) }
26
+ end
27
+
28
+ def method_missing(m, *args, &block)
29
+ if @__inner__.respond_to?(m.to_sym)
30
+ @__inner__.__send__(m.to_sym, *args, &block)
31
+ else
32
+ @__outer__.__send__(m.to_sym, *args, &block)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,59 @@
1
+ module CfScript::Scope
2
+ class Script < Target
3
+ attr_reader :options
4
+
5
+ VALID_OPTIONS = [:api, :org, :username, :password, :space]
6
+
7
+ include CfScript::UI
8
+
9
+ def initialize(options = {})
10
+ super()
11
+
12
+ @options = options
13
+ end
14
+
15
+ def run(&block)
16
+ apply_options unless options.empty?
17
+
18
+ exec_in(self, nil, &block)
19
+ end
20
+
21
+ def check_options
22
+ options.keys.each do |key|
23
+ unless VALID_OPTIONS.include?(key)
24
+ alert :options, "#{key} is not a valid option for the " +
25
+ 'cf method. It will be ignored.'
26
+ end
27
+ end
28
+ end
29
+
30
+ def apply_options
31
+ check_options
32
+
33
+ # If api is given and there are no username or password, set the api
34
+ if options[:api] and (options[:username].nil? or options[:password].nil?)
35
+ unless endpoint = CfScript::Command.api(options[:api])
36
+ error :cf, "Could not set the API endpoint: #{options[:api]}"
37
+ raise "Could not set the API endpoint: #{options[:api]}"
38
+ end
39
+ end
40
+
41
+ # If username and password are given, call login. This also sets the api
42
+ # and the space if they are given.
43
+ if options[:username] and options[:password]
44
+ unless CfScript::Command.login(options[:username], options[:password], options)
45
+ error :cf, 'Could not login with given credentials'
46
+ raise 'Could not login with given credentials'
47
+ end
48
+ end
49
+
50
+ # If a space is given, don't set it if there is a username and password,
51
+ # login would have already set it.
52
+ if options[:space] and (options[:username].nil? or options[:password].nil?)
53
+ # Use target, not CfScript::Command.target, so we can keep track of
54
+ # the previous and current spaces and reset them on scope exit.
55
+ target options[:space]
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ module CfScript::Scope
2
+ class Space < Target
3
+ def initialize(space_name = nil)
4
+ super()
5
+
6
+ target(space_name) if space_name
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,90 @@
1
+ module CfScript::Scope
2
+ class Target < Base
3
+ def current_org
4
+ @current_target ? @current_target.org : ''
5
+ end
6
+
7
+ def current_space
8
+ @current_target ? @current_target.space : ''
9
+ end
10
+
11
+ def target(*args, &block)
12
+ unless args.empty?
13
+ # TODO: check if it's the same org too
14
+ if args.first.to_s == current_space
15
+ return @current_target
16
+ end
17
+ end
18
+
19
+ @current_target = CfScript::Command.run(:target, *args)
20
+
21
+ block_given? ? yield(@current_target) : @current_target
22
+ end
23
+
24
+ def space(space_name, &block)
25
+ space_scope = nil
26
+
27
+ if block_given?
28
+ space_scope = CfScript::Scope::Space.new(space_name)
29
+
30
+ exec_in(space_scope, nil, &block)
31
+ else
32
+ CfScript::Command.space(space_name)
33
+ end
34
+ ensure
35
+ space_scope.finalize if space_scope
36
+ end
37
+
38
+ def app(name_or_info, &block)
39
+ if block_given?
40
+ app = CfScript::Scope::App.new(name_or_info, @current_target)
41
+
42
+ exec_in(app, nil, &block)
43
+ else
44
+ app_name = name_or_info.respond_to?(:name) ? name_or_info.name : name_or_info
45
+
46
+ CfScript::Command.app(app_name)
47
+ end
48
+ end
49
+
50
+ def apps(options = {}, &block)
51
+ apps = CfScript::Command.apps
52
+
53
+ apps.select!(options) unless options.empty?
54
+
55
+ block_given? ? yield(apps) : apps
56
+ end
57
+
58
+ private
59
+
60
+ def target_desc(t)
61
+ desc = ''
62
+ desc << (t.org.nil? || t.org.empty? ? 'ORG-NOT-SET' : t.org)
63
+ desc << ':'
64
+ desc << (t.space.nil? || t.space.empty? ? 'SPACE-NOT-SET' : t.space)
65
+ end
66
+
67
+ def enter_scope
68
+ if current = target
69
+ @initial_target = @current_target = current
70
+ end
71
+
72
+ raise "Could not set initial and current targets" unless
73
+ @initial_target and @current_target
74
+
75
+ trace scope_name, "enter_scope: " +
76
+ "initial: (#{target_desc(@initial_target)}), " +
77
+ "current: (#{target_desc(@current_target)})"
78
+ end
79
+
80
+ def exit_scope
81
+ trace scope_name, "exit_scope: " +
82
+ "initial: (#{target_desc(@initial_target)}), " +
83
+ "current: (#{target_desc(@current_target)})"
84
+
85
+ if @initial_target.org != current_org or @initial_target.space != current_space
86
+ target(@initial_target)
87
+ end
88
+ end
89
+ end
90
+ end