Class: CloudstackCli::Helper

Inherits:
Object
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/cloudstack-cli/helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Helper

Returns a new instance of Helper.



7
8
9
10
11
12
13
14
# File 'lib/cloudstack-cli/helper.rb', line 7

def initialize(config_file)
  @config_file = config_file
 @cs = CloudstackClient::Connection.new(
   options[:url],
   options[:api_key],
   options[:secret_key]
 )
end

Instance Attribute Details

#csObject (readonly)

Returns the value of attribute cs.



5
6
7
# File 'lib/cloudstack-cli/helper.rb', line 5

def cs
  @cs
end

Instance Method Details

#assign_to_load_balancer_rule(id, names) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/cloudstack-cli/helper.rb', line 157

def assign_to_load_balancer_rule(id, names)
  puts "Add #{names.join(', ')} to rule #{id}...".color(:yellow)
  rule = @cs.assign_to_load_balancer_rule(id, names)
  if rule['success']
    puts "OK!".color(:green)
  else
    puts "Failed!".color(:red)
  end
end

#bootstrap_server(name, zone, template, offering, networks, pf_rules = [], project = nil) ⇒ Object



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
# File 'lib/cloudstack-cli/helper.rb', line 93

def bootstrap_server(name, zone, template, offering, networks, pf_rules = [], project = nil)
puts "Create server #{name}...".color(:yellow)
server = @cs.create_server(
  name,
  offering,
  template,
  zone,
  networks,
  project
)

puts
puts "Server #{server["name"]} has been created.".color(:green)
puts
puts "Make sure the server is running...".color(:yellow)
@cs.wait_for_server_state(server["id"], "Running")
puts "OK!".color(:green)
puts
puts "Get the fqdn of the server...".color(:yellow)
server_fqdn = @cs.get_server_fqdn(server)
puts "FQDN is #{server_fqdn}".color(:green)

if pf_rules.size > 0
  puts
  pf_rules.each do |pf_rule|
      ip = pf_rule.split(":")[0]
    ip_addr = @cs.get_public_ip_address(ip)
    port = pf_rule.split(":")[1]
     print "Create port forwarding rule #{ip}:#{port} ".color(:yellow)
     @cs.create_port_forwarding_rule(ip_addr["id"], port, 'TCP', port, server["id"])
     puts
  end
end

puts
puts "Complete!".color(:green)
end

#create_load_balancer_rule(name, ip, private_port, public_port, options = {}) ⇒ Object



151
152
153
154
155
# File 'lib/cloudstack-cli/helper.rb', line 151

def create_load_balancer_rule(name, ip, private_port, public_port, options = {})
  puts "Create rule #{name}...".color(:yellow)
  @cs.create_load_balancer_rule(name, ip, private_port, public_port, options = {})
  puts "OK!".color(:green)
end

#create_offering(params) ⇒ Object



28
29
30
# File 'lib/cloudstack-cli/helper.rb', line 28

def create_offering(params)
  @cs.create_offering(params)
end

#delete_offering(id) ⇒ Object



32
33
34
# File 'lib/cloudstack-cli/helper.rb', line 32

def delete_offering(id)
  @cs.delete_offering(id)
end

#destroy_router(id) ⇒ Object



175
176
177
# File 'lib/cloudstack-cli/helper.rb', line 175

def destroy_router(id)
  @cs.destroy_router(id)
end

#domains(name = nil) ⇒ Object



20
21
22
# File 'lib/cloudstack-cli/helper.rb', line 20

def domains(name = nil)
  @cs.list_domains(name)
end

#interactiveObject



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
# File 'lib/cloudstack-cli/helper.rb', line 197

def interactive
  ARGV.clear 
  puts
  puts %{We are going to deploy a new server and...
   - assign a public IP address
   - create a firewall rule for SSH and HTTP access
   - connect to the server and install the puppet client}.color(:magenta)
  puts

  print "Please provide a name for the new server".background(:blue)
  puts " (spaces or special characters are NOT allowed): "
  server_name = gets.chomp

  if projects.size > 0
    puts "Select a project".background(:blue)
    print_options(projects)
    project = gets.chomp.to_i - 1
  end

  puts "Select a computing offering:".background(:blue)
  print_options(server_offerings)
  service_offering = gets.chomp.to_i - 1

  puts "Select a template:".background(:blue)
  print_options(templates)
  template = gets.chomp.to_i - 1

  puts "Select a availability zone:".background(:blue)
  print_options(zones)
  zone = gets.chomp.to_i - 1
  
  # FIXME: show only networks in selected zone
  puts "Select a network:".background(:blue)
  project_id = projects[project]['id'] rescue nil
  networks = @cs.list_networks(project_id)
  print_options(networks)
  network = gets.chomp.to_i - 1

  bootstrap_server(
    server_name,
    zones[zone]["name"],
    templates[template]["name"],
    server_offerings[service_offering]["name"],
    [networks[network]["name"]],
    projects[project]["name"]
  )
end

#list_accounts(name = nil) ⇒ Object



143
144
145
# File 'lib/cloudstack-cli/helper.rb', line 143

def list_accounts(name = nil)
  @cs.list_accounts({ name: name })
end

#list_load_balancer_rules(project = nil) ⇒ Object



147
148
149
# File 'lib/cloudstack-cli/helper.rb', line 147

def list_load_balancer_rules(project = nil)
  @cs.list_load_balancer_rules(project)
end

#list_routers(args, redundant_state = nil) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/cloudstack-cli/helper.rb', line 167

def list_routers(args, redundant_state = nil)
  routers = @cs.list_routers(args)
  if redundant_state
   return  routers.select {|r| r['redundantstate'].downcase == redundant_state.downcase }
  end
  routers
end

#networks(project_id = nil) ⇒ Object



52
53
54
# File 'lib/cloudstack-cli/helper.rb', line 52

def networks(project_id = nil)
  @cs.list_networks(project_id)
end

#optionsObject



187
188
189
# File 'lib/cloudstack-cli/helper.rb', line 187

def options
   @options ||= CloudstackClient::ConnectionHelper.load_configuration(@config_file)
end

#physical_networksObject



56
57
58
# File 'lib/cloudstack-cli/helper.rb', line 56

def physical_networks
  @cs.list_physical_networks
end


191
192
193
194
195
# File 'lib/cloudstack-cli/helper.rb', line 191

def print_options(options, attr = 'name')
  options.to_enum.with_index(1).each do |option, i|
     puts "#{i}: #{option[attr]}"
  end  
end

#projectsObject



44
45
46
# File 'lib/cloudstack-cli/helper.rb', line 44

def projects
  @projects ||= @cs.list_projects
end

#reboot_server(name) ⇒ Object



139
140
141
# File 'lib/cloudstack-cli/helper.rb', line 139

def reboot_server(name)
  @cs.reboot_server(name)
end

#remove_publicip(id) ⇒ Object



16
17
18
# File 'lib/cloudstack-cli/helper.rb', line 16

def remove_publicip(id)
  @cs.disassociate_ip_address(id)
end

#server_offerings(domain = nil) ⇒ Object



24
25
26
# File 'lib/cloudstack-cli/helper.rb', line 24

def server_offerings(domain = nil)
  @server_offerings ||= @cs.list_service_offerings(domain)
end

#start_router(id) ⇒ Object



179
180
181
# File 'lib/cloudstack-cli/helper.rb', line 179

def start_router(id)
  @cs.start_router(id)
end

#start_server(name) ⇒ Object



135
136
137
# File 'lib/cloudstack-cli/helper.rb', line 135

def start_server(name)
  @cs.start_server(name)
end

#stop_router(id) ⇒ Object



183
184
185
# File 'lib/cloudstack-cli/helper.rb', line 183

def stop_router(id)
  @cs.stop_router(id)
end

#stop_server(name) ⇒ Object



131
132
133
# File 'lib/cloudstack-cli/helper.rb', line 131

def stop_server(name)
  @cs.stop_server(name)
end

#templates(type = 'featured', project_id) ⇒ Object



40
41
42
# File 'lib/cloudstack-cli/helper.rb', line 40

def templates(type = 'featured', project_id)
  @templates ||= @cs.list_templates(type, project_id)
end

#update_offering(args) ⇒ Object



36
37
38
# File 'lib/cloudstack-cli/helper.rb', line 36

def update_offering(args)
  @cs.update_offering(args)
end

#virtual_machines(options = {}) ⇒ Object



64
65
66
# File 'lib/cloudstack-cli/helper.rb', line 64

def virtual_machines(options = {})
  @cs.list_servers(options)
end

#virtual_machines_table(vms) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cloudstack-cli/helper.rb', line 68

def virtual_machines_table(vms)
  table(border: true) do
    row do
      column 'Name', width: 20
      column 'State'
      column 'Offering', align: 'right'
      column 'Template', align: 'right', width: 30
      column 'IP\'s', width: 14
      column 'Project'
      column 'Account'
    end
    vms.each do |vm|
      row do
        column vm['name']
        column vm['state']
        column vm['serviceofferingname']
        column vm['templatename']
        column vm['nic'].map { |nic| nic['ipaddress']}.join(" ")
        column vm['project']
        column vm['account']
      end
    end
  end
end

#volumes(project_id = nil) ⇒ Object



60
61
62
# File 'lib/cloudstack-cli/helper.rb', line 60

def volumes(project_id = nil)
  @cs.list_volumes(project_id)
end

#zonesObject



48
49
50
# File 'lib/cloudstack-cli/helper.rb', line 48

def zones
  @zones ||= @cs.list_zones
end