def post(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) self.form = MassDeploymentForm(self.request.POST) if self.form.is_valid(): create_deployments( self.form.cleaned_data['csv'], self.request.user, mass_deployment, ) mass_deployment.generated_csv = self.form.data['csv'].strip() mass_deployment.is_done = True mass_deployment.save() messages.success(self.request, "Deployment initiated.") return HttpResponseRedirect('/') messages.error(self.request, "Please correct the errors.") return super(MassDeployment, self).get(*args, **kwargs)
def get(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) reserved_hostnames = [] reserved_ip_addresses = [] new_csv_rows = [] csv_rows = UnicodeReader(cStringIO.StringIO( mass_deployment.csv.strip() )) for raw_cols in csv_rows: cols = [] for col in raw_cols: cols.append(" %s " % col.strip()) hostname = "" ip = "" rack = None device = _find_device(cols[0]) try: network, ip = _find_network_ip( cols[2].strip(), reserved_ip_addresses, device, ) except Network.DoesNotExist: pass else: hostname = _find_hostname( network, reserved_hostnames, device, ip, ) for rack in network.racks.filter( deleted=False ).order_by('name')[:1]: break cols[2] = " %s " % network.name cols.insert(0, " %s " % rack.sn if rack else " ") cols.insert(0, " %s " % ip) cols.insert(0, " %s " % hostname) new_csv_rows.append(cols) if device: self.actions.append(( 'warning', "An old device %s will be re-used. Make sure it's not " "used in production anymore!" % device, )) if device.deleted: self.actions.append(( 'info', "Device %s will be undeleted." % device, )) if device.ipaddress_set.exists(): self.actions.append(( 'info', "All DNS entries for IP addresses [%s] will be " "deleted." % ', '.join( ip.address for ip in device.ipaddress_set.all() ), )) self.actions.append(( 'info', "All DHCP entries for IP addresses [%s] " "will be deleted." % ( ', '.join( ip.address for ip in device.ipaddress_set.all() ), ), )) if device.ethernet_set.exists(): self.actions.append(( 'info', "All DHCP entries for " "MAC addresses [%s] will be deleted." % ( ', '.join( eth.mac for eth in device.ethernet_set.all() ), ), )) if device.disksharemount_set.exists(): self.actions.append(( 'info', "All disk shares mounted on %s will be disconnected " "from it." % device, )) self.actions.append(( 'info', "The uptime, operating system and software list for %s " "will be reset." % device, )) else: if hostname: self.actions.append(( 'success', "A new device %s will be created." % hostname, )) if hostname and ip: self.actions.append(( 'info', "An A DNS entry for %s and %s will be created." % (hostname, ip), )) self.actions.append(( 'info', "A PTR DNS entry for %s and %s will be created." % (hostname, ip), )) if cols[0].strip() and ip: self.actions.append(( 'info', "A DHCP entry for %s and %s will be created." % (cols[3], ip), )) csv_string = cStringIO.StringIO() UnicodeWriter(csv_string).writerows(new_csv_rows) self.form = MassDeploymentForm( initial={'csv': csv_string.getvalue()} ) return super(MassDeployment, self).get(*args, **kwargs)
class MassDeployment(Base): template_name = 'ui/mass_deploy.html' def __init__(self, *args, **kwargs): super(MassDeployment, self).__init__(*args, **kwargs) self.actions = [] def get_context_data(self, *args, **kwargs): ret = super(MassDeployment, self).get_context_data(*args, **kwargs) ret.update({ 'form': self.form, 'action_name': 'Deploy', 'template_menu_items': TEMPLATE_MENU_ITEMS, 'template_selected': 'servers', 'actions': self.actions, }) return ret def get(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) reserved_hostnames = [] reserved_ip_addresses = [] new_csv_rows = [] csv_rows = UnicodeReader(cStringIO.StringIO( mass_deployment.csv.strip() )) for raw_cols in csv_rows: cols = [] for col in raw_cols: cols.append(" %s " % col.strip()) hostname = "" ip = "" rack = None device = _find_device(cols[0]) try: network, ip = _find_network_ip( cols[2].strip(), reserved_ip_addresses, device, ) except Network.DoesNotExist: pass else: hostname = _find_hostname( network, reserved_hostnames, device, ip, ) for rack in network.racks.filter( deleted=False ).order_by('name')[:1]: break cols[2] = " %s " % network.name cols.insert(0, " %s " % rack.sn if rack else " ") cols.insert(0, " %s " % ip) cols.insert(0, " %s " % hostname) new_csv_rows.append(cols) if device: self.actions.append(( 'warning', "An old device %s will be re-used. Make sure it's not " "used in production anymore!" % device, )) if device.deleted: self.actions.append(( 'info', "Device %s will be undeleted." % device, )) if device.ipaddress_set.exists(): self.actions.append(( 'info', "All DNS entries for IP addresses [%s] will be " "deleted." % ', '.join( ip.address for ip in device.ipaddress_set.all() ), )) self.actions.append(( 'info', "All DHCP entries for IP addresses [%s] " "will be deleted." % ( ', '.join( ip.address for ip in device.ipaddress_set.all() ), ), )) if device.ethernet_set.exists(): self.actions.append(( 'info', "All DHCP entries for " "MAC addresses [%s] will be deleted." % ( ', '.join( eth.mac for eth in device.ethernet_set.all() ), ), )) if device.disksharemount_set.exists(): self.actions.append(( 'info', "All disk shares mounted on %s will be disconnected " "from it." % device, )) self.actions.append(( 'info', "The uptime, operating system and software list for %s " "will be reset." % device, )) else: if hostname: self.actions.append(( 'success', "A new device %s will be created." % hostname, )) if hostname and ip: self.actions.append(( 'info', "An A DNS entry for %s and %s will be created." % (hostname, ip), )) self.actions.append(( 'info', "A PTR DNS entry for %s and %s will be created." % (hostname, ip), )) if cols[0].strip() and ip: self.actions.append(( 'info', "A DHCP entry for %s and %s will be created." % (cols[3], ip), )) csv_string = cStringIO.StringIO() UnicodeWriter(csv_string).writerows(new_csv_rows) self.form = MassDeploymentForm( initial={'csv': csv_string.getvalue()} ) return super(MassDeployment, self).get(*args, **kwargs) def post(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) self.form = MassDeploymentForm(self.request.POST) if self.form.is_valid(): create_deployments( self.form.cleaned_data['csv'], self.request.user, mass_deployment, ) mass_deployment.generated_csv = self.form.data['csv'].strip() mass_deployment.is_done = True mass_deployment.save() messages.success(self.request, "Deployment initiated.") return HttpResponseRedirect('/') messages.error(self.request, "Please correct the errors.") return super(MassDeployment, self).get(*args, **kwargs)
def get(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) reserved_hostnames = [] reserved_ip_addresses = [] new_csv_rows = [] csv_rows = UnicodeReader( cStringIO.StringIO(mass_deployment.csv.strip())) for raw_cols in csv_rows: cols = [] for col in raw_cols: cols.append(" %s " % col.strip()) hostname = "" ip = "" rack = None asset_identity = None if self.assets_enabled and len(cols) == 7: asset_identity = cols[6].strip() device = _find_device(cols[0], asset_identity) try: network, ip = _find_network_ip( cols[2].strip(), reserved_ip_addresses, device, ) except Network.DoesNotExist: pass else: hostname = _find_hostname( network, reserved_hostnames, device, ip, ) for rack in network.racks.filter( deleted=False).order_by('name')[:1]: break cols[2] = " %s " % network.name cols.insert(0, " %s " % rack.sn if rack else " ") cols.insert(0, " %s " % ip) cols.insert(0, " %s " % hostname) new_csv_rows.append(cols) if device: self.actions.append(( 'warning', "An old device %s will be re-used. Make sure it's not " "used in production anymore!" % device, )) if device.deleted: self.actions.append(( 'info', "Device %s will be undeleted." % device, )) if device.ipaddress_set.exists(): self.actions.append(( 'info', "All DNS entries for IP addresses [%s] will be " "deleted." % ', '.join(ip.address for ip in device.ipaddress_set.all()), )) self.actions.append(( 'info', "All DHCP entries for IP addresses [%s] " "will be deleted." % (', '.join(ip.address for ip in device.ipaddress_set.all()), ), )) if device.ethernet_set.exists(): self.actions.append(( 'info', "All DHCP entries for " "MAC addresses [%s] will be deleted." % (', '.join(eth.mac for eth in device.ethernet_set.all()), ), )) if device.disksharemount_set.exists(): self.actions.append(( 'info', "All disk shares mounted on %s will be disconnected " "from it." % device, )) self.actions.append(( 'info', "The uptime, operating system and software list for %s " "will be reset." % device, )) else: if hostname: self.actions.append(( 'success', "A new device %s will be created." % hostname, )) if hostname and ip: self.actions.append(( 'info', "An A DNS entry for %s and %s will be created." % (hostname, ip), )) self.actions.append(( 'info', "A PTR DNS entry for %s and %s will be created." % (hostname, ip), )) if cols[0].strip() and ip: self.actions.append(( 'info', "A DHCP entry for %s and %s will be created." % (cols[3], ip), )) csv_string = cStringIO.StringIO() UnicodeWriter(csv_string).writerows(new_csv_rows) self.form = MassDeploymentForm(initial={'csv': csv_string.getvalue()}) return super(MassDeployment, self).get(*args, **kwargs)
class MassDeployment(Base): submodule_name = 'racks' template_name = 'ui/mass_deploy.html' assets_enabled = 'ralph_assets' in settings.INSTALLED_APPS def __init__(self, *args, **kwargs): super(MassDeployment, self).__init__(*args, **kwargs) self.actions = [] def get_context_data(self, *args, **kwargs): ret = super(MassDeployment, self).get_context_data(*args, **kwargs) ret.update({ 'form': self.form, 'action_name': 'Deploy', 'template_menu_items': TEMPLATE_MENU_ITEMS, 'template_selected': 'servers', 'actions': self.actions, }) return ret def get(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) reserved_hostnames = [] reserved_ip_addresses = [] new_csv_rows = [] csv_rows = UnicodeReader( cStringIO.StringIO(mass_deployment.csv.strip())) for raw_cols in csv_rows: cols = [] for col in raw_cols: cols.append(" %s " % col.strip()) hostname = "" ip = "" rack = None asset_identity = None if self.assets_enabled and len(cols) == 7: asset_identity = cols[6].strip() device = _find_device(cols[0], asset_identity) try: network, ip = _find_network_ip( cols[2].strip(), reserved_ip_addresses, device, ) except Network.DoesNotExist: pass else: hostname = _find_hostname( network, reserved_hostnames, device, ip, ) for rack in network.racks.filter( deleted=False).order_by('name')[:1]: break cols[2] = " %s " % network.name cols.insert(0, " %s " % rack.sn if rack else " ") cols.insert(0, " %s " % ip) cols.insert(0, " %s " % hostname) new_csv_rows.append(cols) if device: self.actions.append(( 'warning', "An old device %s will be re-used. Make sure it's not " "used in production anymore!" % device, )) if device.deleted: self.actions.append(( 'info', "Device %s will be undeleted." % device, )) if device.ipaddress_set.exists(): self.actions.append(( 'info', "All DNS entries for IP addresses [%s] will be " "deleted." % ', '.join(ip.address for ip in device.ipaddress_set.all()), )) self.actions.append(( 'info', "All DHCP entries for IP addresses [%s] " "will be deleted." % (', '.join(ip.address for ip in device.ipaddress_set.all()), ), )) if device.ethernet_set.exists(): self.actions.append(( 'info', "All DHCP entries for " "MAC addresses [%s] will be deleted." % (', '.join(eth.mac for eth in device.ethernet_set.all()), ), )) if device.disksharemount_set.exists(): self.actions.append(( 'info', "All disk shares mounted on %s will be disconnected " "from it." % device, )) self.actions.append(( 'info', "The uptime, operating system and software list for %s " "will be reset." % device, )) else: if hostname: self.actions.append(( 'success', "A new device %s will be created." % hostname, )) if hostname and ip: self.actions.append(( 'info', "An A DNS entry for %s and %s will be created." % (hostname, ip), )) self.actions.append(( 'info', "A PTR DNS entry for %s and %s will be created." % (hostname, ip), )) if cols[0].strip() and ip: self.actions.append(( 'info', "A DHCP entry for %s and %s will be created." % (cols[3], ip), )) csv_string = cStringIO.StringIO() UnicodeWriter(csv_string).writerows(new_csv_rows) self.form = MassDeploymentForm(initial={'csv': csv_string.getvalue()}) return super(MassDeployment, self).get(*args, **kwargs) def post(self, *args, **kwargs): mass_deployment = get_object_or_404( MassDeploymentModel, id=kwargs.get('deployment'), is_done=False, ) self.form = MassDeploymentForm(self.request.POST) if self.form.is_valid(): create_deployments( self.form.cleaned_data['csv'], self.request.user, mass_deployment, ) mass_deployment.generated_csv = self.form.data['csv'].strip() mass_deployment.is_done = True mass_deployment.save() messages.success(self.request, "Deployment initiated.") return HttpResponseRedirect('/') messages.error(self.request, "Please correct the errors.") return super(MassDeployment, self).get(*args, **kwargs)