@vm_util.Retry(max_retries=10)
def AddPubKeyToHost(host_ip, password, keyfile, username):
  public_key = str()
  if keyfile:
    keyfile = os.path.expanduser(keyfile)
    if not paramiko:
      raise Exception('`paramiko` is required for pushing keyfile to ecs.')
    with open(keyfile, 'r') as f:
      public_key = f.read()
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  try:
    ssh.connect(
        host_ip,
        username=ROOT,  # We should use root to add pubkey to host
        password=password,
    )
    if username == ROOT:
      command_list = [
          'mkdir .ssh; echo "%s" >> ~/.ssh/authorized_keys' % public_key
      ]
    else:
      command_list = [
          'echo "{0} ALL = NOPASSWD: ALL" >> /etc/sudoers'.format(username),
          'useradd {0} --home /home/{0} --shell /bin/bash -m'.format(username),
          'mkdir /home/{0}/.ssh'.format(username),
          'echo "{0}" >> /home/{1}/.ssh/authorized_keys'
          .format(public_key, username),
          'chown -R {0}:{0} /home/{0}/.ssh'.format(username)]
    command = ';'.join(command_list)
    ssh.exec_command(command)

    ssh.close()
    return True
  except __HOLE__:
    raise IOError
dataset/ETHPy150Open GoogleCloudPlatform/PerfKitBenchmarker/perfkitbenchmarker/providers/alicloud/util.py/AddPubKeyToHost