Dropped in fixes for juju ssh.
[osm/N2VC.git] / juju / utils.py
1 import asyncio
2 import os
3 from pathlib import Path
4
5
6 async def execute_process(*cmd, log=None):
7 '''
8 Wrapper around asyncio.create_subprocess_exec.
9
10 '''
11 p = await asyncio.create_subprocess_exec(
12 *cmd,
13 stdin=asyncio.subprocess.PIPE,
14 stdout=asyncio.subprocess.PIPE,
15 stderr=asyncio.subprocess.PIPE,
16 )
17 stdout, stderr = await p.communicate()
18 if log:
19 log.debug("Exec %s -> %d", cmd, p.returncode)
20 if stdout:
21 log.debug(stdout.decode('utf-8'))
22 if stderr:
23 log.debug(stderr.decode('utf-8'))
24 return p.returncode == 0
25
26
27 def read_ssh_key():
28 '''
29 Attempt to read the local juju admin's public ssh key, so that it
30 can be passed on to a model.
31
32 '''
33 default_data_dir = Path(Path.home(), ".local", "share", "juju")
34 juju_data = os.environ.get("JUJU_DATA", default_data_dir)
35 ssh_key_path = Path(juju_data, 'ssh', 'juju_id_rsa.pub')
36 with ssh_key_path.open('r') as ssh_key_file:
37 ssh_key = ssh_key_file.readlines()[0].strip()
38 return ssh_key
39