def promote_ephimeral_ip(self): """ Promote an Ephimeral IP to a Static one For more infos about Ephimeral/Static IPs on GCP, please visit https://cloud.google.com/compute/docs/ip-addresses/ """ # Select an instance floating_ip = None while (True): instance_index = SimpleTUI.list_dialog( "Instances available", self.print_all_instances, question= "Select the instance which floating IP has to be promoted to \"static\"" ) if instance_index is None: return instance = self.instances[instance_index - 1] # Check if the instance has an IP assigned (e.g. no IP is assigned while stopped) if len(instance.public_ips) == 0 or None in instance.public_ips: SimpleTUI.msg_dialog( "Promotion status", "This instance has no available floating IPs to promote!", SimpleTUI.DIALOG_ERROR) # Check if the instance has already a static IP assigned elif self._is_instance_floating_ip_static(instance): SimpleTUI.msg_dialog( "Promotion status", "This instance floating IP is already promoted to \"static\"!", SimpleTUI.DIALOG_ERROR) # Continue the ephimeral to static conversion else: floating_ip = instance.public_ips[0] break # Specify address name address_name = SimpleTUI.input_dialog( "Static Floating IP Name", question="Specify a name for the new Static Floating IP", return_type=str, regex="^[a-zA-Z0-9-]+$") if address_name is None: return if self._promote_ephimeral_ip_to_static(floating_ip, address_name): SimpleTUI.msg_dialog("Static Floating IP Promotion", "Floating IP promoted!", SimpleTUI.DIALOG_SUCCESS) else: SimpleTUI.msg_dialog( "Static Floating IP Promotion", "There was an error while promoting this Floating IP!", SimpleTUI.DIALOG_ERROR)
def _platform_create_volume(self, volume_name, volume_size): """ Create a new volume using the Amazon Web Services API Some specific steps are performed here: - Volume type (standard or io1) - IOPS (only if io1 is selected) - Zone selection (required) Args: volume_name (str): Volume name volume_size (int): Volume size in GB Returns: bool: True if the volume is successfully created, False otherwise """ # Volume type volume_type = SimpleTUI.input_dialog( "Volume type", question="Specify the volume type (standard, io1)", return_type=str, regex="^(standard|io1)$") if volume_type is None: return # IOPS iops = None if volume_type == "io1": iops = SimpleTUI.input_dialog( "IOPS limit", question= "Specify the number of IOPS (I/O operations per second) the volume has to support", return_type=int) if iops is None: return # Zone selection zone_index = SimpleTUI.list_dialog( "Zones available", self.print_all_availability_zones, question="Select a zone where the volume will be created") if zone_index is None: return zone = self.avail_zones[zone_index - 1] # Volume creation return self.ec2_client.create_volume(name=volume_name, size=volume_size, location=zone, ex_volume_type=volume_type, ex_iops=iops)
def _platform_extra_menu(self): """ Print the extra Functions Menu (specific for each platform) """ while (True): menu_header = self.platform_name + " Extra Commands" menu_subheader = [ "Region: \033[1;94m" + self._platform_get_region() + "\033[0m" ] menu_items = [ "Promote ephimeral IP to static", "Demote a static IP", "List instances for all the regions", "List volumes for all the regions", "List floating ips for all the regions", "Back to the Main Menu" ] choice = SimpleTUI.print_menu(menu_header, menu_items, menu_subheader) if choice == 1: # Promote IP self.promote_ephimeral_ip() elif choice == 2: # Demote IP answer = SimpleTUI.yn_dialog( "Demotion status", "You can demote a static IP easily deleting it through \"Manage floating IPs\" > \"Release a reserved Floating IP\".\n" + "NOTE: the static IP won't be removed from the associated instance until the latter is stopped/rebooted/deleted.\n" + "For more infos about Ephimeral/Static IPs on GCP, please visit https://cloud.google.com/compute/docs/ip-addresses/.\n" + "Would you like to start the \"Release a reserved Floating IP\" wizard now?", SimpleTUI.DIALOG_INFO) if answer: self.release_floating_ip() elif choice == 3: # List all the instances (Global) SimpleTUI.list_dialog("Instances available (Global view)", list_printer=self.print_global_instances) elif choice == 4: # List all the volumes (Global) SimpleTUI.list_dialog("Volumes available (Global view)", list_printer=self.print_global_volumes) elif choice == 5: # List all the floating ips (Global) SimpleTUI.list_dialog( "Floating IPs available (Global view)", list_printer=self.print_global_floating_ips) elif choice == 6: break else: SimpleTUI.msg_dialog("Error", "Unimplemented functionality", SimpleTUI.DIALOG_ERROR)
def _platform_create_new_instance(self, instance_name, image, instance_type, monitor_cmd_queue=None): """ Create a new instance using the Amazon Web Services API Some specific steps are performed here: - Ask for security group - Ask for key pair - Instance creation summary Args: instance_name (str): The name of the instance image (<image_type>): The image to be used as a base system instance_type (<instance_type>): The VM flavor monitor_cmd_queue: the monitor commands Quue """ # 5. Security Group security_group_index = SimpleTUI.list_dialog( "Security Groups available", self.print_all_security_groups, question="Select security group") if security_group_index is None: return security_group = self.security_groups[security_group_index - 1] # 6. Key Pair key_pair_index = SimpleTUI.list_dialog("Key Pairs available", self.print_all_key_pairs, question="Select key pair") if key_pair_index is None: return key_pair = self.key_pairs[key_pair_index - 1] # Creation summary print("\n--- Creating a new instance with the following properties:") print("- %-20s %-30s" % ("Name", instance_name)) print("- %-20s %-30s" % ("Image", image.name)) print("- %-20s %-30s" % ("Instance Type", instance_type.name)) print("- %-20s %-30s" % ("Key Pair", key_pair.name)) print("- %-20s %-30s" % ("Security Group", security_group)) # ask for confirm print("") if (SimpleTUI.user_yn("Are you sure?")): instance = self.ec2_client.create_node( name=instance_name, image=image, size=instance_type, ex_keyname=key_pair.name, ex_security_groups=[security_group], ex_monitoring=True, ex_mincount=1, ex_maxcount=1) if instance is None: return False if monitor_cmd_queue is not None and self.is_monitor_running(): monitor_cmd_queue.put({ "command": "add", "instance_id": instance.id }) return True
def _platform_associate_floating_ip(self, floating_ip, instance): """ Associate a floating IP to an instance using the Google Cloud Platform API Some specific steps are performed here: - NIC (Network interface controller) selection - Access config name Args: floating_ip (GCEAddress): The floating IP to attach instance (Node): The instance where the floating IP is to be assigned Returns: bool: True if the floating IP is successfully associated, False otherwise """ # Set an instance, as required by print_all_nics() self.current_instance = instance nic_index = SimpleTUI.list_dialog( "NICs available", self.print_all_nics, question="Select the VM NIC to assign this IP") if nic_index is None: return nic = instance.extra["networkInterfaces"][ nic_index - 1] # serve nome per rimuovere # Check if there's already an active Access Configuration and ask the user for confirm remove_old_access_config = False if self._nic_has_access_config(nic): choice = SimpleTUI.yn_dialog( "Access Configuration Overwrite", "Warning: there's already an access configuration associated to this NIC.\n" + "Do you really want to continue (the current access configuration will be overwritten)?", warning=True) if not choice: return remove_old_access_config = True # Access Configuration name access_config_name = SimpleTUI.input_dialog( "Access configuration", question="Specify an access configuration name", return_type=str, regex="^[a-zA-Z0-9-]+$") if access_config_name is None: return # Remove the old access configuration if it's already existing if remove_old_access_config: SimpleTUI.msg_dialog("Access Configuration Overwrite", "Removing old access configuration...", SimpleTUI.DIALOG_INFO, pause_on_exit=False, clear_on_exit=False) if not self._delete_access_config(instance, nic): SimpleTUI.msg_dialog( "Access Configuration Overwrite", "There was an error while removing the current access configuration!", SimpleTUI.DIALOG_ERROR) return # Associate the Access Configuration to the NIC if self.gcp_client.ex_add_access_config(node=instance, name=access_config_name, nic=nic, nat_ip=floating_ip.address, config_type="ONE_TO_ONE_NAT"): return True return False