vagrant-routes 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d085fb1436e7b170e8da6ab59716facf15b12d5b
4
+ data.tar.gz: 9daafa3112e91db23577fcaf754399379c53643b
5
+ SHA512:
6
+ metadata.gz: e6b0ec5c29ae2e82b5a84774136275523f0cee57b1a8f35061d85f9686335b414d566478069584073985ebd655751058ddd7d9ac21cde4989066bf5e03a61bd6
7
+ data.tar.gz: b43c7b6986486b2f30fc7850b9237a1713824169a74e10d6f73f516fc8a9e49932eeb90251e2fd7d68adfc2175b1656600709e65b0e47c67029e032f96f62bd6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git'
5
+ end
6
+
7
+ group :plugins do
8
+ gem 'vagrant-routes', path: '.'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2015 Josef Strzibny
2
+
3
+ Partially based on the code from the vagrant-hostmanager plugin.
4
+
5
+ Copyright (c) 2013 Shawn Dahlen
6
+
7
+ MIT License
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining
10
+ a copy of this software and associated documentation files (the
11
+ "Software"), to deal in the Software without restriction, including
12
+ without limitation the rights to use, copy, modify, merge, publish,
13
+ distribute, sublicense, and/or sell copies of the Software, and to
14
+ permit persons to whom the Software is furnished to do so, subject to
15
+ the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be
18
+ included in all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # vagrant-routes
2
+
3
+ **This is a proof-of-concept.**
4
+
5
+ Access OpenShift routes from the host.
6
+
7
+ This plugin updates the hosts file on your host with hostname entries of your
8
+ OpenShift applications. Windows support implementation is taken from the
9
+ `vagrant-hostmanager` plugin, thanks guys!
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ $ vagrant plugin install vagrant-routes
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```
20
+ # Start Vagrant VM with OpenShift, log in and select your project
21
+ # Update `hosts` file on the host by running:
22
+ $ vagrant route
23
+ Updating hosts file with new hostnames:
24
+ pyapp-python.router.default.svc.cluster.local
25
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/lib/command.rb ADDED
@@ -0,0 +1,129 @@
1
+ module VagrantPlugins
2
+ module Routes
3
+ class Command < Vagrant.plugin(2, :command)
4
+ def self.synopsis
5
+ 'Access OpenShift routes from the host'
6
+ end
7
+
8
+ def execute
9
+ with_target_vms(nil, single_target: true) do |machine|
10
+ machine.communicate.execute('oc get routes', sudo: false) do |type, data|
11
+ @result = data if type == :stdout
12
+ end
13
+ @env.ui.info("Updating hosts file with new hostnames:\n#{routes_hostnames(@result).join(', ')}")
14
+ ip = machine.ssh_info[:host]
15
+ update_hosts(routes_hostnames(@result), ip)
16
+ end
17
+ rescue
18
+ case @result
19
+ # We are not signed-in
20
+ when /.*the server has asked for the client to provide credentials.*/
21
+ @env.ui.error('You need to sign in and select a project first.')
22
+ # OpenShift is not installed on the guest
23
+ when /.*oc: command not found.*/
24
+ @env.ui.error('oc command not found on guest. Is OpenShift installed?')
25
+ # Command failed
26
+ when /.*Error.*/
27
+ @env.ui.error("oc command returned an error:\n")
28
+ @env.ui.error(result)
29
+ else
30
+ @env.ui.error('Unexpected error occured.')
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ # Add +hostnames+ entries associated with the +ip+ address
37
+ # to the given +file+
38
+ def update_hosts_file(file, hostnames, ip)
39
+ header = "## vagrant-routes-start\n"
40
+ footer = "## vagrant-routes-end\n"
41
+ body = String.new
42
+ hostnames.each { |h| body << "#{ip}\t#{h}\n" }
43
+ block = "\n\n" + header + body + footer + "\n"
44
+ content = file.read
45
+ header_pattern = Regexp.quote(header)
46
+ footer_pattern = Regexp.quote(footer)
47
+ pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
48
+ content = content.match(pattern) ? content.sub(pattern, block) : content.rstrip + blockt
49
+ file.open('wb') { |io| io.write(content) }
50
+ end
51
+
52
+ def update_hosts(hostnames, ip)
53
+ # Copy and modify hosts file on host with Vagrant-managed entries
54
+ file = @env.tmp_path.join('hosts.local')
55
+ if WindowsSupport.windows?
56
+ # Lazily include windows Module
57
+ class << self
58
+ include WindowsSupport unless include? WindowsSupport
59
+ end
60
+ copy_proc = Proc.new { windows_copy_file(file, hosts_file_location) }
61
+ else
62
+ copy_proc = Proc.new { `sudo cp #{file} #{hosts_file_location}` }
63
+ end
64
+ FileUtils.cp(hosts_file_location, file)
65
+ copy_proc.call if update_hosts_file(file, hostnames, ip)
66
+ end
67
+
68
+ # Example output of `oc get routes` is as follows:
69
+ #
70
+ # $ oc get routes
71
+ # NAME HOST/PORT PATH SERVICE LABELS INSECURE POLICY TLS TERMINATION
72
+ # ruby20 ruby20-ruby-app.router.default.svc.cluster.local ruby20 app=ruby20
73
+ # pyapp pyapp-python.router.default.svc.cluster.local pyapp app=pyapp
74
+ def routes_hostnames(output)
75
+ lines = output.split("\n")[1..-1]
76
+ hostnames = []
77
+ lines.each do |line|
78
+ hostnames << line.split[1]
79
+ end
80
+ hostnames
81
+ end
82
+
83
+ # Return appropriate location of +hosts+ file
84
+ def hosts_file_location
85
+ if WindowsSupport.windows?
86
+ "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
87
+ else
88
+ '/etc/hosts'
89
+ end
90
+ end
91
+
92
+ # Windows support is taken from vagrant-hostmanager
93
+ module WindowsSupport
94
+ require 'rbconfig'
95
+
96
+ def self.windows?
97
+ RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
98
+ end
99
+
100
+ require 'win32ole' if windows?
101
+
102
+ def windows_copy_file(source, dest)
103
+ FileUtils.cp(source, dest)
104
+ # Access denied, try with elevated privileges
105
+ rescue Errno::EACCES
106
+ windows_copy_file_elevated(source, dest)
107
+ end
108
+
109
+ private
110
+
111
+ def windows_copy_file_elevated(source, dest)
112
+ # copy command only supports backslashes as separators
113
+ source, dest = [source, dest].map { |s| s.to_s.gsub(/\//, '\\') }
114
+
115
+ # run 'cmd /C copy ...' with elevated privilege, minimized
116
+ copy_cmd = "copy \"#{source}\" \"#{dest}\""
117
+ WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{copy_cmd}", nil, 'runas', 7)
118
+
119
+ # Unfortunately, ShellExecute does not give us a status code and it
120
+ # is non-blocking so we can't reliably compare the file contents
121
+ # to see if they were copied.
122
+ #
123
+ # If the user rejects the UAC prompt, vagrant will silently continue
124
+ # without updating the hostsfile.
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,13 @@
1
+ module VagrantPlugins
2
+ module Routes
3
+ # Update hosts file on the host with OpenShift routes hostnames
4
+ class Plugin < Vagrant.plugin(2)
5
+ name 'route'
6
+
7
+ command('route', primary: false) do
8
+ require_relative 'command'
9
+ Command
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'vagrant-routes'
3
+ spec.version = '0.0.1'
4
+ spec.homepage = 'https://github.com/strzibny/vagrant-routes'
5
+ spec.summary = 'Access OpenShift routes on the host'
6
+
7
+ spec.authors = ['Josef Strzibny']
8
+ spec.email = ['strzibny@strzibny.name']
9
+
10
+ spec.files = `git ls-files -z`.split("\x0")
11
+ spec.require_paths = ['lib']
12
+
13
+ spec.add_development_dependency 'bundler', '~> 1.6'
14
+ spec.add_development_dependency 'rake'
15
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josef Strzibny
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - strzibny@strzibny.name
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/command.rb
53
+ - lib/vagrant-routes.rb
54
+ - vagrant-routes.gemspec
55
+ homepage: https://github.com/strzibny/vagrant-routes
56
+ licenses: []
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.4.8
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Access OpenShift routes on the host
78
+ test_files: []
79
+ has_rdoc: