Class: Gogetit::CLI

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

Instance Method Summary collapse

Methods included from Util

#check_ip_available, #get_gateway, #knife_bootstrap, #knife_remove, #ping_available?, #recognize_env, #run_command, #run_through_ssh, #ssh_available?, #symbolize_keys, #update_databags, #wait_until_available

Instance Method Details

#create(name) ⇒ Object



54
55
56
57
58
59
60
61
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
# File 'lib/gogetit/cli.rb', line 54

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'
    result = Gogetit.lxd.create(name, options.to_hash)
    Gogetit.set_result(result)
  when 'libvirt'
    result = Gogetit.libvirt.create(name, options.to_hash)
    Gogetit.set_result(result)
  else
    abort('Invalid argument entered.')
  end

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

#deploy(name) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/gogetit/cli.rb', line 117

def deploy(name)
  Gogetit.set_result(Gogetit.libvirt.deploy(name, options.to_hash))

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

#destroy(name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gogetit/cli.rb', line 90

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

#listObject



29
30
31
32
33
34
35
# File 'lib/gogetit/cli.rb', line 29

def list
  puts "Listing LXD containers on #{Gogetit.config[:lxd][:nodes][0][:url]}.."
  system("lxc list #{Gogetit.config[:lxd][:nodes][0][:name]}:")
  puts ''
  puts "Listing KVM domains on #{Gogetit.config[:libvirt][:nodes][0][:url]}.."
  system("virsh -c #{Gogetit.config[:libvirt][:nodes][0][:url]} list --all")
end

#rebuild(name) ⇒ Object



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

def rebuild(name)
  # Let Gogetit recognize the provider.
  provider = Gogetit.get_provider_of(name)
  if provider
    case provider
    when 'lxd'
      invoke :destroy, [name]
      alias_name = YAML.load(
        Gogetit.get_result[:info][:config][:"user.user-data"]
      )['source_image_alias']
      invoke :create, [name], :alias => alias_name
    when 'libvirt'
      invoke :release, [name]
      distro_name = Gogetit.get_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, Gogetit.logger) if options[:chef]
    update_databags(Gogetit.config, Gogetit.logger)
  end
end

#release(name) ⇒ Object



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

def release(name)
  result = Gogetit.libvirt.release(name)
  Gogetit.set_result(result)

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