Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ops/cli/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from himl.main import ConfigRunner
from ops.cli.parser import SubParserConfig

logger = logging.getLogger(__name__)

class ConfigGeneratorParserConfig(SubParserConfig):
def get_name(self):
Expand All @@ -35,7 +36,8 @@ class ConfigGeneratorRunner(object):
def __init__(self, cluster_config_path):
self.cluster_config_path = cluster_config_path

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
logging.basicConfig(level=logging.INFO)
args.path = self.cluster_config_path
if args.output_file is None:
Expand Down
25 changes: 10 additions & 15 deletions src/ops/cli/helmfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ def get_help(self):
return 'Wrap common helmfile tasks using hierarchical configuration support'

def configure(self, parser):
parser.add_argument(
'subcommand',
help='plan | sync | apply | template',
type=str)
parser.add_argument(
'extra_args',
type=str,
nargs='*',
help='Extra args')
parser.add_argument(
'--helmfile-path',
type=str,
Expand All @@ -51,7 +42,9 @@ def get_epilog(self):
# Run helmfile sync
ops data/env=dev/region=va6/project=ee/cluster=experiments/composition=helmfiles helmfile sync
# Run helmfile sync for a single chart
ops data/env=dev/region=va6/project=ee/cluster=experiments/composition=helmfiles helmfile sync -- --selector chart=nginx-controller
ops data/env=dev/region=va6/project=ee/cluster=experiments/composition=helmfiles helmfile --selector chart=nginx-controller sync
# Run helmfile sync with concurrency flag
ops data/env=dev/region=va6/project=ee/cluster=experiments/composition=helmfiles helmfile --selector chart=nginx-controller sync --concurrency=1
'''


Expand All @@ -63,7 +56,7 @@ def __init__(self, ops_config, cluster_config_path, execute):
self.cluster_config_path = cluster_config_path
self.execute = execute

def run(self, args):
def run(self, args, extra_args):
config_path_prefix = os.path.join(self.cluster_config_path, '')
default_helmfiles = '../ee-k8s-infra/compositions/helmfiles'
args.helmfile_path = default_helmfiles if args.helmfile_path is None else os.path.join(
Expand All @@ -79,7 +72,7 @@ def run(self, args):
data = self.generate_helmfile_config(conf_path, args)
self.setup_kube_config(data)

command = self.get_helmfile_command(args)
command = self.get_helmfile_command(args, extra_args)
return dict(command=command)

def setup_kube_config(self, data):
Expand Down Expand Up @@ -150,6 +143,8 @@ def generate_helmfile_config(self, path, args):
output_file=output_file,
print_data=True)

def get_helmfile_command(self, args):
cmd = ' '.join(args.extra_args + [args.subcommand])
return "cd {} && helmfile {}".format(args.helmfile_path, cmd)
def get_helmfile_command(self, args, extra_args):
helmfile_args = ' '.join(extra_args)
return "cd {helmfile_path} && helmfile {helmfile_args}".format(
helmfile_path=args.helmfile_path,
helmfile_args=helmfile_args)
5 changes: 4 additions & 1 deletion src/ops/cli/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
# governing permissions and limitations under the License.

import yaml
import logging

from ansible.parsing.yaml.dumper import AnsibleDumper
from ansible.utils.color import stringc
from . import display
from .parser import configure_common_arguments, SubParserConfig

logger = logging.getLogger(__name__)

class InventoryParserConfig(SubParserConfig):
def get_name(self):
Expand Down Expand Up @@ -45,7 +47,8 @@ def __init__(self, ansible_inventory, cluster_name):
self.ansible_inventory = ansible_inventory
self.cluster_name = cluster_name

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
for host in self.get_inventory_hosts(args):
group_names = [group.name for group in host.get_groups()]
group_names = sorted(group_names)
Expand Down
5 changes: 4 additions & 1 deletion src/ops/cli/packer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

import logging
from ops.cli.parser import SubParserConfig
from . import aws

logger = logging.getLogger(__name__)

class PackerParserConfig(SubParserConfig):
def get_name(self):
Expand Down Expand Up @@ -39,7 +41,8 @@ def __init__(self, root_dir, cluster_config):
self.cluster_config = cluster_config
self.root_dir = root_dir

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
config_all = self.cluster_config.all()

packer_variables = config_all['packer']['variables']
Expand Down
4 changes: 4 additions & 0 deletions src/ops/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def parse_args(self, args=None):
RootParser._check_args_for_unicode(args)
return self._get_parser().parse_args(args)

def parse_known_args(self, args=None):
RootParser._check_args_for_unicode(args)
return self._get_parser().parse_known_args(args)


class SubParserConfig(object):
def get_name(self):
Expand Down
5 changes: 4 additions & 1 deletion src/ops/cli/playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from .parser import SubParserConfig
from .parser import configure_common_ansible_args, configure_common_arguments
import getpass
import logging

logger = logging.getLogger(__name__)

class PlaybookParserConfig(SubParserConfig):
def get_name(self):
Expand Down Expand Up @@ -66,7 +68,8 @@ def __init__(self, ops_config, root_dir, inventory_generator,
self.cluster_config_path = cluster_config_path
self.cluster_config = cluster_config

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
inventory_path, ssh_config_path = self.inventory_generator.generate()

ssh_config = "ANSIBLE_SSH_ARGS='-F %s'" % ssh_config_path
Expand Down
5 changes: 4 additions & 1 deletion src/ops/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

import logging
from .parser import configure_common_ansible_args, SubParserConfig

logger = logging.getLogger(__name__)

class CommandParserConfig(SubParserConfig):
def get_epilog(self):
Expand Down Expand Up @@ -61,7 +63,8 @@ def __init__(self, ops_config, root_dir, inventory_generator,
self.cluster_config_path = cluster_config_path
self.cluster_config = cluster_config

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
inventory_path, ssh_config_path = self.inventory_generator.generate()
limit = args.host_pattern

Expand Down
5 changes: 4 additions & 1 deletion src/ops/cli/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import getpass
import re
import os
import logging

logger = logging.getLogger(__name__)
IP_HOST_REG_EX = re.compile(r'^((\d+)\.(\d+)\.(\d+)\.(\d+):)?(\d+)$')


Expand Down Expand Up @@ -126,7 +128,8 @@ def __init__(self, cluster_config_path, cluster_config,
self.cluster_config = cluster_config
self.ansible_inventory = ansible_inventory

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
if args.keygen:
if self.cluster_config.has_ssh_keys:
err('Cluster already has ssh keys, refusing to overwrite')
Expand Down
5 changes: 4 additions & 1 deletion src/ops/cli/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

import logging
import getpass
import subprocess

from .parser import SubParserConfig
from . import *

logger = logging.getLogger(__name__)

class SyncParserConfig(SubParserConfig):
def configure(self, parser):
Expand Down Expand Up @@ -64,7 +66,8 @@ def __init__(self, cluster_config, root_dir,
self.cluster_config = cluster_config
self.ops_config = ops_config

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
inventory_path, ssh_config_path = self.inventory_generator.generate()
src = PathExpr(args.src)
dest = PathExpr(args.dest)
Expand Down
4 changes: 2 additions & 2 deletions src/ops/cli/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

logger = logging.getLogger(__name__)


class TerraformParserConfig(SubParserConfig):
def get_name(self):
return 'terraform'
Expand Down Expand Up @@ -171,7 +170,8 @@ def check_ops_version(self):
self.cluster_config.conf["terraform"]["ops_min_version"])
validate_ops_version(ops_min_version)

def run(self, args):
def run(self, args, extra_args):
logger.info("Found extra_args %s", extra_args)
self.check_ops_version()
terraform_config_path = os.environ.get(
"TF_CLI_CONFIG_FILE",
Expand Down
7 changes: 4 additions & 3 deletions src/ops/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ def configure_inventory(self):
self.inventory_plugins = inventory_plugins

def configure(self):
args = self.root_parser.parse_args(self.argv)
args, extra_args = self.root_parser.parse_known_args(self.argv)
configure_logging(args)

logger.debug('cli args: %s', args)
logger.debug('cli args: %s, extra_args: %s', args, extra_args)

# Bind some very useful dependencies
self.console_args = cache(instance(args))
self.console_extra_args = cache(instance(extra_args))
self.command = lambda c: self.console_args.command
self.cluster_config_path = cache(
lambda c: get_cluster_config_path(
Expand All @@ -144,7 +145,7 @@ def run(self):
command_name = '%s_runner' % self.console_args.command
runner_instance = self.get_instance(command_name)

return runner_instance.run(self.console_args)
return runner_instance.run(self.console_args, self.console_extra_args)


def run(args=None):
Expand Down