Updated on 24.5.2023

Initialization Scripts

Add initialization script

Newly deployed cloud servers usually require some steps to set up. Everyone has their own list of things to go through from adding new user accounts to running updates and installing common applications. With Initialization scripts, you can automate the tasks you would otherwise perform again and again when booting up a new host.

Initialization scripts

Adding scripts

You can manage your scripts with the Initialization Scripts feature in your UpCloud Control Panel under your Account section. Click the Add new button at the top of the page to open the editor window.

Add initialization script

Copy any pre-existing script you might have into the script text field, or write a new script straight in the browser. Name your scripts so that you can easily distinguish between them. Once you are done, click the Update script button at the bottom to save the changes.

Deploying with a script

The Initialization scripts are supported by all of our public Linux templates. After adding any SSH keys you may wish to use, you can find the option to load your previously saved initialization scripts.

Selecting one of your stored scripts will bring it to the edit field below. You can make any last-minute changes to the script still before deployment, or you can write a completely new script right on the spot. Once you are done making changes, click the Create button as usual to deploy the new server. The server will perform the actions dictated by the script during the first boot up saving you considerable time and effort.

Using initialization script at deployment

Testing new initialization scripts is also easy thanks to the extremely fast deployment to any of the UpCloud availability zones. If your script didn’t perform as expected, you can always delete the server and deploy it again. Iterating your scripts will let you fine-tune the tasks at boot up and be running again within seconds.

Writing scripts

Initialization scripts support anything a regular Linux shell script would.  Depending on your choice of distribution, the default shell might use different implementations, but the principles remain the same. Below is an example of how to have the server automatically run the update and upgrade routines as also seen in the picture above.

#!/bin/bash
# Run the commands in a noninteractive mode
export DEBIAN_FRONTEND=noninteractive
# Update source list and log the output to file
apt-get update > /tmp/init-script.log
# Upgrade available software packages with default options when available
apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y upgrade >> /temp/init-script.log

Certain OS versions can use a separate script format with a setup file cloud-config. The config file is read at boot, which allows a new node e.g. to be automatically updated and restarted.

#cloud-config
coreos:
  update:
    reboot-strategy: "reboot"
users:
  - name: "elroy"
    passwd: "$6$5s2u6/jR$un0AvWnqilcgaNB3Mkxd5yYv6mTlWfOoCYHZmfi3LDKVltj.E8XNKEcwWm..."
    groups:
      - "sudo"
      - "docker"
    ssh-authorized-keys:
      - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0g+ZTxC7weoIJLUafOgrm+h..."

It is also possible to enter a URL in an initialization script. If the script contains an address, the deployment process will attempt to fetch the script and use it instead. This allows greater flexibility including dynamically updating the initialization script between deployments.

Janne Ruostemaa

Editor-in-Chief

  1. Fleet and etxd2 are deprecated and updated Upcloud CoreOS containers break when you use these old init scripts….

  2. Janne Ruostemaa

    Hi Dave, thanks for the comment. CoreOS is developed so fast that documentation had difficulty to keep up with all the changes. We’ve updated the example init script to remove references to deprecated services.

  3. I can’t make it work..i got a really simple bash script that updates the repos and install docker..just that..but looking the logs looks like the server starts before that the script can finish ..by the way how i use the URL initialization script method..thanks

  4. Janne Ruostemaa

    Hi there, thanks for the comment. The initialization script is executed once the server has started and run on the background. Note that longer processes such as updates may take a few minutes to complete. The output from running the init script is saved to /var/log/upcloud_userdata.log

    As for the URL method, you just need to make the initialization script available online without password protection. Then enter the URL of your script in the initialization script field.

  5. Walker Wallace

    There does not seem to be any method to invoke initialization scripts if the API is used to setup a server. Apologies if I am missing something in the UpCloud documentation, but this would be an unfortunate limitation on automating processes.

  6. Janne Ruostemaa

    Hi Walker, thanks for the question. You can run an initialization script while creating a server using the API by including user_data parameter. For example:

    {
    	"server": {
    		"zone": "uk-lon1",
    		"title": "example",
    		"hostname": "example.com",
    		"plan": "1xCPU-1GB",
    		
    		"storage_devices": {
    			"storage_device": [
    				{
    					"action": "clone",
    					"storage": "01000000-0000-4000-8000-000030200200",
    					"title":"example-disk0",
    					"tier": "maxiops",
                        "size": 25
    				}
    			]
    		},
    		"password_delivery": "none",
    		"login_user": {
    			"username":"root",
    			"ssh_keys": {
    				"ssh_key": [
    					"ssh-rsa xxx"
    				]
    			}
    		},
    		"user_data":"apt-get update && apt-get -y upgrade"
    	}
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top