Using Phing to SSH into a Vagrant Box
Join the DZone community and get the full member experience.
Join For FreeNow that I've started using migrations, I've discovered a minor irritant.
I run this project on a Vagrant VM and have discovered that I keep forgetting to ssh into the vagrant box before running the migrations script. The obvious solution is to automate this and I decided to use Phing to do so.
Firstly, I needed to install the PHP ssh2 extension:
$ brew install libssh2 $ sudo pecl install pecl.php.net/ssh2-0.12
I'm on OS X, so installed libssh2 via homebrew and as the PECL ssh2 extension is marked as beta, the simplest way to install it is to reference the exact version.
Once this is done, we can now use Phing's SshTask in our build scripts.
This is a simple build.xml file to check it all works:
<?xml version="1.0"?> <project name="sshtest" default="testssh"> <target name="testssh"> <ssh username="vagrant" password="vagrant" host="192.168.123.456" command="pwd" property="pwd" display="false" /> <echo>The current working directory is ${pwd}</echo> </target> </project>
The output looks like:
$ phing Buildfile: /www/19ft/projectname/build.xml sshtest > testssh: [echo] The current working directory is /home/vagrant BUILD FINISHED Total time: 0.3923 seconds
Now that it works, we can write a target that does something useful:
<target name="migrate" > <ssh username="vagrant" password="vagrant" host="192.168.123.456" command="cd /vagrant; php bin/migrations.php migrate" /> </target>
Now, I simply run `phing migrate` and the MySQL in my Vagrant box is migrated as I expect.
Obviously Phing isn't the only way to do this, but it worked for me.
Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Strategies for Reducing Total Cost of Ownership (TCO) For Integration Solutions
-
Integrating AWS With Salesforce Using Terraform
-
How To Use Pandas and Matplotlib To Perform EDA In Python
-
A Deep Dive Into the Differences Between Kafka and Pulsar
Comments