When I deploy a GCP compute instance from a script eg test_deploy.py using the Pulumi Automation API the instance is deployed without a hitch. When I run the exact same code from a Django POST endpoint one of two things happen. If I run the deploy against an existing stack it will delete the running instance. If I run the deploy on a new stack nothing happens. Is there some setting I am missing? What is weird this does not happen when I deploy Firewalls or VPC,s via the endpoint. Everything will work as expected. code:
########## instance_stack.py##################
class DeployVmInstance(BaseStack):
def __init__(self,
project_id,
subnet_name,
stack_name,
stack_region,
stack_zone,
instance_tags,
image_name,
disk_size=DEFAULT_DISK_SIZE,
machine_type=DEFAULT_VM_HOST_MACHINE_TYPE,
timeouts=DEFAULT_TIMEOUTS,
desired_status=DEFAULT_VM_DESIRED_STATUS):
self.subnet_name = subnet_name
self.stack_name = stack_name
self.project_id = project_id
self.project_name = f'{self.stack_name}-vm'
self.vm_name = self.project_name
self.stack_region = stack_region
self.stack_zone = stack_zone
self.machine_type = machine_type
self.instance_tags = instance_tags
self.disk_size = disk_size
self.timeouts = timeouts
self.image_name = image_name
self.desired_status = desired_status
def set_instance_status(self, status):
self.desired_status = status
def create_stack(self):
'''
Create instance
subnet = compute.get_subnetwork(name=self.subnet_name,
region=self.stack_region)
vm_instance = compute.Instance(self.vm_name,
desired_status=self.desired_status,
machine_type=self.machine_type,
advanced_machine_features=FEATURE_ARGS(enable_nested_virtualization=True), # noqa
boot_disk=BOOT_DISK_ARGS(
initialize_params=BOOT_INIT_ARGS(
image=self.image_name,
size=self.disk_size
)
),
network_interfaces=[NETWORK_ARGS(subnetwork=subnet.id)], # noqa
tags=self.instance_tags,
zone=self.stack_zone)
network_ip = vm_instance.network_interfaces[0]['network_ip']
pulumi.export('network_ip', network_ip)
pulumi.export('boot_disk', vm_instance.boot_disk.source)
pulumi.export('boot_disk_name', vm_instance.boot_disk.device_name)
def deploy_stack(self):
'''
Deploy stack
'''
stack = auto.create_or_select_stack(stack_name=self.stack_name,
project_name=self.project_name,
program=self.create_stack)
stack.set_config("gcp:project", auto.ConfigValue(self.project_id))
up_res = stack.up(on_output=print)
return up_res.outputs
############test_deploy.py(works fine)############################
from instance_stack.py import DeployVmInstance
PROJECT_ID = '***************'
DEFAULT_REGION = 'us-central1'
new_vm_tags = ['test-vm']
new_vm = DeployVmInstance(project_id=PROJECT_ID,
subnet_name='testvm-dimcorp-subnet',
stack_name='test-created-vm',
stack_region=DEFAULT_REGION,
stack_zone='us-central1-b',
instance_tags = new_vm_tags,
image_name="****/global/images/ubuntu")
new_vm.deploy_stack()
############# From Django launch view.py(does not work)############
# Bad behavior includes deleting existing stack when run
# or Running new stack with no instance being deployed.
# no errors displayed.
from instance_stack.py import DeployVmInstance
PROJECT_ID = '***************'
DEFAULT_REGION = 'us-central1'
class ImageLaunchView(APIView):
def post(self, request, pk, format=None):
new_vm_tags = ['test-vm']
new_vm = DeployVmInstance(project_id=PROJECT_ID,
subnet_name='testvm-dimcorp-subnet',
stack_name='test-created-vm',
stack_region=DEFAULT_REGION,
stack_zone='us-central1-b',
instance_tags = new_vm_tags,
image_name="****/global/images/ubuntu")
new_vm.deploy_stack()