# License under the MIT License - see LICENSE


from boto.manage.cmdshell import sshclient_from_instance

from launch_instance import launch


def create_root_drive(name, region='us-west-2', key_name='admin_root_maker',
                      orig_image_id="ami-5189a661"):
    '''
    Creates the root drive for AstroCompute instances.
    '''

    kwargs = {"install_casa": True,
              "install_miniconda": True}

    instance = launch(key_name, region=region, image_id=orig_image_id,
                      install_packages=True, kwargs=kwargs)


def install_packages(instance, path_to_key, install_casa=True,
                     install_miniconda=False,
                     casa_version="4.3", user_name='ubuntu'):
    '''
    Install packages in a running instance.

    Requires paramiko for the SSH connection.

    Parameters
    ----------
    instance : Running instance object
        A running instance.
    path_to_key : str
        Path to the SSH key attached to the instance.
    install_casa : bool, optional
        Install CASA on the VM.
    install_miniconda : bool, optional
        Installs a miniconda environment on the VM. Needed to run the
        web-server.
    casa_version : str {4.3}, optional
        Version of CASA to install. Currently only 4.3 is supported.
    '''

    if casa_version != "4.3":
        raise TypeError("Only CASA 4.3 is currently supported.")

    # The submodule requires some fiddling around, as it is setup to use SSH
    # keys. The extra lines allow use of https instead.
    # http://stackoverflow.com/questions/15674064/github-submodule-access-rights-travis-ci
    run_script = \
        """
        #!/bin/bash

        apt-get update
        apt-get -y install git

        cd $HOME

        mkdir code

        cd $HOME/code

        git clone https://github.com/Astroua/aws_controller.git
        cd aws_controller
        sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules
        sed -i 's/git@github.com:/https:\/\/github.com\//' .git/config
        git submodule update --init --recursive

        cd $HOME
        chown -R ubuntu:ubuntu code

        sh $HOME/code/aws_controller/casa-deploy/general_install.sh
        """

    if install_casa:
        casa_install_script = "deploy_casa"+str(casa_version)+".sh"
        run_script = run_script + \
            "sh $HOME/aws_controller/casa-deploy/"+casa_install_script+"\n"
        run_script = run_script + \
            "sh $HOME/aws_controller/casa-deploy/install_casa_pip.sh\n"
        run_script = run_script + \
            "sh $HOME/aws_controller/casa-deploy/install_casa_packages.sh\n"

    if install_miniconda:
        run_script = run_script + \
            "sh $HOME/aws_controller/casa-deploy/install_miniconda.sh\n"

    # Start-up the SSH connection
    ssh_shell = sshclient_from_instance(instance, path_to_key,
                                        user_name=user_name)

@@ -1645,52 +1645,10 @@
 pt =
- %5C%0A        %22%22%22%0A        #!/bin/bash%0A%0A        
+%5B%22
 apt-
@@ -1657,25 +1657,20 @@
 t update
-%0A        
+%22, %22
 apt-get 
@@ -1687,56 +1687,42 @@
  git
-%0A%0A        cd $HOME%0A%0A        mkdir code%0A%0A
+%22, %22mkdir code%22,%0A         
         
 cd $
@@ -1709,32 +1709,33 @@
                 
+%22
 cd $HOME/code%0A%0A 
@@ -1727,25 +1727,26 @@
 d $HOME/code
-%0A
+%22,
 %0A        git
@@ -1738,24 +1738,34 @@
 e%22,%0A        
+         %22
 git clone ht
@@ -1807,25 +1807,32 @@
 ller.git
-%0A
+%22,%0A     
         
 cd aws_c
@@ -1823,16 +1823,21 @@
         
+    %22
 cd aws_c
@@ -1845,16 +1845,18 @@
 ntroller
+%22,
 %0A       
@@ -1848,32 +1848,42 @@
 oller%22,%0A        
+         %22
 sed -i 's/git@gi
@@ -1928,16 +1928,18 @@
 tmodules
+%22,
 %0A       
@@ -1939,16 +1939,26 @@
         
+         %22
 sed -i '
@@ -2011,25 +2011,28 @@
 t/config
-%0A
+%22,%0A 
         
 git subm
@@ -2023,16 +2023,25 @@
         
+        %22
 git subm
@@ -2071,26 +2071,36 @@
 ecursive
-%0A%0A
+%22,%0A         
         
 cd $HOME
@@ -2091,24 +2091,25 @@
         
+%22
 cd $HOME
 %0A       
@@ -2100,16 +2100,18 @@
 cd $HOME
+%22,
 %0A       
@@ -2115,45 +2115,17 @@
     
-chown -R ubuntu:ubuntu code%0A%0A
+ 
         
 sh $
@@ -2120,16 +2120,17 @@
         
+%22
 sh $HOME
@@ -2184,20 +2184,10 @@
 l.sh
-%0A        %22%22
 %22
+%5D
 %0A%0A  

Separate into separate commands