def connect(self): output('\nPlease enter the name of the instance you would like to connect to.') try: fip = None instance = self.obtain_entity(filter_f=self.is_active) if instance: fip = self.manager.get_instance_fip(instance) if fip is None: if boolean_input('This instance is not publicly routable yet.\n' 'Do you want to make it routable'): fip = self.make_instance_routable(instance) if fip is None: output('No instance connected to.') return output('%s is publicly routable via %s', instance.name, fip) output() suggested_username, suggested_password = self.get_suggested_instance_credentials(instance) output('Connecting to %s...', fip) username = string_input('Username', default=suggested_username) password = string_input('Password', default=suggested_password) # connect self.manager.connect_to_instance(instance, username, password) output() output('Successfully established a connection.\n' 'Returning to MiniCloud now.') except DoesNotExistException: output('No instance connected to.') except MiniCloudException: error('Connecting to the instance failed.')
def add_entity(self): def_os_auth_url = shell_variable('OS_AUTH_URL', 'http://x86.trystack.org:5000/v2.0') def_tenant_name = shell_variable('OS_TENANT_NAME', 'your-tenant>') def_username = shell_variable('OS_USERNAME', 'your-username>') def_password = shell_variable('OS_PASSWORD', 'your-password>') if boolean_input('Do you want a REAL cloud to be added (Y), or STUB (n)'): cloud = Cloud( string_input('Name', default='trystack'), string_input('Type', default='OPENSTACK'), string_input('Type', default='Kilo'), string_input('Location', default='trystack'), string_input('Path', default=def_os_auth_url), string_input('Tenant', default=def_tenant_name), string_input('Username', default=def_username), string_input('Password', default=def_password)) return self.manager.add(cloud) else: cloud = StubCloud() self.manager.add(cloud) new_cluster = Cluster(StubDriverContext.CLUSTER_NAME, cloud.name) return self.minicloud.cluster_manager.add(new_cluster)
def obtain_entity_to_add(self): name = string_input('Name', default='demo') if name == Cluster.UNNAMED_CLUSTER: error('This is a reserved name.') return None cmm = self.obtain_entity_name(self.minicloud.cloud_manager) return Cluster(name, cmm) if cmm else None
def obtain_entity_to_add(self): name = string_input('Name') external = False # boolean_input('Do you want this network to be external (mind policies!)', default=False) cidr = self.obtain_cidr() cloud_manager = self.obtain_entity_from_manager(self.minicloud.cloud_manager) if cloud_manager: router = None if not external: if self.router_manager.list(): router = self.obtain_entity_from_manager(self.router_manager, 'Router to attach to, if any (else give none)') elif boolean_input('Do you want this network to be connected to a router', default=False): router_name = string_input('Enter the router name') router = Router(router_name, cloud_manager) self.router_manager.add_entity(router) return Network(name, cidr, external, router, cloud_manager) else: return None
def obtain_cidr(): cidr = None while True: cidr = string_input('CIDR') if cidr == 'none': return None elif not Network.check_cidr(cidr): error('This is not a valid (IPv4) CIDR. Please enter again, or give \'none\'') else: break return cidr
def obtain_entity_name(self, manager=None, input_string=None, filter_f=None, choose_from_display=False, allow_none=False, skip_questioning_if_only_one_entity=False, return_immediately_if_no_entity=False): name = None if not manager: manager = self.manager if not input_string: input_string = manager.entity_title() + ' name' if choose_from_display: output() # skip line first, is nicer names = manager.list_entity_names(filter_f) if names and choose_from_display: name_idx = choice_input_list(input_string, names, add_none=allow_none, zero_based_return=True, skip_line=False) if name_idx == len(names): return None else: return names[name_idx] else: if not names: if return_immediately_if_no_entity: return None else: def_name = 'none' elif len(names) == 1: if skip_questioning_if_only_one_entity: return names[0] else: def_name = names[0] else: def_name = 'show' while True: name = string_input(input_string, default=def_name) if name == 'show': if len(name) > 0: for n in names: print n output('or give \'none\' to return.') else: output('Nothing to show.') elif name == 'none': return None else: if name in names: break else: output('%s is not a valid %s, pls re-enter or give \'none\'.', (name, manager.entity_name())) return name
def obtain_update_data(self, entity): options_list = ['Location \t(%s)' % entity.location, # 1 'Path \t(%s)' % entity.path, # 2 'Tenant \t(%s)' % entity.tenant, # 3 'Username \t(%s)' % entity.username, # 4 'Password \t(%s)' % entity.password # 5 # none # 6 ] field = choice_input_list('What would you like to update', options_list, add_none=True, default_last=True) data = None if field < 6: if field == 1: data = dict(name=entity.name, location=string_input('New location')) elif field == 2: data = dict(name=entity.name, path=string_input('New path')) elif field == 3: data = dict(name=entity.name, tenant=string_input('New tenant')) elif field == 4: data = dict(name=entity.name, username=string_input('New username')) elif field == 5: data = dict(name=entity.name, password=string_input('New password')) return data
def make_instance_routable(self, instance): fip = self.manager.get_instance_fip(instance) if fip: output('The instance is already routable as %s', fip) return fip network = self.manager.get_instance_network(instance) routers = self.network_manager.get_routers(network) if routers: router = routers[0] else: output('The instance\'s network (\'' + network.name + '\') has no router attached to it.') router = self.make_network_routable(network) if not router: return None external_networks = self.network_manager.external_networks(router) if not external_networks: if not self.network_manager.external_networks(): error('You unfortunately have no external networks') if boolean_input('Do you want to create an external network'): new_name = string_input('Please enter the network name') new_cidr = '169.169.10.0/24' # fixed for now external_network = Network(new_name, new_cidr, True, router, instance.cloud_manager) self.network_manager.add(external_network) else: return None external_network = self.obtain_entity_from_manager( self.network_manager, 'Please enter the name of the external network you want to route from', self.network_manager.external, skip_questioning_if_only_one_entity=True) if external_network: output('Uplinking router.') self.router_manager.uplink(router, external_network) elif len(external_networks) == 1: external_network = external_networks[0] else: output('\nWhich network do you want to make the instance routable to?') network_idx = self.network_manager.obtain_entity_name(filter=self.network_manager.external) external_network = external_networks[network_idx] if external_network and self.check_instance_is_ready(instance): fip = self.manager.make_routable(instance, external_network) return fip
def obtain_entity_to_add(self): name = string_input('Name') cloud_manager = self.obtain_entity_from_manager(self.minicloud.cloud_manager) if cloud_manager: ext_net = None if boolean_input('Do you want to attach this router to an external network'): ext_net = self.obtain_entity_from_manager( self.network_manager, 'Please enter the network name', self.network_manager.external, skip_questioning_if_only_one_entity=True) return Router(name, cloud_manager=cloud_manager, ext_network=ext_net) else: return None
def make_network_routable(self, network): router = None if self.router_manager.list() and boolean_input('Do you want to attach to existing router'): router = self.obtain_entity_from_manager( self.router_manager, 'Please enter the name of the router to attach to', skip_questioning_if_only_one_entity=True) elif boolean_input('Do you want to create a router'): name = string_input('Name') output('Creating router.') router = Router(name, cloud_manager=network.cloud_manager) self.router_manager.add(router) if router: output('Attaching network.') self.network_manager.attach_network_to_router(network, router) return router
def add_entity(self): name = string_input('Name') cluster_name = self.obtain_entity_name(self.cluster_manager, choose_from_display=True, allow_none=True) flavor = self.obtain_entity_from_manager( self.manager.flavor_manager, 'Which flavor do you want to boot?', choose_from_display=True, return_immediately_if_no_entity=True) if not flavor: error('No flavor found.') return None image = self.obtain_entity_from_manager( self.manager.image_manager, 'Which image do you want to boot?', choose_from_display=True, return_immediately_if_no_entity=True) if not image: error('No image found.') return None output('\nScanning security groups.') nova_sgs = self.manager.nova_sg_manager.list() public_ssh_sg = None for sg in nova_sgs: if sg.name == "public_ssh": public_ssh_sg = sg break if not public_ssh_sg and boolean_input("No public_ssh security group was found. Would you like to create it"): sg = NovaSg('public_ssh', 'sg for ssh access', self.manager.get_cloud_manager(cluster_name)) sg.add_rule(NovaSgRule('tcp', 22, 22, '0.0.0.0/0')) self.manager.nova_sg_manager.add(sg) nova_sg = self.obtain_entity_from_manager( self.manager.nova_sg_manager, 'Which nova security group do you want to boot with?', choose_from_display=True, return_immediately_if_no_entity=True) if not nova_sg: error('No Nova Security Group found.') return None network = self.obtain_entity_from_manager( self.network_manager, 'Which network do you want to boot in?', self.network_manager.non_external, choose_from_display=True, return_immediately_if_no_entity=True) if not network: error('No network found.') return None make_routable = boolean_input('\nDo you want this instance to be routable') output() instance = Instance(name, cluster_name, flavor, image, nova_sg, network) self.manager.add(instance) if make_routable: fip = self.make_instance_routable(instance) if fip: output('Instance is routable as %s.', fip) return instance