Where do GCE store metadata's startup-script in VM? - google-cloud-platform

After I create a vm with startup script, where can I find startup script in vm ?
Will this startup script store in vm or outsite the vm ?
If I want to edit my startup script, how could it edit it ?

The startup script is taken from the metadata server.
If you restart your instance, after it boots up it will connect to the metadata server and take the script from there and then execute it.
Therefore, you need to change the instance metadata in order to change your startup sript (uses the compute.instances.setMetadata permission).
You can do that straight from the UI, API or CLI tool. More info on all of the above here - Compute Engine Docs - Running Startup Scripts
After you change the startup script for an instance it will execute on the next (re)boot. The article above also provides a command you can use if you wanted to force its execution straight away:
$ sudo google_metadata_script_runner --script-type startup --debug

Related

Run a python script on an EC2 instance without uploading the python file to the instance

I currently have an EC2 instance running with directories of tenants and files in those directories. I have to run a python script to go into those directories, find out how much memory each tenant is using, how many files each tenant has, and send that informatin to a grafana dashboard. I have completed the python script and it will go into each tenant, calculate what I need, and send that information to the grafana dashboard if i manually upload the python script to the instance and run it from the command line.
The goal is to automate this process so the script will run every 15 minutes without ever having to upload the python script to the instance. There is a lot of red tape and I am unable to make the script a part of the AMI when the instance is launched, and I haven't found any examples of people trying to do this before.
First of all is what I am trying to do even possible? Ideally I'd like to run the script from a lambda because that would make it very easy to schedule every 15 minutes and my dependencies for the python script would be very easy to put into place. Suggestions have been brought up about using CodeDeploy but I don't know enough about it to know how that would help.
I have created a python script that works and will run on an ec2 instance if it is uploaded and run from the command line, but I haven't been able to run the script "remotely" as I wold like to.
If you are doing specific tasks within the EC2 file or memory system, running it as a Lambda is not going to work. Instead look into using SSM to run a remote script on an EC2 instance.
https://aws.amazon.com/getting-started/hands-on/remotely-run-commands-ec2-instance-systems-manager/

Google cloud compute engine - startup script - How to start java spring application

I have a Compute Engine instance with a java spring application
How can I execute it on my VM Instance startup ? If i start my VM, it should execute my application automatically.
Looking at the article -google doc
(the document does a lot, but I only need to run the existing war artifact)
I added a few lines - editing VM - Automation >> Startup script
#!/bin/sh
cd /home/souin/p****my/build/libs
# Start server
java -jar py*****my-0.1.0-SNAPSHOT.war
It is not working.
Do I need to create a shell file inside VM and make use of it from startup ?
Please advice how to run my application?

How to run bash script on Google Cloud VM?

I found this auto shutdown script for VM instances on GCP and tried to add that into the VM's metadata.
Here's a link to that shutdown script.
The config sets it so that after 20 mins the idle VM will shut down, but it's been a few hours and it never shut down. Are there any more steps I have to do after adding the script to the VM metadata?
The metadata script I added:
Startup scripts are executed while the VM starts. If you execute your "shutdown script" at the boot there will be nothing for it to do. Additionally in order for this to work a proper service has to be created and it will be using this script to detect & shutdown the VM in case it's idling.
So - even if the main script ashutdown was executed at boot and there was no idling it did nothing. And since the service wasn't there to run it again your instance will run indefinatelly.
For this to work you need to install everything on the VM in question;
Download all three files to some directory in your vm, for example with curl:
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/ashutdown
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/ashutdown.service
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/install.sh
Make install.sh exacutable: sudo chmod +x install.sh
Run it: sudo ./install.sh.
This should install & run the ashutdown service in your system.
You can check if it's running with service ashutdown status.
These instructions are for Debian system so if you're running CentOS or other flavour of Linux they may differ.

How to keep a server processing running on a Google Cloud VM?

This question seems very basic, but I wasn't able to quickly find an answer at https://cloud.google.com/compute/docs/instances/create-start-instance. I'm running a MicroMDM server on a Google Cloud VM by connecting to is using SSH (from the VM instances page in the Google Cloud Console) and then running the command
> sudo micromdm serve
However, I notice that when I shut down my laptop, the server also stops, which is actually why I wanted to run the server in a VM in the first place.
What would be the recommended way to keep the server running? Should I use systemd or perhaps run the process as a Docker container?
When you run the service from the command line, you "attach" it to your shell process, when you terminate your ssh session, your job gets terminated also.
To make a process run in background, simply append the & at the end of the command, in your case:
sudo micromdm serve &
This way your server is alive even after you quit your session.
I also suggest you to add that line in the instance startup script, if you want that server to always be up, so that you don't have to run the command by hand each time :)
More on Compute Engine startup scripts here.
As the Using MicroMDM with systemd documentation, it suggested to use systemd command to run MicroMDM service on linux.First, on our linux host, we create the micromdm.service file, then we move it to the location ‘/etc/systemd/system/micromdm.service’ . We can start the service. In this way, it will keep the service running, or restart service after the service fails or server restart.

Customizing the cloud environment to include a package permanently

I have been using some packages by installing them using the sudo apt-get command in the cloud shell. But now I want to make it permanent. I got this message in the shell
You are running apt-get inside of Cloud Shell. Note that your Cloud Shell
machine is ephemeral and no system-wide change will persist beyond session end.
You can customize your environment to permanently include this package by
updating your environment at https://cloud.google.com/console/cloudshell/environment/view.
So how to customize the cloud environment to include a package permanently?
You have several options.
1) Reinstall everything each time you launch Cloud Shell. This sounds bad but if you keep your files on GCS, the copy happens very fast.
2) Cloud Shell is a Docker container. You can modify that container so that you launch Cloud Shell using your customized container. Launch Cloud Shell. In the title bar on the right hand side is a icon that looks like a laptop. Click it. This will open a window with details on configuring the Docker container.
3) Keep everything local to your home directory. You home directory tree is persistent and will be restored each time your Cloud Shell VM is recreated.