def install_from_nvidia_apt(): """ Install cuda docker from the nvidia apt repository. :return: None """ status_set('maintenance', 'Installing docker-engine from Nvidia PPA.') # Get the server and key in the apt-key management tool. add_apt_key('9DC858229FC7DD38854AE2D88D81803C0EBFCD88') # Install key for nvidia-docker. This key changes frequently # ([expires: 2019-09-20]) so we should do what the official docs say and # not try to get it through its fingerprint. add_apt_key_url('https://nvidia.github.io/nvidia-container-runtime/gpgkey') # Get the lsb information as a dictionary. lsb = host.lsb_release() code = lsb['DISTRIB_CODENAME'] release = lsb['DISTRIB_RELEASE'] ubuntu = str(lsb['DISTRIB_ID']).lower() docker_url = 'https://download.docker.com/linux/ubuntu' nvidia_url = 'https://nvidia.github.io' repo = 'stable' debs = list() debs.append('deb [arch={}] {} {} {}'.format(arch(), docker_url, code, repo)) packages = [ 'libnvidia-container', 'nvidia-container-runtime', 'nvidia-docker' ] for package in packages: debs.append('deb {}/{}/ubuntu{}/{} /'.format(nvidia_url, package, release, arch())) write_docker_sources(debs) install_cuda_drivers_repo(arch(), release, ubuntu) apt_update(fatal=True) # Actually install the required packages docker-ce nvidia-docker2. docker_ce = hookenv.config('docker-ce-package') nvidia_docker2 = hookenv.config('nvidia-docker-package') nv_container_runtime = hookenv.config('nvidia-container-runtime-package') apt_install( ['cuda-drivers', docker_ce, nvidia_docker2, nv_container_runtime], fatal=True) fix_docker_runtime_nvidia()
def install_from_upstream_apt(): """ Install docker from the apt repository. This is a pyton adaptation of the shell script found at https://get.docker.com/. :return: None """ status_set('maintenance', 'Installing docker-ce from upstream PPA.') key_url = 'https://download.docker.com/linux/ubuntu/gpg' add_apt_key_url(key_url) # The url to the server that contains the docker apt packages. apt_url = 'https://download.docker.com/linux/ubuntu' # Get the lsb information as a dictionary. lsb = host.lsb_release() # The codename for the release. code = lsb['DISTRIB_CODENAME'] # Repo can be: stable, edge or test. repo = 'stable' # E.g. # deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable debs = list() debs.append('deb [arch={}] {} {} {}'.format(arch(), apt_url, code, repo)) write_docker_sources(debs) apt_update(fatal=True) # Install Docker via apt. apt_install(docker_packages['upstream'], fatal=True)
def get_sandbox_image(): '''Return the container image location for the sandbox_image. Set an appropriate sandbox image based on known registries. Precedence should be: - related docker-registry - default charmed k8s registry (if related to kubernetes) - upstream :return: str container image location ''' db = unitdata.kv() canonical_registry = 'rocks.canonical.com:443/cdk' upstream_registry = 'k8s.gcr.io' docker_registry = db.get('registry', None) if docker_registry: sandbox_registry = docker_registry['url'] else: try: deployment = hookenv.goal_state() except NotImplementedError: relations = [] for rid in hookenv.relation_ids('containerd'): relations.append(hookenv.remote_service_name(rid)) else: relations = deployment.get('relations', {}).get('containerd', {}) if any(k in relations for k in ('kubernetes-master', 'kubernetes-worker')): sandbox_registry = canonical_registry else: sandbox_registry = upstream_registry return '{}/pause-{}:3.1'.format(sandbox_registry, host.arch())
def install_calico_binaries(): ''' Unpack the Calico binaries. ''' # on intel, the resource is called 'calico'; other arches have a suffix architecture = arch() if architecture == 'amd64': resource_name = 'calico-cni' else: resource_name = 'calico-cni-{}'.format(architecture) try: archive = resource_get(resource_name) except Exception: message = 'Error fetching the calico resource.' log(message) status.blocked(message) return if not archive: message = 'Missing calico resource.' log(message) status.blocked(message) return filesize = os.stat(archive).st_size if filesize < 1000000: message = 'Incomplete calico resource' log(message) status.blocked(message) return status.maintenance('Unpacking calico resource.') charm_dir = os.getenv('CHARM_DIR') unpack_path = os.path.join(charm_dir, 'files', 'calico') os.makedirs(unpack_path, exist_ok=True) cmd = ['tar', 'xfz', archive, '-C', unpack_path] log(cmd) check_call(cmd) apps = [ { 'name': 'calico', 'path': '/opt/cni/bin' }, { 'name': 'calico-ipam', 'path': '/opt/cni/bin' }, ] for app in apps: unpacked = os.path.join(unpack_path, app['name']) app_path = os.path.join(app['path'], app['name']) install = ['install', '-v', '-D', unpacked, app_path] check_call(install) set_state('calico.binaries.installed')
def determine_apt_source(): """ :return: String docker runtime """ docker_runtime = config('docker_runtime') if config('install_from_upstream'): docker_runtime = 'upstream' if docker_runtime == 'auto': out = check_output(['lspci', '-nnk']).rstrip() if arch() == 'amd64' \ and out.decode('utf-8').lower().count('nvidia') > 0: docker_runtime = 'nvidia' else: docker_runtime = 'apt' hookenv.log('Setting runtime to {}'.format(docker_packages)) return docker_runtime
def determine_apt_source(): """ :return: String docker runtime """ docker_runtime = config("docker_runtime") if config("install_from_upstream"): docker_runtime = "upstream" if docker_runtime == "auto": out = check_output(["lspci", "-nnk"]).rstrip() if arch( ) == "amd64" and out.decode("utf-8").lower().count("nvidia") > 0: docker_runtime = "nvidia" else: docker_runtime = "apt" hookenv.log("Setting runtime to {}".format(docker_packages)) return docker_runtime
def install_from_custom_apt(): """ Install docker from custom repository. :return: None or False """ status.maintenance("Installing Docker from custom repository.") repo_string = config("docker_runtime_repo") key_url = config("docker_runtime_key_url") package_name = config("docker_runtime_package") if not repo_string: message = "`docker_runtime_repo` must be set" hookenv.log(message) status.blocked(message) return False if not key_url: message = "`docker_runtime_key_url` must be set" hookenv.log(message) status.blocked(message) return False if not package_name: message = "`docker_runtime_package` must be set" hookenv.log(message) status.blocked(message) return False lsb = host.lsb_release() format_dictionary = {"ARCH": arch(), "CODE": lsb["DISTRIB_CODENAME"]} add_apt_key_url(key_url) write_docker_sources([repo_string.format(**format_dictionary)]) apt_update() apt_install([package_name]) return True
def install_from_custom_apt(): """ Install docker from custom repository. :return: None or False """ status_set('maintenance', 'Installing Docker from custom repository.') repo_string = config('docker_runtime_repo') key_url = config('docker_runtime_key_url') package_name = config('docker_runtime_package') if not repo_string: message = '`docker_runtime_repo` must be set' hookenv.log(message) hookenv.status_set('blocked', message) return False if not key_url: message = '`docker_runtime_key_url` must be set' hookenv.log(message) hookenv.status_set('blocked', message) return False if not package_name: message = '`docker_runtime_package` must be set' hookenv.log(message) hookenv.status_set('blocked', message) return False lsb = host.lsb_release() format_dictionary = {'ARCH': arch(), 'CODE': lsb['DISTRIB_CODENAME']} add_apt_key_url(key_url) write_docker_sources([repo_string.format(**format_dictionary)]) apt_update() apt_install([package_name])