lib/cloudstack-cli/helper.rb in cloudstack-cli-0.15.1 vs lib/cloudstack-cli/helper.rb in cloudstack-cli-1.0.0.rc1
- old
+ new
@@ -45,11 +45,11 @@
end
def update_job_status(jobs)
jobs.each do |job|
unless job[:status] && job[:status] > 0
- job[:status] = client.query_job(job[:id])['jobstatus']
+ job[:status] = client.query_async_job_result(job_id: job[:id])['jobstatus']
end
end
jobs
end
@@ -66,72 +66,80 @@
spinner.push spinner.shift
spinner
end
def bootstrap_server(args = {})
- if args[:project] && project = client(quiet: true).get_project(args[:project])
+ if args[:project] && project = find_project(args[:project])
project_id = project["id"]
project_name = project['name']
end
- server = client(quiet: true).get_server(args[:name], project_id)
- unless server
- say "Create server #{args[:name]}...", :yellow
- server = client.create_server(args)
- say "Server #{server["name"]} has been created.", :green
- client.wait_for_server_state(server["id"], "Running")
- say "Server #{server["name"]} is running.", :green
+
+ if args[:name]
+ args['displayname'] = args[:name]
+ name = args[:name]
+ elsif args[:displayname]
+ name = args[:displayname]
+ end
+
+ unless server = client.list_virtual_machines(name: args[:name], project_id: project_id).first
+ say "Create VM #{name}...", :yellow
+ server = client.deploy_virtual_machine(args)
+ puts
+ say "VM #{name} has been created.", :green
else
- say "Server #{args[:name]} already exists (#{server['state']}).", :yellow
+ say "VM #{name} already exists (#{server["state"]}).", :yellow
end
if args[:port_rules] && args[:port_rules].size > 0
create_port_rules(server, args[:port_rules])
end
server
end
def create_server(args = {})
- if args[:project] && project = client(quiet: true).get_project(args[:project])
+ if args[:project] && project = find_project(args[:project])
project_id = project["id"]
project_name = project['name']
end
- server = client(quiet: true).get_server(args[:name], project_id)
- unless server
- server = client.create_server(args)
+ unless server = client.list_virtual_machines(name: args[:name], project_id: project_id)
+ server = client.deploy_virtual_machine(args)
end
server
end
def create_port_rules(server, port_rules, async = true)
frontendip = nil
jobs = []
client.verbose = async
- project_id = server['project'] ? client.get_project(server['project'])['id'] : nil
+ project_id = server['projectid'] || nil
port_rules.each do |pf_rule|
ip = pf_rule.split(":")[0]
if ip != ''
ip_addr = client.get_public_ip_address(ip, project_id)
unless ip_addr
say "Error: IP #{ip} not found.", :red
next
end
else
ip_addr = frontendip ||= client.associate_ip_address(
- server["nic"].first["networkid"]
+ network_id: server["nic"].first["networkid"]
)
end
port = pf_rule.split(":")[1]
+ args = {
+ ipaddressid: ip_addr["id"],
+ publicport: port,
+ privateport: port,
+ protocol: 'TCP',
+ virtualmachineid: server["id"]
+ }
if async
say "Create port forwarding rule #{ip_addr['ipaddress']}:#{port} for server #{server["name"]}.", :yellow
- client.create_port_forwarding_rule(ip_addr["id"], port, 'TCP', port, server["id"])
+ client.create_port_forwarding_rule(args)
return
else
- jobs << client.create_port_forwarding_rule(
- ip_addr["id"],
- port, 'TCP', port, server["id"],
- false
- )['jobid']
+ jobs << client.create_port_forwarding_rule(args, {sync: true})['jobid']
end
end
jobs
end
@@ -144,29 +152,30 @@
else
zone = 0
end
projects = client.list_projects
- if yes?("Do you want to deploy your server within a project?") && projects.size > 0
- if projects.size > 0
+ project_id = nil
+ if projects.size > 0
+ if yes?("Do you want to deploy your VM within a project? (y/N)") && projects.size > 0
say "Select a project", :yellow
print_options(projects)
project = ask_number("Project Nr.: ")
+ project_id = projects[project]['id'] rescue nil
end
- project_id = projects[project]['id'] rescue nil
end
- say "Please provide a name for the new server", :yellow
+ say "Please provide a name for the new VM", :yellow
say "(spaces or special characters are NOT allowed)"
server_name = ask("Server name: ")
server_offerings = client.list_service_offerings
say "Select a computing offering:", :yellow
print_options(server_offerings)
service_offering = ask_number("Offering Nr.: ")
- templates = client.list_templates(project_id: project_id, zone_id: zones[zone]["id"])
+ templates = client.list_templates(project_id: project_id, zone_id: zones[zone]["id"], template_filter: "all")
say "Select a template:", :yellow
print_options(templates)
template = ask_number("Template Nr.: ")
networks = client.list_networks(project_id: project_id, zone_id: zones[zone]["id"])
@@ -178,25 +187,77 @@
network = 0
end
say "You entered the following configuration:", :yellow
table = [["Zone", zones[zone]["name"]]]
- table << ["Server Name", server_name]
+ table << ["VM Name", server_name]
table << ["Template", templates[template]["name"]]
table << ["Offering", server_offerings[service_offering]["name"]]
table << ["Network", networks[network]["name"]]
table << ["Project", projects[project]["name"]] if project
print_table table
- if yes? "Do you want to deploy this server?"
+ if yes? "Do you want to deploy this VM? (y/N)"
bootstrap_server(
- server_name,
- zones[zone]["name"],
- templates[template]["name"],
- server_offerings[service_offering]["name"],
- [networks[network]["name"]], nil,
- project ? projects[project]["name"] : nil
+ name: server_name,
+ zone_id: zones[zone]["id"],
+ template_id: templates[template]["id"],
+ serviceoffering_id: server_offerings[service_offering]["id"],
+ network_ids: network ? networks[network]["id"] : nil,
+ project_id: project_id
)
+ end
+ end
+
+ ##
+ # Finds the public ip for a server
+
+ def get_server_public_ip(server, cached_rules=nil)
+ return nil unless server
+
+ # find the public ip
+ nic = get_server_default_nic(server) || {}
+ if nic['type'] == 'Virtual'
+ ssh_rule = get_ssh_port_forwarding_rule(server, cached_rules)
+ ssh_rule ? ssh_rule['ipaddress'] : nil
+ else
+ nic['ipaddress']
+ end
+ end
+
+ ##
+ # Gets the SSH port forwarding rule for the specified server.
+
+ def get_ssh_port_forwarding_rule(server, cached_rules=nil)
+ rules = cached_rules || client.list_port_forwarding_rules(project_id: server["projectid"]) || []
+ rules.find_all { |r|
+ r['virtualmachineid'] == server['id'] &&
+ r['privateport'] == '22'&&
+ r['publicport'] == '22'
+ }.first
+ end
+
+ ##
+ # Returns the fully qualified domain name for a server.
+
+ def get_server_fqdn(server)
+ return nil unless server
+
+ nic = get_server_default_nic(server) || {}
+ networks = client.list_networks(project_id: server['projectid']) || {}
+
+ id = nic['networkid']
+ network = networks.select { |net|
+ net['id'] == id
+ }.first
+ return nil unless network
+
+ "#{server['name']}.#{network['networkdomain']}"
+ end
+
+ def get_server_default_nic(server)
+ server['nic'].each do |nic|
+ return nic if nic['isdefault']
end
end
end
end