cf 4.0.1.rc2 → 4.1.0rc1

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.
@@ -0,0 +1,79 @@
1
+ require "micro/errors"
2
+
3
+ module CFMicro::Switcher
4
+ class Base
5
+
6
+ def initialize(config)
7
+ @config = config
8
+
9
+ @vmrun = CFMicro::VMrun.new(config)
10
+ end
11
+
12
+ #wrapper methods
13
+ def vmx
14
+ @vmrun.vmx
15
+ end
16
+
17
+ def domain
18
+ @vmrun.domain
19
+ end
20
+
21
+ def ip
22
+ @vmrun.ip
23
+ end
24
+
25
+ def running?
26
+ @vmrun.running?
27
+ end
28
+
29
+ def start!
30
+ @vmrun.start!
31
+ end
32
+
33
+ def ready?
34
+ @vmrun.ready?
35
+ end
36
+
37
+ def offline?
38
+ @vmrun.offline?
39
+ end
40
+
41
+ def nat?
42
+ @config['online_connection_type'] ||= @vmrun.connection_type
43
+ @config["online_connection_type"] == "nat"
44
+ end
45
+
46
+ def reset_to_nat!
47
+ @vmrun.connection_type = 'nat'
48
+ @vmrun.reset
49
+ end
50
+
51
+ def set_host_dns!
52
+ @config['domain'] ||= @vmrun.domain
53
+ @config['ip'] ||= @vmrun.ip
54
+ set_nameserver(@config['domain'], @config['ip'])
55
+ end
56
+
57
+ def unset_host_dns!
58
+ @config['domain'] ||= @vmrun.domain
59
+ @config['ip'] ||= @vmrun.ip
60
+ unset_nameserver(@config['domain'], @config['ip'])
61
+ end
62
+
63
+ def offline!
64
+ if @vmrun.offline?
65
+ raise CFMicro::MCFError, "Micro Cloud Foundry VM already in offline mode"
66
+ else
67
+ @vmrun.offline!
68
+ end
69
+ end
70
+
71
+ def online!
72
+ if @vmrun.offline?
73
+ @vmrun.online!
74
+ else
75
+ raise CFMirco::MCFError, "Micro Cloud Foundry already in online mode"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,21 @@
1
+ require "micro/errors"
2
+
3
+ module CFMicro::Switcher
4
+
5
+ class Darwin < Base
6
+ def adminrun(command)
7
+ CFMicro.run_command("osascript", "-e 'do shell script \"#{command}\" with administrator privileges'")
8
+ end
9
+
10
+ def set_nameserver(domain, ip)
11
+ File.open("/tmp/#{domain}", 'w') { |file| file.write("nameserver #{ip}") }
12
+ adminrun("mkdir -p /etc/resolver;mv /tmp/#{domain} /etc/resolver/")
13
+ end
14
+
15
+ def unset_nameserver(domain, ip)
16
+ raise CFMicro::MCFError, "domain missing" unless domain
17
+ adminrun("rm -f /etc/resolver/#{domain}")
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,15 @@
1
+ # only used for testing
2
+ module CFMicro::Switcher
3
+
4
+ class Dummy < Base
5
+ def adminrun(command)
6
+ end
7
+
8
+ def set_nameserver(domain, ip)
9
+ end
10
+
11
+ def unset_nameserver(domain, ip)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,16 @@
1
+ module CFMicro::Switcher
2
+
3
+ class Linux < Base
4
+ def set_nameserver(domain, ip)
5
+ CFMicro.run_command("sudo", "sed -i'.backup' '1 i nameserver #{ip}' /etc/resolv.conf")
6
+ # lock resolv.conf so Network Manager doesn't clear out the file when offline
7
+ CFMicro.run_command("sudo", "chattr +i /etc/resolv.conf")
8
+ end
9
+
10
+ def unset_nameserver(domain, ip)
11
+ CFMicro.run_command("sudo", "chattr -i /etc/resolv.conf")
12
+ CFMicro.run_command("sudo", "sed -i'.backup' '/#{ip}/d' /etc/resolv.conf")
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,31 @@
1
+ module CFMicro::Switcher
2
+
3
+ class Windows < Base
4
+ def version
5
+ CFMicro.run_command("cmd", "/c ver").to_s.scan(/\d+\.\d+/).first.to_f
6
+ end
7
+
8
+ def adminrun(command, args=nil)
9
+ if version > 5.2
10
+ require 'win32ole'
11
+ shell = WIN32OLE.new("Shell.Application")
12
+ shell.ShellExecute(command, args, nil, "runas", 0)
13
+ else
14
+ # on older version this will try to run the command, and if you don't have
15
+ # admin privilges it will tell you so and exit
16
+ CFMicro.run_command(command, args)
17
+ end
18
+ end
19
+
20
+ # TODO better method to figure out the interface name is to get the NAT ip and find the
21
+ # interface with the correct subnet
22
+ def set_nameserver(domain, ip)
23
+ adminrun("netsh", "interface ip set dns \"VMware Network Adapter VMnet8\" static #{ip}")
24
+ end
25
+
26
+ def unset_nameserver(domain, ip)
27
+ adminrun("netsh", "interface ip set dns \"VMware Network Adapter VMnet8\" static none")
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,175 @@
1
+ require "micro/errors"
2
+
3
+ module CFMicro
4
+ class VMrun
5
+ attr_reader :vmx, :vmrun
6
+
7
+ def initialize(config)
8
+ @platform = config[:platform]
9
+ @user = 'root' # must use root as we muck around with system settings
10
+ @password = config[:password]
11
+ @vmrun = config[:vmrun]
12
+ @vmx = config[:vmx]
13
+
14
+ # TODO honor TMPDIR
15
+ if @platform == :windows
16
+ @temp_dir = ENV['temp']
17
+ else
18
+ @temp_dir = '/tmp'
19
+ end
20
+ end
21
+
22
+ def vmx
23
+ @vmx
24
+ end
25
+
26
+ def connection_type
27
+ read_variable('ethernet0.connectionType')
28
+ end
29
+
30
+ def connection_type=(type)
31
+ write_variable("ethernet0.connectionType", type)
32
+ end
33
+
34
+ def nat?
35
+ connection_type == "nat"
36
+ end
37
+
38
+ def bridged?
39
+ connection_type == "bridged"
40
+ end
41
+
42
+ def domain
43
+ # switch to Dir.mktmpdir
44
+ state_config = CFMicro.escape_path(File.join(@temp_dir, 'state.yml'))
45
+ run('CopyFileFromGuestToHost', "/var/vcap/bosh/state.yml #{state_config}")
46
+ bosh_config = YAML.load_file(state_config)
47
+ bosh_config['properties']['domain']
48
+ end
49
+
50
+ def ip
51
+ # switch to Dir.mktmpdir
52
+ path = CFMicro.escape_path(CFMicro.config_file('refresh_ip.rb'))
53
+ ip_file = CFMicro.escape_path(File.join(@temp_dir, 'ip.txt'))
54
+ run('CopyFileFromHostToGuest', "#{path} /tmp/refresh_ip.rb")
55
+ run('runProgramInGuest', '/tmp/refresh_ip.rb')
56
+ run('CopyFileFromGuestToHost', "/tmp/ip.txt #{ip_file}")
57
+ File.open(ip_file, 'r') { |file| file.read }
58
+ end
59
+
60
+ def list
61
+ vms = run("list")
62
+ vms.delete_if { |line| line =~ /^Total/ }
63
+ vms.map { |line| CFMicro.escape_path(File.expand_path(line)) }
64
+ end
65
+
66
+ def offline?
67
+ command = "-gu #{@user} -gp #{@password} runProgramInGuest"
68
+ args = '/usr/bin/test -e /var/vcap/micro/offline'
69
+ # why not use run_command?
70
+ result = %x{#{@vmrun} #{command} #{@vmx} #{args}}
71
+
72
+ if result.include?('Guest program exited with non-zero exit code: 1')
73
+ return false
74
+ elsif $?.exitstatus == 0
75
+ return true
76
+ else
77
+ raise CFMicro::MCFError, "failed to execute vmrun:\n#{result}"
78
+ end
79
+ end
80
+
81
+ def offline!
82
+ path = CFMicro.escape_path(CFMicro.config_file('offline.conf'))
83
+ run('CopyFileFromHostToGuest', "#{path} /etc/dnsmasq.d/offline.conf")
84
+ run('runProgramInGuest', '/usr/bin/touch /var/vcap/micro/offline')
85
+ run('runProgramInGuest',
86
+ "/bin/sed -i -e 's/^[^#]/# &/g' /etc/dnsmasq.d/server || true")
87
+ restart_dnsmasq
88
+ end
89
+
90
+ def online!
91
+ run('runProgramInGuest', '/bin/rm -f /etc/dnsmasq.d/offline.conf')
92
+ run('runProgramInGuest', '/bin/rm -f /var/vcap/micro/offline')
93
+ run('runProgramInGuest',
94
+ "/bin/sed -i -e 's/^# //g' /etc/dnsmasq.d/server || true")
95
+ restart_dnsmasq
96
+ end
97
+
98
+ # check to see if the micro cloud has been configured
99
+ # uses default password to check
100
+ def ready?
101
+ command = "-gu root -gp 'ca$hc0w' runProgramInGuest"
102
+ args = '/usr/bin/test -e /var/vcap/micro/micro.json'
103
+ result = %x{#{@vmrun} #{command} #{@vmx} #{args}}
104
+
105
+ if result.include?('Invalid user name or password for the guest OS') || $?.exitstatus == 0
106
+ return true
107
+ elsif $?.exitstatus == 1
108
+ return false
109
+ else
110
+ raise CFMicro::MCFError, "failed to execute vmrun:\n#{result}"
111
+ end
112
+ end
113
+
114
+ def read_variable(var)
115
+ # TODO deal with non-ok return
116
+ run("readVariable", "runtimeConfig #{var}").first
117
+ end
118
+
119
+ def write_variable(var, value)
120
+ run('writeVariable', "runtimeConfig #{var} #{value}")
121
+ end
122
+
123
+ def reset
124
+ run('reset', 'soft')
125
+ end
126
+
127
+ def restart_dnsmasq
128
+ # restart command doesn't always work, start and stop seems to be more reliable
129
+ run('runProgramInGuest', '/etc/init.d/dnsmasq stop')
130
+ run('runProgramInGuest', '/etc/init.d/dnsmasq start')
131
+ end
132
+
133
+ def run(command, args=nil)
134
+ if command.include?('Guest')
135
+ command = "-gu #{@user} -gp #{@password} #{command}"
136
+ end
137
+ CFMicro.run_command(@vmrun, "#{command} #{@vmx} #{args}")
138
+ end
139
+
140
+ def running?
141
+ vms = list
142
+ if @platform == :windows
143
+ vms.any? { |x|
144
+ x.downcase == @vmx.downcase
145
+ }
146
+ else
147
+ # Handle vmx being in a symlinked dir.
148
+ real_path = nil
149
+ begin
150
+ real_path = File.realpath(@vmx)
151
+ rescue
152
+ end
153
+ vms.include?(@vmx) || (real_path && vms.include?(real_path))
154
+ end
155
+ end
156
+
157
+ def start!
158
+ run('start') unless running?
159
+ end
160
+
161
+ def stop!
162
+ run('stop') if running?
163
+ end
164
+
165
+ def self.locate(platform)
166
+ paths = YAML.load_file(CFMicro.config_file('paths.yml'))
167
+ vmrun_paths = paths[platform.to_s]['vmrun']
168
+ vmrun_exe = @platform == :windows ? 'vmrun.exe' : 'vmrun'
169
+ vmrun = CFMicro.locate_file(vmrun_exe, "VMware", vmrun_paths)
170
+ err "Unable to locate vmrun, please supply --vmrun option" unless vmrun
171
+ vmrun
172
+ end
173
+ end
174
+
175
+ end
@@ -0,0 +1,34 @@
1
+ namespace :gem do
2
+ desc "Bump gem version, push to RubyGems, push to Github, add release notes"
3
+ task :release, [:version] do |_, args|
4
+ version = args[:version] || 'rc'
5
+ old_version = gem_version
6
+
7
+ sh! "gem bump --version #{version} --no-commit"
8
+ generate_release_notes(old_version)
9
+ sh!("git commit -m 'Bumping to version #{gem_version}.'")
10
+ #sh!("git push")
11
+ #sh!("gem release --tag")
12
+ end
13
+
14
+ private
15
+ def generate_release_notes(old_version)
16
+ print_with_purpose "Generating release notes..."
17
+ file_name = "release_#{gem_version}"
18
+ sh!("anchorman notes --name=#{file_name} --from=v#{old_version}")
19
+ sh!("git add release_notes")
20
+ end
21
+
22
+ def sh!(cmd)
23
+ `#{cmd}`
24
+ raise "borked with #{$?}" unless $?.success?
25
+ end
26
+
27
+ def print_with_purpose(text)
28
+ puts "\033[34m#{text}\033[0m"
29
+ end
30
+
31
+ def gem_version
32
+ Gem::Specification.load("cf.gemspec").version.to_s
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ module CF
4
+ module App
5
+ describe "Help" do
6
+ let(:global) { {} }
7
+ let(:given) { {} }
8
+
9
+ subject do
10
+ capture_output { Mothership.new.invoke(:help, :command => "app") }
11
+ end
12
+
13
+ it "describes the command" do
14
+ subject
15
+ stdout.rewind
16
+ expect(stdout.readlines.first).to match /Show app information/
17
+ end
18
+
19
+ it "prints the options" do
20
+ subject
21
+ stdout.rewind
22
+ expect(stdout.readlines.any? {|line| line =~ /Options:/ }).to be_true
23
+
24
+ end
25
+
26
+ context "when the user is not logged in" do
27
+ before do
28
+ capture_output { Mothership.new.invoke(:logout) }
29
+ end
30
+
31
+ it "does not require login" do
32
+ subject
33
+ stdout.rewind
34
+ expect(stdout.readlines.first).to_not match /Please log in first to proceed/
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ module CF
4
+ module Domain
5
+ describe "Help" do
6
+ let(:global) { {} }
7
+ let(:given) { {} }
8
+
9
+ subject do
10
+ capture_output { Mothership.new.invoke(:help, :command => "domains") }
11
+ end
12
+
13
+ it "describes the command" do
14
+ subject
15
+ stdout.rewind
16
+ expect(stdout.readlines.first).to match /List domains in a space/
17
+ end
18
+
19
+ it "prints the options" do
20
+ subject
21
+ stdout.rewind
22
+ expect(stdout.readlines.any? {|line| line =~ /Options:/ }).to be_true
23
+
24
+ end
25
+
26
+ context "when the user is not logged in" do
27
+ before do
28
+ capture_output { Mothership.new.invoke(:logout) }
29
+ end
30
+
31
+ it "does not require login" do
32
+ subject
33
+ stdout.rewind
34
+ expect(stdout.readlines.first).to_not match /Please log in first to proceed/
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end