Class: CloudstackCli::Helper

Inherits:
Object
  • Object
show all
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.



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

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.



3
4
5
# File 'lib/cloudstack-cli/helper.rb', line 3

def cs
  @cs
end

Instance Method Details

#assign_to_load_balancer_rule(id, names) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/cloudstack-cli/helper.rb', line 109

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



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
87
88
89
90
91
92
93
# File 'lib/cloudstack-cli/helper.rb', line 57

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

#bootstrap_server_interactiveObject



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

def bootstrap_server_interactive
  ARGV.clear 
	puts
	puts "We are going to deploy a new server and..."
	puts "- assign a public IP address"
	puts "- create a firewall rule for SSH and HTTP access"
	puts "- connect to the server and install the puppet client}"
	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

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



103
104
105
106
107
# File 'lib/cloudstack-cli/helper.rb', line 103

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

#domains(name = nil) ⇒ Object



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

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

#list_accounts(name = nil) ⇒ Object



95
96
97
# File 'lib/cloudstack-cli/helper.rb', line 95

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

#list_load_balancer_rules(project = nil) ⇒ Object



99
100
101
# File 'lib/cloudstack-cli/helper.rb', line 99

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

#networks(project_id = nil) ⇒ Object



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

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

#optionsObject



14
15
16
# File 'lib/cloudstack-cli/helper.rb', line 14

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


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

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

#projectsObject



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

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

#server_offerings(domain = nil) ⇒ Object



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

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

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



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

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

#virtual_machines(options = {}) ⇒ Object



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

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

#volumes(project_id = nil) ⇒ Object



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

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

#zonesObject



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

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