I'm new to Pulumi and learning myself pulumi instead of terraform and I'm trying to create a simple script to provision a new virtual machine (VM) in vSphere every time I run 'pulumi up'. However, it seems that my current script updates the existing VM instead of creating a new one.
import pulumi
import pulumi_vsphere as vsphere
# Names of your resources in vSphere
datastore_name = 'roels-bart'
network_name = 'VM Network'
resource_pool_name = 'test'
datacenter_name = 'StudentDC'
template_name = 'CleanUbuntu'
# Lookup the Datacenter by name
datacenter = vsphere.get_datacenter(name=datacenter_name)
# Lookup the Datastore by name
datastore = vsphere.get_datastore(name="roels-bart", datacenter_id=datacenter.id)
# Lookup network by name
network = vsphere.get_network(name="VM Network", datacenter_id=datacenter.id)
# Default parent resource pool
pool = vsphere.get_resource_pool(name="Resources", datacenter_id=datacenter.id)
# Lookup template by name
template = vsphere.get_virtual_machine(name="CleanUbuntu", datacenter_id=datacenter.id)
# Create a when this script is runned
virtual_machine = vsphere.VirtualMachine("pulumi-vm",
name='pulumi-vm',
datastore_id=datastore.id,
resource_pool_id=pool.id,
num_cpus=1,
memory=1024,
guest_id='ubuntu64Guest',
scsi_type='pvscsi',
network_interfaces=[{
'network_id': network.id,
'adapter_type': 'vmxnet3',
}],
clone={
'template_uuid': template.id,
},
# Assing disk
disks=[
{
'label': 'disk0',
'size': 20,
'eagerly_scrub': False,
'thin_provisioned': True,
},
],
# Customise the VM's configuration
)
# Export the virtual machine's properties
pulumi.export('vm_name', virtual_machine.name)
pulumi.export('vm_id', virtual_machine.id)
Can someone help me modify this script to ensure that a new VM is created with every 'pulumi up' run, rather than updating the existing one?
Also, I'm curious if there's a recommended way to run Pulumi scripts from another location, like a Flask application. I'm thinking of a scenario where users can input parameters through a web interface to customize the VM creation process. Any suggestions or best practices for achieving this would be greatly appreciated.
Thanks in advance!
So the nature of pulumi is to declarativly and implicitly define your infrastructure. It allows you to define your entire infrastructure in a reproducible way, so when you define a server in pulumi it is going to build that exact server, when you change that exact server in the code it is going to update that exact server. That exact server is then tied to an id in pulumi so it can be referenced again.
To make more servers you will need to copy and paste that block of code for as many VMs that you want, or wrap it in a function. In my script I have a CreateVM function that takes in the parameters of the vm, and that function builds the vm. One VM per function call.
Here is a link to he create vm function on my GitHub. It is in typescript, but should not be hard to apply these concepts to python. https://github.com/agluck91/infrastructure/blob/main/createVM.ts
So what you are going to want to do is take this code block below, and put it into a function. Setup arguments to that function (I think they are kwargs in python) and then use those variable name in place of values you have set below. This is very rough, but should give you the idea.
If I misunderstood the question and your are wanting to build the exact same server each time from scratch, your best bet is to run pulumi down on it so pulumi will delete the resources, and then make your changes and run pulumi up again. That is really the only way to force it to delete the server and bring the exact same one up from scratch again. There may be something in the vsphere lib that lets your force a rebuild, but I have not come across that for proxmox.
Does this answer the question well enough to get you going?
To your flask question. I do not think it can be done directly (like calling the pulumi libs from flask) I think you will need to either patch into the cli with your flask app, or you will need to call the pulumi apis from flask.