| 1 |
# __init__.py - High-level automation interfaces |
| 2 |
# |
| 3 |
# Copyright 2019 Gregory Szorc <[email protected]> |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
# no-check-code because Python 3 native. |
| 9 |
|
| 10 |
import pathlib |
| 11 |
import secrets |
| 12 |
|
| 13 |
from .aws import AWSConnection |
| 14 |
|
| 15 |
|
| 16 |
class HGAutomation: |
| 17 |
"""High-level interface for Mercurial automation. |
| 18 |
|
| 19 |
Holds global state, provides access to other primitives, etc. |
| 20 |
""" |
| 21 |
|
| 22 |
def __init__(self, state_path: pathlib.Path): |
| 23 |
self.state_path = state_path |
| 24 |
|
| 25 |
state_path.mkdir(exist_ok=True) |
| 26 |
|
| 27 |
def default_password(self): |
| 28 |
"""Obtain the default password to use for remote machines. |
| 29 |
|
| 30 |
A new password will be generated if one is not stored. |
| 31 |
""" |
| 32 |
p = self.state_path / 'default-password' |
| 33 |
|
| 34 |
try: |
| 35 |
with p.open('r', encoding='ascii') as fh: |
| 36 |
data = fh.read().strip() |
| 37 |
|
| 38 |
if data: |
| 39 |
return data |
| 40 |
|
| 41 |
except FileNotFoundError: |
| 42 |
pass |
| 43 |
|
| 44 |
password = secrets.token_urlsafe(24) |
| 45 |
|
| 46 |
with p.open('w', encoding='ascii') as fh: |
| 47 |
fh.write(password) |
| 48 |
fh.write('\n') |
| 49 |
|
| 50 |
p.chmod(0o0600) |
| 51 |
|
| 52 |
return password |
| 53 |
|
| 54 |
def aws_connection(self, region: str, ensure_ec2_state: bool = True): |
| 55 |
"""Obtain an AWSConnection instance bound to a specific region.""" |
| 56 |
|
| 57 |
return AWSConnection(self, region, ensure_ec2_state=ensure_ec2_state) |