-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(JOBS): extract class hierarchy
- Loading branch information
1 parent
3e12767
commit 67e553c
Showing
15 changed files
with
557 additions
and
501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Job base classes for the Mac Maker.""" | ||
|
||
import abc | ||
import sys | ||
|
||
import click | ||
from ..ansible_controller.inventory import InventoryFile | ||
from ..ansible_controller.runner import AnsibleRunner | ||
from ..utilities.password import SUDO | ||
from ..utilities.precheck import PrecheckConfig, TypePrecheckFileData | ||
from ..utilities.spec import JobSpec | ||
from ..utilities.state import TypeState | ||
|
||
|
||
class JobBase(abc.ABC): | ||
"""Job base class for the Mac Maker.""" | ||
|
||
def __init__(self) -> None: | ||
self.jobspec = JobSpec() | ||
|
||
@abc.abstractmethod | ||
def get_precheck_content(self) -> TypePrecheckFileData: | ||
"""Extract the profile's precheck file contents.""" | ||
raise NotImplementedError # nocover | ||
|
||
@abc.abstractmethod | ||
def get_state(self) -> TypeState: | ||
"""Assemble and return a state object.""" | ||
raise NotImplementedError # nocover | ||
|
||
def precheck(self) -> None: | ||
"""Precheck the profile for validity and environment variable content.""" | ||
|
||
precheck_data = self.get_precheck_content() | ||
|
||
validator = PrecheckConfig(precheck_data['env']) | ||
validator.is_valid_env_file() | ||
results = validator.validate_environment() | ||
if not results['is_valid']: | ||
for violation in results['violations']: | ||
click.echo(violation) | ||
sys.exit(1) | ||
click.echo(precheck_data['notes']) | ||
|
||
def provision(self) -> None: | ||
"""Begin provisioning the local machine.""" | ||
|
||
loaded_state = self.get_state() | ||
|
||
sudo = SUDO() | ||
sudo.prompt_for_sudo() | ||
|
||
inventory = InventoryFile(loaded_state) | ||
inventory.write_inventory_file() | ||
|
||
ansible_job = AnsibleRunner(loaded_state) | ||
ansible_job.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""A provisioning job for a spec file and profile on the local file system.""" | ||
|
||
import click | ||
from .. import config | ||
from ..utilities.precheck import TypePrecheckFileData | ||
from ..utilities.state import TypeState | ||
from . import bases | ||
|
||
|
||
class FileSystemJob(bases.JobBase): | ||
"""A provisioning job for a spec file and profile on the local file system. | ||
:param spec_file_location: The path to the spec file. | ||
""" | ||
|
||
spec_file_location: str | ||
|
||
def __init__(self, spec_file_location: str): | ||
super().__init__() | ||
self.spec_file_location = spec_file_location | ||
|
||
def get_precheck_content(self) -> TypePrecheckFileData: | ||
"""Read the precheck data using a spec file. | ||
:returns: The precheck file data. | ||
""" | ||
|
||
precheck_data = self.jobspec.extract_precheck_from_job_spec( | ||
self.spec_file_location | ||
) | ||
return precheck_data | ||
|
||
def get_state(self) -> TypeState: | ||
"""Read a spec file from the filesystem and build a state object. | ||
:returns: The created state object. | ||
""" | ||
|
||
job_spec = self.jobspec.create_job_spec_from_filesystem( | ||
self.spec_file_location | ||
) | ||
click.echo(config.ANSIBLE_JOB_SPEC_READ_MESSAGE) | ||
click.echo(job_spec['spec_file_location']) | ||
return job_spec['spec_file_content'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""A provisioning job for a profile in a Github repository.""" | ||
|
||
from typing import Optional, cast | ||
|
||
import click | ||
from .. import config | ||
from ..utilities.github import GithubRepository | ||
from ..utilities.precheck import TypePrecheckFileData | ||
from ..utilities.state import TypeState | ||
from ..utilities.workspace import WorkSpace | ||
from . import bases | ||
|
||
|
||
class GitHubJob(bases.JobBase): | ||
"""A provisioning job for a profile in a Github repository. | ||
:param repository_url: The GitHub Repository URL. | ||
:param branch_name: The GitHub Repository branch name. | ||
""" | ||
|
||
repository_url: str | ||
branch_name: Optional[str] | ||
|
||
def __init__(self, repository_url: str, branch_name: Optional[str]): | ||
super().__init__() | ||
self.repository_url = repository_url | ||
self.branch_name = branch_name | ||
|
||
def get_precheck_content(self) -> TypePrecheckFileData: | ||
"""Read the precheck data from a GitHub repository. | ||
:returns: The precheck file data. | ||
""" | ||
|
||
repo = GithubRepository(self.repository_url) | ||
precheck_data = repo.download_zip_bundle_files( | ||
self.branch_name, | ||
{ | ||
'notes': str(config.PRECHECK['notes']), | ||
'env': str(config.PRECHECK['env']), | ||
}, | ||
) | ||
return cast(TypePrecheckFileData, precheck_data) | ||
|
||
def get_state(self) -> TypeState: | ||
"""Fetch a GitHub zip bundle, and build a state object. | ||
:returns: The created state object. | ||
""" | ||
|
||
click.echo(config.ANSIBLE_RETRIEVE_MESSAGE) | ||
|
||
repo = GithubRepository(self.repository_url) | ||
|
||
workspace = WorkSpace() | ||
workspace.add_repository(repo, self.branch_name) | ||
|
||
repo.download_zip_bundle_profile(workspace.root, self.branch_name) | ||
job_spec = self.jobspec.create_job_spec_from_github(workspace) | ||
|
||
click.echo(config.ANSIBLE_JOB_SPEC_MESSAGE) | ||
click.echo(job_spec['spec_file_location']) | ||
return job_spec['spec_file_content'] |
Empty file.
Oops, something went wrong.