Commits

Anonymous committed ca6b7d1

Mostly upgraded aws command

Comments (0)

Files changed (1)

djaboto/management/commands/aws.py

             dest='sitename',
             help='Name of site (directory, "mysite")',
         ),
+        optparse.make_option('--repo',
+            action='store',
+            type='string',
+            dest='git_repo',
+            default=None,
+            help='URL of the git repo',
+        ),
         optparse.make_option('--instance',
             action='store',
             type='string',
         if options.get('sitename', None):
             fab_api.env['SITENAME'] = options['sitename']
             fab_api.env['unsaved'] = True
+        if options.get('git_repo', False):
+            fab_api.env['git_repo'] = options['git_repo']
         fab_api.env['rcfile'] = options.get('fabricrc', './.aws_fabric')
 
         ###TODO: this should be yanked and replaced with a boto config file solution instead
             elif 'firewall' in args:
                 self.set_awsSecurityGroups()
             elif 'create' in args:
-                self.set_awsSSHKey(ssh_pubkey_file)
-                self.set_awsSecurityGroups()
-                self.install_awsUbuntuUpgrades()
+                # self.set_awsSSHKey(ssh_pubkey_file)
+                # self.set_awsSecurityGroups()
+                # self.install_awsUbuntuUpgrades()
                 ###TODO: need to enable this as default, as soon as the function works safely
                 #self.set_awsElasticIP()
                 self.sync_awsProject(push=True)
-                self.install_awsPythonRequirements(reinstall=True)
-                self.aws_DropDB()
-                self.sync_awsDB()
+                # self.install_awsPythonRequirements(reinstall=True)
+                ###TODO: DropDB needs to use the RDS Settings
+                # self.aws_DropDB()
+                # self.sync_awsDB()
                 self.sync_awsStaticfiles()
                 self.set_awsPermissions()
                 self.set_awsApacheconf(options.get('domain', None))
 
         instance = self.get_awsInstance()
 
-        ###TODO: Add -ignore flags for unison call
+        if fab_api.env.git_repo is not None:
 
-        here = os.getcwd()
-        there = "ssh://ubuntu@%s//home/ubuntu/django/%s" % (instance.ip_address ,fab_api.env.SITENAME)
+            if not fab_exists('~/.ssh/id_rsa'):
+                if not fab_exists('~/django/tmp/%s/etc/ssh' % fab_api.env.SITENAME) and not fab_exists('~/django/%s/etc/ssh/id_rsa' % fab_api.env.SITENAME):
+                    fab_api.run('mkdir -p /home/ubuntu/django/tmp/%s/etc/ssh' % fab_api.env.SITENAME)
+                    fab_api.run('ssh-keygen -f /home/ubuntu/django/tmp/%s/etc/ssh/id_rsa -N ""' % fab_api.env.SITENAME)
+                    fab_api.run('cat /home/ubuntu/django/tmp/%s/etc/ssh/id_rsa.pub' % fab_api.env.SITENAME)
+                    fab_api.prompt('Press return once the key has been added to the repo', '')
+                    fab_api.run('cp /home/ubuntu/django/tmp/%s/etc/ssh/id_rsa* /home/ubuntu/.ssh/' % fab_api.env.SITENAME)
+                elif fab_exists('~/django/%s/etc/ssh/id_rsa' % fab_api.env.SITENAME) and not fab_exists('~/.ssh/id_rsa' % fab_api.env.SITENAME):
+                    fab_api.run('cp /home/ubuntu/django/%s/etc/ssh/id_rsa* /home/ubuntu/.ssh/' % fab_api.env.SITENAME)
 
-        print(fab_colors.yellow("Synchronizing project files..."))
 
-        with fab_api.settings(warn_only=True): # unison has return code 1 when no changes
-            if push:
-                fab_api.local('unison -ignore "Path httpdocs"  -silent -force %s %s %s' % (here, here, there))
+            if not fab_exists('~/django/%s' % fab_api.env.SITENAME):
+                fab_api.run('git clone %s /home/ubuntu/django/%s' % (fab_api.env.git_repo ,fab_api.env.SITENAME))
+
             else:
-                fab_api.local('unison -auto -ignore "Path httpdocs"  %s %s' % (here, there))
+                fab_api.cd('/home/ubuntu/django/%s/' % fab_api.env.SITENAME)
+                fab_api.run('cd /home/ubuntu/django/%s/; git pull' % fab_api.env.SITENAME)
+
+
+            if fab_exists('~/django/tmp/%s/etc/ssh/id_rsa' % fab_api.env.SITENAME):
+                fab_api.run('mv /home/ubuntu/django/tmp/%s/etc/ssh/id_rsa /home/ubuntu/django/%s/etc/ssh/id_rsa' % (fab_api.env.SITENAME, fab_api.env.SITENAME))
+            if fab_exists('~/django/tmp/%s/etc/ssh/id_rsa.pub' % fab_api.env.SITENAME):
+                fab_api.run('mv /home/ubuntu/django/tmp/%s/etc/ssh/id_rsa.pub /home/ubuntu/django/%s/etc/ssh/id_rsa.pub' % (fab_api.env.SITENAME, fab_api.env.SITENAME))
+            if fab_exists('~/django/tmp/%s' % fab_api.env.SITENAME):
+                fab_api.run('rm -rf /home/ubuntu/django/tmp/%s' % fab_api.env.SITENAME)
+
+
+        else:
+            ###TODO: Add -ignore flags for unison call
+
+            here = os.getcwd()
+            there = "ssh://ubuntu@%s//home/ubuntu/django/%s" % (instance.ip_address ,fab_api.env.SITENAME)
+
+            print(fab_colors.yellow("Synchronizing project files..."))
+
+            with fab_api.settings(warn_only=True): # unison has return code 1 when no changes
+                if push:
+                    fab_api.local('unison -ignore "Path httpdocs"  -silent -force %s %s %s' % (here, here, there))
+                else:
+                    fab_api.local('unison -auto -ignore "Path httpdocs"  %s %s' % (here, there))
 
     def sync_awsPurify(self):
         """
 
         with fab_api.settings(
                             fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
-                            fab_api.prefix('source ~/django/python/bin/activate')):
+                            fab_api.prefix('source ~/django/python/bin/activate'),
+                            fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
+                            fab_api.prefix('source ~/django/%s/bin/remote_data' % fab_api.env.SITENAME)):
             fab_api.run('python manage.py reset --noinput menus')
 
 
 
         with fab_api.settings(
                             fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
-                            fab_api.prefix('source ~/django/python/bin/activate')):
+                            fab_api.prefix('source ~/django/python/bin/activate'),
+                            fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
+                            fab_api.prefix('source ~/django/%s/bin/remote_data' % fab_api.env.SITENAME)):
 
             # Update the database and link all media files
             fab_api.run('python manage.py syncdb --noinput --verbosity=0')
         ###TODO: the directory should be puled from the settings.py file
         with fab_api.settings(
                             fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
-                            fab_api.prefix('source ~/django/python/bin/activate')):
+                            fab_api.prefix('source ~/django/python/bin/activate'),
+                            fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
+                            fab_api.prefix('source ~/django/%s/bin/remote_data' % fab_api.env.SITENAME)):
 
             # Link the media files
 
             if fab_exists('~/django/%s/httpdocs/static' % fab_api.env.SITENAME):
                 fab_api.sudo('rm -rf ~/django/%s/httpdocs/static' % fab_api.env.SITENAME)
+            if not fab_exists('~/django/%s/httpdocs/media' % fab_api.env.SITENAME):
+                fab_api.sudo('mkdir -p ~/django/%s/httpdocs/media' % fab_api.env.SITENAME)
             fab_api.run('python manage.py collectstatic -l --noinput --verbosity=0')
 
     def set_awsApacheconf(self, domain=None):
         template = os.path.join('etc','apache2_vhost.conf')
         fab_op.put(template, target, use_sudo=True)
 
-        if fab_exists('/etc/apache2/sites-enabled/000-default'):
-            fab_api.prompt('Disable 000-default? (y/n) ', 'nopchdflt')
+        if fab_exists('/etc/apache2/sites-enabled/000-default') or fab_exists('/etc/apache2/sites-enabled/default'):
+            fab_api.prompt('Disable [000-]default? (y/n) ', 'nopchdflt')
             if fab_api.env.nopchdflt == 'y':
-                fab_api.sudo('a2dissite 000-default')
+                if fab_exists('/etc/apache2/sites-enabled/000-default'):
+                    fab_api.sudo('a2dissite 000-default')
+                if fab_exists('/etc/apache2/sites-enabled/default'):
+                    fab_api.sudo('a2dissite default')
 
         fab_api.sudo('a2ensite %s.conf' % fab_api.env.domain)
 
 
         with fab_api.settings(
                             fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
-                            fab_api.prefix('source ~/django/python/bin/activate')):
+                            fab_api.prefix('source ~/django/python/bin/activate'),
+                            fab_api.cd('~/django/%s' % fab_api.env.SITENAME),
+                            fab_api.prefix('source ~/django/%s/bin/remote_data' % fab_api.env.SITENAME)):
 
             for cmd in args[0]:
                 fab_api.run(command='python manage.py %s' % cmd)
 
         print(fab_colors.yellow("Settings apropriate default permissions on project files..."))
 
-        fab_api.sudo('chown -R %s:%s ~/django/%s/%s/static' % (fab_api.env.AWS_USER, fab_api.env.AWS_HTTPD_GROUP, fab_api.env.SITENAME, fab_api.env.SITENAME))
-        fab_api.sudo('chmod -R ug+rw ~/django/%s/%s/static' % (fab_api.env.SITENAME, fab_api.env.SITENAME))
+        if not fab_exists('~/django/%s/httpdocs/media' % fab_api.env.SITENAME):
+            fab_api.sudo('mkdir -p ~/django/%s/httpdocs/media' % fab_api.env.SITENAME)
+
+        fab_api.sudo('chown -R %s:%s ~/django/%s/httpdocs/media' % (fab_api.env.AWS_USER, fab_api.env.AWS_HTTPD_GROUP, fab_api.env.SITENAME))
+        fab_api.sudo('chmod -R ug+rw ~/django/%s/httpdocs/media' % (fab_api.env.SITENAME))
 
         # Add access to the webserver to write into the cache directory
         fab_api.sudo('chown -R %s:%s ~/django/%s/' % (fab_api.env.AWS_USER, fab_api.env.AWS_HTTPD_GROUP, fab_api.env.SITENAME))