Class: Gogetit::CLI

Inherits:
Thor
  • Object
show all
Includes:
Util
Defined in:
lib/gogetit/cli.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#check_ip_available, #get_gateway, #get_http_content, #get_provider_of, #is_port_open?, #knife_bootstrap, #knife_remove, #ping_available?, #run_command, #run_through_ssh, #ssh_available?, #update_databags, #wait_until_available

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gogetit/cli.rb', line 18

def initialize(*args)
  super
  @config = Gogetit.config
  @logger = Gogetit.logger
  @lxd = Gogetit.lxd
  @libvirt = Gogetit.libvirt
  @providers = {
    lxd: lxd,
    libvirt: libvirt
  }
end

Class Attribute Details

.resultObject

Returns the value of attribute result.



13
14
15
# File 'lib/gogetit/cli.rb', line 13

def result
  @result
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/gogetit/cli.rb', line 16

def config
  @config
end

#libvirtObject (readonly)

Returns the value of attribute libvirt.



16
17
18
# File 'lib/gogetit/cli.rb', line 16

def libvirt
  @libvirt
end

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/gogetit/cli.rb', line 16

def logger
  @logger
end

#lxdObject (readonly)

Returns the value of attribute lxd.



16
17
18
# File 'lib/gogetit/cli.rb', line 16

def lxd
  @lxd
end

#providersObject (readonly)

Returns the value of attribute providers.



16
17
18
# File 'lib/gogetit/cli.rb', line 16

def providers
  @providers
end

Instance Method Details

#create(name) ⇒ Object



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
# File 'lib/gogetit/cli.rb', line 70

def create(name)
  abort("'vlans' and 'ipaddresses' can not be set together.") \
    if options['vlans'] and options['ipaddresses']

  abort("when 'no-maas', the network configuration have to be set by 'file'.") \
    if options['no-maas'] and (options['vlans'] or options['ipaddresses'])

  abort("'no-maas' and 'file' have to be set together.") \
    if options['no-maas'] ^ !!options['file']

  abort("'distro' has to be set with libvirt provider.") \
    if options['distro'] and options['provider'] == 'lxd'

  abort("'alias' has to be set with lxd provider.") \
    if options['alias'] and options['provider'] == 'libvirt'

  case options['provider']
  when 'lxd'
    Gogetit::CLI.result = lxd.create(name, options.to_hash)
  when 'libvirt'
    Gogetit::CLI.result = libvirt.create(name, options.to_hash)
  else
    abort('Invalid argument entered.')
  end

  # post-tasks
  if options['chef']
    knife_bootstrap(name, options[:provider], config, logger)
    update_databags(config, logger)
  end
end

#deploy(name) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/gogetit/cli.rb', line 128

def deploy(name)
  Gogetit::CLI.result = libvirt.deploy(name, options.to_hash)

  # post-tasks
  if options['chef']
    knife_bootstrap(name, options[:provider], config, logger)
    update_databags(config, logger)
  end
end

#destroy(name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gogetit/cli.rb', line 104

def destroy(name)
  provider = get_provider_of(name, providers)
  if provider
    case provider
    when 'lxd'
      Gogetit::CLI.result = lxd.destroy(name)
    when 'libvirt'
      Gogetit::CLI.result = libvirt.destroy(name)
    else
      abort('Invalid argument entered.')
    end
  end
  # post-tasks
  if options['chef']
    knife_remove(name) if options[:chef]
    update_databags(config)
  end
end

#listObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gogetit/cli.rb', line 33

def list
  case options[:out]
  when 'custom'
    lxd.list_all_containers
  when 'all'
    config[:lxd][:nodes].each do |node|
      puts "Listing LXD containers on #{node[:url]}.."
      system("lxc list #{node[:name]}:")
    end
  when ''
    puts "Listing LXD containers on #{config[:lxd][:nodes][0][:url]}.."
    system("lxc list #{config[:lxd][:nodes][0][:name]}:")
    puts ''
  else
    puts "Invalid option or command"
  end
  puts "Listing KVM domains on #{config[:libvirt][:nodes][0][:url]}.."
  system("virsh -c #{config[:libvirt][:nodes][0][:url]} list --all")
end

#rebuild(name) ⇒ Object



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
# File 'lib/gogetit/cli.rb', line 154

def rebuild(name)
  provider = get_provider_of(name, providers)
  if provider
    case provider
    when 'lxd'
      1.upto(100) { print '_' }; puts
      puts "Destroying #{name}.."
      invoke :destroy, [name]
      alias_name = YAML.load(
        Gogetit::CLI.result[:info][:config][:"user.user-data"]
      )['source_image_alias']
      1.upto(100) { print '_' }; puts
      puts "Creating #{name}.."
      invoke :create, [name], :alias => alias_name
    when 'libvirt'
      invoke :release, [name]
      distro_name = Gogetit::CLI.result[:info][:machine]['distro_series']
      invoke :deploy, [name], :distro => distro_name
    else
      abort('Invalid argument entered.')
    end
  end
  # post-tasks
  if options['chef']
    knife_remove(name, logger) if options[:chef]
    update_databags(config, logger)
  end
end

#release(name) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/gogetit/cli.rb', line 140

def release(name)
  Gogetit::CLI.result = libvirt.release(name)

  # post-tasks
  if options['chef']
    knife_remove(name) if options[:chef]
    update_databags(config)
  end
end