Пример #1
0
def api_relation_changed():
    """
    On the relation to the api server, this function determines the appropriate
    architecture and the configured version to copy the qingyuan binary files
    from the qingyuan-master charm and installs it locally on this machine.
    """
    hookenv.log('Starting api-relation-changed')
    charm_dir = path(hookenv.charm_dir())
    # Get the package architecture, rather than the from the kernel (uname -m).
    arch = subprocess.check_output(['dpkg', '--print-architecture']).strip()
    qingyuan_bin_dir = path('/opt/qingyuan/bin')
    # Get the version of qingyuan to install.
    version = subprocess.check_output(['relation-get', 'version']).strip()
    print('Relation version: ', version)
    if not version:
        print('No version present in the relation.')
        exit(0)
    version_file = charm_dir / '.version'
    if version_file.exists():
        previous_version = version_file.text()
        print('Previous version: ', previous_version)
        if version == previous_version:
            exit(0)
    # Can not download binaries while the service is running, so stop it.
    # TODO: Figure out a better way to handle upgraded qingyuan binaries.
    for service in ('qinglet', 'proxy'):
        if host.service_running(service):
            host.service_stop(service)
    command = ['relation-get', 'private-address']
    # Get the qingyuan-master address.
    server = subprocess.check_output(command).strip()
    print('QingYuan master private address: ', server)
    installer = QingYuanInstaller(arch, version, server, qingyuan_bin_dir)
    installer.download()
    installer.install()
    # Write the most recently installed version number to the file.
    version_file.write_text(version)
    relation_changed()
Пример #2
0
def config_changed():
    """
    On the execution of the juju event 'config-changed' this function
    determines the appropriate architecture and the configured version to
    create qingyuan binary files.
    """
    hookenv.log('Starting config-changed')
    charm_dir = path(hookenv.charm_dir())
    config = hookenv.config()
    # Get the version of qingyuan to install.
    version = config['version']

    if version == 'master':
        # The 'master' branch of qingrentes is used when master is configured.
        branch = 'master'
    elif version == 'local':
        # Check for qingyuan binaries in the local files/output directory.
        branch = None
    else:
        # Create a branch to a tag to get the release version.
        branch = 'tags/{0}'.format(version)

    # Get the package architecture, rather than arch from the kernel (uname -m).
    arch = subprocess.check_output(['dpkg', '--print-architecture']).strip()

    if not branch:
        output_path = charm_dir / 'files/output'
        installer = QingYuanInstaller(arch, version, output_path)
    else:

        # Build the qingrentes binaries from source on the units.
        qingyuan_dir = path('/opt/qingyuan')

        # Construct the path to the binaries using the arch.
        output_path = qingyuan_dir / '_output/local/bin/linux' / arch
        installer = QingYuanInstaller(arch, version, output_path)

        if not qingyuan_dir.exists():
            print('The source directory {0} does not exist'.format(qingyuan_dir))
            print('Was the qingyuan code cloned during install?')
            exit(1)

        # Change to the qingyuan directory (git repository).
        with qingyuan_dir:

            # Create a command to get the current branch.
            git_branch = 'git branch | grep "\*" | cut -d" " -f2'
            current_branch = subprocess.check_output(git_branch, shell=True).strip()
            print('Current branch: ', current_branch)
            # Create the path to a file to indicate if the build was broken.
            broken_build = charm_dir / '.broken_build'
            # write out the .broken_build file while this block is executing.
            with check_sentinel(broken_build) as last_build_failed:
                print('Last build failed: ', last_build_failed)
                # Rebuild if current version is different or last build failed.
                if current_branch != version or last_build_failed:
                    installer.build(branch)
            if not output_path.isdir():
                broken_build.touch()

    # Create the symoblic links to the right directories.
    installer.install()

    relation_changed()

    hookenv.log('The config-changed hook completed successfully.')