commit 6868804aa474f49a3a90f3e3effa119831152050
Author: Ilias Tsitsimpis <iliastsi@grnet.gr>
Date:   Mon Aug 5 16:46:14 2013 +0300

    ci: Add --ssh-keys command line option
    
    Using this option one can specify a file containing
    ssh authentication keys to be uploaded and installed
    on the newly created server.

diff --git a/ci/new_config b/ci/new_config
index c3185ed..8aceff8 100644
--- a/ci/new_config
+++ b/ci/new_config
@@ -19,9 +19,9 @@ patch_pydist = True
 # Configuration of git (on remote server)
 git_config_name = Buildbot
 git_config_mail = synnefo@builder.dev.grnet.gr
-# Network address from which we allow access to server. If not set, access
-# to server is not restricted.
-filter_access_network =
+# Network address from which we allow access to server.
+# If not set, access to server is not restricted.
+accept_ssh_from =
 # Config file to save temporary options (eg IPs, passwords etc)
 temporary_config = /tmp/ci_temp_conf
 
@@ -36,6 +36,9 @@ server_name = Synnefo Deployment
 flavor_id = 639
 # Image to use (name must contain this)
 image_name = OldStable
+# File containing the ssh keys to upload/install to server
+# If not set, no ssh keys will be installed
+ssh_keys =
 
 
 [Burnin]
diff --git a/ci/snf-ci b/ci/snf-ci
index c5d78df..3db22fc 100755
--- a/ci/snf-ci
+++ b/ci/snf-ci
@@ -60,6 +60,9 @@ def main():  # Too many branches. pylint: disable-msg=R0912
                       help="Name of flavor to use for the server.")
     parser.add_option("-i", "--image", dest="image", default=None,
                       help="UUID of image to use for the server.")
+    parser.add_option("--ssh-keys", dest="ssh_keys", default=None,
+                      help="Upload/Install the public ssh keys contained"
+                           " in this file to the server")
     parser.add_option("--fetch-packages", dest="fetch_packages",
                       default=None,
                       help="Download the debian packages that were created"
@@ -102,7 +105,8 @@ def main():  # Too many branches. pylint: disable-msg=R0912
 
     if getattr(options, CREATE_SERVER_CMD, False):
         synnefo_ci.create_server(flavor_id=options.flavor,
-                                 image_id=options.image)
+                                 image_id=options.image,
+                                 ssh_keys=options.ssh_keys)
         synnefo_ci.clone_repo()
     if getattr(options, BUILD_SYNNEFO_CMD, False):
         synnefo_ci.build_synnefo()
diff --git a/ci/utils.py b/ci/utils.py
index 1990459..08243f4 100644
--- a/ci/utils.py
+++ b/ci/utils.py
@@ -36,6 +36,12 @@ def _run(cmd, verbose):
         return fabric.run(cmd)
 
 
+def _put(local, remote):
+    """Run fabric put command without output"""
+    with fabric.quiet():
+        fabric.put(local, remote)
+
+
 def _red(msg):
     """Red color"""
     #return "\x1b[31m" + str(msg) + "\x1b[0m"
@@ -226,7 +232,7 @@ class SynnefoCI(object):
             self._wait_transition(server_id, "ACTIVE", "DELETED")
 
     @_check_kamaki
-    def create_server(self, image_id=None, flavor_id=None):
+    def create_server(self, image_id=None, flavor_id=None, ssh_keys=None):
         """Create slave server"""
         self.logger.info("Create a new server..")
         if image_id is None:
@@ -251,11 +257,11 @@ class SynnefoCI(object):
 
         server = self._wait_transition(server_id, "BUILD", "ACTIVE")
         self._get_server_ip_and_port(server)
-        self._copy_ssh_keys()
+        self._copy_ssh_keys(ssh_keys)
 
         self.setup_fabric()
         self.logger.info("Setup firewall")
-        accept_ssh_from = self.config.get('Global', 'filter_access_network')
+        accept_ssh_from = self.config.get('Global', 'accept_ssh_from')
         if accept_ssh_from != "":
             self.logger.debug("Block ssh except from %s" % accept_ssh_from)
             cmd = """
@@ -300,18 +306,20 @@ class SynnefoCI(object):
         self.logger.debug("Server's IPv4 is %s" % _green(server_ip))
         self.write_config('server_port', server_port)
         self.logger.debug("Server's ssh port is %s" % _green(server_port))
+        self.logger.debug("Access server using \"ssh -p %s %s@%s\"" %
+                          (server_port, fabric.env.user, server_ip))
 
     @_check_fabric
-    def _copy_ssh_keys(self):
+    def _copy_ssh_keys(self, ssh_keys):
         """Upload/Install ssh keys to server"""
-        if not self.config.has_option("Deployment", "ssh_keys"):
-            return
-        authorized_keys = self.config.get("Deployment",
-                                          "ssh_keys")
-        if authorized_keys != "" and os.path.exists(authorized_keys):
+        self.logger.debug("Check for authentication keys to upload")
+        if ssh_keys is None:
+            ssh_keys = self.config.get("Deployment", "ssh_keys")
+
+        if ssh_keys != "" and os.path.exists(ssh_keys):
             keyfile = '/tmp/%s.pub' % fabric.env.user
             _run('mkdir -p ~/.ssh && chmod 700 ~/.ssh', False)
-            fabric.put(authorized_keys, keyfile)
+            _put(ssh_keys, keyfile)
             _run('cat %s >> ~/.ssh/authorized_keys' % keyfile, False)
             _run('rm %s' % keyfile, False)
             self.logger.debug("Uploaded ssh authorized keys")
@@ -506,8 +514,7 @@ class SynnefoCI(object):
             raise ValueError("Unknown schema: %s" % schema)
 
         self.logger.debug("Upload schema files to server")
-        with fabric.quiet():
-            fabric.put(os.path.join(schema_dir, "*"), "/etc/snf-deploy/")
+        _put(os.path.join(schema_dir, "*"), "/etc/snf-deploy/")
 
         self.logger.debug("Change password in nodes.conf file")
         cmd = """
@@ -536,8 +543,7 @@ class SynnefoCI(object):
 
         self.logger.debug("Upload tests.sh file")
         unit_tests_file = os.path.join(self.ci_dir, "tests.sh")
-        with fabric.quiet():
-            fabric.put(unit_tests_file, ".")
+        _put(unit_tests_file, ".")
 
         self.logger.debug("Run unit tests")
         cmd = """
