Exemplo n.º 1
0
def switch_packages(instance):
    """Switch Package symlinks, if no package symlinks are present add them.

    Arguments:
        instance {dict} -- full instance record
    """
    log.info('Instance | Switch package | Instance - %s', instance['sid'])
    log.debug('Instance | Switch package | Instance - %s', instance)
    instance_code_path_sid = '{0}/{1}/{1}'.format(INSTANCE_ROOT,
                                                  instance['sid'])
    # Get rid of old symlinks
    # List sites/all/{modules|themes|libraries} and remove all symlinks
    for package_type_path in ['modules', 'themes', 'libraries']:
        package_path = instance_code_path_sid + '/sites/all/' + package_type_path
        log.debug('Instance | Switch Packages | listdir - %s',
                  os.listdir(package_path))
        for item in os.listdir(package_path):
            # Get full path of item
            path = package_path + '/' + item
            log.debug('Instance | Switch Packages | Item to unlink - %s', path)
            if os.path.islink(path):
                os.remove(path)
    if 'package' in instance['code']:
        for item in instance['code']['package']:
            package = utilities.get_single_eve('code', item)
            package_path = utilities.code_path(package)
            package_type_path = utilities.code_type_directory_name(
                package['meta']['code_type'])
            destination_path = instance_code_path_sid + '/sites/all/' + \
                package_type_path + '/' + package['meta']['name']
            # Add new relative symlink
            if not os.access(destination_path, os.F_OK):
                utilities.relative_symlink(package_path, destination_path)
Exemplo n.º 2
0
def repository_remove(item):
    """
    Remove code from the local server.

    :param item:
    :return:
    """
    log.info('Code | Remove | Item - %s', item['_id'])
    log.debug('Code | Remove | Item - %s', item)
    shutil.rmtree(utilities.code_path(item))
Exemplo n.º 3
0
def update_symlink_current(item):
    """
    Determine the path for a code item
    """
    code_folder_current = '{0}/{1}/{2}/{2}-current'.format(
        CODE_ROOT,
        utilities.code_type_directory_name(item['meta']['code_type']),
        item['meta']['name'])
    # Remove symlink if it exists
    if os.path.islink(code_folder_current):
        os.unlink(code_folder_current)
    os.symlink(utilities.code_path(item), code_folder_current)
    log.debug('Code deploy | Symlink | %s', code_folder_current)
Exemplo n.º 4
0
def repository_checkout(item):
    """
    Checkout a code to the local server.

    :param item:
    :return:
    """
    log.info('Code | Checkout | Hash - %s', item['commit_hash'])
    log.debug('Code | Checkout | Item - %s', item)
    # Inializa and fetch repo
    repo = git.Repo(utilities.code_path(item))
    repo.remote().fetch()
    # Point HEAD to the correct commit and reset
    repo.head.reference = repo.commit(item['commit_hash'])
    repo.head.reset(index=True, working_tree=True)
Exemplo n.º 5
0
def repository_clone(item):
    """
    Clone code to the local server.

    :param item:
    :return:
    """
    log.info('Code | Clone | URL - %s', item['git_url'])
    log.debug('Code | Clone | Item - %s', item)
    code_dir = utilities.code_path(item)
    # Clone repo
    if os.path.exists(code_dir):
        raise Exception('Destinaton directory already exists')
    os.makedirs(code_dir)
    clone = git.Repo.clone_from(item['git_url'], code_dir)
    log.info('Code | Clone | Result - %s', clone)
Exemplo n.º 6
0
def deploy_static(item):
    """Deploy static asset to the web root
    
    Arguments:
        item {dict} -- Item record for static asset to deploy
    """
    log.info('Code | Deploy static')
    # Create symlink in web root
    web_root_static_name = WEB_ROOT + '/static/' + item['meta']['name']
    # Make static directories if needed.
    if not os.path.isdir(web_root_static_name):
        os.makedirs(web_root_static_name)
    # Remove symlink if it exists
    web_root_path = web_root_static_name + '/' + item['meta']['version']
    if os.path.islink(web_root_path):
        os.unlink(web_root_path)
    os.symlink(utilities.code_path(item), web_root_path)
Exemplo n.º 7
0
def switch_profile(instance):
    """Switch non-core Profile symlinks, if no appropriate symlinks are present add them.

    Arguments:
        instance {dict} -- full instance record
    """
    log.info('Instance | Switch profile | Instance - %s', instance['sid'])
    log.debug('Instance | Switch profile | Instance - %s', instance)
    # Lookup the profile we want to use.
    profile = utilities.get_single_eve('code', instance['code']['profile'])
    # Setup variables
    profile_path = utilities.code_path(profile)
    instance_code_path_sid = '{0}/{1}/{1}'.format(INSTANCE_ROOT,
                                                  instance['sid'])
    destination_path = instance_code_path_sid + '/profiles/' + profile['meta'][
        'name']
    # Remove old symlink
    if os.path.islink(destination_path):
        os.remove(destination_path)
    # Add new relative symlink
    if not os.access(destination_path, os.F_OK):
        utilities.relative_symlink(profile_path, destination_path)
Exemplo n.º 8
0
def switch_core(instance):
    """Switch Drupal core symlinks, if no core symlinks are present add them.

    Arguments:
        instance {dict} -- full instance record
    """
    # Lookup the core we want to use.
    core = utilities.get_single_eve('code', instance['code']['core'])
    # Setup variables
    core_path = utilities.code_path(core)
    instance_code_path_sid = '{0}/{1}/{1}'.format(INSTANCE_ROOT,
                                                  instance['sid'])
    # Get a list of files in the Core source directory
    core_files = os.listdir(core_path)
    # Get a list of files in the Instance target directory
    instance_files = os.listdir(instance_code_path_sid)
    # Remove any existing symlinks to a core.
    for instance_file in instance_files:
        full_path = instance_code_path_sid + '/' + instance_file
        # Check if path is a symlink.
        if os.path.islink(full_path):
            # Get the target of the symlink.
            symlink_target = os.readlink(full_path)
            # Get the name of the directory that contains the symlink target
            code_dir = os.path.dirname(symlink_target)
            # Check to see if the directory is a Drupal core, if so remove the symlink.
            regex = '((drupal)\-([\d\.x]+\-*[dev|alph|beta|rc|pl]*[\d]*))$i'
            if re.match(regex, code_dir):
                os.remove(full_path)
    # Iterate through the source files and symlink when applicable.
    for core_file in core_files:
        if core_file in ['sites', 'profiles']:
            continue
        if utilities.ignore_code_file(core_file):
            continue
        source_path = core_path + '/' + core_file
        destination_path = instance_code_path_sid + '/' + core_file
        # Remove existing symlink and add new one.
        if os.path.islink(destination_path):
            os.remove(destination_path)
            # F_OK to test the existence of path
        if not os.access(destination_path, os.F_OK):
            utilities.relative_symlink(source_path, destination_path)
    # Create Instance specific directory structure
    directories_to_create = [
        'sites', 'sites/all', 'sites/all/modules', 'sites/all/libraries',
        'sites/all/themes', 'sites/default', 'sites/default/files', 'profiles'
    ]
    for directory in directories_to_create:
        target_dir = instance_code_path_sid + '/' + directory
        # If the directoey does not already exist, create it.
        if not os.access(target_dir, os.F_OK):
            os.mkdir(target_dir)
    # Copy over default settings file so that this instance is like a default install.
    source_path = core_path + '/sites/default/default.settings.php'
    destination_path = instance_code_path_sid + '/sites/default/default.settings.php'
    if os.access(destination_path, os.F_OK):
        os.remove(destination_path)
    copyfile(source_path, destination_path)
    # Include links to the profiles that we are not using so that the site doesn't white screen if
    # the deployed profile gets disabled.
    core_profiles = os.listdir(core_path + '/profiles')
    for core_profile in core_profiles:
        source_path = core_path + '/profiles/' + core_profile
        destination_path = instance_code_path_sid + '/profiles/' + core_profile
        if os.path.islink(destination_path):
            os.remove(destination_path)
            # F_OK to test the existence of path
        if not os.access(destination_path, os.F_OK):
            utilities.relative_symlink(source_path, destination_path)