{"id":2323,"date":"2016-07-07T09:48:26","date_gmt":"2016-07-07T06:48:26","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/initialization-scripts\/"},"modified":"2016-07-07T09:48:26","modified_gmt":"2016-07-07T06:48:26","slug":"initialization-scripts","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/initialization-scripts\/","title":{"rendered":"Initialization Scripts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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&nbsp;<em>Initialization scripts<\/em>, you can automate the tasks you would otherwise perform again and again when booting up a new host.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/initialization-scripts-1.png\" alt=\"Initialization scripts\" class=\"wp-image-5882\" \/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Test hosting on UpCloud!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Adding scripts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can manage your scripts with the Initialization Scripts feature in your UpCloud Control Panel under your Account section. Click the <em>Add\u00a0new<\/em>\u00a0button at the top of the page to open the editor window.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/add-initialization-script.png\" alt=\"Add initialization script\" class=\"wp-image-5390\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">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 <em>Update script<\/em> button at the bottom to save the changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deploying with a script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Initialization scripts are supported by all of our public Linux templates. After adding any <a href=\"https:\/\/upcloud.com\/global\/community\/\n\/tutorials\/managing-ssh-keys\/\" target=\"_blank\" rel=\"noopener\">SSH keys<\/a> you may wish to use, you can find the option to load your previously saved initialization scripts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<em>Create<\/em>&nbsp;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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/use-initialization-script.png\" alt=\"Using initialization script at deployment\" class=\"wp-image-5391\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Testing new initialization scripts is also easy thanks to the extremely fast deployment to any of the UpCloud availability zones. If your script didn\u2019t 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing scripts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Initialization scripts support anything a regular Linux shell script would. &nbsp;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&nbsp;update and upgrade routines as also seen in the picture above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n# Run the commands in a noninteractive mode\nexport DEBIAN_FRONTEND=noninteractive\n# Update source list and log the output to file\napt-get update &gt; \/tmp\/init-script.log\n# Upgrade available software packages with default options when available\napt-get -o Dpkg::Options::=\"--force-confdef\" -o Dpkg::Options::=\"--force-confold\" -y upgrade &gt;&gt; \/temp\/init-script.log<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Certain OS versions can use a separate script format with a setup file\u00a0<a href=\"https:\/\/cloudinit.readthedocs.io\/en\/latest\/topics\/examples.html\" target=\"_blank\" rel=\"noopener\"><em>cloud-config<\/em><\/a>.\u00a0The config file is read at boot, which\u00a0allows a new node e.g. to be automatically updated and restarted.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#cloud-config\ncoreos:\n  update:\n    reboot-strategy: \"reboot\"\nusers:\n  - name: \"elroy\"\n    passwd: \"$6$5s2u6\/jR$un0AvWnqilcgaNB3Mkxd5yYv6mTlWfOoCYHZmfi3LDKVltj.E8XNKEcwWm...\"\n    groups:\n      - \"sudo\"\n      - \"docker\"\n    ssh-authorized-keys:\n      - \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0g+ZTxC7weoIJLUafOgrm+h...\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is also possible to enter a URL&nbsp;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.<\/p>\n","protected":false},"author":3,"featured_media":27239,"comment_status":"open","ping_status":"closed","template":"","community-category":[283,247],"class_list":["post-2323","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2323"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2323\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2323"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}