Пример #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 kubernetes binary files
    from the kubernetes-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()
    kubernetes_bin_dir = Path('/opt/kubernetes/bin')
    # Get the version of kubernetes 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 kubernetes binaries.
    for service in ('kubelet', 'proxy'):
        if host.service_running(service):
            host.service_stop(service)
    command = ['relation-get', 'private-address']
    # Get the kubernetes-master address.
    server = subprocess.check_output(command).strip()
    print('Kubernetes master private address: ', server)
    installer = KubernetesInstaller(arch, version, server, kubernetes_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 kubernetes binary files.
    """
    hookenv.log('Starting config-changed')
    charm_dir = path(hookenv.charm_dir())
    config = hookenv.config()
    # Get the version of kubernetes to install.
    version = config['version']
    # Get the package architecture, rather than the from the kernel (uname -m).
    arch = subprocess.check_output(['dpkg', '--print-architecture']).strip()
    kubernetes_dir = path('/opt/kubernetes')
    if not kubernetes_dir.exists():
        print('The source directory {0} does not exist'.format(kubernetes_dir))
        print('Was the kubernetes code cloned during install?')
        exit(1)

    if version in ['source', 'head', 'master']:
        branch = 'master'
    else:
        # Create a branch to a tag.
        branch = 'tags/{0}'.format(version)

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

    # Change to the kubernetes directory (git repository).
    with kubernetes_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 the current version is different or last build failed.
            if current_branch != version or last_build_failed:
                installer.build(branch)
        if not output_path.exists():
            broken_build.touch()
        else:
            print('Notifying minions of verison ' + version)
            # Notify the minions of a version change.
            for r in hookenv.relation_ids('minions-api'):
                hookenv.relation_set(r, version=version)
            print('Done notifing minions of version ' + version)

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

    relation_changed()

    hookenv.log('The config-changed hook completed successfully.')
Пример #3
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 kubernetes binary files
    from the kubernetes-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()
    kubernetes_bin_dir = path('/opt/kubernetes/bin')
    # Get the version of kubernetes 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 kubernetes binaries.
    for service in ('kubelet', 'proxy'):
        if host.service_running(service):
            host.service_stop(service)
    command = ['relation-get', 'private-address']
    # Get the kubernetes-master address.
    server = subprocess.check_output(command).strip()
    print('Kubernetes master private address: ', server)
    installer = KubernetesInstaller(arch, version, server, kubernetes_bin_dir)
    installer.download()
    installer.install()
    # Write the most recently installed version number to the file.
    version_file.write_text(version)
    relation_changed()
Пример #4
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 kubernetes binary files.
    """
    hookenv.log('Starting config-changed')
    charm_dir = Path(hookenv.charm_dir())
    config = hookenv.config()
    # Get the version of kubernetes to install.
    version = config['version']
    username = config['username']
    password = config['password']
    certificate = config['apiserver-cert']
    key = config['apiserver-key']

    if version == 'master':
        # The 'master' branch of kuberentes is used when master is configured.
        branch = 'master'
    elif version == 'local':
        # Check for kubernetes 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)

    cert_file = '/srv/kubernetes/apiserver.crt'
    key_file = '/srv/kubernetes/apiserver.key'
    # When the cert or key changes we need to restart the apiserver.
    if config.changed('apiserver-cert') or config.changed('apiserver-key'):
        hookenv.log('Certificate or key has changed.')
        if not certificate or not key:
            generate_cert(key=key_file, cert=cert_file)
        else:
            hookenv.log('Writing new certificate and key to server.')
            with open(key_file, 'w') as file:
                file.write(key)
            with open(cert_file, 'w') as file:
                file.write(certificate)
        # Restart apiserver as the certificate or key has changed.
        if host.service_running('apiserver'):
            host.service_restart('apiserver')
        # Reload nginx because it proxies https to apiserver.
        if host.service_running('nginx'):
            host.service_reload('nginx')

    if config.changed('username') or config.changed('password'):
        hookenv.log('Username or password changed, creating authentication.')
        basic_auth(username, username, password)
        if host.service_running('apiserver'):
            host.service_restart('apiserver')

    # Get 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'
        kube_installer = KubernetesInstaller(arch, version, output_path)
    else:

        # Build the kuberentes binaries from source on the units.
        kubernetes_dir = Path('/opt/kubernetes')

        # Construct the path to the binaries using the arch.
        output_path = kubernetes_dir / '_output/local/bin/linux' / arch
        kube_installer = KubernetesInstaller(arch, version, output_path)

        if not kubernetes_dir.exists():
            message = 'The kubernetes source directory {0} does not exist. ' \
                'Was the kubernetes repository cloned during the install?'
            print(message.format(kubernetes_dir))
            exit(1)

        # Change to the kubernetes directory (git repository).
        with kubernetes_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)
            current_branch = current_branch.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:
                    kube_installer.build(branch)
            if not output_path.isdir():
                broken_build.touch()

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

    relation_changed()

    hookenv.log('The config-changed hook completed successfully.')
Пример #5
0
 def makeone(self, *args, **kw):
     """ Create the KubernetesInstaller object and return it. """
     from kubernetes_installer import KubernetesInstaller
     return KubernetesInstaller(*args, **kw)
Пример #6
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 kubernetes binary files.
    """
    hookenv.log("Starting config-changed")
    charm_dir = path(hookenv.charm_dir())
    config = hookenv.config()
    # Get the version of kubernetes to install.
    version = config["version"]

    if version == "master":
        # The 'master' branch of kuberentes is used when master is configured.
        branch = "master"
    elif version == "local":
        # Check for kubernetes 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 = KubernetesInstaller(arch, version, output_path)
    else:

        # Build the kuberentes binaries from source on the units.
        kubernetes_dir = path("/opt/kubernetes")

        # Construct the path to the binaries using the arch.
        output_path = kubernetes_dir / "_output/local/bin/linux" / arch
        installer = KubernetesInstaller(arch, version, output_path)

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

        # Change to the kubernetes directory (git repository).
        with kubernetes_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.")
Пример #7
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 kubernetes binary files.
    """
    hookenv.log('Starting config-changed')
    charm_dir = Path(hookenv.charm_dir())
    config = hookenv.config()
    # Get the version of kubernetes to install.
    version = config['version']
    username = config['username']
    password = config['password']
    certificate = config['apiserver-cert']
    key = config['apiserver-key']

    if version == 'master':
        # The 'master' branch of kuberentes is used when master is configured.
        branch = 'master'
    elif version == 'local':
        # Check for kubernetes 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)

    cert_file = '/srv/kubernetes/apiserver.crt'
    key_file = '/srv/kubernetes/apiserver.key'
    # When the cert or key changes we need to restart the apiserver.
    if config.changed('apiserver-cert') or config.changed('apiserver-key'):
        hookenv.log('Certificate or key has changed.')
        if not certificate or not key:
            generate_cert(key=key_file, cert=cert_file)
        else:
            hookenv.log('Writing new certificate and key to server.')
            with open(key_file, 'w') as file:
                file.write(key)
            with open(cert_file, 'w') as file:
                file.write(certificate)
        # Restart apiserver as the certificate or key has changed.
        if host.service_running('apiserver'):
            host.service_restart('apiserver')
        # Reload nginx because it proxies https to apiserver.
        if host.service_running('nginx'):
            host.service_reload('nginx')

    if config.changed('username') or config.changed('password'):
        hookenv.log('Username or password changed, creating authentication.')
        basic_auth(username, username, password)
        if host.service_running('apiserver'):
            host.service_restart('apiserver')

    # Get 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'
        kube_installer = KubernetesInstaller(arch, version, output_path)
    else:

        # Build the kuberentes binaries from source on the units.
        kubernetes_dir = Path('/opt/kubernetes')

        # Construct the path to the binaries using the arch.
        output_path = kubernetes_dir / '_output/local/bin/linux' / arch
        kube_installer = KubernetesInstaller(arch, version, output_path)

        if not kubernetes_dir.exists():
            message = 'The kubernetes source directory {0} does not exist. ' \
                'Was the kubernetes repository cloned during the install?'
            print(message.format(kubernetes_dir))
            exit(1)

        # Change to the kubernetes directory (git repository).
        with kubernetes_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)
            current_branch = current_branch.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:
                    kube_installer.build(branch)
            if not output_path.isdir():
                broken_build.touch()

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

    relation_changed()

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