Skip to content

Commit

Permalink
LegacyBackend: init
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Mar 26, 2020
1 parent 6dc8b80 commit 4370ba1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nixops/plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Dict, Type
import nixops.plugins
from nixops.storage import StorageBackend
from nixops.storage.legacy import LegacyBackend


@nixops.plugins.hookimpl
def register_backends() -> Dict[str, Type[StorageBackend]]:
return {}
return {"legacy": LegacyBackend}
1 change: 0 additions & 1 deletion nixops/script_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def network_state(args: Namespace) -> Generator[nixops.statefile.StateFile, None
with TemporaryDirectory("nixops") as statedir:
statefile = statedir + "/state.nixops"
storage.fetchToFile(statefile)

state = nixops.statefile.StateFile(statefile)
try:
storage.onOpen(state)
Expand Down
54 changes: 54 additions & 0 deletions nixops/storage/legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from nixops.storage import StorageArgDescriptions, StorageArgValues
import nixops.statefile
import sys
import os
import os.path

class LegacyBackend:
@staticmethod
def arguments() -> StorageArgDescriptions:
raise NotImplementedError

def __init__(self, args: StorageArgValues) -> None:
pass

# fetchToFile: acquire a lock and download the state file to
# the local disk. Note: no arguments will be passed over kwargs.
# Making it part of the type definition allows adding new
# arguments later.
def fetchToFile(self, path: str, **kwargs) -> None:
os.symlink(os.path.abspath(self.state_location()), path)

def onOpen(self, sf: nixops.statefile.StateFile, **kwargs) -> None:
pass

def state_location(self) -> str:
env_override = os.environ.get("NIXOPS_STATE", os.environ.get("CHARON_STATE"))
if env_override is not None:
return env_override

home_dir = os.environ.get("HOME", "")
charon_dir = f"{home_dir}/.charon"
nixops_dir = f"{home_dir}/.nixops"

if not os.path.exists(nixops_dir):
if os.path.exists(charon_dir):
sys.stderr.write(
"renaming ‘{0}’ to ‘{1}’...\n".format(charon_dir, nixops_dir)
)
os.rename(charon_dir, nixops_dir)
if os.path.exists(nixops_dir + "/deployments.charon"):
os.rename(
nixops_dir + "/deployments.charon",
nixops_dir + "/deployments.nixops",
)
else:
os.makedirs(nixops_dir, 0o700)

return nixops_dir + "/deployments.nixops"

# uploadFromFile: upload the new state file and release any locks
# Note: no arguments will be passed over kwargs. Making it part of
# the type definition allows adding new arguments later.
def uploadFromFile(self, path: str, **kwargs) -> None:
pass

0 comments on commit 4370ba1

Please sign in to comment.