One place for hosting & domains

      StackScripts

      Create and Manage StackScripts – A Tutorial


      Updated by Linode

      Written by Linode

      What are StackScripts?

      StackScripts provide Linode users with the ability to automate the deployment of custom systems on top of Linode’s default Linux distribution images. For example, every time you deploy a new Linode you might execute the same tasks, like updating your system’s software, installing your favorite Linux tools, and adding a limited user account. These tasks can be automated using a StackScript that will perform these actions for you as part of your Linode’s first boot process.

      All StackScripts are stored in the Linode Cloud Manager and can be accessed whenever you deploy a Linode. A StackScript authored by you is an Account StackScript. While a Community StackScript is a StackScript created by a Linode community member that has made their StackScript publicly available in the Linode Cloud Manager.

      In this Guide

      This guide will show you how to do the following:

      Note

      Create a New StackScript

      1. Log into the Linode Cloud Manager.

      2. Click on the StackScripts link in the left-hand navigation menu. You will be brought to the StackScripts page.

        Click on the StackScripts link in the left-hand navigation menu.

      3. Viewing the Account StackScripts section, click on the Create New StackScript link at the top of the page.

        Click on the Create New StackScript link.

      4. On the Create New StackScript page, provide the required configurations to create your StackScript.

        Field Description
        StackScript Label The name with which to identify your StackScript. Required.
        Description An overview of what your StackScript does.
        Target Images The Linux distributions that can run this StackScript. Required.
        Script The body of the script. See the Writing Scripts for Use with Linode StackScripts section for more details. Required.
        Revision Note A short description of the updates made to your StackScript in this version of your script.



        Example Script

        The file below displays an example of a simple script that executes some basic set up steps on a Linode. Review the example’s comments for details on what each line of the script does.

        Example Script
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        
        #!/bin/bash
        # This block defines the variables the user of the script needs to provide
        # when deploying using this script.
        #
        #
        #<UDF name="hostname" label="The hostname for the new Linode.">
        # HOSTNAME=
        #
        #<UDF name="fqdn" label="The new Linode's Fully Qualified Domain Name">
        # FQDN=
        
        # This sets the variable $IPADDR to the IP address the new Linode receives.
        IPADDR=$(/sbin/ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://')
        
        # This updates the packages on the system from the distribution repositories.
        apt-get update
        apt-get upgrade -y
        
        # This section sets the hostname.
        echo $HOSTNAME > /etc/hostname
        hostname -F /etc/hostname
        
        # This section sets the Fully Qualified Domain Name (FQDN) in the hosts file.
        echo $IPADDR $FQDN $HOSTNAME >> /etc/hosts
      5. Click Save when you are done. You can always edit your script later if needed. You will be brought back to the StackScripts page, where your new StackScript will be visible and ready to use with a new Linode deployment.

        Note

        View your new StackScript on the StackScripts page.

      Manage StackScripts

      Edit an Account StackScript

      After you’ve created an Account StackScript, you can go back and edit it as required. This section will show you how to access a StackScript for editing.

      1. Log into the Linode Cloud Manager.

      2. Click on the StackScripts link in the left-hand navigation menu. You will be brought to the StackScripts page.

        Click on the StackScripts link in the left-hand navigation menu.

      3. Viewing the Account StackScripts tab, you will see a list of all of your account’s StackScripts. This will include both private and public StackScripts. Click on the more options ellipsis corresponding to the StackScript you’d like to modify and select Edit from the dropdown menu.

        Select **Edit** from the dropdown menu to edit your StackScript.

      4. On your StackScript’s Edit page, modify it as needed and click Save to keep your changes.

      Make an Account StackScript Public

      You can make any of your Account StackScripts public. When an account StackScript is public it will become available to the rest of the Linode Community in the Cloud Manager as a Community StackScript.

      Caution

      Once you make an Account StackScript public, you will not be able to make it private again or to delete it. You can, however, edit your public StackScript, if needed.

      1. Log into the Linode Cloud Manager.

      2. Click on the StackScripts link in the left-hand navigation menu. You will be brought to the StackScripts page.

        Click on the StackScripts link in the left-hand navigation menu.

      3. Viewing the Account StackScripts tab, you will see a list of all of your account’s StackScripts. This will include both private and public StackScripts. Click on the more options ellipsis corresponding to the StackScript you’d like to make public and select Make StackScript Public from the dropdown menu.

        Select Make StackScript Public from the dropdown menu.

        A dialog box will appear asking you to confirm the action. Click on Yes, make me a star! if you would like to proceed. You will be brought back to the Account StackScripts page.

        Note

      Delete an Account StackScript

      Note

      You cannot delete Account StackScripts that have been made public.
      1. Click on the StackScripts link in the left-hand navigation menu. You will be brought to the StackScripts page.

        Click on the StackScripts link in the left-hand navigation menu.

      2. Viewing the Account StackScripts tab, you will see a list of all of your account’s StackScripts. Click on the more options ellipsis corresponding to the StackScript you’d like to delete and select Delete from the dropdown menu.

        Select Make StackScript Public from the dropdown menu.

        A dialog box will appear asking you to confirm the action. Click on Delete if you would like to proceed.

      Next Steps

      Now that you have created a StackScript, you can deploy a Linode using your StackScript. See the Deploying a New Linode Using a StackScript guide for details.

      This guide is published under a CC BY-ND 4.0 license.



      Source link

      Writing Scripts for Use with Linode StackScripts – A Tutorial


      Updated by Linode

      Contributed by
      Linode

      What are StackScripts?

      StackScripts provide Linode users with the ability to automate the deployment of custom systems on top of Linode’s default Linux distribution images. For example, every time you deploy a new Linode you might execute the same tasks, like updating your system’s software, installing your favorite Linux tools, and adding a limited user account. These tasks can be automated using a StackScript that will perform these actions for you as part of your Linode’s first boot process.

      All StackScripts are stored in the Linode Cloud Manager and can be accessed whenever you deploy a Linode. A StackScript authored by you is an Account StackScript. While a Community StackScript is a StackScript created by a Linode community member that has made their StackScript publicly available in the Linode Cloud Manager.

      In this Guide

      Writing a script for use in a StackScript will generally be the same as writing a script that will be executed from the command line or another program. This guide includes information about the StackScript system, including the following:

      The StackScript System

      StackScript Requirements

      • The primary requirement for your scripts is that the interpreter needed to execute your script should exist in the Linode base image you are deploying. While Bash is an obvious choice for a script, you may choose any scripting language.

        Note

        Linode images are created using “vanilla” versions of its given distribution. Consult our Choosing a Linux Distribution guide to see list of all distributions Linode provides and to access each distribution’s corresponding websites. You can find more information on the interpreters available for each distribution on their official websites.
      • When writing a script, you must use a shebang as the first line of your script. This indicates to your Linux system which interpreter to use when running the script. For example, if you are writing a Bash script, the beginning of your script should include the following line:

        Or, if you are writing a Python script, the beginning of your script should include the following line:

        1
        2
        
        #!/usr/bin/env python
              

      Import a StackScript

      Your scripts can import any Account StackScript that you own or any Community StackScript. This allows you to reuse code minimizing what you need to write in your own scripts.

      • The example below shows the syntax to import another StackScript. As a result of including this line in a script, the imported StackScript will be downloaded as ssinclude-[NUMBER] to your Linode. However, it must be run in order to execute its contents.

        1
        2
        
        <ssinclude StackScriptID="[NUMBER]">
            

        In Bash, you can download and run the script in the following way:

        1
        2
        
        source <ssinclude StackScriptID="[NUMBER]">
            

        If you’re scripting in another language, import the StackScript, then execute it on a second line:

        1
        2
        3
        
        <ssinclude StackScriptID="[NUMBER]">
        ./ssinclude-[NUMBER]
            
      • Linode provides a StackScript Bash Library that includes a set of functions that perform various common tasks users might wish to execute on their Linodes. This script creates the functions, but does not run them. A new StackScript can import the Bash Library and then execute functions from it.

        Note

      Access a StackScript’s ID Number

      Follow the steps in this section to find the ID number of a StackScript.

      1. Log into the Linode Cloud Manager.

      2. Click on the StackScripts link in the left-hand navigation menu. You will be brought to the StackScripts page.

        Click on the StackScripts link in the left-hand navigation menu.

      3. Click on the Account StackScripts tab or the Community StackScripts tab, depending on the type of StackScript whose ID you’d like to find

      4. Click on the StackScript whose ID you’d like to access. This will bring you to its StackScript detail page.

        View the details and contents of an Account StackScript.

      5. The StackScript detail page’s URL will display the StackScript’s ID number. You can now use this number to import the StackScript into your own script.

        Access a StackScript's ID number.

      User Defined Fields (UDFs)

      The StackScript system provides a basic markup specification that interfaces with the Linode deployment process so that users can customize the behavior of a StackScript on a per-deployment basis. When a StackScript contains a user defined field (UDF), the Linode Cloud Manager will present the UDF as a form field, so a user can insert a corresponding custom value. The values and their related variables are inserted into the script’s environment when used to deploy a new Linode.

      Default Environment Variables

      Linode StackScripts provide a set of default environment variables that you can use to provide your script with information about the Linode it has deployed.

      Environment Variable Description
      LINODE_ID The deployed Linode’s ID number
      LINODE_LISHUSERNAME The deployed Linode’s full Linode Shell (LISH) accessible name.
      LINODE_RAM The RAM available on this Linode’s plan.
      LINODE_DATACENTERID The ID number of the data center containing the Linode. You can use the Linode API to see a list of all data center IDs.



      Set your Environment Variables Using an External File

      It is possible to set your script’s environment variables using externally hosted files. The example Bash script uses the wget utility to download two files named base.env and $IPADDR.env from the external site http://example.com/. The source command will load the downloaded files into the script.

      StackScript
      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      # [...]
      IPADDR=$(/sbin/ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://')
      
      wget http://example.com/base.env --output-document=/tmp/base.env
      wget http://example.com/$IPADDR.env --output-document=/tmp/system.env
      
      source /tmp/base.env
      source /tmp/system.env
      # [...]

      Note

      The files you reference within your script must exist and be accessible via HTTP. Also, ensure that the files you host externally do not contain any sensitive information.

      StackScript Examples

      Using an External Script

      • If you have an existing deployment script, you can use a StackScript to deploy Linode instances with it. The following example StackScript installs PHP on the Linode, downloads an external PHP script from the URL http://example.com/deployment-script.php, makes it executable, and then runs the downloaded script.

        StackScript
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        
        #!/bin/bash
        if [ -f /etc/apt/sources.list ]; then
           apt-get upgrade
           apt-get -y install php
        elif [-f /etc/yum.conf ]; then
           yum -y install php
        elif [-f /etc/pacman.conf ]; then
           pacman -Sy
           pacman -S --noconfirm pacman
           pacman -S --noconfirm php
        else
           echo "Your distribution is not supported by this StackScript"
           exit
        fi
        
        wget http://example.com/deployment-script.php --output-document=/opt/deployment-script.php
        chmod +x /opt/deployment-script.php
        
        ./opt/deployment-script.php
            
      • If you do not want to rely on an existing external server to host your scripts for download, you can embed the bootstrapped script into the StackScript.

        StackScript
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        
        #!/bin/bash
        
        if [ -f /etc/apt/sources.list ]; then
           apt-get upgrade
           apt-get -y install php5
        elif [-f /etc/yum.conf ]; then
           yum -y install php
        elif [-f /etc/pacman.conf ]; then
           pacman -Sy
           pacman -S --noconfirm pacman
           pacman -S --noconfirm php
        else
           echo "Your distribution is not supported by this StackScript"
           exit
        fi
        
        cat >/opt/deployment-script.php <<EOF
        #!/usr/bin/php
        <?php print('Hello World!'); ?>
        EOF
        
        chmod +x /opt/deployment-script.php
        
        ./opt/deployment-script.php
        
            

      Next Steps

      This guide is published under a CC BY-ND 4.0 license.



      Source link

      Common Linode StackScripts Use Cases


      Updated by Linode

      Contributed by
      Linode

      What are StackScripts?

      StackScripts provide Linode users with the ability to automate the deployment of custom systems on top of Linode’s default Linux distribution images. For example, every time you deploy a new Linode you might execute the same tasks, like updating your system’s software, installing your favorite Linux tools, and adding a limited user account. These tasks can be automated using a StackScript that will perform these actions for you as part of your Linode’s first boot process.

      All StackScripts are stored in the Linode Cloud Manager and can be accessed whenever you deploy a Linode. A StackScript authored by you is an Account StackScript. A Community StackScript is a StackScript created by a Linode community member that has made their StackScript publicly available in the Linode Cloud Manager.

      StackScript Use Cases

      Automating Common System Administration Tasks

      Whenever you deploy a new Linode, there are basic system administration tasks that you must perform, like installing system software updates, setting your Linode’s hostname, setting the timezone, and securing your server. You can create a StackScript to automate all these steps and use it each time you deploy a new Linode. There are few limitations to what you can automate using a StackScript, because its underlying mechanism works just like any script you might execute on a Linux system. StackScripts ensure that each Linode you deploy is configured exactly to your preferences each time.

      Since you can make any StackScript public to the Linode Community, your entire team can use the StackScripts you create to easily deploy base identical systems.

      Demonstrating your Software

      If you develop software, you can use StackScripts to deploy a demonstration instance of your software. The resulting system may not need to be particularly durable or be fully configured, since you can redeploy a new Linode exactly as written in your StackScript. This is an easy and reproducible way to spin up quick demos of your software.

      Distributing your Software

      Community StackScripts are publicly available to the entire Linode Community. This means if you have an open source project you’d like to make easily available to Linode users, you can write a StackScript that installs and configures your project’s software on a Linode. Include user defined variables in your StackScript to make it customizable to users during each deployment of the StackScript.

      Note

      If you would also like to make your open source project available to the Linode Community as an App in the Linode Marketplace, see the Linode One-Click App Marketplace page for details.

      Deploy Cluster Instances

      If your application makes use of a cluster of nodes, you may be able to automate the deployment of a new cluster-member by using StackScripts to configure the instance. StackScripts, in combination with the Linode API, can help you to elastically automate deployment and management of a cluster’s node. Similarly, you can apply the same concept to creating a server appliance instance.

      Next Steps

      To get started creating your first StackScript, see the A Tutorial for Creating and Managing StackScripts.

      This guide is published under a CC BY-ND 4.0 license.



      Source link