class Shell(object): def __init__(self): super(Shell, self).__init__() self.params = self.get_params() self.manager = Manager() def execute(self): self.commands.get(self.params.command)(self) def do_list(self): print self.manager.environment_list().values('name') def node_dict(self, node): return { 'name': node.name, 'vnc': node.get_vnc_port(), } def do_show(self): environment = self.manager.environment_get(self.params.name) print { 'name': environment.name, 'nodes': map(lambda x: {'node': self.node_dict(x)}, environment.nodes) } def do_erase(self): self.manager.environment_get(self.params.name).erase() def do_start(self): self.manager.environment_get(self.params.name).start() def do_destroy(self): self.manager.environment_get(self.params.name).destroy(verbose=False) def do_suspend(self): self.manager.environment_get(self.params.name).suspend() def do_resume(self): self.manager.environment_get(self.params.name).resume() def do_revert(self): self.manager.environment_get(self.params.name).revert( self.params.snapshot_name) def do_snapshot(self): self.manager.environment_get(self.params.name).snapshot( self.params.snapshot_name) commands = { 'list': do_list, 'show': do_show, 'erase': do_erase, 'start': do_start, 'destroy': do_destroy, 'suspend': do_suspend, 'resume': do_resume, 'revert': do_revert, 'snapshot': do_snapshot } def get_params(self): name_parser = argparse.ArgumentParser(add_help=False) name_parser.add_argument('name', help='environment name', default=os.getenv('ENV_NAME')) snapshot_name_parser = argparse.ArgumentParser(add_help=False) snapshot_name_parser.add_argument('--snapshot-name', help='snapshot name', default=os.getenv('SNAPSHOT_NAME')) parser = argparse.ArgumentParser( description="Manage virtual environments") subparsers = parser.add_subparsers(help='commands', dest='command') subparsers.add_parser('list') subparsers.add_parser('show', parents=[name_parser]) subparsers.add_parser('erase', parents=[name_parser]) subparsers.add_parser('start', parents=[name_parser]) subparsers.add_parser('destroy', parents=[name_parser]) subparsers.add_parser('suspend', parents=[name_parser]) subparsers.add_parser('resume', parents=[name_parser]) subparsers.add_parser('revert', parents=[name_parser, snapshot_name_parser]) subparsers.add_parser('snapshot', parents=[name_parser, snapshot_name_parser]) return parser.parse_args()
class Shell(object): def __init__(self): super(Shell, self).__init__() self.params = self.get_params() self.manager = Manager() def execute(self): self.commands.get(self.params.command)(self) def do_list(self): env_list = self.manager.environment_list().values('name') for env in env_list: print env['name'] return env_list def node_dict(self, node): return {'name': node.name, 'vnc': node.get_vnc_port()} def do_show(self): environment = self.manager.environment_get(self.params.name) print '%5s %25s' % ("VNC", "NODE-NAME") for item in map(lambda x: self.node_dict(x), environment.nodes): print '%5s %25s' % (item['vnc'], item['name']) def do_erase(self): self.manager.environment_get(self.params.name).erase() def do_start(self): self.manager.environment_get(self.params.name).start() def do_destroy(self): self.manager.environment_get(self.params.name).destroy(verbose=False) def do_suspend(self): self.manager.environment_get(self.params.name).suspend(verbose=False) def do_resume(self): self.manager.environment_get(self.params.name).resume(verbose=False) def do_revert(self): self.manager.environment_get(self.params.name).revert( self.params.snapshot_name) def do_snapshot(self): self.manager.environment_get(self.params.name).snapshot( self.params.snapshot_name) def do_synchronize(self): self.manager.synchronize_environments() commands = { 'list': do_list, 'show': do_show, 'erase': do_erase, 'start': do_start, 'destroy': do_destroy, 'suspend': do_suspend, 'resume': do_resume, 'revert': do_revert, 'snapshot': do_snapshot, 'sync': do_synchronize } def get_params(self): name_parser = argparse.ArgumentParser(add_help=False) name_parser.add_argument('name', help='environment name', default=environ.get('ENV_NAME')) snapshot_name_parser = argparse.ArgumentParser(add_help=False) snapshot_name_parser.add_argument('--snapshot-name', help='snapshot name', default=environ.get('SNAPSHOT_NAME')) parser = argparse.ArgumentParser( description="Manage virtual environments") subparsers = parser.add_subparsers(help='commands', dest='command') subparsers.add_parser('list') subparsers.add_parser('show', parents=[name_parser]) subparsers.add_parser('erase', parents=[name_parser]) subparsers.add_parser('start', parents=[name_parser]) subparsers.add_parser('destroy', parents=[name_parser]) subparsers.add_parser('suspend', parents=[name_parser]) subparsers.add_parser('resume', parents=[name_parser]) subparsers.add_parser('revert', parents=[name_parser, snapshot_name_parser]) subparsers.add_parser('snapshot', parents=[name_parser, snapshot_name_parser]) subparsers.add_parser('sync') return parser.parse_args()
class Shell(object): def __init__(self): super(Shell, self).__init__() self.params = self.get_params() self.manager = Manager() def execute(self): self.commands.get(self.params.command)(self) def do_list(self): print self.manager.environment_list().values('name') def node_dict(self, node): return {'name': node.name, 'vnc': node.get_vnc_port(), } def do_show(self): environment = self.manager.environment_get(self.params.name) print { 'name': environment.name, 'nodes': map(lambda x: {'node': self.node_dict(x)}, environment.nodes) } def do_erase(self): self.manager.environment_get(self.params.name).erase() def do_start(self): self.manager.environment_get(self.params.name).start() def do_destroy(self): self.manager.environment_get(self.params.name).destroy(verbose=False) def do_suspend(self): self.manager.environment_get(self.params.name).suspend() def do_resume(self): self.manager.environment_get(self.params.name).resume() def do_revert(self): self.manager.environment_get(self.params.name).revert( self.params.snapshot_name) def do_snapshot(self): self.manager.environment_get(self.params.name).snapshot( self.params.snapshot_name) commands = { 'list': do_list, 'show': do_show, 'erase': do_erase, 'start': do_start, 'destroy': do_destroy, 'suspend': do_suspend, 'resume': do_resume, 'revert': do_revert, 'snapshot': do_snapshot } def get_params(self): name_parser = argparse.ArgumentParser(add_help=False) name_parser.add_argument('name', help='environment name', default=os.getenv('ENV_NAME')) snapshot_name_parser = argparse.ArgumentParser(add_help=False) snapshot_name_parser.add_argument('--snapshot-name', help='snapshot name', default=os.getenv('SNAPSHOT_NAME')) parser = argparse.ArgumentParser( description="Manage virtual environments") subparsers = parser.add_subparsers(help='commands', dest='command') subparsers.add_parser('list') subparsers.add_parser('show', parents=[name_parser]) subparsers.add_parser('erase', parents=[name_parser]) subparsers.add_parser('start', parents=[name_parser]) subparsers.add_parser('destroy', parents=[name_parser]) subparsers.add_parser('suspend', parents=[name_parser]) subparsers.add_parser('resume', parents=[name_parser]) subparsers.add_parser('revert', parents=[name_parser, snapshot_name_parser]) subparsers.add_parser('snapshot', parents=[name_parser, snapshot_name_parser]) return parser.parse_args()
class Shell(object): def __init__(self): super(Shell, self).__init__() self.params = self.get_params() self.manager = Manager() def execute(self): self.commands.get(self.params.command)(self) def do_list(self): env_list = self.manager.environment_list().values('name') for env in env_list: print(env['name']) return env_list def node_dict(self, node): return {'name': node.name, 'vnc': node.get_vnc_port()} def do_show(self): environment = self.manager.environment_get(self.params.name) print('%5s %25s' % ("VNC", "NODE-NAME")) for item in map(lambda x: self.node_dict(x), environment.nodes): print ('%5s %25s' % (item['vnc'], item['name'])) def do_erase(self): self.manager.environment_get(self.params.name).erase() def do_start(self): self.manager.environment_get(self.params.name).start() def do_destroy(self): self.manager.environment_get(self.params.name).destroy(verbose=False) def do_suspend(self): self.manager.environment_get(self.params.name).suspend(verbose=False) def do_resume(self): self.manager.environment_get(self.params.name).resume(verbose=False) def do_revert(self): self.manager.environment_get(self.params.name).revert( self.params.snapshot_name) def do_snapshot(self): self.manager.environment_get(self.params.name).snapshot( self.params.snapshot_name) def do_synchronize(self): self.manager.synchronize_environments() def do_snapshot_list(self): environment = self.manager.environment_get(self.params.name) snap_nodes = {} max_len = 0 for node in environment.nodes: snaps = sorted(node.get_snapshots()) for snap in snaps: if len(snap) > max_len: max_len = len(snap) if snap in snap_nodes: snap_nodes[snap].append(node.name) else: snap_nodes[snap] = [node.name, ] print("%*s %50s" % (max_len, "SNAPSHOT", "NODES-NAME")) for snap in snap_nodes: print("%*s %50s" % (max_len, snap, ', '.join(snap_nodes[snap]))) def do_snapshot_delete(self): environment = self.manager.environment_get(self.params.name) for node in environment.nodes: snaps = sorted(node.get_snapshots()) if self.params.snapshot_name in snaps: node.erase_snapshot(name=self.params.snapshot_name) def do_net_list(self): environment = self.manager.environment_get(self.params.name) networks = environment.networks print("%15s %10s" % ("NETWORK NAME", "IP NET")) for network in networks: print("%15s %10s" % (network.name, network.ip_network)) def do_timesync(self): env = self.manager.environment_get(self.params.name) if not self.params.node_name: _nodes = {node.name: node.get_vnc_port() for node in env.nodes} for node_name in sorted(_nodes.keys()): if _nodes[node_name] != '-1': sync_node_time(env, node_name) else: sync_node_time(env, self.params.node_name) def do_revert_resume(self): self.manager.environment_get(self.params.name).revert( self.params.snapshot_name) self.manager.environment_get(self.params.name).resume(verbose=False) if not self.params.no_timesync: print('time synchronization is starting') self.do_timesync() commands = { 'list': do_list, 'show': do_show, 'erase': do_erase, 'start': do_start, 'destroy': do_destroy, 'suspend': do_suspend, 'resume': do_resume, 'revert': do_revert, 'snapshot': do_snapshot, 'sync': do_synchronize, 'snapshot-list': do_snapshot_list, 'snapshot-delete': do_snapshot_delete, 'net-list': do_net_list, 'time-sync': do_timesync, 'revert-resume': do_revert_resume } def get_params(self): name_parser = argparse.ArgumentParser(add_help=False) name_parser.add_argument('name', help='environment name', default=environ.get('ENV_NAME'), metavar='ENV_NAME') snapshot_name_parser = argparse.ArgumentParser(add_help=False) snapshot_name_parser.add_argument('--snapshot-name', help='snapshot name', default=environ.get('SNAPSHOT_NAME')) node_name_parser = argparse.ArgumentParser(add_help=False) node_name_parser.add_argument('--node-name', help='node name', default=None) no_timesync_parser = argparse.ArgumentParser(add_help=False) no_timesync_parser.add_argument('--no-timesync', dest='no_timesync', action='store_const', const=True, help='revert without timesync', default=False) parser = argparse.ArgumentParser( description="Manage virtual environments. " "For addional help use command with -h/--help") subparsers = parser.add_subparsers(title="Operation commands", help='available commands', dest='command') subparsers.add_parser('list', help="Show virtual environments", description="Show virtual environments on host") subparsers.add_parser('show', parents=[name_parser], help="Show VMs in environment", description="Show VMs in environment") subparsers.add_parser('erase', parents=[name_parser], help="Delete environment", description="Delete environment and VMs on it") subparsers.add_parser('start', parents=[name_parser], help="Start VMs", description="Start VMs in selected environment") subparsers.add_parser('destroy', parents=[name_parser], help="Destroy(stop) VMs", description="Stop VMs in selected environment") subparsers.add_parser('suspend', parents=[name_parser], help="Suspend VMs", description="Suspend VMs in selected " "environment") subparsers.add_parser('resume', parents=[name_parser], help="Resume VMs", description="Resume VMs in selected environment") subparsers.add_parser('revert', parents=[name_parser, snapshot_name_parser], help="Apply snapshot to environment", description="Apply selected snapshot to " "environment") subparsers.add_parser('snapshot', parents=[name_parser, snapshot_name_parser], help="Make environment snapshot", description="Make environment snapshot") subparsers.add_parser('sync', help="Synchronization environment and devops", description="Synchronization environment " "and devops"), subparsers.add_parser('snapshot-list', parents=[name_parser], help="Show snapshots in environment", description="Show snapshots in selected " "environment") subparsers.add_parser('snapshot-delete', parents=[name_parser, snapshot_name_parser], help="Delete snapshot from environment", description="Delete snapshot from selected " "environment") subparsers.add_parser('net-list', parents=[name_parser], help="Show networks in environment", description="Display allocated networks for " "environment") subparsers.add_parser('time-sync', parents=[name_parser, node_name_parser], help="Sync time on all env nodes", description="Sync time on all active nodes " "of environment starting from " "admin") subparsers.add_parser('revert-resume', parents=[name_parser, snapshot_name_parser, node_name_parser, no_timesync_parser], help="Revert, resume, sync time on VMs", description="Revert and resume VMs in selected" "environment, then" " sync time on VMs") return parser.parse_args()