skip to Main Content

I am playing around with Compute Engine. I have written a Python script that calls the Compute Engine API and tries to create a VM instance with an accompanying start up script. The start up script is meant to create a simple HTML file using the echo command.

The Python script is creating the VM instance successfully. However, when I SSH into the VM instance there is no trace of the HTML file.

However, if I run the echo command manually in the secure shell…

echo "<html><body><h1>Hello World</h1><p>This page was created from a startup scri
pt.</p></body></html>" > index.html

…the file is successfully created.

Why is this not working in my start up script? Is there something I need to tweak in the Python code below?

service = discovery.build('compute', 'v1', credentials=credentials)

def create_instance(compute, project, zone, name):
    # Get the latest Debian Jessie image.
    image_response = (
        compute.images()
        .getFromFamily(project="debian-cloud", family="debian-9")
        .execute()
    )
    source_disk_image = image_response["selfLink"]

    # Configure the machine
    machine_type = "zones/%s/machineTypes/n1-standard-1" % zone
    config = {
        "name": name,
        "machineType": machine_type,
        # Specify the boot disk and the image to use as a source.
        "disks": [
            {
                "boot": True,
                "autoDelete": True,
                "initializeParams": {
                    "sourceImage": source_disk_image,
                },
            }
        ],
        "networkInterfaces": [
            {
                "network": "global/networks/default",
                "accessConfigs": [{"type": "ONE_TO_ONE_NAT", "name": "External NAT"}],
            }
        ],
        "metadata": {
            "kind": "compute#metadata",
 "items": [
      {
        "key": "startup-script",
        "value": 'sudo apt-get updatenexport DEBIAN_FRONTEND=noninteractivenecho "<html><body><h1>Hello World</h1><p>This page was created from a startup script.</p></body></html>" > index.html'
      }
    ]
        },
        "tags": {"items": ["http-server", "https-server"]},
    }

    return compute.instances().insert(project=project, zone=zone, body=config).execute()


create_instance(service, project_id, zone, "pandora-instance")

2

Answers


  1. You need to add the shebang at the start of the script to tell the operating system which interpreter to use. In your case that would be:

    {
      ...
      "metadata": {
        "items": [
          {
           "key": "startup-script",
           "value": "#! /bin/bashnnsudo apt-get updatenexport DEBIAN_FRONTEND=noninteractivenecho "<html><body><h1>Hello World</h1><p>This page was created from a startup script.</p></body></html>" > index.html"
          }
        ]
      }
      ...
    }
    

    It is essentially sending via API as described here.

    Note that you can troubleshoot startup scripts issues by checking Cloud Logging for that VM instance or in the the instance’s serial console log directly.

    Login or Signup to reply.
  2. When you run a startup-script, it runs as root.

    So, firstly, the sudo is useless. Then, your home directory is /root and this directory is in write only. So, when you write your index.html file in the /root directory, it’s impossible

    Prefer a well known location, use /tmp for tests.

    AND because you do write the file in root mode, the owner of the file will be root also. Perform a chown to change this if it’s a blocker for the rest of your processes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search