Created
May 11, 2016 16:02
-
-
Save 20after4/fe657619821ac76afc0646546ac30d68 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from . import graph | |
from . import op | |
class Phabricator(Deployment): | |
class source(GitRepo): | |
load_submodules = True | |
class conf(ConfigFile): | |
src = '{realm}.json' | |
secrets = '/etc/deployment/phabricator/secrets.json' | |
dest = '/srv/phab/phabricator/conf/local/local.json' | |
@needs('conf', 'storage') | |
class apache(Service): | |
command = 'service apache2 {action}' | |
@needs('conf', 'storage') | |
class phd(Daemon): | |
command = 'service phd {action}' | |
@arg('action') | |
class storage(Command): | |
command = 'phabricator/bin/storage {action}' | |
def upgrade(self): | |
self.set_state(State.offline) | |
self.run(command, action='upgrade') | |
self.set_state(State.online) | |
def check(self): | |
status = self.run(command, action='status') | |
self.set_state(status) |
This file contains hidden or 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
# Ensure Resource | |
# Works exactly like puppet resource definitions | |
import os | |
import os.path | |
from pathlib import Path | |
# from sh import git | |
class StateChangeFailure(Exception): | |
def __init__(self, path, ensure): | |
msg = 'State Change Failed for %s(%s)' % (ensure, path) | |
return super(Exception, self).__init__(msg) | |
class State(object): | |
name = 'state' | |
def __init__(self, desired_state): | |
if (callable(desired_state)): | |
self.name = str(desired_state) | |
self.desired_state = desired_state | |
else: | |
self.name = desired_state | |
self.desired_state = getattr(self, desired_state) | |
def __str__(self): | |
return self.name | |
def is_desired_state(self, path): | |
test_func = self.desired_state | |
return test_func(path) | |
def ensure_state(self, path): | |
ensure_func = getattr(self, 'ensure_%s' % self.name, None) | |
if callable(ensure_func): | |
return ensure_func(path) | |
class FileState(State): | |
@staticmethod | |
def exists(path): | |
p = Path(path) | |
return p.exists() | |
@staticmethod | |
def is_file(path): | |
p = Path(path) | |
return p.is_file() | |
@staticmethod | |
def ensure_is_file(path): | |
p = Path(path) | |
if p.is_file(): | |
return False # no state change necessary | |
elif p.is_dir(): | |
p.rmdir() | |
p.touch() | |
return True | |
@staticmethod | |
def ensure_exists(path): | |
p = Path(path) | |
if p.exists(): | |
return False # no state change necessary | |
if not p.parent.is_dir(): | |
State.ensure_is_dir(p.parent) | |
return State.ensure_is_file(path) | |
@staticmethod | |
def ensure_is_dir(path): | |
p = Path(path) | |
if p.is_dir(): | |
return False # no state change necessary | |
else: | |
return p.mkdir(parents=True) | |
class Resource(object): | |
ensure = State('exists') | |
def __init__(self, path): | |
self.path = Path(path) | |
def ensure_desired_state(self): | |
if self.is_desired_state(): | |
return False | |
self.ensure.ensure_state(self.path) | |
if not self.is_desired_state(): | |
raise StateChangeFailure(self.path, self.ensure) | |
def is_desired_state(self): | |
return self.ensure.is_desired_state(self.path) | |
class File(Resource): | |
ensure = State('is_file') | |
class GitDir(Resource): | |
def __init__(self, path, desired_state): | |
self.path = Path(path) | |
os.environ['GIT_DIR'] = path | |
def set_desired_state(self, desired_state): | |
self.desired_state = desired_state | |
def __enter__(self): | |
self.oldcwd = os.getcwd() | |
self.in_context = True | |
os.chdir(self.path) | |
def __exit__(self, type, value, tb): | |
self.in_context = False | |
try: | |
os.environ['GIT_DIR'] = None | |
os.chdir(self.oldcwd) | |
except: | |
pass | |
return False | |
def is_desired_state(self, path): | |
return path == self.desired_state | |
@staticmethod | |
def is_git_repo(path): | |
p = Path(path) | |
gitdir = p.joinpath('.git') | |
return gitdir.exists() | |
def Service(Resource): | |
def __init__(self): | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment