bosh_lite_helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99ac7ab9cd4e4a9bea25e6c90726698387f1a22a
4
+ data.tar.gz: 69ac7a3bdc56160af17fab36c39af6c47c4080ef
5
+ SHA512:
6
+ metadata.gz: b2ab7e4c04a962807bf62e8e8aefd87d274bb13218e27bed76e65f3009d17af268a8bc3f8c0bc08f2cb64a0b4268c9692952018fca515f1a6451636632d666a1
7
+ data.tar.gz: b1bbd407e4918da59693e9346c4c78b56c23643ef9b343d06cc346396131911c020ff2e83f11c758ac7c6d29f8937e4021e1bd7af862610d8578e44aa4d1927b
@@ -0,0 +1,2 @@
1
+ require_relative 'bosh_lite_helpers/api'
2
+ require_relative 'bosh_lite_helpers/command_runner'
@@ -0,0 +1,126 @@
1
+ require 'pathname'
2
+ require 'tmpdir'
3
+ require_relative 'command_runner'
4
+
5
+ # BOSH Lite Helpers
6
+ module BoshLiteHelpers
7
+ class BoshLiteError < StandardError; end
8
+
9
+ # Include this module in your specs
10
+ module Api
11
+ attr_writer :command_runner
12
+
13
+ def bosh_lite
14
+ bosh 'target https://192.168.50.4:25555'
15
+ bosh 'login admin admin'
16
+ yield
17
+ end
18
+
19
+ def create_release
20
+ bosh 'create release --force'
21
+ end
22
+
23
+ def delete_deployment(name)
24
+ return unless bosh_object_exists?(:deployment, name)
25
+ bosh "delete deployment #{name}"
26
+ end
27
+
28
+ def delete_release(name)
29
+ return unless bosh_object_exists?(:release, name)
30
+ bosh "delete release #{name}"
31
+ end
32
+
33
+ def deploy(manifest_path)
34
+ bosh "deployment #{manifest_path}"
35
+ bosh 'deploy'
36
+ end
37
+
38
+ def upload_release
39
+ ignore_error 'Rebase is attempted without any job or package changes' do
40
+ bosh 'upload release --rebase'
41
+ end
42
+ end
43
+
44
+ def upload_stemcell(path)
45
+ stemcell = stemcell_from_path(path)
46
+ return if existing_stemcells.include?(stemcell)
47
+ bosh "upload stemcell #{path}"
48
+ end
49
+
50
+ def with_new_empty_directory
51
+ old_dir = Dir.pwd
52
+ begin
53
+ Dir.mktmpdir do |empty_dir|
54
+ Dir.chdir(empty_dir)
55
+ yield
56
+ end
57
+ ensure
58
+ Dir.chdir(old_dir)
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def bosh(command)
65
+ result = command_runner.run "bosh -n #{command}"
66
+ unless result[:exit_code] == 0
67
+ fail BoshLiteError, "#{result[:stderr]}\n#{result[:stdout]}"
68
+ end
69
+ result[:stdout]
70
+ end
71
+
72
+ def bosh_object_exists?(type, name)
73
+ ignore_error "No #{type}s" do
74
+ bosh("#{type}s").lines.any? { |line| line.include?(" #{name} ") }
75
+ end
76
+ end
77
+
78
+ def command_runner
79
+ @command_runner ||= CommandRunner.new
80
+ end
81
+
82
+ def existing_stemcells
83
+ ignore_error 'No stemcells' do
84
+ columns = first_two_columns(bosh('stemcells'))
85
+ stemcells = columns.map do |name, version|
86
+ [name.strip, version.strip.to_i]
87
+ end
88
+ stemcells.reject! { |name, _version| name == 'Name' }
89
+ return stemcell_columns_to_hash(stemcells)
90
+ end
91
+ []
92
+ end
93
+
94
+ def extract_path(url)
95
+ return Pathname.new(url.path) if url.respond_to?(:path)
96
+ Pathname.new(url)
97
+ end
98
+
99
+ def first_two_columns(output)
100
+ columns = output.lines.map do |line|
101
+ line.split('|')[1..2]
102
+ end
103
+ columns.reject { |s| s.empty? }
104
+ end
105
+
106
+ def ignore_error(error_message)
107
+ yield
108
+ rescue BoshLiteError => e
109
+ raise unless e.message.include?(error_message)
110
+ end
111
+
112
+ def stemcell_columns_to_hash(columns)
113
+ columns.map do |name, version|
114
+ { name: name, version: version }
115
+ end
116
+ end
117
+
118
+ def stemcell_from_path(path)
119
+ bn = extract_path(path).basename.to_s
120
+ {
121
+ name: bn.sub(/stemcell-[0-9]+-/, '').sub('.tgz', ''),
122
+ version: bn.match(/stemcell-([0-9]+)-/)[1].to_i
123
+ }
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,32 @@
1
+ require 'open3'
2
+
3
+ module BoshLiteHelpers
4
+ # Wraps running operating system commands
5
+ class CommandRunner
6
+ def run(command, options = {})
7
+ stdout_lines, stderr_lines, status = [], [], nil
8
+ puts command unless options[:quiet]
9
+ Open3.popen3(command) do |_stdin, stdout, stderr, wait_thr|
10
+ read_output(stdout_lines, stdout, options[:quiet])
11
+ read_output(stderr_lines, stderr, options[:quiet])
12
+ status = wait_thr.value
13
+ end
14
+ command_result(status.exitstatus, stdout_lines, stderr_lines)
15
+ end
16
+
17
+ def read_output(lines, stream, quiet)
18
+ line = stream.read
19
+ return if line.empty?
20
+ puts line unless quiet
21
+ lines << line
22
+ end
23
+
24
+ def command_result(exit_code, stdout_lines, stderr_lines)
25
+ {
26
+ exit_code: exit_code,
27
+ stdout: stdout_lines.join(''),
28
+ stderr: stderr_lines.join('')
29
+ }
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bosh_lite_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Crump
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bosh_cli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2624'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2624'
27
+ description: Basic wrapper around bosh-lite
28
+ email: andrew@cloudcredo.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/bosh_lite_helpers.rb
34
+ - lib/bosh_lite_helpers/api.rb
35
+ - lib/bosh_lite_helpers/command_runner.rb
36
+ homepage: https://github.com/cloudcredo/bosh_lite_helpers
37
+ licenses:
38
+ - Apache
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.2
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Basic wrapper around bosh-lite
60
+ test_files: []