gcloud_hosts 0.1.1 → 0.1.2
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 +4 -4
- data/lib/gcloud_hosts/options.rb +5 -1
- data/lib/gcloud_hosts/runner.rb +24 -14
- data/lib/gcloud_hosts/updater.rb +39 -2
- data/lib/gcloud_hosts/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67c9cf2078e975e924cf0c7da637214ef0959379
|
4
|
+
data.tar.gz: 22e9c351e240c14e1e0ec341bd2dee7f7b66020d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a97080304ac9a2d7332d9ed2e127478d291e645114b83a7d5b7b9144789d66c692e0c5691169969fe80482f4d5763f8566041f522e09bb6e840d9980c5c37099
|
7
|
+
data.tar.gz: 4597b5ba293d1e6b9dff6126f095a230af5a3373332c9cdd37ef026ddbc85374a697b84f7e556c827528fb6de981c1e30213eb0355280b79018f5423881a1b3f
|
data/lib/gcloud_hosts/options.rb
CHANGED
@@ -16,7 +16,8 @@ module GcloudHosts
|
|
16
16
|
file: '/etc/hosts',
|
17
17
|
backup: nil,
|
18
18
|
dry_run: false,
|
19
|
-
delete: false
|
19
|
+
delete: false,
|
20
|
+
clear: false
|
20
21
|
}
|
21
22
|
parser.parse!(args)
|
22
23
|
end
|
@@ -57,6 +58,9 @@ module GcloudHosts
|
|
57
58
|
opts.on('--[no-]delete', "Delete the project from hosts file. Defaults to false") do |opt|
|
58
59
|
@options[:delete] = opt
|
59
60
|
end
|
61
|
+
opts.on('--[no-]clear', "Clear all gcloud host entries from hosts file. Defaults to false") do |opt|
|
62
|
+
@options[:clear] = opt
|
63
|
+
end
|
60
64
|
opts.on_tail("--help", "Show this message") do
|
61
65
|
puts opts
|
62
66
|
exit
|
data/lib/gcloud_hosts/runner.rb
CHANGED
@@ -8,29 +8,39 @@ module GcloudHosts
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def run!
|
11
|
-
project = @options[:project]
|
12
|
-
if
|
13
|
-
project
|
11
|
+
project = @options[:project].to_s.strip
|
12
|
+
if @options[:clear]
|
13
|
+
if project != ""
|
14
|
+
raise ArgumentError.new("Cannot specify 'clear' and 'project' at the same time.")
|
15
|
+
end
|
14
16
|
end
|
15
|
-
if project
|
16
|
-
|
17
|
+
if project == ""
|
18
|
+
project = env["core"]["project"].to_s.strip
|
17
19
|
end
|
18
|
-
|
19
|
-
|
20
|
-
domain = @options[:domain].to_s.strip
|
21
|
-
else
|
22
|
-
domain = "c.#{project}.internal"
|
20
|
+
if project == ""
|
21
|
+
raise AuthError.new("No gcloud project specified.")
|
23
22
|
end
|
24
23
|
|
25
24
|
backup = @options[:backup] ||
|
26
25
|
@options[:file] + '.bak'
|
27
26
|
|
28
|
-
if @options[:
|
29
|
-
|
27
|
+
if @options[:clear]
|
28
|
+
Updater.clear(@options[:file], backup, @options[:dry_run])
|
30
29
|
else
|
31
|
-
|
30
|
+
if @options[:domain]
|
31
|
+
domain = @options[:domain].to_s.strip
|
32
|
+
else
|
33
|
+
domain = "c.#{project}.internal"
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
if @options[:delete]
|
38
|
+
new_hosts_list = []
|
39
|
+
else
|
40
|
+
new_hosts_list = Hosts.hosts(@options[:gcloud], project, @options[:network], domain, @options[:public], @options[:exclude_public])
|
41
|
+
end
|
42
|
+
Updater.update(new_hosts_list.join("\n"), project, @options[:file], backup, @options[:dry_run], @options[:delete])
|
32
43
|
end
|
33
|
-
Updater.update(new_hosts_list.join("\n"), project, @options[:file], backup, @options[:dry_run], @options[:delete])
|
34
44
|
end
|
35
45
|
|
36
46
|
private
|
data/lib/gcloud_hosts/updater.rb
CHANGED
@@ -11,8 +11,8 @@ module GcloudHosts
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.update(new_hosts, project, file, backup_file, dry_run, delete)
|
14
|
-
start_marker =
|
15
|
-
end_marker =
|
14
|
+
start_marker = get_start_marker(project)
|
15
|
+
end_marker = get_end_marker(project)
|
16
16
|
|
17
17
|
old_hosts = File.read(file)
|
18
18
|
|
@@ -98,6 +98,35 @@ module GcloudHosts
|
|
98
98
|
new_content
|
99
99
|
end
|
100
100
|
|
101
|
+
def self.clear(file, backup_file, dry_run)
|
102
|
+
old_hosts = File.read(file)
|
103
|
+
new_content = old_hosts.dup
|
104
|
+
|
105
|
+
markers = old_hosts.each_line.map do |line|
|
106
|
+
regex = "\# (START|END) GCLOUD HOSTS - (.+) \#"
|
107
|
+
if m = line.match(/^#{regex}$/)
|
108
|
+
m[2]
|
109
|
+
end
|
110
|
+
end.compact.uniq
|
111
|
+
|
112
|
+
markers.each do |project|
|
113
|
+
start_marker = get_start_marker(project)
|
114
|
+
end_marker = get_end_marker(project)
|
115
|
+
|
116
|
+
new_content = delete_project_hosts(new_content, start_marker, end_marker)
|
117
|
+
end
|
118
|
+
new_content.gsub!(/\s+$/, "\n")
|
119
|
+
|
120
|
+
if dry_run
|
121
|
+
puts new_content
|
122
|
+
elsif new_content != old_hosts
|
123
|
+
# backup old host file
|
124
|
+
File.open(backup_file, 'w') { |f| f << old_hosts }
|
125
|
+
# write new content
|
126
|
+
File.open(file, 'w') { |f| f << new_content }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
101
130
|
def self.delete_project_hosts(hosts, start_marker, end_marker)
|
102
131
|
new_content = ''
|
103
132
|
marker_state = Marker::BEFORE
|
@@ -128,5 +157,13 @@ module GcloudHosts
|
|
128
157
|
new_content
|
129
158
|
end
|
130
159
|
|
160
|
+
def self.get_start_marker(project)
|
161
|
+
"# START GCLOUD HOSTS - #{project} #"
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.get_end_marker(project)
|
165
|
+
"# END GCLOUD HOSTS - #{project} #"
|
166
|
+
end
|
167
|
+
|
131
168
|
end
|
132
169
|
end
|
data/lib/gcloud_hosts/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gcloud_hosts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Tongen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|