def __init__(self, context): cmd.Cmd.__init__(self) self.variables = {} self.command_topics = {} self.register_topics() self.context = context # TODO get loglevel from DB or yaml file, if not defined set to ERROR self.loglevel = "DEBUG" self._hist = [] if self.context.debug: print("init CloudmeshConsole") self.prompt = 'cm> ' self.doc_header = "Documented commands (type help <command>):" self.banner = textwrap.dedent(""" +=======================================================+ . ____ _ _ _ . . / ___| | ___ _ _ __| |_ __ ___ ___ ___| |__ . . | | | |/ _ \| | | |/ _` | '_ ` _ \ / _ \/ __| '_ \ . . | |___| | (_) | |_| | (_| | | | | | | __/\__ \ | | | . . \____|_|\___/ \__,_|\__,_|_| |_| |_|\___||___/_| |_| . +=======================================================+ Cloudmesh Shell """) # KeyCommands.__init__(self, context) # # set default cloud and default group if they do not exist # use the first cloud in cloudmesh.yaml as default # Console.set_debug(Default.debug) filename = path_expand("~/.cloudmesh/cloudmesh.yaml") # moved to import cloudmesh_client # create_cloudmesh_yaml(filename) setup_yaml() # Initialize Logging # LogUtil.initialize_logging() # sys,exit(1) # ################## # DEFAULTS # # # SET DEFAULT CLOUD # value = Default.get(name='cloud', category='general') if value is None: config = ConfigDict(filename=filename)["cloudmesh"] if 'active' in config: cloud = config["active"][0] else: clouds = config["clouds"] cloud = list(clouds.keys())[0] Default.set('cloud', cloud, category='general') # # NOT SURE WHAT THIS IS FOR # value = Default.get(name='default', category='general') if value is None: Default.set('default', 'default', category='general') # # SET DEFAULT CLUSTER # ''' cluster = ConfigDict(filename="cloudmesh.yaml")["cloudmesh"]["active"][0] value = Default.get(name='cluster', category='general') if value is None: try: hosts = ssh_config().names() if hosts is not None: cluster = hosts[0] except: pass # use the hardcoded cluster else: cluster = value Default.set('cluster', cluster, category='general') ''' # # SET DEFAULT GROUP # group = Default.group if group is None: Default.set_group("default") # # LOAD DEFAULTS FROM YAML # Default.load("cloudmesh.yaml") try: d = Key.get_from_dir("~/.ssh", store=False) except Exception as e: Console.error(e.message) # # SET DEFAULT TIMER # on = Default.timer # # SET DEFUALT SECGROUP # # # SET DEFAULT REFRESH # # r = Default.refresh # print ("REFRESH", r) # if r is None: # Default.set_refresh("on") # # SET DEFAULT USER # user = Default.user if user is None: user = ConfigDict(filename=filename)["cloudmesh"]["profile"]["user"] Default.set_user(user) r = Default.secgroup if r is None: secgroup = "{}-default".format(Default.user) Default.set_secgroup(secgroup) SecGroup.add_rule_to_db(group=secgroup, name="ssh",from_port="22",to_port="22",protocol="tcp", cidr="0.0.0.0/0") SecGroup.add_rule_to_db(group=secgroup, name="http",from_port="80",to_port="80",protocol="tcp", cidr="0.0.0.0/0") SecGroup.add_rule_to_db(group=secgroup, name="https", from_port="443", to_port="443", protocol="tcp", cidr="0.0.0.0/0") """ try: sshm = SSHKeyManager() m = sshm.get_from_yaml( load_order="~/.cloudmesh/cloudmesh.yaml") d = dict(m.__keys__) sshdb = SSHKeyDBManager() for keyname in m.__keys__: filename = m[keyname]["path"] try: sshdb.add(filename, keyname, source="yaml", uri="file://" + filename) except Exception as e: pass except Exception as e: Console.error("Problem adding keys from yaml file") """ for c in CloudmeshConsole.__bases__[1:]: # noinspection PyArgumentList c.__init__(self, context)
def do_secgroup(self, args, arguments): """ :: Usage: secgroup list [--format=FORMAT] secgroup list --cloud=CLOUD [--format=FORMAT] secgroup list GROUP [--format=FORMAT] secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR secgroup delete GROUP [--cloud=CLOUD] secgroup delete GROUP RULE secgroup upload [GROUP] [--cloud=CLOUD] Options: --format=FORMAT Specify output format, in one of the following: table, csv, json, yaml, dict. The default value is 'table'. --cloud=CLOUD Name of the IaaS cloud e.g. kilo,chameleoon. The clouds are defined in the yaml file. If the name "all" is used for the cloud all clouds will be selected. Arguments: RULE The security group rule name GROUP The label/name of the security group FROMPORT Staring port of the rule, e.g. 22 TOPORT Ending port of the rule, e.g. 22 PROTOCOL Protocol applied, e.g. TCP,UDP,ICMP CIDR IP address range in CIDR format, e.g., 129.79.0.0/16 Description: security_group command provides list/add/delete security_groups for a tenant of a cloud, as well as list/add/delete of rules for a security group from a specified cloud and tenant. Examples: secgroup list secgroup list --cloud=kilo secgroup add my_new_group webapp 8080 8080 tcp 0.0.0.0/0 seggroup delete my_group my_rule secgroup delete my_unused_group --cloud=kilo secgroup upload --cloud=kilo Description: Security groups are first assembled in a local database. Once they are defined they can be added to the clouds. secgroup list [--format=FORMAT] lists all security groups and rules in the database secgroup list GROUP [--format=FORMAT] lists a given security group and its rules defined locally in the database secgroup list --cloud=CLOUD [--format=FORMAT] lists the security groups and rules on the specified clouds. secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR adds a security rule with the given group and the details of the security ruls secgroup delete GROUP [--cloud=CLOUD] Deletes a security group from the local database. To make the change on the remote cloud, using the 'upload' command afterwards. If the --cloud parameter is specified, the change would be made directly on the specified cloud secgroup delete GROUP RULE deletes the given rule from the group. To make this change on the remote cloud, using 'upload' command. secgroup upload [GROUP] [--cloud=CLOUD...] uploads a given group to the given cloud. If the cloud is not specified the default cloud is used. If the parameter for cloud is "all" the rules and groups will be uploaded to all active clouds. This will synchronize the changes (add/delete on security groups, rules) made locally to the remote cloud(s). """ arg = dotdict(arguments) if arguments["--cloud"] is not None: is_cloud = True arg.cloud = arguments["--cloud"] or Default.cloud else: is_cloud = False arg.FORMAT = arguments["--format"] or 'table' # list all security-groups in cloud if arguments["list"]: if not is_cloud: if arg.RULE is None: print(SecGroup.list(group=arg.GROUP, name=arg.RULE, output=arg.FORMAT)) else: print(SecGroup.list(group=arg.GROUP, output=arg.FORMAT)) else: print(SecGroup.list(category=arg.cloud, output=arg.FORMAT)) elif arguments["add"]: try: SecGroup.add_rule_to_db( name=arg.RULE, group=arg.GROUP, from_port=arg.FROMPORT, to_port=arg.TOPORT, protocol=arg.PROTOCOL, cidr=arg.CIDR) except: Console.error("Problem adding security group to db") # Delete a security-group elif arguments["delete"]: if arg["RULE"] is not None: SecGroup.delete_rule_from_db(group=arg["GROUP"], name=arg["RULE"]) else: self._delete(arg) elif arguments["upload"]: # rewrite the _delete # # upload does not implicitly deleting a secgroup anymore # instead, it will check and update the rules only # self._delete(arg) SecGroup.upload(cloud=arg.cloud, group=arg.GROUP) return ""
def do_secgroup(self, args, arguments): """ :: Usage: secgroup list [--format=FORMAT] secgroup list --cloud=CLOUD [--format=FORMAT] secgroup list GROUP [--format=FORMAT] secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR secgroup delete GROUP [--cloud=CLOUD] secgroup delete GROUP RULE secgroup upload [GROUP] [--cloud=CLOUD] Options: --format=FORMAT Specify output format, in one of the following: table, csv, json, yaml, dict. The default value is 'table'. --cloud=CLOUD Name of the IaaS cloud e.g. kilo,chameleoon. The clouds are defined in the yaml file. If the name "all" is used for the cloud all clouds will be selected. Arguments: RULE The security group rule name GROUP The label/name of the security group FROMPORT Staring port of the rule, e.g. 22 TOPORT Ending port of the rule, e.g. 22 PROTOCOL Protocol applied, e.g. TCP,UDP,ICMP CIDR IP address range in CIDR format, e.g., 129.79.0.0/16 Description: security_group command provides list/add/delete security_groups for a tenant of a cloud, as well as list/add/delete of rules for a security group from a specified cloud and tenant. Examples: secgroup list secgroup list --cloud=kilo secgroup add my_new_group webapp 8080 8080 tcp 0.0.0.0/0 seggroup delete my_group my_rule secgroup delete my_unused_group --cloud=kilo secgroup upload --cloud=kilo Description: Security groups are first assembled in a local database. Once they are defined they can be added to the clouds. secgroup list [--format=FORMAT] lists all security groups and rules in the database secgroup list GROUP [--format=FORMAT] lists a given security group and its rules defined locally in the database secgroup list --cloud=CLOUD [--format=FORMAT] lists the security groups and rules on the specified clouds. secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR adds a security rule with the given group and the details of the security ruls secgroup delete GROUP [--cloud=CLOUD] Deletes a security group from the local database. To make the change on the remote cloud, using the 'upload' command afterwards. If the --cloud parameter is specified, the change would be made directly on the specified cloud secgroup delete GROUP RULE deletes the given rule from the group. To make this change on the remote cloud, using 'upload' command. secgroup upload [GROUP] [--cloud=CLOUD...] uploads a given group to the given cloud. If the cloud is not specified the default cloud is used. If the parameter for cloud is "all" the rules and groups will be uploaded to all active clouds. This will synchronize the changes (add/delete on security groups, rules) made locally to the remote cloud(s). """ arg = dotdict(arguments) if arguments["--cloud"] is not None: is_cloud = True arg.cloud = arguments["--cloud"] or Default.cloud else: is_cloud = False arg.FORMAT = arguments["--format"] or 'table' # list all security-groups in cloud if arguments["list"]: if not is_cloud: if arg.RULE is None: print( SecGroup.list(group=arg.GROUP, name=arg.RULE, output=arg.FORMAT)) else: print(SecGroup.list(group=arg.GROUP, output=arg.FORMAT)) else: print(SecGroup.list(category=arg.cloud, output=arg.FORMAT)) elif arguments["add"]: try: SecGroup.add_rule_to_db(name=arg.RULE, group=arg.GROUP, from_port=arg.FROMPORT, to_port=arg.TOPORT, protocol=arg.PROTOCOL, cidr=arg.CIDR) except: Console.error("Problem adding security group to db") # Delete a security-group elif arguments["delete"]: if arg["RULE"] is not None: SecGroup.delete_rule_from_db(group=arg["GROUP"], name=arg["RULE"]) else: self._delete(arg) elif arguments["upload"]: # rewrite the _delete # # upload does not implicitly deleting a secgroup anymore # instead, it will check and update the rules only # self._delete(arg) SecGroup.upload(cloud=arg.cloud, group=arg.GROUP) return ""
def do_secgroup(self, args, arguments): """ :: Usage: secgroup list secgroup list --cloud=CLOUD [--format=FORMAT] secgroup list GROUP [RULE] [--format=FORMAT] secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR secgroup delete GROUP [--cloud=CLOUD] secgroup upload [GROUP] [--cloud=CLOUD] Options: --cloud=CLOUD Name of the IaaS cloud e.g. kilo, chameleoon. The clouds are defined in the yaml file. If the name "all" is used for the cloud all clouds will be selected. Arguments: RULE The security group rule name GROUP The label/name of the security group FROMPORT Staring port of the rule, e.g. 22 TOPORT Ending port of the rule, e.g. 22 PROTOCOL Protocol applied, e.g. TCP,UDP,ICMP CIDR IP address range in CIDR format, e.g., 129.79.0.0/16 Description: security_group command provides list/add/delete security_groups for a tenant of a cloud, as well as list/add/delete of rules for a security group from a specified cloud and tenant. Examples: secgroup list --cloud india secgroup rules-list --cloud=kilo default secgroup create --cloud=kilo webservice secgroup rules-add --cloud=kilo webservice 8080 8088 TCP 129.79.0.0/16 secgroup rules-delete --cloud=kilo webservice 8080 8088 TCP 129.79.0.0/16 secgroup rules-delete --all Description: Security groups are first assembled in a local database. Once they are defined they can be added to the clouds. secgroup list lists all security groups and rules in the database secgroup list --cloud=CLOUD... [--format=FORMAT] lists the security groups and rules on the specified clouds. secgroup list GROUP [RULE] [--format=FORMAT] lists a given security group. If in addition the RULE is specified it only lists the RULE secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR adds a security rule with the given group and teh details of the security ruls secgroup delete GROUP deletes all security rules related to the specified group secgroup delete GROUP RULE deletes just the given rule from the group secgroup upload [GROUP] [--cloud=CLOUD...] uploads a given group to the given cloud. if the cloud is not specified the default cloud is used. If the parameter for cloud is "all" the rules and groups will be uploaded to all active clouds. Example: cm secgroup list cm secgroup list --cloud=kilo cm secgroup add cm-gregor-default web 80 80 tcp 0.0.0.0/0 cm secgroup add cm-gregor-default ssh 22 22 tcp 0.0.0.0/0 cm secgroup upload --cloud=kilo """ arg = dotdict(arguments) if arguments["--cloud"] is not None: is_cloud = True arg.cloud = arguments["--cloud"] or Default.cloud else: is_cloud = False arg.FORMAT = arguments["--format"] or 'table' # list all security-groups in cloud if arguments["list"]: if not is_cloud: if arg.RULE is None: print(SecGroup.list(group=arg.GROUP, name=arg.RULE, output=arg.FORMAT)) else: print(SecGroup.list(group=arg.GROUP, output=arg.FORMAT)) else: print(SecGroup.list(category=arg.cloud, output=arg.FORMAT)) elif arguments["add"]: try: SecGroup.add_rule_to_db( name=arg.RULE, group=arg.GROUP, from_port=arg.FROMPORT, to_port=arg.TOPORT, protocol=arg.PROTOCOL, cidr=arg.CIDR) except: Console.error("Problem adding security group to db") # Delete a security-group elif arguments["delete"]: # if no arguments read default # If default not set, terminate if arg.cloud is None: SecGroup.delete(group=arg.GROUP) else: result = SecGroup.delete_secgroup(name=arg.GROUP, cloud=arg.cloud) if result is not None: Console.ok("Security Group={GROUP} in cloud={cloud} deleted successfully." .format(**arg)) else: Console.error("Failed to delete Security Group={GROUP} in cloud={cloud}" .format(**arg)) elif arguments["upload"]: SecGroup.upload(cloud=arg.cloud, group=arg.GROUP) return ""
def __init__(self, context): cmd.Cmd.__init__(self) self.variables = {} self.command_topics = {} self.register_topics() self.context = context # TODO get loglevel from DB or yaml file, if not defined set to ERROR self.loglevel = "DEBUG" self._hist = [] if self.context.debug: print("init CloudmeshConsole") self.prompt = 'cm> ' self.doc_header = "Documented commands (type help <command>):" self.banner = textwrap.dedent(""" +=======================================================+ . ____ _ _ _ . . / ___| | ___ _ _ __| |_ __ ___ ___ ___| |__ . . | | | |/ _ \| | | |/ _` | '_ ` _ \ / _ \/ __| '_ \ . . | |___| | (_) | |_| | (_| | | | | | | __/\__ \ | | | . . \____|_|\___/ \__,_|\__,_|_| |_| |_|\___||___/_| |_| . +=======================================================+ Cloudmesh Shell """) # KeyCommands.__init__(self, context) # # set default cloud and default group if they do not exist # use the first cloud in cloudmesh.yaml as default # Console.set_debug(Default.debug) filename = path_expand("~/.cloudmesh/cloudmesh.yaml") # moved to import cloudmesh_client # create_cloudmesh_yaml(filename) setup_yaml() # Initialize Logging # LogUtil.initialize_logging() # sys,exit(1) # ################## # DEFAULTS # # # SET DEFAULT CLOUD # value = Default.get(name='cloud', category='general') if value is None: config = ConfigDict(filename=filename)["cloudmesh"] if 'active' in config: cloud = config["active"][0] else: clouds = config["clouds"] cloud = list(clouds.keys())[0] Default.set('cloud', cloud, category='general') # # NOT SURE WHAT THIS IS FOR # value = Default.get(name='default', category='general') if value is None: Default.set('default', 'default', category='general') # # SET DEFAULT CLUSTER # ''' cluster = ConfigDict(filename="cloudmesh.yaml")["cloudmesh"]["active"][0] value = Default.get(name='cluster', category='general') if value is None: try: hosts = ssh_config().names() if hosts is not None: cluster = hosts[0] except: pass # use the hardcoded cluster else: cluster = value Default.set('cluster', cluster, category='general') ''' # # SET DEFAULT GROUP # group = Default.group if group is None: Default.set_group("default") # # LOAD DEFAULTS FROM YAML # Default.load("cloudmesh.yaml") try: d = Key.get_from_dir("~/.ssh", store=False) except Exception as e: Console.error(e.message) # # SET DEFAULT TIMER # on = Default.timer # # SET DEFUALT SECGROUP # # # SET DEFAULT REFRESH # # r = Default.refresh # print ("REFRESH", r) # if r is None: # Default.set_refresh("on") # # SET DEFAULT USER # user = Default.user if user is None: user = ConfigDict(filename=filename)["cloudmesh"]["profile"]["user"] Default.set_user(user) r = Default.secgroup if r is None: secgroup = "{}-default".format(Default.user) Default.set_secgroup(secgroup) SecGroup.add_rule_to_db(group=secgroup, name="ssh",from_port="22",to_port="22",protocol="tcp", cidr="0.0.0.0/0") SecGroup.add_rule_to_db(group=secgroup, name="http",from_port="80",to_port="80",protocol="tcp", cidr="0.0.0.0/0") SecGroup.add_rule_to_db(group=secgroup, name="https", from_port="443", to_port="443", protocol="tcp", cidr="0.0.0.0/0") """ try: sshm = SSHKeyManager() m = sshm.get_from_yaml( load_order="~/.cloudmesh/cloudmesh.yaml") d = dict(m.__keys__) sshdb = SSHKeyDBManager() for keyname in m.__keys__: filename = m[keyname]["path"] try: sshdb.add(filename, keyname, source="yaml", uri="file://" + filename) except Exception as e: pass except Exception as e: Console.error("Problem adding keys from yaml file") """ for c in CloudmeshConsole.__bases__[1:]: # noinspection PyArgumentList c.__init__(self, context)
def do_secgroup(self, args, arguments): """ :: Usage: secgroup list secgroup list --cloud=CLOUD [--format=FORMAT] secgroup list GROUP [RULE] [--format=FORMAT] secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR secgroup delete GROUP [--cloud=CLOUD] secgroup upload [GROUP] [--cloud=CLOUD] Options: --cloud=CLOUD Name of the IaaS cloud e.g. kilo, chameleoon. The clouds are defined in the yaml file. If the name "all" is used for the cloud all clouds will be selected. Arguments: RULE The security group rule name GROUP The label/name of the security group FROMPORT Staring port of the rule, e.g. 22 TOPORT Ending port of the rule, e.g. 22 PROTOCOL Protocol applied, e.g. TCP,UDP,ICMP CIDR IP address range in CIDR format, e.g., 129.79.0.0/16 Description: security_group command provides list/add/delete security_groups for a tenant of a cloud, as well as list/add/delete of rules for a security group from a specified cloud and tenant. Examples: secgroup list --cloud india secgroup rules-list --cloud=kilo default secgroup create --cloud=kilo webservice secgroup rules-add --cloud=kilo webservice 8080 8088 TCP 129.79.0.0/16 secgroup rules-delete --cloud=kilo webservice 8080 8088 TCP 129.79.0.0/16 secgroup rules-delete --all Description: Security groups are first assembled in a local database. Once they are defined they can be added to the clouds. secgroup list lists all security groups and rules in the database secgroup list --cloud=CLOUD... [--format=FORMAT] lists the security groups and rules on the specified clouds. secgroup list GROUP [RULE] [--format=FORMAT] lists a given security group. If in addition the RULE is specified it only lists the RULE secgroup add GROUP RULE FROMPORT TOPORT PROTOCOL CIDR adds a security rule with the given group and teh details of the security ruls secgroup delete GROUP deletes all security rules related to the specified group secgroup delete GROUP RULE deletes just the given rule from the group secgroup upload [GROUP] [--cloud=CLOUD...] uploads a given group to the given cloud. if the cloud is not specified the default cloud is used. If the parameter for cloud is "all" the rules and groups will be uploaded to all active clouds. Example: cm secgroup list cm secgroup list --cloud=kilo cm secgroup add cm-gregor-default web 80 80 tcp 0.0.0.0/0 cm secgroup add cm-gregor-default ssh 22 22 tcp 0.0.0.0/0 cm secgroup upload --cloud=kilo """ arg = dotdict(arguments) if arguments["--cloud"] is not None: is_cloud = True arg.cloud = arguments["--cloud"] or Default.cloud else: is_cloud = False arg.FORMAT = arguments["--format"] or 'table' # list all security-groups in cloud if arguments["list"]: if not is_cloud: if arg.RULE is None: print( SecGroup.list(group=arg.GROUP, name=arg.RULE, output=arg.FORMAT)) else: print(SecGroup.list(group=arg.GROUP, output=arg.FORMAT)) else: print(SecGroup.list(category=arg.cloud, output=arg.FORMAT)) elif arguments["add"]: try: SecGroup.add_rule_to_db(name=arg.RULE, group=arg.GROUP, from_port=arg.FROMPORT, to_port=arg.TOPORT, protocol=arg.PROTOCOL, cidr=arg.CIDR) except: Console.error("Problem adding security group to db") # Delete a security-group elif arguments["delete"]: self._delete(arg) elif arguments["upload"]: # rewrite the _delete # # upload does not implicitly deleting a secgroup anymore # instead, it will check and update the rules only # self._delete(arg) SecGroup.upload(cloud=arg.cloud, group=arg.GROUP) return ""