The following is a primer on creating RightScale scripts that execute with attachments.
Creating RightScale scripts can be a little daunting at first, but with a little time and effort one can quickly ramp up from beginner to expert. The examples show here will be executed on a Linux CentOS instance.
So let’s go over some RightScale script basics. First let’s start by creating a new script. Click the Design->RightScripts menu option.
Next click the “New” button.Now you should be looking at the “New RightScript” screen. Here we will give the script a name, enter a description, identify input values and write the actual script to be executed on the client machine. Let’s begin by entering a name and description for the RightScript. Enter any name and description you want.
It’s important to note here, that changing the RightScript name, will not affect any instance you have the scripts attached to. In other words, once you attach a script to an instance, if you decide to rename that script later, feel free to do so with no consequences. The next time you see the script on the instance, it will have the updated name, there’s no need to go back and re-add it.
Inputs will be used to dynamically get values entered from the user or from a predefined value. I’ll explain this more in detail in a little while. First let’s create a simple script. Let’s create a simple script that copies a file from one location to the other. Enter the following into the script field:
cp /tmp/file1 /tmp/file2
Now I know that this is extremely simple, but we’ll build from this. So the script when executed get’s run against the default interpreter, in this instance BASH, and it copies a file that does not exist called “file1” to the same directory and names it “file2”. Pretty useless, but now let’s make it a bit more useful. Let’s say we wanted to have a utility to copy any file from one location to another from within the RightScale interface. Let’s modify the script to do so. Change the script to the following:
cp $FROM_LOCATION $TO_LOCATION
Now click the “Identify” button located just above the script field. Did you see what happened? RightScale realizes that the variables “$FROM_LOCATION” and “$TO_LOCATION” are not defined and offers them as input values to be input by the user and the time of execution. In other words, when you decide to execute this script, it will prompt the user for the value of the “$FROM_LOCATION” variable and the “$TO_LOCATION” variable.
By default, the input types are “Any”, meaning any free text value. You can if you wish change the type to be a dropdown and set a distinct set of values from the user to choose from. But for this example let’s leave it as any.
Now this example is a bit more useful. We can now pretty much move any file from one location to another through the RightScale interface. But what about if we wanted to have a file uploaded and then copied out to a directory. Let’s try that shall we? First let’s modify the script a bit to use an uploaded attachment:
cp $ATTACH_DIR/* $TO_LOCATION
Next let’s hit the “Identify” button again. Notice, the “$FROM_LOCATION” input field disappears because it is no longer needed, but where is the “$ATTACH_DIR” input variable. The “$ATTACH_DIR” input variable is defined by RightScale to allow you to access any attachments you decide to upload to the script. Let’s try this and see how it works.
Let’s first enter a description for the “$TO_LOCATION” field we’ve added. This will allow users to understand what the input field is to be used for. For now enter this in the “TO_LOCATION” description field:
“Enter the destination for the attached file. (ie. /tmp)”
Then click the “Save” button. You should now be presented with the tabbed interface to your script.
Next click the “Attachments” tab.
Here we will click the “Choose File” button and upload a file attachment to our script. Create a simple text file called “test.txt”, click “Choose File”, select the newly created file “test.txt” and finally click “Upload”. You should then see the file listed about the “Upload file” area.Finally let’s try running the script to see how it works.
Executing The Script
To execute the script, we’ll need to select a running instance to have the script act upon. First let’s get our deployments and select and instance. Do this by selecting Manage->Deployments.
Then click on the server template you want to execute the script on from the list of servers. Now click on the “Scripts” tab for your selected server template.
Next click the “Add Script” button under the “Operational Scripts” area.
You should now be presented with the “Select a script” dialog. Click “Unpublished” and click on your newly created script, then click the “Select” button. This action will attach the script to the server template and make it executable on running instances built off this template. To execute the script, Go back to your Manage->Deployments screen and select the actual running server, click the “Scripts” tab, then click the blue “Run” button located to the right of the script.
Once the “Run” button has been clicked, the “Queued”, “Status” and “Completed” status will appear in the top left status pane. If all goes well, you’ll have a successfully created and run script. You can verify this by clicking on the “Completed” status when it appears. Or by opening a terminal into to instance and verifying the appearance of the test.txt file.