collins-cli 0.2.10 → 0.2.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 209c04a6091f4804bef7e1a5cbf1ca726579b1c4
4
- data.tar.gz: c40f7f6f63a8ea3115d3418396daf0d254d5cdb2
3
+ metadata.gz: 56d5918ebb4b33429b6e2c77fe3d595b4acc7f99
4
+ data.tar.gz: 10596f7ab67031db7aef1c00bd837ded1880bd3a
5
5
  SHA512:
6
- metadata.gz: 182660aa823a21025f765a3557b09f99956861ef694ea61b34f16a2ea444d5de0114a3272749792b38e5e1c43942a4de2a6ed7a9c1c380bffc041b9346bf34ff
7
- data.tar.gz: 973bfc0938c23efb85f88013ccf488c913a9271f32a8aa8ba58cccf02bb510273994cc92c16c22144389229538535bdd641ca37cabde54577aa5de0a55c28f82
6
+ metadata.gz: d0a907787655318e1e5c1258eb6c3a8781b0eea602f86dd16589d58116c4afaf8be0728d6e912398a68db76b0d684f8c13c278f503f91f222b9ebc36086d05b6
7
+ data.tar.gz: 62fa500c7bb7379741f6e099f052418e20b0b8564c30c710598d5d2ed669c15c44261071c99251b31344ae1fd88227a8890ab11949fb4d11460388a98f436321
data/README.md CHANGED
@@ -229,21 +229,51 @@ Allocate and delete addresses, and show what address pools are configured in Col
229
229
 
230
230
  List statuses and states. TODO: implement state creation, deleting, modification
231
231
 
232
- Usage: collins state [options]
233
- -l, --list List states.
234
- -h, --help Help
232
+ Usage: collins state [options]
233
+ -l, --list List states.
234
+ -h, --help Help
235
+
236
+ Table formatting:
237
+ -H, --show-header Show header fields in output
238
+ -f, --field-separator SEPARATOR Separator between columns in output (Default: )
239
+
240
+ Extra options:
241
+ --timeout SECONDS Timeout in seconds (0 == forever)
242
+ -C, --config CONFIG Use specific Collins config yaml for Collins::Client
243
+
244
+ Examples:
245
+ Show states and statuses:
246
+ collins state --list
235
247
 
236
- Table formatting:
237
- -H, --show-header Show header fields in output
238
- -f, --field-separator SEPARATOR Separator between columns in output (Default: )
248
+ ## Datacenter configurations - collins dc
239
249
 
240
- Extra options:
241
- --timeout SECONDS Timeout in seconds (0 == forever)
242
- -C, --config CONFIG Use specific Collins config yaml for Collins::Client
250
+ Create and manage multiple Collins configurations. This utility allows you to keep multiple collins configuration files in your home directory, one for each collins site/datacenter/instance in your organization. This utility lets you easily set up new collins configurations, switch between them, and list all your configured datacenters.
243
251
 
244
- Examples:
245
- Show states and statuses:
246
- collins state --list
252
+ Usage: collins dc [options]
253
+
254
+ New Configuration:
255
+ -n, --new DATACENTER Create a new configuration file for DATACENTER at ~/.collins.yml.DATACENTER
256
+ -H, --host URI Use URI for host when setting up new datacenter
257
+ -u, --username USER Use USER for username when setting up new datacenter
258
+ -p, --password PASSWORD Use PASSWORD for password when setting up new datacenter
259
+
260
+ List:
261
+ -l, --list List configured collins instances
262
+
263
+ General:
264
+ -h, --help Help
265
+
266
+ Examples:
267
+ Show current Collins instance in use
268
+ collins dc
269
+ Set current Collins instance to jfk01
270
+ collins dc jfk01
271
+ List all Collins instances configured
272
+ collins dc -l
273
+ Set up new Collins instance for sfo01
274
+ collins dc --new sfo01 --host https://collins.sfo01.company.net
275
+ Iterate over all instances and find assets
276
+ for dc in $(collins dc -l) ; do collins dc $dc ; collins find -p DEVELOPMENT ; done
247
277
 
248
278
  ## TODO
249
279
 
data/bin/collins CHANGED
@@ -2,17 +2,19 @@
2
2
  require 'collins-cli'
3
3
 
4
4
  ALLOWED_ACTIONS = {
5
- Collins::CLI::Find => ['query','find'],
6
- Collins::CLI::Modify => ['modify','set'],
7
- Collins::CLI::Log => ['log'],
5
+ Collins::CLI::Find => ['query','find'],
6
+ Collins::CLI::Modify => ['modify','set'],
7
+ Collins::CLI::Log => ['log'],
8
8
  Collins::CLI::Provision => ['provision'],
9
- Collins::CLI::Power => ['power'],
10
- Collins::CLI::IPAM => ['ipam','address','ipaddress'],
11
- Collins::CLI::State => ['state','status'],
9
+ Collins::CLI::Power => ['power'],
10
+ Collins::CLI::IPAM => ['ipam','address','ipaddress'],
11
+ Collins::CLI::State => ['state','status'],
12
+ Collins::CLI::DC => ['dc','datacenter'],
12
13
  }
13
14
 
14
15
  HELP_MESSAGE = "Usage: #{File.basename(File.realpath($0))} <command> [options]
15
16
  Available commands:
17
+ dc, datacenter: Manage multiple Collins configurations
16
18
  query, find: Search for assets in Collins
17
19
  modify, set: Add and remove attributes, change statuses, and log to assets
18
20
  log: Display log messages on assets
data/lib/collins-cli.rb CHANGED
@@ -6,7 +6,7 @@ require 'json'
6
6
  require 'optparse'
7
7
  require 'colorize'
8
8
 
9
- ['mixins','formatter','log','modify','find','power','provision','ipam','state'].
9
+ ['mixins','formatter','log','modify','find','power','provision','ipam','state','dc'].
10
10
  each {|r| require File.join('collins/cli',r) }
11
11
 
12
12
  module Collins ; module CLI ; end ; end
@@ -0,0 +1,197 @@
1
+ require 'collins-cli'
2
+ require 'fileutils'
3
+ require 'optparse/uri'
4
+ require 'etc'
5
+ require 'io/console'
6
+
7
+ module Collins::CLI
8
+ class DC
9
+ include Mixins
10
+ include Formatter
11
+ PROG_NAME = 'collins dc'
12
+ DEFAULT_CONFIG_PATH = '~/.collins.yml'
13
+
14
+ DEFAULT_OPTIONS = {
15
+ :show_current => true, # used for :list
16
+ :mode => :show,
17
+ :dc => nil, # used for :set
18
+ :timeout => 120, # used for :new
19
+ :host => nil, # used for :new
20
+ :pw => nil, # used for :new
21
+ :user => nil, # used for :new
22
+ }
23
+
24
+ attr_reader :options, :parser
25
+
26
+ def initialize
27
+ @options = DEFAULT_OPTIONS.clone
28
+ @parsed, @validated = false, false
29
+ @parser = nil
30
+ end
31
+
32
+ def parse!(argv = ARGV)
33
+ @parser = OptionParser.new do |opts|
34
+ opts.banner = "Usage: #{PROG_NAME} [options]"
35
+ opts.separator ""
36
+ opts.separator "New Configuration:"
37
+ opts.on('-n', '--new DATACENTER', String, "Create a new configuration file for DATACENTER at #{DEFAULT_CONFIG_PATH}.DATACENTER") {|v| @options[:dc] = v ; @options[:mode] = :new}
38
+ opts.on('-H', '--host URI', URI, 'Use URI for host when setting up new datacenter') {|v| @options[:host] = v}
39
+ opts.on('-u', '--username USER', String, 'Use USER for username when setting up new datacenter') {|v| @options[:user] = v}
40
+ opts.on('-p', '--password PASSWORD', String, 'Use PASSWORD for password when setting up new datacenter') {|v| @options[:pw] = v}
41
+
42
+ opts.separator ""
43
+ opts.separator "List:"
44
+ opts.on('-l', '--list', 'List configured collins instances') {|v| @options[:mode] = :list}
45
+
46
+ opts.separator ""
47
+ opts.separator "General:"
48
+ opts.on('-h','--help',"Help") {@options[:mode] = :help}
49
+
50
+ opts.separator ""
51
+ opts.separator "Examples:"
52
+ opts.separator " Show current Collins instance in use"
53
+ opts.separator " #{PROG_NAME}"
54
+ opts.separator " Set current Collins instance to jfk01"
55
+ opts.separator " #{PROG_NAME} jfk01"
56
+ opts.separator " List all Collins instances configured"
57
+ opts.separator " #{PROG_NAME} -l"
58
+ opts.separator " Set up new Collins instance for sfo01"
59
+ opts.separator " #{PROG_NAME} --new sfo01 --host https://collins.sfo01.company.net"
60
+ opts.separator " Iterate over all instances and find assets"
61
+ opts.separator " for dc in $(#{PROG_NAME} -l) ; do #{PROG_NAME} $dc ; collins find -p DEVELOPMENT ; done"
62
+ end
63
+ @parser.parse!(argv)
64
+ #TODO(gabe): if user not specified, read from env or something
65
+
66
+ # if mode is :show (default), and an argument was provided in argv, set mode to :set
67
+ if @options[:mode] == :show && argv.length > 0
68
+ @options[:mode] = :set
69
+ @options[:dc] = argv.first
70
+ end
71
+
72
+ @parsed = true
73
+ self
74
+ end
75
+
76
+ def validate!
77
+ raise "You need to tell me to do something!" if @options[:mode].nil?
78
+ raise "No asset tags found via ARGF" if [:allocate,:delete].include?(options[:mode]) && (options[:tags].nil? or options[:tags].empty?)
79
+ @validated = true
80
+ self
81
+ end
82
+
83
+ def run!
84
+ raise "Options not yet parsed with #parse!" unless @parsed
85
+ raise "Options not yet validated with #validate!" unless @validated
86
+ success = true
87
+ case options[:mode]
88
+ when :help
89
+ puts parser
90
+ when :show
91
+ puts default_datacenter
92
+ when :set
93
+ dc = @options[:dc]
94
+ if !configured_datacenters.include?(dc)
95
+ raise "No Collins configuration for datacenter #{dc.inspect} found. Perhaps you want to create it with '#{PROG_NAME} --new #{dc}'?"
96
+ end
97
+ set_default_datacenter(dc)
98
+ when :list
99
+ dcs = configured_datacenters
100
+ default = default_datacenter
101
+ dcs.each do |dc|
102
+ if dc == default && @options[:show_current]
103
+ puts "#{dc} *"
104
+ else
105
+ puts dc
106
+ end
107
+ end
108
+ when :new
109
+ # check if dc already exists
110
+ dc = @options[:dc]
111
+ if configured_datacenters.include? dc
112
+ raise "Unable to create new configuration for #{dc}: Configuration already exists at #{DEFAULT_CONFIG_PATH}.#{@options[:dc]}"
113
+ end
114
+
115
+ uri = @options[:host]
116
+ if uri.nil?
117
+ print "Enter Collins URI for #{dc} (i.e. https://collins.#{dc}.company.net): "
118
+ x = gets
119
+ uri = URI.parse(x.strip)
120
+ end
121
+
122
+ user = @options[:user]
123
+ if user.nil?
124
+ default = Etc.getlogin
125
+ print "Enter username (default: #{default}): "
126
+ x = gets.strip
127
+ if x.empty?
128
+ user = default
129
+ else
130
+ user = x
131
+ end
132
+ end
133
+
134
+ pw = @options[:pw]
135
+ if pw.nil?
136
+ print "Enter password: "
137
+ pw = STDIN.noecho(&:gets).chomp
138
+ print "\n"
139
+ end
140
+ create_new_datacenter(dc, uri, user, pw, @options[:timeout])
141
+
142
+ # if this is the only DC, use it
143
+ has_default_dc = File.symlink?(File.expand_path(DEFAULT_CONFIG_PATH)) rescue false
144
+ if !has_default_dc
145
+ set_default_datacenter(dc)
146
+ puts "Automatically selected #{dc.inspect} as the current datacenter with '#{PROG_NAME} #{dc}'"
147
+ else
148
+ puts "Now, select the Collins instance #{dc.inspect} with '#{PROG_NAME} #{dc}' to access Collins"
149
+ end
150
+ end
151
+ success
152
+ end
153
+
154
+ def configured_datacenters
155
+ # return list of dc names, followed by an asterisk if default?
156
+ files = Dir.glob(File.expand_path(DEFAULT_CONFIG_PATH + ".*"))
157
+ files.map do |f|
158
+ File.basename(f).split('.').last
159
+ end.sort
160
+ end
161
+
162
+ def set_default_datacenter dc
163
+ ln = File.expand_path(DEFAULT_CONFIG_PATH)
164
+ if File.exist?(ln) && !File.symlink?(ln)
165
+ raise "Unable to set config to use #{dc}: #{ln} is not a symlink, which means #{PROG_NAME.inspect} is not managing this configuration"
166
+ end
167
+ FileUtils.ln_sf(File.expand_path("~/.collins.yml.#{dc}"), File.expand_path(DEFAULT_CONFIG_PATH))
168
+ end
169
+
170
+ def default_datacenter
171
+ def_cfg = File.expand_path(DEFAULT_CONFIG_PATH)
172
+ if !File.symlink?(def_cfg)
173
+ raise "Unable to determine default Collins datacenter: #{def_cfg} is not a symlink, which means #{PROG_NAME.inspect} is not managing this configuration"
174
+ end
175
+ target = File.basename(File.readlink(def_cfg))
176
+ if target =~ /^\.collins\.yml\.([A-Za-z0-9\-_]+)$/
177
+ return $1
178
+ else
179
+ raise "Unable to determine datacenter from target config #{target}; does not match .collins.yml.$DATACENTER"
180
+ end
181
+ end
182
+
183
+ def create_new_datacenter dc, uri, user, pw, t
184
+ o = {
185
+ host: uri.to_s,
186
+ username: user,
187
+ timeout: t
188
+ }
189
+ o[:password] = pw unless pw.nil? || pw.empty?
190
+ File.open(File.expand_path("#{DEFAULT_CONFIG_PATH}.#{dc}"), 'w', 0600) do |f|
191
+ f.puts o.to_yaml
192
+ end
193
+ end
194
+
195
+ end
196
+ end
197
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collins-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabe Conradi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-17 00:00:00.000000000 Z
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -92,6 +92,7 @@ files:
92
92
  - README.md
93
93
  - bin/collins
94
94
  - lib/collins-cli.rb
95
+ - lib/collins/cli/dc.rb
95
96
  - lib/collins/cli/find.rb
96
97
  - lib/collins/cli/formatter.rb
97
98
  - lib/collins/cli/ipam.rb
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  version: '0'
128
129
  requirements: []
129
130
  rubyforge_project:
130
- rubygems_version: 2.5.1
131
+ rubygems_version: 2.5.2
131
132
  signing_key:
132
133
  specification_version: 4
133
134
  summary: CLI utilities to interact with the Collins API