<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>bash on gavincampbell.dev</title>
		<link>https://gavincampbell.dev/tags/bash/</link>
		<description>Recent content in bash on gavincampbell.dev</description>
		<generator>Hugo 0.121.2</generator>
		<language>en-gb</language>
		<managingEditor>gavin@gavincampbell.dev (Gavin Campbell)</managingEditor>
		<webMaster>gavin@gavincampbell.dev (Gavin Campbell)</webMaster>
		<copyright>
Except as otherwise noted, all blog posts, photos, graphics, presentations and other media on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;Attribution 4.0 International&lt;/a&gt;. Except as otherwise noted, all code snippets on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/publicdomain/zero/1.0?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;CC0 1.0 Universal&lt;/a&gt;. Built with the &lt;a href=&#34;https://github.com/spookey/slick&#34;&gt;Slick&lt;/a&gt; theme for &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt;, hosted in the UK by &lt;a href=&#34;https://www.mythic-beasts.com//&#34;&gt;Mythic Beasts&lt;/a&gt;</copyright>
		<lastBuildDate>Wed, 07 Oct 2020 00:00:00 +0000</lastBuildDate>
		<atom:link href="https://gavincampbell.dev/tags/bash/index.xml" rel="self" type="application/rss+xml"/>
		<item>
			<title>Post-deployment configuration for Azure Web Apps</title>
			<link>https://gavincampbell.dev/post/post-deployment-configuration-azure-web-apps/</link>
			<pubDate>Wed, 07 Oct 2020 00:00:00 +0000</pubDate>
			<author>gavin@gavincampbell.dev (Gavin Campbell)</author>
			<guid isPermaLink="true">https://gavincampbell.dev/post/post-deployment-configuration-azure-web-apps/</guid>
			<description>&lt;p&gt;There are many &lt;a href=&#34;https://adamtheautomator.com/wordpress-blog-microsoft-azure/&#34;&gt;tutorials&lt;/a&gt; on the web that describe more or less automated ways of deploying packaged software to &lt;a href=&#34;https://azure.microsoft.com/en-gb/services/app-service/web/&#34;&gt;Azure Web Apps&lt;/a&gt;. For this particular one I&amp;rsquo;ve decided to use the popular Content Management System &lt;a href=&#34;https://en-gb.wordpress.org/&#34;&gt;WordPress&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What many such applications have in common is that there is an initial installation procedure, consisting of copying files to a web host and configuring database access, followed by a web-based graphical installer that sets up all of the application configuration.&lt;/p&gt;
&lt;p&gt;In the case of WordPress, this is known as the &amp;ldquo;famous five minute installation process&amp;rdquo;.&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;famous-five-minute-wordpress-installer.png&#34;
         alt=&#34;The famous five-minute WordPress installation process&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;The famous five-minute WordPress installation process&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;I&amp;rsquo;ve often wondered whether it would be possible to combine provisioning the resources in Azure, deploying the web site assets, and running through the post-deployment configuration in a single step. It&amp;rsquo;s quite common for such applications to provide a command-line interface for managing the system, and WordPress is no exception. The WordPress CLI is known as &lt;a href=&#34;https://wp-cli.org/&#34;&gt;WP-CLI&lt;/a&gt;. WP-CLI isn&amp;rsquo;t included in the main WordPress distribution and must be &lt;a href=&#34;https://wp-cli.org/#installing&#34;&gt;downloaded separately&lt;/a&gt;. WP-CLI can do most actions that can be done through the WordPress admin UI, as well as a few that can&amp;rsquo;t.&lt;/p&gt;
&lt;p&gt;Before going any further, it&amp;rsquo;s worth pointing out that this is probably not the best way to install and configure WordPress for your personal website&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; . WordPress isn&amp;rsquo;t really the point of this discussion, I&amp;rsquo;m just using it as an example of a packaged web application with a graphical installer.&lt;/p&gt;
&lt;h2 id=&#34;the-script&#34;&gt;The script&lt;/h2&gt;
&lt;h3 id=&#34;initial-installation&#34;&gt;Initial installation&lt;/h3&gt;
&lt;p&gt;The main script is a shell script that uses the &lt;a href=&#34;https://docs.microsoft.com/en-us/cli/azure/&#34;&gt;Azure CLI&lt;/a&gt; to create and configure resources. You&amp;rsquo;ll need to connect with &lt;code&gt;az login&lt;/code&gt; or equivalent before running it. There are a couple of other dependencies that may not be present in all Linux distributions, notably &lt;a href=&#34;https://core.tcl-lang.org/expect/index&#34;&gt;&lt;code&gt;expect&lt;/code&gt;&lt;/a&gt;, but you should be able to install these with your package manager. I&amp;rsquo;ve only tested this on &lt;a href=&#34;https://silverblue.fedoraproject.org/&#34;&gt;Fedora Silverblue&lt;/a&gt;, but there&amp;rsquo;s no reason why it shouldn&amp;rsquo;t work in &lt;code&gt;bash&lt;/code&gt; on OSX, &lt;a href=&#34;https://docs.microsoft.com/en-us/windows/wsl/about&#34;&gt;WSL&lt;/a&gt;, or even a build agent, which is probably where you want to be doing things like this.&lt;/p&gt;
&lt;p&gt;The script creates all the required resources, namely an App Service Plan, a Web App, and a MariaDB server and database. All the config information required by WordPress is stored in AppSettings in the Web App. In &amp;ldquo;real life&amp;rdquo;, you&amp;rsquo;d want to use &lt;a href=&#34;https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references&#34;&gt;Azure Key Vault references&lt;/a&gt; for many of these app settings, as under the current arrangement the secret values will all be echoed to the console, but I&amp;rsquo;ve omitted the key vault here in the interest of brevity.&lt;/p&gt;
&lt;p&gt;All the settings are read from environment variables, if you have a file named &lt;code&gt;.env&lt;/code&gt; in the same folder as the script then its contents will be sourced. I have included a &lt;code&gt;.env.example&lt;/code&gt; file with all the variables the script requires.&lt;/p&gt;
&lt;p&gt;The WordPress code is downloaded in a zip file and deployed with &lt;a href=&#34;https://docs.microsoft.com/en-us/cli/azure/webapp/deployment/source?view=azure-cli-latest#az_webapp_deployment_source_config_zip&#34;&gt;&lt;code&gt;az webapp deployment source config-zip&lt;/code&gt;&lt;/a&gt;, which deploys directly to the site. There is an app setting &lt;code&gt;project&lt;/code&gt; that specifies which subfolder of the zip file we want to extract.&lt;/p&gt;
&lt;h3 id=&#34;post-deployment-configuration&#34;&gt;Post-deployment configuration&lt;/h3&gt;
&lt;p&gt;Rather than &lt;code&gt;ssh&lt;/code&gt;ing directly to the Web App instance, the script attempts to use &lt;a href=&#34;https://docs.microsoft.com/en-us/cli/azure/webapp?view=azure-cli-latest#az_webapp_create_remote_connection&#34;&gt;&lt;code&gt;az webapp create-remote-connection&lt;/code&gt;&lt;/a&gt; to connect to the running app. This isn&amp;rsquo;t particularly reliable, but I&amp;rsquo;ve attempted to work around the worst symptoms in the script. These workarounds involve a combination of waiting for things to start, killing processes by name to clear up old connections, and other classic techniques. It does appear there are fixes in this area of Azure CLI &lt;a href=&#34;https://github.com/Azure/azure-cli/issues/8831&#34;&gt;in development&lt;/a&gt;. My workarounds still aren&amp;rsquo;t 100% reliable, the ssh tunnel sometimes fails, but will generally work the second time the script is run. Any suggestions for improving this would be welcome.&lt;/p&gt;
&lt;p&gt;Having created the tunnel, we need to &lt;code&gt;ssh&lt;/code&gt; with a password, which is set in the web app image. &lt;a href=&#34;https://stackoverflow.com/search?q=automate+password+ssh&#34;&gt;Searching StackOverflow for techniques to automate password-based &lt;code&gt;ssh&lt;/code&gt;&lt;/a&gt; will uncover a large number of suggestions that this is a bad idea, some more polite than others.&lt;/p&gt;
&lt;p&gt;In our case, since we have no alternative, we use &lt;code&gt;expect&lt;/code&gt; to automate the sending of the &lt;code&gt;ssh&lt;/code&gt; command and responding to the &lt;code&gt;Password:&lt;/code&gt; prompt. We also specify a couple of extra &lt;code&gt;ssh&lt;/code&gt; options with &lt;code&gt;-o &amp;quot;UserKnownHostsFile=/dev/null&amp;quot; -o &amp;quot;StrictHostKeyChecking=no&amp;quot; &lt;/code&gt; to suppress error messages about unknown hosts.&lt;/p&gt;
&lt;p&gt;Once &lt;code&gt;ssh&lt;/code&gt; is working, we upload a &lt;code&gt;wp-config.php&lt;/code&gt; file which is pretty similar to the &lt;a href=&#34;https://github.com/WordPress/WordPress/blob/master/wp-config-sample.php&#34;&gt;boilerplate one&lt;/a&gt;, but with changes to enable SSL between the browser and the site, as well as between the site and the database&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;. The configuration values which would be hard-coded in this file in a &amp;ldquo;classic&amp;rdquo; WordPress installation are also moved out to environment variables - created by App Settings in the web app - and read dynamically.&lt;/p&gt;
&lt;p&gt;Since the MariaDB server is &lt;em&gt;only&lt;/em&gt; accessible from Azure IPs, we &lt;code&gt;ssh&lt;/code&gt; again to use the &lt;code&gt;mysql&lt;/code&gt; client provided inside the web app to log in and configure the WordPress database and user.&lt;/p&gt;
&lt;p&gt;Finally we download the WP-CLI &lt;code&gt;.phar&lt;/code&gt; and place it in the path. By convention the downloaded file is renamed to &lt;code&gt;wp&lt;/code&gt;. The &lt;code&gt;wp&lt;/code&gt; client can then be used to configure our new installation. By far the most difficult part of this script was getting the quoting and escaping right for strings that are parsed by &lt;code&gt;bash&lt;/code&gt;, by &lt;code&gt;expect&lt;/code&gt;, by &lt;code&gt;bash&lt;/code&gt; again, then by WP-CLI (i.e. &lt;code&gt;php&lt;/code&gt;), and then by the WordPress API. This is the reason the &lt;code&gt;expect&lt;/code&gt; interactions are broken out into three separate blocks.&lt;/p&gt;
&lt;script src=&#34;https://gist.github.com/gavincampbell/f59f61cf98ae597dc56a5bce5b830cea.js?file=infra.sh&#34;&gt;&lt;/script&gt;

&lt;h3 id=&#34;success&#34;&gt;Success&lt;/h3&gt;
&lt;p&gt;At the end of the script, the site should look like this:&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;wordpress-install-complete.png&#34;
         alt=&#34;The famous zero-minute WordPress installation&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;The famous zero-minute WordPress installation&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;I&amp;rsquo;ve created a multi-file &lt;a href=&#34;https://gist.github.com/gavincampbell/f59f61cf98ae597dc56a5bce5b830cea&#34;&gt;gist&lt;/a&gt; with the main script, along with the &lt;code&gt;wp-config.php&lt;/code&gt; and &lt;code&gt;.env.example&lt;/code&gt; files. You can clone the whole repo with&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;git clone https://gist.github.com/gavincampbell/f59f61cf98ae597dc56a5bce5b830cea some_folder
&lt;/code&gt;&lt;/pre&gt;&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;If you&amp;rsquo;ve stumbled on this page whilst searching for instructions on how to set up WordPress for your personal website, you could do worse than looking at gandi.net. They do all kinds of things, including WordPress hosting. If you sign up through &lt;a href=&#34;&#34;&gt;this link&lt;/a&gt; you get some kind of discount, and they give me a small credit on my existing bill. This site isn&amp;rsquo;t hosted there, but I have others that are, and all my domains are registered with them.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;
&lt;p&gt;Although WP-CLI can create this config file, it seems that it can&amp;rsquo;t create the file until it has connected to the database, but it can&amp;rsquo;t connect to the database until it has read the SSL options from the file.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
		</item>
	</channel>
</rss>
