From c303c3e2a2e853dc0f6d26edd837e29ff44335c4 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Sat, 4 Mar 2017 00:28:24 -0800 Subject: [PATCH] The first commit: nlx-init cmd --- bin/nlx-cloud-instance-create | 0 bin/nlx-cloud-instance-delete | 0 bin/nlx-cloud-instance-describe | 0 bin/nlx-cloud-run | 0 bin/nlx-data-import | 0 bin/nlx-data-remove | 0 bin/nlx-data-sync | 0 bin/nlx-init | 3 ++ bin/nlx-repro | 0 bin/nlx-run | 0 neatlynx.conf | 29 ++++++++++ neatlynx/base_cmd.py | 96 +++++++++++++++++++++++++++++++++ neatlynx/cmd_init.py | 87 ++++++++++++++++++++++++++++++ neatlynx/exceptions.py | 13 +++++ 14 files changed, 228 insertions(+) create mode 100755 bin/nlx-cloud-instance-create create mode 100755 bin/nlx-cloud-instance-delete create mode 100755 bin/nlx-cloud-instance-describe create mode 100755 bin/nlx-cloud-run create mode 100755 bin/nlx-data-import create mode 100755 bin/nlx-data-remove create mode 100755 bin/nlx-data-sync create mode 100755 bin/nlx-init create mode 100755 bin/nlx-repro create mode 100755 bin/nlx-run create mode 100644 neatlynx.conf create mode 100644 neatlynx/base_cmd.py create mode 100644 neatlynx/cmd_init.py create mode 100644 neatlynx/exceptions.py diff --git a/bin/nlx-cloud-instance-create b/bin/nlx-cloud-instance-create new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-cloud-instance-delete b/bin/nlx-cloud-instance-delete new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-cloud-instance-describe b/bin/nlx-cloud-instance-describe new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-cloud-run b/bin/nlx-cloud-run new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-data-import b/bin/nlx-data-import new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-data-remove b/bin/nlx-data-remove new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-data-sync b/bin/nlx-data-sync new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-init b/bin/nlx-init new file mode 100755 index 000000000..6f74d8ba9 --- /dev/null +++ b/bin/nlx-init @@ -0,0 +1,3 @@ +#!/bin/bash + +python $NEATLYNX_HOME/lib/cmd_init.py $@ diff --git a/bin/nlx-repro b/bin/nlx-repro new file mode 100755 index 000000000..e69de29bb diff --git a/bin/nlx-run b/bin/nlx-run new file mode 100755 index 000000000..e69de29bb diff --git a/neatlynx.conf b/neatlynx.conf new file mode 100644 index 000000000..9fb339636 --- /dev/null +++ b/neatlynx.conf @@ -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 diff --git a/neatlynx/base_cmd.py b/neatlynx/base_cmd.py new file mode 100644 index 000000000..190aec7e2 --- /dev/null +++ b/neatlynx/base_cmd.py @@ -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 \ No newline at end of file diff --git a/neatlynx/cmd_init.py b/neatlynx/cmd_init.py new file mode 100644 index 000000000..c067dff4a --- /dev/null +++ b/neatlynx/cmd_init.py @@ -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) diff --git a/neatlynx/exceptions.py b/neatlynx/exceptions.py new file mode 100644 index 000000000..d30f29b29 --- /dev/null +++ b/neatlynx/exceptions.py @@ -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)) \ No newline at end of file