Ejemplo n.º 1
0
    def init(self, delete_if_exists, python_path=""):
        if self.use_virtualenv:
            virtualenv_full_path = path(self.virtualenv_path).joinpath(self.virtualenv_name)
            if cuisine.dir_exists(virtualenv_full_path) and delete_if_exists:
                dir_delete(virtualenv_full_path)

            with cuisine_sudo():
                pip_install(['virtualenv'])

            with cuisine_sudo():
                dir_ensure(self.virtualenv_path, recursive=True, mode=777)
                dir_attribs(self.virtualenv_path, mode=777, recursive=True)

            # make lib synlinks to install PIL correctly
            with cuisine_sudo():
                if not file_exists("/usr/lib/libfreetype.so"):
                    run("ln -sf /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/")

                if not file_exists("/usr/lib/libz.so"):
                    run("ln -sf /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/")

                if not file_exists("/usr/lib/libjpeg.so"):
                    run("ln -sf /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/")

            with cd(self.virtualenv_path):
                run('VIRTUALENV_EXTRA_SEARCH_DIR="%s" && virtualenv %s' % (python_path, self.virtualenv_name))
Ejemplo n.º 2
0
    def configure_webserver(self, name, config, delete_other_sites=False):
        if delete_other_sites:
            with cuisine_sudo():
                clear_dir('/etc/apache2/sites-enabled')

        with cuisine_sudo():
            cuisine.file_write('/etc/apache2/sites-enabled/%s' % name, config)
            print("Apache configured\n%s" % config)
Ejemplo n.º 3
0
    def upload_code(self, update_submodules=True):
        self.before_upload_code()

        # we need to ensure the directory is open for writing
        with cuisine_sudo():
            dir_attribs(self.remote_project_path, mode="777")

        temp_dir_prefix = "django_temp_"

        # zip and upload file
        temp_dir = temp_dir_prefix + self.project_name + "_" + timestamp_str()

        temp_remote_path = path(remote_home()).joinpath("tmp").joinpath(temp_dir)
        temp_local_path = path(self.project_local_path).joinpath(temp_dir)
        local_dir_ensure(temp_local_path)
        with cuisine_sudo():
            dir_ensure(temp_remote_path, recursive=True, mode="666")

        files = self.scm.local_archive(temp_local_path, include_submodules=update_submodules)

        # upload files
        for dir, file in files.iteritems():
            local_archive_path = temp_local_path.joinpath(file)
            remote_archive_path = temp_remote_path.joinpath(file)
            operations.put(str(local_archive_path), str(temp_remote_path), use_sudo=True)
            local_file_delete(local_archive_path)

        # reset project dir
        self.reset_project_dir()

        # unpack files
        for dir, file in files.iteritems():
            remote_archive_path = temp_remote_path.joinpath(file)

            # unzip file
            with cuisine_sudo():
                if self.webserver:
                    self.webserver.stop()

                extdir = path(self.remote_project_path).joinpath(dir).abspath()
                dir_ensure(extdir, recursive=True, mode="777")
                file_unzip(remote_archive_path, extdir)
                file_delete(remote_archive_path)

        cuisine.run("cd %s" % self.src_root)
        cuisine.run("pwd")

        with cuisine_sudo():
            cuisine.dir_attribs(self.remote_project_path, mode="777", recursive=True)

        for precomp in self.precompilers:
            precomp.compile()

        # clear old archives
        local_dirs_delete(self.project_local_path, "%s%s.*" % (temp_dir_prefix, self.project_name))

        ## upload ends here
        self.after_upload_code()
Ejemplo n.º 4
0
    def configure_webserver(self, name, config, delete_other_sites=False):
        if delete_other_sites:
            with cuisine_sudo():
                clear_dir('/etc/apache2/sites-enabled')

        with cuisine_sudo():
            cuisine.file_write('/etc/apache2/sites-enabled/%s' % name, config)
            run("ln -fs /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load")
            run("ln -s /etc/apache2/mods-available/ssl.load /etc/apache2/mods-enabled/ssl.load")

            print("Apache configured\n%s" % config)
Ejemplo n.º 5
0
    def upload_code(self, update_submodules=True):
        if self.webserver:
            self.webserver.stop()

        self.before_upload_code()

        # we need to ensure the directory is open for writing
        with cuisine_sudo():
            dir_attribs(self.remote_project_path, mode='777')

        self.clear_remote_project_path_save_site()

        temp_dir_prefix = 'django_temp_'

        # zip and upload file
        temp_dir = temp_dir_prefix + self.project_name + '_' + timestamp_str()

        temp_remote_path = path(self.remote_project_path).joinpath(temp_dir)
        temp_local_path = path(self.project_local_path).joinpath(temp_dir)
        local_dir_ensure(temp_local_path)
        dir_ensure(temp_remote_path)

        files = self.scm.local_archive(temp_local_path, include_submodules=update_submodules)

        for dir, file in files.iteritems():
            local_archive_path = temp_local_path.joinpath(file)
            remote_archive_path = temp_remote_path.joinpath(file)
            put(str(local_archive_path), str(temp_remote_path), use_sudo=True)
            local_file_delete(local_archive_path)

            #unzip file
            with cuisine_sudo():
                extdir = path(self.remote_project_path).joinpath(dir).abspath()
                dir_ensure(extdir, recursive=True, mode='777')
                file_unzip(remote_archive_path, extdir)
                file_delete(remote_archive_path)

        cuisine.run("cd %s" % self.src_root)
        cuisine.run("pwd")

        with cuisine_sudo():
            cuisine.dir_attribs(self.remote_project_path, mode="777", recursive=True)

        for precomp in self.precompilers:
            precomp.compile()

        # clear old archives
        local_dirs_delete(self.project_local_path, '%s%s.*' % (temp_dir_prefix, self.project_name))

        ## upload ends here
        self.after_upload_code()

        if self.webserver:
            self.webserver.start()
Ejemplo n.º 6
0
    def init(self, delete_if_exists, python_path=""):
        if self.use_virtualenv:
            virtualenv_full_path = path(self.virtualenv_path).joinpath(self.virtualenv_name)
            if cuisine.dir_exists(virtualenv_full_path) and delete_if_exists:
                dir_delete(virtualenv_full_path)

            with cuisine_sudo():
                pip_install(['virtualenv'])

            with cuisine_sudo():
                dir_ensure(self.virtualenv_path, recursive=True, mode=777)
                dir_attribs(self.virtualenv_path, mode=777, recursive=True)

            with cd(self.virtualenv_path):
                run('VIRTUALENV_EXTRA_SEARCH_DIR="%s" && virtualenv %s' % (python_path, self.virtualenv_name))
Ejemplo n.º 7
0
    def restore_latest_media(self):
        dump_basename = self.latest_media_dump_basename()

        dump_local_path = self.local_media_dump_dir.joinpath(dump_basename)
        dump_remote_path = path(self.django.media_root).joinpath(dump_basename)

        put(str(dump_local_path), str(dump_remote_path), use_sudo=True, mode=0777)

        with cd(self.django.media_root):
            sudo("tar -xvzf %s" % dump_remote_path)

        with cuisine_sudo():
            dir_attribs(self.django.media_root, mode='777', recursive=True)

        with cuisine_sudo(): file_delete(dump_remote_path)
Ejemplo n.º 8
0
    def process_postgresql_conf(self):
        with cuisine_sudo():
            pg_hba = cuisine.file_read(self.postgresql_conf_path())

            # replaces "#listen_addresses = 'localhost'	" type of lines
            # with "listen_addresses = '*'"
            new_text, replaced = text_replace_line_re(pg_hba, ".*listen_addresses\s*=", "listen_addresses = '*'	")

        return new_text
Ejemplo n.º 9
0
    def compile(self):
        super(CoffeePrecompiler, self).compile()

        abs_dir_from = self.abs_dir_from()
        abs_dir_to = self.abs_dir_to()

        with cuisine.cuisine_sudo():
            dir_ensure(abs_dir_to, mode='777', recursive=True)
        
        cuisine.sudo('sudo coffee --compile --output %(abs_dir_to)s %(abs_dir_from)s' % {'abs_dir_from': abs_dir_from, 'abs_dir_to': abs_dir_to})
Ejemplo n.º 10
0
    def set_debug(self, debug):
        with cuisine_sudo():
            settings_file_path = path(self.src_root).joinpath(self.settings_module.replace(".", "/") + ".py")
            settings_content = cuisine.file_read(settings_file_path)

            # replaces "#listen_addresses = 'localhost'	" type of lines
            # with "listen_addresses = '*'"
            settings_content, replaced = text_replace_line_re(settings_content, "^DEBUG\s*=", "DEBUG=%s" % debug)

            return cuisine.file_write(settings_file_path, unix_eol(settings_content)), replaced
Ejemplo n.º 11
0
    def backup_database(self, filename, zip=False, folder=None):
        folder = folder or self.db_backup_folder

        with cuisine.cuisine_sudo():
            cuisine.dir_ensure(folder, recursive=True, mode="777")

        file_full_path = "/".join([folder, filename])

        with self.pg_pass():
            sudo_pipeline(("pg_dump -O -x %s | gzip > %s" if zip else "pg_dump %s > %s")
            % (self.database_name, file_full_path), user=self.superuser_login)
Ejemplo n.º 12
0
    def download_media(self):
        media_dump_basename = "%s_media_%s.tar.gz" % (self.django.project_name, timestamp_str())
        media_dump_remote_path = "%s/%s" % (remote_home(), media_dump_basename)

        media_dump_local_path = self.local_media_dump_dir.joinpath(media_dump_basename)

        with cd(self.django.media_root): cuisine.run("tar -cvzf %s ." % media_dump_remote_path)
        cuisine.file_attribs(media_dump_remote_path, '777')

        get(media_dump_remote_path, media_dump_local_path)

        with cuisine_sudo(): file_delete(media_dump_remote_path)
Ejemplo n.º 13
0
    def restore_latest_db_dump(self):
        dump_basename = self.latest_db_dump_basename()
        dump_path = path(self.local_db_dump_dir).joinpath(dump_basename)
        remote_dump_path = "~/%s" % dump_basename

        put(dump_path, "")

        self.database.init_database(init_sql_file=remote_dump_path, delete_if_exists=True, unzip=True)

        self.django.migrate_data()

        with cuisine_sudo(): file_delete(remote_dump_path)
Ejemplo n.º 14
0
    def download_db_dump(self):
        remote_file_basename = self._create_db_backup_name()
        remote_dir = "/tmp"
        remote_file_path = "%s/%s" % (remote_dir, remote_file_basename)

        dir_ensure(remote_dir, mode='777')
        self.database.backup_database(remote_file_basename, folder=remote_dir, zip=True)

        local_dir_ensure(self.local_db_dump_dir)
        get(remote_file_path, self.local_db_dump_dir)

        with cuisine_sudo(): file_delete(remote_file_path)
Ejemplo n.º 15
0
    def process_pg_hba_conf(self):
        with cuisine_sudo():
            pg_hba = cuisine.file_read(self.pg_hba_path())

            # replaces "host all all 127.0.0.1/32 md5" type of lines
            # with "host all all 0.0.0.0/0 md5"
            host_replaced, replaced = text_replace_line_re(pg_hba, "^host[\s\w\./]+$", "host\tall\tall\t0.0.0.0/0\tmd5")

            local_replaced, replaced = text_replace_line_re(
                host_replaced, "^local\s+all\s+all.*$", "local\tall\tall\t\tmd5"
            )

        return local_replaced
Ejemplo n.º 16
0
    def compile(self):
        super(CoffeePrecompiler, self).compile()

        abs_dir_from = self.abs_dir_from()
        abs_dir_to = self.abs_dir_to()

        with cuisine.cuisine_sudo():
            dir_ensure(abs_dir_to, mode="777", recursive=True)

        cuisine.sudo(
            "sudo coffee --compile --output %(abs_dir_to)s %(abs_dir_from)s"
            % {"abs_dir_from": abs_dir_from, "abs_dir_to": abs_dir_to}
        )
Ejemplo n.º 17
0
    def setup_dependencies(self):
        file_dependencies = []
        if self.req_file:
            with open(self.req_file, 'r') as file:
                dep_str = file.read()
                file_dependencies = [str.split('==') for str in dep_str.split('\n') if str != '']

        if self.use_virtualenv:
            with virtualenv(self.virtualenv_path, self.virtualenv_name):
                pip_install(self.dependencies)
                pip_install(file_dependencies)
        else:
            with cuisine_sudo():
                pip_install(self.dependencies)
                pip_install(file_dependencies)
Ejemplo n.º 18
0
    def setup_dependencies(self):
        # file_dependencies = []
        # if self.req_file:
        #    with open(self.req_file, 'r') as file:
        #        dep_str = file.read()
        #        file_dependencies = [str.split('==') for str in dep_str.split('\n') if str != '']
        #

        temp_path = path(remote_home()).joinpath("tmp")
        with cuisine_sudo():
            dir_ensure(temp_path)

        operations.put(self.req_file, str(temp_path), use_sudo=True)

        remote_req_file = temp_path.joinpath(path(self.req_file).basename())

        if self.use_virtualenv:
            with virtualenv(self.virtualenv_path, self.virtualenv_name):
                pip_install(self.dependencies)
                pip_install_file_requirements(remote_req_file)
        else:
            with cuisine_sudo():
                pip_install(self.dependencies)
                pip_install_file_requirements(remote_req_file)
Ejemplo n.º 19
0
    def reset_project_dir(self):
        home_dir = remote_home()
        site_path_basename = path(self.remote_site_path).name
        with cuisine_sudo():
            cuisine.dir_ensure(self.remote_site_path, recursive=True, mode="777")
            cuisine.dir_ensure("%s/tmp" % home_dir, mode="777")
            dir_delete("%(home_dir)s/tmp/%(site_path_basename)s" % locals())
            cuisine.run("mv %(site_dir)s %(home_dir)s/tmp" % {"site_dir": self.remote_site_path, "home_dir": home_dir})

            clear_dir(self.remote_project_path)

            # restore site dir
            cuisine.run(
                "mv %(home_dir)s/tmp/%(site_dir_basename)s %(proj_path)s"
                % {"site_dir_basename": site_path_basename, "proj_path": self.remote_project_path, "home_dir": home_dir}
            )
Ejemplo n.º 20
0
    def compile(self):
        super(LessPrecompiler, self).compile()
        abs_dir_from = self.abs_dir_from()
        abs_dir_to = self.abs_dir_to()

        with cuisine.cuisine_sudo():
            dir_ensure(abs_dir_to, mode='777', recursive=True)

        for a_file in ls_re(abs_dir_from, '.*\\.less'):
            cuisine.sudo('%(lessc_path)s %(dir_from)s/%(basename)s.less %(dir_to)s/%(basename)s.css' % \
             {
                 'lessc_path': self.lessc_path(),
                 'dir_from': abs_dir_from,
                 'dir_to': abs_dir_to,
                 'basename': a_file[:-5]
             })
Ejemplo n.º 21
0
    def clear_remote_project_path_save_site(self):
        home_dir = remote_home()
        site_path_basename = path(self.remote_site_path).name
        with cuisine_sudo():
            cuisine.dir_ensure(self.remote_site_path, recursive=True, mode='777')
            cuisine.dir_ensure("%s/tmp" % home_dir, mode='777')
            dir_delete("%(home_dir)s/tmp/%(site_path_basename)s" % locals())
            cuisine.run('mv %(site_dir)s %(home_dir)s/tmp' % {'site_dir': self.remote_site_path, 'home_dir': home_dir})

            clear_dir(self.remote_project_path)


            #restore site dir
            cuisine.run('mv %(home_dir)s/tmp/%(site_dir_basename)s %(proj_path)s' % {
                'site_dir_basename': site_path_basename,
                'proj_path': self.remote_project_path,
                'home_dir': home_dir
            })
Ejemplo n.º 22
0
    def compile(self):
        super(LessPrecompiler, self).compile()
        abs_dir_from = self.abs_dir_from()
        abs_dir_to = self.abs_dir_to()

        with cuisine.cuisine_sudo():
            dir_ensure(abs_dir_to, mode="777", recursive=True)

        for a_file in ls_re(abs_dir_from, ".*\\.less"):
            cuisine.sudo(
                "%(lessc_path)s %(dir_from)s/%(basename)s.less %(dir_to)s/%(basename)s.css"
                % {
                    "lessc_path": self.lessc_path(),
                    "dir_from": abs_dir_from,
                    "dir_to": abs_dir_to,
                    "basename": a_file[:-5],
                }
            )
Ejemplo n.º 23
0
    def backup_database(self, filename, zip=False, folder=None, ignore_tables=None):
        folder = folder or self.db_backup_folder

        with cuisine.cuisine_sudo():
            cuisine.dir_ensure(folder, recursive=True, mode="777")

        file_full_path = "/".join([folder, filename])

        additional_argument = ""

        if ignore_tables:
            for table in ignore_tables:
                additional_argument = "%s -T '%s'" % (additional_argument, table)

        with self.pg_pass():
            sudo_pipeline(
                ("pg_dump -O -x %s %s | gzip > %s" if zip else "pg_dump %s %s > %s")
                % (self.database_name, additional_argument, file_full_path),
                user=self.superuser_login,
            )
Ejemplo n.º 24
0
    def init(self):
        """
        Should be called at least one before uploading the code. Creates project dirs and copies static_upload files.
        """
        with cuisine_sudo():
            cuisine.dir_ensure(self.remote_project_path, recursive=True,
                owner=self.webserver.webserver_user, group=self.webserver.webserver_group)

            cuisine.dir_ensure(self.remote_site_path, recursive=True,
                owner=self.webserver.webserver_user, group=self.webserver.webserver_group)

            cuisine.dir_ensure(self.log_path, recursive=True,
                owner=self.webserver.webserver_user, group=self.webserver.webserver_group)

            cuisine.dir_ensure(self.media_root, recursive=True,
                owner=self.webserver.webserver_user, group=self.webserver.webserver_group)

            cuisine.dir_ensure(self.static_root, recursive=True,
                owner=self.webserver.webserver_user, group=self.webserver.webserver_group)

        self.configure_virtualenv()
Ejemplo n.º 25
0
 def configure(self, enable_remote_access=False):
     if enable_remote_access:
         with cuisine_sudo():
             cuisine.file_write(self.pg_hba_path(), unix_eol(self.process_pg_hba_conf()))
             cuisine.file_write(self.postgresql_conf_path(), unix_eol(self.process_postgresql_conf()))