Commits

turanct committed 3965d86

Initial commit

  • Participants

Comments (0)

Files changed (4)

+# Ignore the phpunit.phar file
+/phpunit.phar
+
+# Ignore the composer.phar file
+/composer.phar
+
+# Ignore the vendor directory
+/vendor/*
+{
+	"name": "turanct/deeploy",
+	"description": "A simple deploy tool over ssh, in php",
+	"type": "library",
+	"homepage": "http://bitbucket.org/turanct/deeploy",
+	"license": "MIT",
+	"authors": [
+		{
+			"name": "Toon Daelman",
+			"email": "spinnewebber_toon@hotmail.com",
+			"homepage": "http://turanct.wordpress.com",
+			"role": "Developer"
+		}
+	],
+	"require": {
+		"phpseclib/phpseclib": "dev-master"
+	},
+	"minimum-stability": "dev",
+	"autoload": {
+		"psr-0": {
+			"Deeploy": "src/"
+		}
+	}
+}
+{
+    "_readme": [
+        "This file locks the dependencies of your project to a known state",
+        "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
+    ],
+    "hash": "219697cd1982c7585ac3f837896e457f",
+    "packages": [
+        {
+            "name": "phpseclib/phpseclib",
+            "version": "dev-master",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/phpseclib/phpseclib.git",
+                "reference": "adf8afd4eb7566615decfc65892266d21f49612c"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/adf8afd4eb7566615decfc65892266d21f49612c",
+                "reference": "adf8afd4eb7566615decfc65892266d21f49612c",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.0.0"
+            },
+            "suggest": {
+                "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.",
+                "ext-mcrypt": "Install the Mcrypt extension in order to speed up a wide variety of cryptographic operations.",
+                "pear-pear/PHP_Compat": "Install PHP_Compat to get phpseclib working on PHP >= 4.3.3."
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "0.3-dev"
+                }
+            },
+            "autoload": {
+                "psr-0": {
+                    "Crypt": "phpseclib/",
+                    "File": "phpseclib/",
+                    "Math": "phpseclib/",
+                    "Net": "phpseclib/"
+                },
+                "files": [
+                    "phpseclib/Crypt/Random.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "include-path": [
+                "phpseclib/"
+            ],
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Jim Wigginton",
+                    "email": "terrafrost@php.net",
+                    "role": "Developer"
+                }
+            ],
+            "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.",
+            "homepage": "http://phpseclib.sourceforge.net",
+            "keywords": [
+                "BigInteger",
+                "aes",
+                "asn.1",
+                "asn1",
+                "crypto",
+                "cryptography",
+                "encryption",
+                "rsa",
+                "security",
+                "sftp",
+                "signature",
+                "signing",
+                "ssh",
+                "x.509",
+                "x509"
+            ],
+            "time": "2013-06-21 01:51:50"
+        }
+    ],
+    "packages-dev": [
+
+    ],
+    "aliases": [
+
+    ],
+    "minimum-stability": "dev",
+    "stability-flags": {
+        "phpseclib/phpseclib": 20
+    },
+    "platform": [
+
+    ],
+    "platform-dev": [
+
+    ]
+}

src/Deeploy/Deeploy.php

+<?php
+namespace Deeploy;
+
+/**
+ * Deep Loy
+ */
+class Deeploy {
+	// --------------------------------------------------------- Variables ---------------------------------------------------------
+	/**
+	 * @var stdClass
+	 */
+	protected $config;
+
+	/**
+	 * @var array		An array of servers on which we want to run some routines
+	 */
+	protected $servers;
+
+	/**
+	 * @var array		An array of routines that we want to execute
+	 */
+	protected $routines;
+
+	/**
+	 * @var Net_SSH2	The server instance that's currently active
+	 */
+	protected $server;
+
+
+
+	// ---------------------------------------------------------- Methods ----------------------------------------------------------
+	/**
+	 * Constructor Method
+	 *
+	 * @param string		$config		A JSON formatted string with deploy options specified.
+	 */
+	public function __construct($config = '') {
+		// Typecast
+		$config = (string) $config;
+
+		// If the config was set, try to read it
+		if (!empty($config)) {
+			$this->config($config);
+		}
+
+		// Prepare servers and routines variables
+		$this->servers = array();
+		$this->routines = array();
+	}
+
+
+	/**
+	 * Method to read out the config data
+	 *
+	 * @param string		$config		A JSON formatted string with deploy options specified.
+	 */
+	public function config($config) {
+		// Typecast
+		$config = (string) $config;
+
+		// Decode the json
+		$this->config = @json_decode($config);
+
+		// Do some checks
+		if (empty($this->config)) {
+			throw new \Exception('Wrong config input');
+		}
+
+		// Return
+		return $this;
+	}
+
+
+	/**
+	 * Method to set a list of servers
+	 *
+	 * @param array			$servers	A list of servers to deploy on.
+	 */
+	public function servers($servers) {
+		// Typecast
+		$servers = (array) $servers;
+
+		// Walk through the list of servers and check them
+		foreach ($servers as $server) {
+			// Check if the server exists
+			if (!isset($this->config->$server)) {
+				// Throw an exception and continue
+				throw new \Exception('The server "'.$server.'" does not seem to exist.');
+				continue;
+			}
+
+			// Add the server to the list
+			$this->servers[] = $server;
+		}
+
+		// Return
+		return $this;
+	}
+
+
+	/**
+	 * Method to set a list of routines to run
+	 *
+	 * @param array			$routines	A list of routines to run.
+	 */
+	public function routines($routines) {
+		// Typecast
+		$routines = (array) $routines;
+
+		// Walk through the list of routines and check them
+		foreach ($routines as $routine) {
+			// Check if the routine exists
+			if (!method_exists($this, $routine)) {
+				// Throw an exception and continue
+				throw new \Exception('The routine "'.$routine.'" does not seem to exist.');
+				continue;
+			}
+
+			// Add the routine to the list
+			$this->routines[] = $routine;
+		}
+
+		// Return
+		return $this;
+	}
+
+
+	/**
+	 * Method to run the desired routines on the desired servers
+	 */
+	public function run() {
+		// Walk through the list of servers
+		foreach ($this->servers as $server) {
+			// Log in to the server
+			$this->login($server);
+
+			// Walk through the list of routines and execute them on the server
+			foreach ($this->routines as $routine) {
+				// Check if the routine exists
+				if (!method_exists($this, $routine)) {
+					// Throw an exception and continue
+					throw new \Exception('The routine "'.$routine.'" does not seem to exist, so we did not run it.');
+					continue;
+				}
+
+				// Run the routine
+				$this->$routine();
+			}
+		}
+	}
+
+
+	/**
+	 * Method to log in to a server
+	 *
+	 * @param string		$server		The name of the server
+	 */
+	protected function login($server) {
+		// Load new private key instance
+		$key = new \Crypt_RSA();
+
+		// If the key is protected with a password, fill it in
+		if (isset($this->config->$server->keypass)) {
+			$key->setPassword($this->config->$server->keypass);
+		}
+
+		// Load the key file
+		$key->loadKey(@file_get_contents($this->config->$server->keyfile));
+
+		// Prepare for the ssh connection
+		$this->server = new \Net_SSH2($this->config->$server->host);
+
+		// Login to the server
+		if (!$this->server->login($this->config->$server->user, $key)) {
+			// If we failed, throw an error
+			throw new \Exception('Could not log in to the server using '.$this->config->$server->user.'@'.$this->config->$server->host);
+		}
+	}
+}