The first commit: nlx-init cmd

This commit is contained in:
Dmitry Petrov
2017-03-04 00:28:24 -08:00
parent 595ac51b5a
commit c303c3e2a2
14 changed files with 228 additions and 0 deletions
View File
View File
View File
View File
View File
View File
View File
Executable
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
python $NEATLYNX_HOME/lib/cmd_init.py $@
Executable
View File
Executable
View File
+29
View File
@@ -0,0 +1,29 @@
[Global]
DataDir = data
CacheDir = cache
StateDir = state
Cloud = AWS
[AWS]
AccessKeyId = AKIAJ4RMIOK6WVQRRFUA
SecretAccessKey = 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
StoragePath = neatlynx/tutorial
Region = us-east-1
Zone = us-east-1a
Image = ami-2d39803a
InstanceType = t2.nano
SpotPrice =
SpotTimeout = 300
Storage = my-100gb-drive-io
KeyDir = ~/.ssh
KeyName = neatlynx-key
SecurityGroup = neatlynx-group
+96
View File
@@ -0,0 +1,96 @@
import configparser
import argparse
import os
import git
import subprocess
from neatlynx.exceptions import GitCmdError, ConfigError
class Logger(object):
@staticmethod
def info(msg):
print('{}'.format(msg))
@staticmethod
def warn(msg):
print('{}'.format(msg))
@staticmethod
def error(msg):
print('{}'.format(msg))
class BaseCmd(object):
CONFIG = 'neatlynx.conf'
def __init__(self, parse_config=True):
self._git_dir = None
self._args = None
self._lnx_home = None
if parse_config:
self._config = configparser.ConfigParser()
self._config.read(os.path.join(self.git_dir, self.CONFIG))
parser = argparse.ArgumentParser()
self.define_args(parser)
self._args = parser.parse_args()
self._lnx_home = os.environ.get('NEATLYNX_HOME')
if not self.lnx_home:
raise ConfigError('NEATLYNX_HOME environment variable is not defined')
if not os.path.exists(self._lnx_home):
raise ConfigError("NEATLYNX_HOME directory doesn't exists")
pass
@property
def lnx_home(self):
return self._lnx_home
@property
def args(self):
return self._args
@property
def git_dir(self):
if self._git_dir:
return self._git_dir
try:
p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = map(lambda s: s.decode().strip('\n\r'), p.communicate())
if p.returncode != 0:
raise GitCmdError('Git command error - {}'.format(err))
self._git_dir = out
return self._git_dir
except GitCmdError as e:
raise
except Exception as e:
raise GitCmdError('Unable to run git command: {}'.format(e))
pass
def define_args(self):
pass
def add_string_arg(self, parser, name, default, message,
conf_section=None, conf_name=None):
if conf_section and conf_name:
section = self._config[conf_section]
if not section:
raise ConfigError("")
default_value = section.get(conf_section, default)
else:
default_value = default
parser.add_argument(name,
metavar='',
default=default_value,
help=message)
def run(self):
pass
+87
View File
@@ -0,0 +1,87 @@
import os
import sys
from pathlib import Path
from neatlynx.base_cmd import BaseCmd, Logger
from neatlynx.exceptions import NeatLynxException
class InitError(NeatLynxException):
def __init__(self, msg):
NeatLynxException.__init__(self, 'Init error: {}'.format(msg))
class CmdInit(BaseCmd):
DEFAULT_CONFIG = 'neatlynx.conf.default'
CONFIG_TEMPLATE = '''[Global]
DataDir = {}
StateDir = {}
CacheDir = {}
Cloud = AWS
[AWS]
AccessKeyID =
SecretAccessKey =
Region = us-east-1
Zone = us-east-1a
StorageBucket = neatlynx-tutorial
Image = ami-2d39803a
InstanceType = t2.nano
SpotPrice =
SpotTimeout = 300
Storage = my-100gb-drive-io
KeyDir = ~/.ssh
KeyName = neatlynx-key
SecurityGroup = neatlynx-group'''
def __init__(self):
BaseCmd.__init__(self, parse_config=False)
pass
def define_args(self, parser):
self.add_string_arg(parser, '--data-dir', 'data', 'NeatLynx data directory')
self.add_string_arg(parser, '--cache-dir', 'cache', 'NeatLynx cache directory')
self.add_string_arg(parser, '--state-dir', 'state', 'NeatLynx state directory')
pass
def get_not_existing_dir(self, dir):
path = Path(os.path.join(self.git_dir, dir))
if path.exists():
raise InitError('Directory "{}" already exist'.format(path.name))
return path
def get_not_existing_conf_file(self):
path = Path(os.path.join(self.git_dir, self.CONFIG))
if path.exists():
raise InitError('Configuration file "{}" already exist'.format(path.name))
return path
def run(self):
data_dir_path = self.get_not_existing_dir(self.args.data_dir)
cache_dir_path = self.get_not_existing_dir(self.args.cache_dir)
state_dir_path = self.get_not_existing_dir(self.args.state_dir)
conf = self.get_not_existing_conf_file()
data_dir_path.mkdir()
cache_dir_path.mkdir()
state_dir_path.mkdir()
conf.write_text(self.CONFIG_TEMPLATE.format(data_dir_path.name,
cache_dir_path.name,
state_dir_path.name))
pass
if __name__ == '__main__':
try:
CmdInit().run()
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
+13
View File
@@ -0,0 +1,13 @@
class NeatLynxException(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
class GitCmdError(NeatLynxException):
def __init__(self, msg):
NeatLynxException.__init__(self, msg)
class ConfigError(NeatLynxException):
def __init__(self, msg):
NeatLynxException.__init__(self, 'Config file error: {}'.format(msg))