Class: DevLXC::CLI::DevLXC

Inherits:
Thor
  • Object
show all
Defined in:
lib/dev-lxc/cli.rb

Instance Method Summary collapse

Instance Method Details

#attach(server_name_regex) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/dev-lxc/cli.rb', line 280

def attach(server_name_regex)
  servers = get_cluster(options[:config]).get_sorted_servers(server_name_regex)
  if servers.length > 1
    puts "ERROR: The following servers matched '#{server_name_regex}'"
    servers.map { |s| puts "       #{s.name}" }
    puts "       Please specify a single server to attach to"
    exit 1
  elsif servers.empty?
    puts "ERROR: No servers matched '#{server_name_regex}'"
    puts "       Please specify a single server to attach to"
    exit 1
  end
  container = servers.first.container
  if !container.defined? || !container.running?
    puts "ERROR: Server '#{container.name}' is not running"
    exit 1
  end
  attach_opts = {
    wait: true,
    env_policy: LXC::LXC_ATTACH_CLEAR_ENV,
    extra_env_vars: ["LANG=en_US.UTF-8", "TERM=linux", "HOME=#{ENV['HOME']}"]
  }
  shell = ENV['SHELL']
  container.attach(attach_opts) { system(shell) }
end

#chef_repoObject



310
311
312
# File 'lib/dev-lxc/cli.rb', line 310

def chef_repo
  get_cluster(options[:config]).chef_repo(options[:force], options[:pivotal])
end

#create_base_container(base_container_name = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dev-lxc/cli.rb', line 34

def create_base_container(base_container_name=nil)
  start_time = Time.now
  base_container_names = %w(b-ubuntu-1204 b-ubuntu-1404 b-ubuntu-1604 b-centos-5 b-centos-6 b-centos-7)
  if base_container_name.nil? || ! base_container_names.include?(base_container_name)
    base_container_names_with_index = base_container_names.map.with_index{ |a, i| [i+1, *a]}
    print_table base_container_names_with_index
    selection = ask("Which base container do you want to create?", :limited_to => base_container_names_with_index.map{|c| c[0].to_s})
    base_container_name = base_container_names[selection.to_i - 1]
  end
  ::DevLXC.create_base_container(base_container_name, options[:options])
  puts
  print_elapsed_time(Time.now - start_time)
end

#destroy(server_name_regex = nil) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/dev-lxc/cli.rb', line 405

def destroy(server_name_regex=nil)
  servers = get_cluster(options[:config]).get_sorted_servers(server_name_regex)
  if servers.empty?
    puts "No matching server names were found"
    exit
  end
  unless options[:force]
    confirmation_string = String.new
    servers.reverse_each { |s| confirmation_string += "#{s.name}\n" }
    confirmation_string += "Are you sure you want to destroy these servers? (y/N)\n"
    return unless yes?(confirmation_string)
  end
  start_time = Time.now
  get_cluster(options[:config]).destroy(server_name_regex)
  print_elapsed_time(Time.now - start_time)
end

#halt(server_name_regex = nil) ⇒ Object



348
349
350
351
352
# File 'lib/dev-lxc/cli.rb', line 348

def halt(server_name_regex=nil)
  start_time = Time.now
  get_cluster(options[:config]).halt(server_name_regex)
  print_elapsed_time(Time.now - start_time)
end

#initObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/dev-lxc/cli.rb', line 62

def init
  header = %Q(# base_container must be the name of an existing container
base_container: b-ubuntu-1404

# list any host directories you want mounted into the servers
#mounts:
#  - /root/work root/work

# list any SSH public keys you want added to /home/dev-lxc/.ssh/authorized_keys
#ssh-keys:
#  - /root/work/clusters/id_rsa.pub

# DHCP reserved (static) IPs must be selected from the IP range 10.0.3.150 - 254
)
  chef_tier_config = %Q(
chef-server:
  topology: tier
  api_fqdn: chef.lxc
  users:          # a user's password will be the same as its username
- mary-admin
- joe-user
  orgs:
demo:
  admins:
    - mary-admin
  non-admins:
    - joe-user
  servers:
chef-be.lxc:
  ipaddress: 10.0.3.201
  role: backend
  bootstrap: true
  products:
    chef-server:
    push-jobs-server:
    reporting:
chef-fe1.lxc:
  ipaddress: 10.0.3.202
  role: frontend
  products:
    chef-server:
    manage:
    push-jobs-server:
    reporting:
)
  chef_config = %Q(
chef-server:
  users:          # a user's password will be the same as its username
- mary-admin
- joe-user
  orgs:
demo:
  admins:
    - mary-admin
  non-admins:
    - joe-user
  servers:
chef.lxc:
  ipaddress: 10.0.3.203
  products:
    chef-server:
    manage:
    push-jobs-server:
    reporting:
)
  automate_config = %Q(
automate:
  servers:
automate.lxc:
  ipaddress: 10.0.3.200
  products:
    delivery:
  license_path: /path/for/automate.license
  chef_org: delivery
  enterprise_name: demo-ent
)
  build_nodes_config = %Q(
build-nodes:
  servers:
build-node-1.lxc:
  products:
    chefdk:     # downloaded only
)
  runners_config = %Q(
runners:
  servers:
runner-1.lxc:
  products:
    chefdk:     # downloaded only
)
  analytics_config = %Q(
analytics:
  servers:
analytics.lxc:
  ipaddress: 10.0.3.204
  products:
    analytics:
)
  compliance_config = %Q(
compliance:
  admin_user: admin         # the password will be the same as the username
  servers:
compliance.lxc:
  ipaddress: 10.0.3.205
  products:
    compliance:
)
  supermarket_config = %Q(
supermarket:
  servers:
supermarket.lxc:
  ipaddress: 10.0.3.206
  products:
    supermarket:
)
  adhoc_config = %Q(
adhoc:
  servers:
adhoc.lxc:
  ipaddress: 10.0.3.207
)
  chef_backend_config = %Q(
chef-backend:
  api_fqdn: chef.lxc
  users:          # a user's password will be the same as its username
- mary-admin
- joe-user
  orgs:
demo:
  admins:
    - mary-admin
  non-admins:
    - joe-user
  servers:
chef-backend1.lxc:
  ipaddress: 10.0.3.208
  role: backend
  leader: true
  products:
    chef-backend:
chef-backend2.lxc:
  ipaddress: 10.0.3.209
  role: backend
  products:
    chef-backend:
chef-backend3.lxc:
  ipaddress: 10.0.3.210
  role: backend
  products:
    chef-backend:
chef-frontend1.lxc:
  ipaddress: 10.0.3.211
  role: frontend
  bootstrap: true
  products:
    chef-server:
    manage:
)
  nodes_config = %Q(
nodes:
  chef_server_url: https://chef.lxc/organizations/demo
  validation_client_name: demo-validator
  # comment out or remove the validation_key path to use chef-server keys generated by dev-lxc
  validation_key: # /path/for/ORG-validator.pem
  servers:
node-1.lxc:
  products:
    chef:
)
  config = ""
  config += header unless options[:append]
  config += chef_config if options[:chef]
  config += chef_tier_config if options[:chef_tier]
  config += analytics_config if options[:analytics]
  config += compliance_config if options[:compliance]
  config += supermarket_config if options[:supermarket]
  config += automate_config if options[:automate]
  config += build_nodes_config if options[:build_nodes]
  config += runners_config if options[:runners]
  config += adhoc_config if options[:adhoc]
  config += chef_backend_config if options[:chef_backend]
  config += nodes_config if options[:nodes]
  if options[:filename]
    mode = options[:append] ? 'a' : 'w'
    IO.write(options[:filename], config, mode: mode)
  else
    puts config
  end
end

#prepare_product_cache(server_name_regex = nil) ⇒ Object



330
331
332
333
334
335
336
# File 'lib/dev-lxc/cli.rb', line 330

def prepare_product_cache(server_name_regex=nil)
  start_time = Time.now
  cluster = get_cluster(options[:config])
  servers = cluster.get_sorted_servers(server_name_regex)
  cluster.prep_product_cache(servers, true)
  print_elapsed_time(Time.now - start_time)
end


316
317
318
# File 'lib/dev-lxc/cli.rb', line 316

def print_automate_credentials
  get_cluster(options[:config]).print_automate_credentials
end

#run_command(server_name_regex = nil, command) ⇒ Object



322
323
324
325
326
# File 'lib/dev-lxc/cli.rb', line 322

def run_command(server_name_regex=nil, command)
  start_time = Time.now
  get_cluster(options[:config]).get_sorted_servers(server_name_regex).each { |s| s.run_command(command); puts }
  print_elapsed_time(Time.now - start_time)
end

#show_configObject



28
29
30
# File 'lib/dev-lxc/cli.rb', line 28

def show_config
  get_cluster(options[:config]).show_config(options[:include_products])
end

#snapshot(server_name_regex = nil) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/dev-lxc/cli.rb', line 360

def snapshot(server_name_regex=nil)
  start_time = Time.now
  servers = get_cluster(options[:config]).get_sorted_servers(server_name_regex)
  if options[:list]
    servers.each_with_index do |s, server_index|
      puts s.name
      s.snapshot_list.each do |snapname, snaptime, snap_comment|
        printf "  |_ %s %s %s\n", snapname, snaptime, snap_comment
      end
      puts if server_index + 1 < servers.length
    end
    return
  elsif options[:destroy]
    snapname = options[:destroy] == 'destroy' ? "LAST" : options[:destroy]
    servers.each { |s| s.snapshot_destroy(snapname); puts }
  elsif options[:restore]
    running_servers = Array.new
    servers.each do |s|
      running_servers << s.name if s.container.running?
    end
    unless running_servers.empty?
      puts "ERROR: Aborting snapshot restore because the following servers are running"
      puts running_servers
      exit 1
    end
    snapname = options[:restore] == 'restore' ? "LAST" : options[:restore]
    servers.each { |s| s.snapshot_restore(snapname); puts }
  else
    running_servers = Array.new
    servers.each do |s|
      running_servers << s.name if s.container.running?
    end
    unless running_servers.empty?
      puts "ERROR: Aborting snapshot because the following servers are running"
      puts running_servers
      exit 1
    end
    servers.each { |s| s.snapshot(options[:comment]); puts }
  end
  print_elapsed_time(Time.now - start_time)
end

#status(server_name_regex = nil) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/dev-lxc/cli.rb', line 254

def status(server_name_regex=nil)
  cluster = get_cluster(options[:config])
  if cluster.config['chef-server'][:topology] == "tier" && cluster.config['chef-server'][:fqdn]
    printf "Chef Server FQDN: %s\n\n", cluster.config['chef-server'][:fqdn]
  end
  if cluster.config['chef-backend'][:fqdn]
    printf "Chef Server FQDN: %s\n\n", cluster.config['chef-backend'][:fqdn]
  end
  if cluster.config['analytics'][:topology] == "tier" && cluster.config['analytics'][:fqdn]
    printf "Analytics FQDN: %s\n\n", cluster.config['analytics'][:fqdn]
  end
  servers = Array.new
  cluster.get_sorted_servers(server_name_regex).map { |s| servers << s.status }
  max_server_name_length = servers.max_by { |s| s['name'].length }['name'].length unless servers.empty?
  servers.each_with_index do |s, server_index|
    printf "%-#{max_server_name_length}s     %-15s %s\n", s['name'], s['state'].upcase, s['ip_addresses']
    server = cluster.get_server(s['name'])
    server.snapshot_list.each do |snapname, snaptime, snap_comment|
      printf "  |_ %s %s %s\n", snapname, snaptime, snap_comment
    end
    puts if server_index + 1 < servers.length
  end
end

#up(server_name_regex = nil) ⇒ Object



340
341
342
343
344
# File 'lib/dev-lxc/cli.rb', line 340

def up(server_name_regex=nil)
  start_time = Time.now
  get_cluster(options[:config]).up(server_name_regex)
  print_elapsed_time(Time.now - start_time)
end