Skip to content

Commit

Permalink
Merge pull request #3 from sandwichcloud/removedhcp
Browse files Browse the repository at this point in the history
remove dhcp requirement
  • Loading branch information
rmb938 authored Nov 4, 2017
2 parents 0220d62 + e7cbc35 commit ef6f446
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 54 deletions.
40 changes: 0 additions & 40 deletions ingredients_tasks/omapi.py

This file was deleted.

23 changes: 11 additions & 12 deletions ingredients_tasks/tasks/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from ingredients_db.models.instance import InstanceState
from ingredients_db.models.network import Network
from ingredients_db.models.network_port import NetworkPort
from ingredients_tasks.omapi import OmapiClient
from ingredients_tasks.tasks.tasks import InstanceTask
from ingredients_tasks.vmware import VMWareClient

Expand Down Expand Up @@ -43,11 +42,16 @@ def create_instance(self, **kwargs):
# calculate the next available ip address which is at most O(n) time with n being the number of
# ip addresses in the cidr
with self.database.session() as nested_session:
network_port = nested_session.query(NetworkPort).filter(NetworkPort.id == instance.network_port_id).first()
network_port: NetworkPort = nested_session.query(NetworkPort).filter(
NetworkPort.id == instance.network_port_id).first()

network = nested_session.query(Network).filter(
network: Network = nested_session.query(Network).filter(
Network.id == network_port.network_id).with_for_update().first()

dns_servers = []
for dns_server in network.dns_servers:
dns_servers.append(str(dns_server))

logger.info('Allocating IP address for instance %s' % str(instance.id))
if network_port.ip_address is not None:
# An ip address was already allocated (how?! the task should have failed) so let's reset it
Expand All @@ -65,15 +69,9 @@ def create_instance(self, **kwargs):
nested_session.commit()

logger.info('Creating backing vm for instance %s' % str(instance.id))
vmware_vm = vmware.create_vm(vm_name=str(instance.id), image=vmware_image, port_group=port_group)

nic_mac = vmware.find_vm_mac(vmware_vm)
if nic_mac is None:
raise LookupError("Could not find mac address of nic")

logger.info('Telling DHCP about our IP for instance %s' % str(instance.id))
with OmapiClient.client_session() as omapi:
omapi.add_host(str(ip_address), nic_mac)
vmware_vm = vmware.create_vm(vm_name=str(instance.id), image=vmware_image, port_group=port_group,
ip_address=str(ip_address), gateway=str(network.gateway),
subnet_mask=str(network.gateway.netmask), dns_servers=dns_servers)

logger.info('Powering on backing vm for instance %s' % str(instance.id))
vmware.power_on_vm(vmware_vm)
Expand All @@ -100,6 +98,7 @@ def delete_instance(self, delete_backing: bool, **kwargs):

instance.state = InstanceState.DELETED
self.request.session.delete(instance)
self.request.session.flush()
self.request.session.delete(network_port)


Expand Down
29 changes: 28 additions & 1 deletion ingredients_tasks/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_vm(self, vm_name):
raise LookupError("Could not find Instances folder with the name of %s" % settings.VCENTER_INSTANCES_FOLDER)
return self.get_obj_in_folder([vim.VirtualMachine], vms_folder, vm_name)

def create_vm(self, vm_name, image, port_group):
def create_vm(self, vm_name, image, port_group, ip_address, gateway, subnet_mask, dns_servers):
vms_folder = self.get_obj([vim.Folder], settings.VCENTER_INSTANCES_FOLDER)
if vms_folder is None:
raise LookupError("Could not find Instances folder with the name of %s" % settings.VCENTER_INSTANCES_FOLDER)
Expand Down Expand Up @@ -82,6 +82,33 @@ def create_vm(self, vm_name, image, port_group):
nic.device.connectable.startConnected = True
nic.device.connectable.allowGuestControl = True

globalip = vim.vm.customization.GlobalIPSettings()
globalip.dnsServerList = dns_servers

guest_map = vim.vm.customization.AdapterMapping()
guest_map.adapter = vim.vm.customization.IPSettings()
guest_map.adapter.ip = vim.vm.customization.FixedIp()
guest_map.adapter.ip.ipAddress = ip_address
guest_map.adapter.subnetMask = subnet_mask
guest_map.adapter.gateway = gateway

ident = vim.vm.customization.LinuxPrep()
ident.domain = 'sandwich.local'
ident.hostName = vim.vm.customization.FixedName()
ident.hostName.name = 'ip-' + ip_address.replace(".", "-")

customspec = vim.vm.customization.Specification()
customspec.nicSettingMap = [guest_map]
customspec.globalIPSettings = globalip
customspec.identity = ident

clonespec.customization = customspec

# TODO: guest customization with ip
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

vmconf = vim.vm.ConfigSpec()
vmconf.numCPUs = 1 # TODO: allow customization of these
vmconf.memoryMB = 1024
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
celery==4.1.0 # BSD 3-clause
pyvmomi==6.5.0.2017.5-1 # Apache 2.0
pypureomapi==0.6 # Apache 2.0
ingredients.db # MIT

0 comments on commit ef6f446

Please sign in to comment.