Skip to content

Commit

Permalink
Finalize Configuration and CLI (#37)
Browse files Browse the repository at this point in the history
* Add missing CLI flags and ENV variables to configuration
* Restructure CLI layout. Add 'branch' and 'domain' sub commands
* Refactor: Separate Config sources from Settings-Value
  MiqFlow::Config now does the config processing and presents the
  update_* methods to the rest of the program
  MiqFlow::Settings implements the update_* methods
* FIX wrong boolean for clear_tmp
* Add support for /etc/miqflow.yaml
* Remove support for configuration files in the current working directory
* Add support for configurable branch naming conventions
* Removes unnecessary restriction for branch names
* Replace invalid charachters in MIQ domain names
* Shorten domain prefix to 'feat' (prev 'feature')
* Refactor Travis build
  • Loading branch information
ThomasBuchinger committed Jan 27, 2019
1 parent 8db97f0 commit 408be68
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 188 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Metrics/MethodLength:
Max: 50
Metrics/AbcSize:
Max: 20
Exclude:
# mixin_config tends to overshoot, but is
# still easily readable
- lib/miq_flow/mixin_config.rb

Layout/SpaceBeforeBlockBraces:
EnforcedStyle: no_space
Expand Down
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ matrix:
env: TAG=latest
- name: "Hammer + Integration"
rvm: 2.4
env: TAG=latest-hammer SUITE=integration

before_install:
- script/prepare_manageiq.sh
- ruby -ryaml -e "c={'log_level'=>'debug', 'git'=>{'url'=>'https://github.com/ThomasBuchinger/automate-example'}}; puts c.to_yaml" > config.yaml
env: TAG=latest-hammer GIT_URL=https://github.com/ThomasBuchinger/automate-example
stage: integration
script:
- script/prepare_manageiq.sh
- rake install:local
- miq-flow deploy feature-1-f1 --provider docker --git-index 2
- ./script/verify_import.sh
script:
- bundle check # ManageIQ Container/Appliance needs to satisfy all dependencies
- rake install:local
- miq-flow deploy feature-1-f1 --provider docker
- ./script/verify_import.sh
- bundle install --with development
- bundle exec rake travis
15 changes: 13 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
# Set logging level
# If set to no_log, no log output will be generated
# Default: info
# Possible values: debug, info, warn, error, fatal
# Possible values: debug, info, warn, error, fatal, no_log
# log_level: info

# git:
Expand All @@ -19,7 +20,17 @@
# Specify credentials for the repository.
# NOTE: only HTTP Authenication is supported
# user: someone
# password: <TOKEN>
# pashsword: <TOKEN>

# Branch naming scheme
# This configures the naming scheme for your git branches (if you use one)
# and how MiqFlow derives a name for the domains imported from that branch.
# The default configuration matches: TYPE-NAME-optional-description
#
# characters used to separate part of yu naming convention
# separators: [ '-', '/' ]
# index of the NAME part in your naming convention (starts with 0)
# index: 1

# miq:
# url: https://localhost/api
Expand Down
5 changes: 3 additions & 2 deletions lib/miq_flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
require 'miq_flow/mixin_git'
require 'miq_flow/mixin_api'
require 'miq_flow/mixin_settings'
require 'miq_flow/mixin_config'
require 'miq_flow/manageiq'
require 'miq_flow/domain'
require 'miq_flow/feature'
require 'miq_flow/miqflow'
require 'miq_flow/cli/cli'
require 'miq_flow/cli'
require 'miq_flow/version'

$settings = {}
$settings[:miq] = {}
$settings[:git] = {}
MiqFlow::Settings.set_defaults()
MiqFlow::Config.set_defaults()
2 changes: 1 addition & 1 deletion lib/miq_flow/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

require_relative 'cli/cli.rb'
require_relative 'cli/main.rb'
31 changes: 31 additions & 0 deletions lib/miq_flow/cli/branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'thor'

module MiqFlow
module Cli
# Implements list subcommand
class BranchCli < Thor
include MiqFlow::Cli

desc 'list', 'List avaliable Feature Branches'
def list
cli_setup(options, %i[git])
branches = MiqFlow::GitMethods.get_remote_branches()
text = branches.map{ |b| MiqFlow::Feature.new(b.name, {}).show_summary() }
puts text
MiqFlow.tear_down()
end

desc 'inspect BRANCH', 'Show detailed information about this Feature-Branch'
option :short, type: :boolean, default: false, desc: 'Same as list'
def inspect(name)
cli_setup(options, %i[git])
feature = MiqFlow::Feature.new(name, {})
text = options[:short] ? feature.show_summary() : feature.show_details()
puts text
MiqFlow.tear_down()
end
end
end
end
68 changes: 0 additions & 68 deletions lib/miq_flow/cli/cli.rb

This file was deleted.

19 changes: 19 additions & 0 deletions lib/miq_flow/cli/domain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'thor'

module MiqFlow
module Cli
# Implements list subcommand
class DomainCli < Thor
include MiqFlow::Cli

desc 'list', 'List existing Automate Domains in ManageIQ'
def list
cli_setup(options, %i[api])
api = MiqFlow::ManageIQ.new
puts api.list_domains
end
end
end
end
28 changes: 0 additions & 28 deletions lib/miq_flow/cli/list.rb

This file was deleted.

73 changes: 73 additions & 0 deletions lib/miq_flow/cli/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require 'thor'
require_relative 'branch.rb'
require_relative 'domain.rb'

module MiqFlow
# Implements common CLI methods
module Cli
def cli_setup(options={}, mode=[])
MiqFlow::Config.process_cli_flags(options) # Set log level first
MiqFlow::Config.search_config_files()
MiqFlow::Config.process_environment_variables()
MiqFlow::Config.process_config_file(options['config'])
MiqFlow::Config.process_cli_flags(options) # And again for precedence

MiqFlow.validate(mode)
MiqFlow.init()
MiqFlow.prepare_repo()
end

# Implements CLI
class MainCli < Thor
include MiqFlow::Cli

def self.exit_on_failure?
true
end

class_option :verbose, type: :boolean, default: false, desc: 'Turn on verbose logging'
class_option :quiet, type: :boolean, default: false, desc: 'Only show errors and warnings'
class_option :silent, type: :boolean, default: false, desc: 'Do not output anything'
class_option :cleanup, type: :boolean, desc: 'Clean up the working dir before exiting'
class_option :workdir, type: :string, desc: 'Override the working directory'
class_option :config, type: :string, alias: '-c', desc: 'Specify config file to load'

class_option :git_url, type: :string, desc: 'Git clone URL for remote repositories'
class_option :git_path, type: :string, desc: 'path to a local git repositories'
class_option :git_user, type: :string, desc: 'Username for remote repositories'
class_option :git_password, type: :string, desc: 'Password/token for remote repositories'
class_option :git_separator, type: :string, desc: 'List of characters separating part of your ' \
'branch naming convention'
class_option :git_index, type: :numeric, desc: 'Index the NAME par of your branch naming convenion'

class_option :miq_url, type: :string, desc: 'ManageIQ API URL. (e.g. https://localhost/api)'
class_option :miq_user, type: :string, desc: 'ManageIQ API User. (default: admin)'
class_option :miq_password, type: :string, desc: 'Passwork/login-token for the ManageIQ API User'

desc 'branch', 'Branch commands'
subcommand 'branch', MiqFlow::Cli::BranchCli

desc 'domain', 'Domain commands'
subcommand 'domain', MiqFlow::Cli::DomainCli

desc 'deploy BRANCH', 'Deploy a Feature Branch'
option :name, desc: 'specify domain identifier (default: 3rd segment of NAME, separated by \'-\')'
option :priority, type: :numeric, desc: 'Not-yet-implemented'
option :provider, desc: 'How to talk to ManageIQ (default: noop)'
def deploy(branch)
cli_setup(options, %i[git miq])
miq_domain = options[:name]
provider = options.fetch(:provider, 'default')
prio = options[:miq_priority]

opts = { feature_name: miq_domain, provider: provider }
opts[:miq_priotiry] = prio
feature = MiqFlow::Feature.new(branch, opts)
feature.deploy()
MiqFlow.tear_down()
end
end
end
end
2 changes: 1 addition & 1 deletion lib/miq_flow/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self.create_from_file(dom)
opts[:provider_name] = dom[:provider]
opts[:branch_name] = dom[:branch_name]

new_name = "feature_#{dom[:feature_name]}_#{opts[:export_name]}"
new_name = "feat_#{dom[:feature_name]}_#{opts[:export_name]}"
opts.reject!{ |_, value| value.nil? }
self.new(new_name, opts)
end
Expand Down
9 changes: 5 additions & 4 deletions lib/miq_flow/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ class Feature
# @option opts [String] :base('master')
# @option opts [Array<String>] :prefix(feature, fix)
def _set_defaults(opts={})
@remote_name = opts.fetch(:remote_name, 'origin')
@base = opts.fetch(:base, 'master')
@prefixes = opts.fetch(:prefix, %w[feature fix])
@remote_name = opts.fetch(:remote_name, 'origin')
@base = opts.fetch(:base, 'master')
# unused
@prefixes = opts.fetch(:prefix, [''])
end

# Represents a feature-branch
Expand All @@ -32,7 +33,7 @@ def _set_defaults(opts={})
# @option opts @see _set_defaults
def initialize(branch_name, opts={})
_set_defaults(opts)
@name = opts.fetch(:feature_name, branch_name.split(/-/)[2]) || File.basename(branch_name)
@name = opts.fetch(:feature_name, nil) || name_from_branch(branch_name)
$logger.debug("Creating Feature: branch=#{branch_name} domain=#{@name}")

@git_repo = opts.fetch(:git_repo, nil) || $git_repo
Expand Down
3 changes: 2 additions & 1 deletion lib/miq_flow/miqflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Global Methods
module MiqFlow
include MiqFlow::Settings
include MiqFlow::Config
include MiqFlow::GitMethods

def self.init
Expand All @@ -23,7 +24,7 @@ def self.prepare_repo
end

def self.tear_down
clean_tmp_dir() unless $settings[:clear_tmp]
clean_tmp_dir() if $settings[:clear_tmp]
end

def self.clean_tmp_dir
Expand Down
Loading

0 comments on commit 408be68

Please sign in to comment.