X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=modules%2Flibjuju%2Fjuju%2Fprovisioner.py;h=d937cad41d9fb41323c576e8016a7c4df6d8502d;hp=91747a453d01bd34058407b5a570207dc4d009e5;hb=0cd1c02c85f5dbd6d06bd28b79f964fb209ee90a;hpb=7b4702c2e118bab49def498b4b4c236d430dbc13;ds=sidebyside diff --git a/modules/libjuju/juju/provisioner.py b/modules/libjuju/juju/provisioner.py index 91747a4..d937cad 100644 --- a/modules/libjuju/juju/provisioner.py +++ b/modules/libjuju/juju/provisioner.py @@ -1,23 +1,34 @@ -from .client import client - -import paramiko +# Copyright 2019 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. import os import re -import tempfile import shlex -from subprocess import ( - CalledProcessError, -) +import tempfile import uuid +from subprocess import CalledProcessError + +import paramiko +from .client import client arches = [ - [re.compile("amd64|x86_64"), "amd64"], - [re.compile("i?[3-9]86"), "i386"], - [re.compile("(arm$)|(armv.*)"), "armhf"], - [re.compile("aarch64"), "arm64"], - [re.compile("ppc64|ppc64el|ppc64le"), "ppc64el"], - [re.compile("ppc64|ppc64el|ppc64le"), "s390x"], + [re.compile(r"amd64|x86_64"), "amd64"], + [re.compile(r"i?[3-9]86"), "i386"], + [re.compile(r"(arm$)|(armv.*)"), "armhf"], + [re.compile(r"aarch64"), "arm64"], + [re.compile(r"ppc64|ppc64el|ppc64le"), "ppc64el"], + [re.compile(r"s390x?"), "s390x"], ] @@ -155,28 +166,21 @@ class SSHProvisioner: if the authentication fails """ - # TODO: Test this on an image without the ubuntu user setup. - - auth_user = self.user + ssh = None try: # Run w/o allocating a pty, so we fail if sudo prompts for a passwd ssh = self._get_ssh_client( self.host, - "ubuntu", + self.user, self.private_key_path, ) - stdout, stderr = self._run_command(ssh, "sudo -n true", pty=False) except paramiko.ssh_exception.AuthenticationException as e: raise e - else: - auth_user = "ubuntu" finally: if ssh: ssh.close() - # if the above fails, run the init script as the authenticated user - # Infer the public key public_key = None public_key_path = "{}.pub".format(self.private_key_path) @@ -194,7 +198,7 @@ class SSHProvisioner: try: ssh = self._get_ssh_client( self.host, - auth_user, + self.user, self.private_key_path, ) @@ -234,7 +238,7 @@ class SSHProvisioner: info['series'] = lines[0].strip() info['arch'] = normalize_arch(lines[1].strip()) - memKb = re.split('\s+', lines[2])[1] + memKb = re.split(r'\s+', lines[2])[1] # Convert megabytes -> kilobytes info['mem'] = round(int(memKb) / 1024) @@ -251,8 +255,8 @@ class SSHProvisioner: cores = line.split(":")[1].strip() if physical_id not in recorded.keys(): - info['cpu-cores'] += cores - recorded[physical_id] = True + info['cpu-cores'] += cores + recorded[physical_id] = True return info @@ -317,10 +321,10 @@ class SSHProvisioner: client_facade = client.ClientFacade.from_connection(connection) results = await client_facade.ProvisioningScript( - data_dir, - disable_package_commands, - machine_id, - nonce, + data_dir=data_dir, + disable_package_commands=disable_package_commands, + machine_id=machine_id, + nonce=nonce, ) self._run_configure_script(results.script)