From 76b15e15b2a8832c8bd42178c71fe0393ae6551c Mon Sep 17 00:00:00 2001 From: Austin Culter Date: Mon, 14 Feb 2022 19:03:28 +0100 Subject: [PATCH] Add all config options as CLI args (+new opt for repo_update) (#64) * Add new no_updates flag * autofix lint * moar moar lint * Nearly all the lint * Actually all of the lint * Thats what I get for using git add --all * I am king of introducing MD lint * Move repo.update gate to one line suggestion Co-authored-by: Phil Dibowitz Co-authored-by: Phil Dibowitz --- CONTRIBUTING.md | 3 +- README.md | 295 +++++++++++++++++++++++++++++---- bin/grocery-delivery | 97 +++++++++-- lib/grocery_delivery/config.rb | 29 ++-- 4 files changed, 364 insertions(+), 60 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 28e7a7f..ca7972e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,7 +5,8 @@ possible. ## Our Development Process All changes to Grocery Delivery are developed on this GitHub repo. Changes committed are then rolled out internally before a formal release is done on -RubyGems. +RubyGems. Do not bump the Gem version in `grocery_delivery.gemspec`; +maintainers will bump when the new release is ready. ## Pull Requests We actively welcome your pull requests. diff --git a/README.md b/README.md index 6d7244c..e9ea67d 100644 --- a/README.md +++ b/README.md @@ -48,41 +48,270 @@ another. The config file works the same as client.rb does for Chef - there are a series of keywords that take an argument and anything else is just standard Ruby. -All command-line options are available in the config file: -* dry_run (bool, default: false) -* debug (bool, default: false) -* config_file (string, default: `/etc/gd-config.rb`) -* lockfile (string, default: `/var/lock/subsys/grocery_delivery`) -* pidfile (string, default: `/var/run/grocery_delivery.pid`) - -In addition the following are also available: -* `master_path` - The top-level path for Grocery Delivery's work. Most other - paths are relative to this. Default: `/var/chef/grocery_delivery_work` -* `repo_url` - The URL to clone/checkout if it doesn't exist. Default: `nil` -* `reponame` - The relative directory to check the repo out to, inside of - `master_path`. Default: `ops` -* `cookbook_paths` - An array of directories that contain cookbooks relative to - `reponame`. Default: `['chef/cookbooks']` -* `role_path` - A directory to find roles in relative to `reponame`. Default: - `'chef/roles'` -* `role_type` - RB or JSON roles? Default: `rb` -* `databag_path` - A directory to find databags in relative to `reponame`. +The following configuration options are available: +* **`berks`** + + Description: Determines if we should use Berkshelf to resolve deps, upload cookbooks + + CLI Argument(s): `'-b'`, `'--berks'` + + Value data type: `Boolean` + + Default: `false` + + Notes: Existence of CLI argument equates to `true` + +* **`berks_bin`** + + Description: Path to Berkshelf binary. + + CLI Argument(s): `'-B', '--berks-bin FILE'` + + Value data type: `String` + + Default: `/opt/chefdk/bin/berks` + +* **`berks_config`** + + Description: Path to Berkshelf config. + + CLI Argument(s): `'--berks-config FILE'` + + Value data type: `String` + + Default: `nil` + +* **`config_file`** + + Description: Path to Grocery Delivery config + + CLI Argument(s): `-c`, `--config-file FILE` + + Value data type: `String` + + Default: `/etc/gd-config.rb` + +* **`cookbook_paths`** + + Description: Space-delimited array of dirs that contain cookbooks relative + to `reponame` + + CLI Argument(s): `'--cookbook-paths DIRECTORY1 DIRECTORY2 DIRECTORY3 ...'` + + Value data type: `String` + + Default: `['chef/cookbooks']` + +* **`databag_path`** + + Description: A directory to find databags in relative to `reponame`. + + CLI Argument(s): `'-d', '--databag-path FILE'` + + Value data type: `String` + Default: `'chef/databags'` -* `rev_checkpoint` - Name of the file to store the last-uploaded revision, - relative to `reponame`. Default: `gd_revision` -* `knife_config` - Knife config to use for uploads. Default: - `/root/.chef/knife.rb` - Note: `knife.rb` will need to set `cookbook_path` pointing to the cookbook - path in the work directory, - e.g. `/var/chef/grocery_delivery_work/ops/chef/cookbooks` -* `knife_bin` - Path to knife. Default: `/opt/chef/bin/knife` -* `vcs_type` - Git or SVN? Default: `svn` -* `vcs_path` - Path to git or svn binary. If not given, just uses 'git' or 'svn'. + +* **`dry_run`** + + Description: Enable dry-run mode. + + CLI Argument(s): `'-n', '--dry-run'` + + Value data type: `Boolean` + + Default: `false` + + Notes: Existence of CLI argument equates to `true` + +* **`knife_bin`** + + Description: Path to `knife` binary. + + CLI Argument(s): `'-k', '--knife-bin FILE'` + + Value data type: `String` + + Default: `/opt/chef/bin/knife` + + Notes: e.g. `/var/chef/grocery_delivery_work/ops/chef/cookbooks` + +* **`knife_config`** + + Description: Knife config to use for uploads. + + CLI Argument(s): `'-K', '--knife-config FILE'` + + Value data type: `String` + + Default: `/root/.chef/knife.rb` + + Notes: `knife.rb` will need to set `cookbook_path` pointing to the cookbook + path in the work directory + +* **`lockfile`** + + Description: Path to lockfile. + + CLI Argument(s): `'-l', '--lockfile FILE'` + + Value data type: `String` + + Default: `/var/lock/subsys/grocery_delivery` + +* **`master_path`** + + Description: The top-level path for Grocery Delivery's work (most other paths + are relative to this). + + CLI Argument(s): `'-m', '--master-path FILE'` + + Value data type: `String` + + Default: `/var/chef/grocery_delivery_work` + +* **`pidfile`** + + Description: Path to pidfile. + + CLI Argument(s): `-p, --pidfile FILE` + + Value data type: `String` + + Default: `/var/run/grocery_delivery.pid` + +* **`plugin_path`** + + Description: Path to plugin file. + + CLI Argument(s): `'-P', '--plugin-path FILE'` + + Value data type: `String` + + Default: `/etc/gd-plugin.rb` + +* **`repo_update`** + + Description: Enables/disables cookbook repo update before run. + + CLI Argument(s): `'-U', '--no-repo-update'` + + Value data type: `Boolean` + + Default: `true` + + Note: Existence of CLI argument equates to `false`. This might be beneficial + for those wanting to run Grocery Delivery in CI, within a container, or some + other non-standard environment where there is no need to update the cookbook + repository before running Grocery Delivery. + +* **`repo_url`** + + Description: The URL to clone/checkout if it doesn't exist. + + CLI Argument(s): `'-u', '--repo-url URL'` + + Value data type: `String` + Default: `nil` -* `plugin_path` - Path to plugin file. Default: `/etc/gd-plugin.rb` -* `berks` - Boolean to determine if we should use berkshelf to resolve - dependencies and upload cookbooks. Default: `false` -* `berks_bin` - Path to berkshelf. Default: `/opt/chefdk/bin/berks` + +* **`reponame`** + + Description: The relative directory to check the repo out to, inside of `master_path`. + + CLI Argument(s): `'-N', '--repo-name'` + + Value data type: `String` + + Default: `ops` + +* **`rev_checkpoint`** + + Description: File with last-uploaded revision, relative to reponame. + + CLI Argument(s): `'-C', '--revision-checkpoint FILE'` + + Value data type: `String` + + Default: `gd_revision` + +* **`role_path`** + + Description: A directory to find roles in relative to `reponame`. + + CLI Argument(s): `'-r', '--role-path FILE'` + + Value data type: `String` + + Default: `'chef/roles'` + +* **`role_type`** + + Description: RB or JSON roles? + + CLI Argument(s): `'-R', '--role-type TYPE'` + + Value data type: `String` + + Default: `rb` + +* **`stdout`** + + Description: Log to stdout as well. + + CLI Argument(s): `'--stdout'` + + Value data type: `Boolean` + + Default: `false` + + Notes: Existence of CLI argument equates to `true` + +* **`track_symlinks`** + + Description: Whether or not to track symlinks. + + CLI Argument(s): `'--track-symlinks'` + + Value data type: `Boolean` + + Default: `false` + + Notes: Existence of CLI argument equates to `true` + +* **`vcs_path`** + + Description: Path to git or svn binary. + + CLI Argument(s): `'--vcs-path FILE'` + + Value data type: `String` + + Default: `nil` + + Notes: If not given, just uses 'git' or 'svn' + +* **`vcs_type`** + + Description: Git or SVN? + + CLI Argument(s): `'--vcs-type TYPE'` + + Value data type: `String` + + Default: `svn` + +* **`verbosity`** + + Description: Verbosity level. + + CLI Argument(s): `'-v'`, `'--verbosity'` + + Value data type: N/A + + Default: `WARN` + + Notes: Specify twice via CLI for debug. ## Plugin diff --git a/bin/grocery-delivery b/bin/grocery-delivery index bb1008b..369c532 100755 --- a/bin/grocery-delivery +++ b/bin/grocery-delivery @@ -187,16 +187,16 @@ def setup_config options = {} OptionParser.new do |opts| options[:config_file] = GroceryDelivery::Config.config_file - opts.on('-n', '--dry-run', 'Dryrun mode') do |s| - options[:dry_run] = s + opts.on('-b', + '--berks', + 'Use Berkshelf to resolve deps, upload cookbooks.') do |_s| + options[:berks] = true end - opts.on('-v', '--verbosity', 'Verbosity level. Twice for debug.') do - # If -vv is supplied this block is executed twice - if options[:verbosity] - options[:verbosity] = ::Logger::DEBUG - else - options[:verbosity] = ::Logger::INFO - end + opts.on('-B', '--berks-bin FILE', 'Path to berkshelf binary') do |s| + options[:berks_bin] = s + end + opts.on('--berks-config FILE', 'Path to berkshelf config') do |s| + options[:berks_config] = s end opts.on('-c', '--config-file FILE', 'config file') do |s| unless File.exist?(File.expand_path(s)) @@ -205,14 +205,85 @@ def setup_config end options[:config_file] = s end + opts.on('-C', + '--revision-checkpoint FILE', + 'File with last-uploaded revision, relative to reponame') do |s| + options[:rev_checkpoint] = s + end + opts.on('--cookbook-paths DIRECTORY1 DIRECTORY2 DIRECTORY3 ...', + Array, + 'Space-delimited dirs array to cookbooks rel to reponame') do |s| + options[:cookbook_paths] = s + end + opts.on('-d', '--databag-path FILE', + 'Directory with databags in relative to reponame') do |s| + options[:databag_path] = s + end + opts.on('-k', '--knife-bin FILE', 'Path to knife') do |s| + options[:knife_bin] = s + end + opts.on('-K', + '--knife-config FILE', + 'Path to knife config to use for uploads') do |s| + options[:knife_config] = s + end opts.on('-l', '--lockfile FILE', 'lockfile') do |s| options[:lockfile] = s end + opts.on('-m', '--master-path FILE', 'master path') do |s| + options[:master_path] = s + end + opts.on('-n', '--dry-run', 'Dryrun mode') do |_s| + options[:dry_run] = true + end + opts.on('-N', + '--repo-name FILE', + 'Relative directory to clone to, inside of master_path') do |s| + options[:reponame] = s + end + opts.on('-p', '--pidfile FILE', 'Path to pidfile') do |s| + options[:pidfile] = s + end + opts.on('-P', '--plugin-path FILE', 'Path to plugin file') do |s| + options[:plugin_path] = s + end + opts.on('-r', + '--role-path FILE', + 'A directory to find roles in relative to reponame') do |s| + options[:role_path] = s + end + opts.on('-R', '--role-type TYPE', 'RB or JSON roles') do |s| + options[:role_type] = s + end opts.on('--stdout', 'Log to stdout as well.') do |_s| options[:stdout] = true end - opts.on('-p', '--pidfile FILE', 'pidfile') do |s| - options[:pidfile] = s + opts.on('--track-symlinks', 'Track symlinks.') do |_s| + options[:track_symlinks] = true + end + opts.on('-u', + '--repo-url URL', + 'URL to clone/checkout if it does not exist') do |s| + options[:repo_url] = s + end + opts.on('-U', + '--no-repo-update', + 'Disable cookbook repo update before run') do |_s| + options[:repo_update] = false + end + opts.on('--vcs-path FILE', 'Path to git or svn binary') do |s| + options[:vcs_path] = s + end + opts.on('--vcs-type TYPE', 'Git or SVN') do |s| + options[:vcs_type] = s + end + opts.on('-v', '--verbosity', 'Verbosity level. Twice for debug.') do + # If -vv is supplied this block is executed twice + if options[:verbosity] + options[:verbosity] = ::Logger::DEBUG + else + options[:verbosity] = ::Logger::INFO + end end end.parse! if File.exist?(File.expand_path(options[:config_file])) @@ -254,7 +325,9 @@ GroceryDelivery::Hooks.prerun(GroceryDelivery::Config.dry_run) if repo.exists? action('Updating repo') - repo.update unless GroceryDelivery::Config.dry_run + if GroceryDelivery::Config.repo_update && !GroceryDelivery::Config.dry_run + repo.update + end else unless GroceryDelivery::Config.repo_url GroceryDelivery::Log.error( diff --git a/lib/grocery_delivery/config.rb b/lib/grocery_delivery/config.rb index a87b5b1..50890f5 100644 --- a/lib/grocery_delivery/config.rb +++ b/lib/grocery_delivery/config.rb @@ -23,28 +23,29 @@ module GroceryDelivery # it's compatible with v2, so it should work in 11 too. class Config extend Mixlib::Config - stdout false - dry_run false - verbosity Logger::WARN + berks false + berks_bin '/opt/chefdk/bin/berks' + berks_config nil config_file '/etc/gd-config.rb' - pidfile '/var/run/grocery_delivery.pid' + cookbook_paths ['chef/cookbooks'] + databag_path 'chef/databags' + dry_run false lockfile '/var/lock/subsys/grocery_delivery' master_path '/var/chef/grocery_delivery_work' + knife_bin '/opt/chef/bin/knife' + knife_config '/root/.chef/knife.rb' + pidfile '/var/run/grocery_delivery.pid' + plugin_path '/etc/gd-plugin.rb' + repo_update true repo_url nil reponame 'ops' - cookbook_paths ['chef/cookbooks'] role_path 'chef/roles' role_type 'rb' - databag_path 'chef/databags' + stdout false + track_symlinks false rev_checkpoint 'gd_revision' - knife_config '/root/.chef/knife.rb' - knife_bin '/opt/chef/bin/knife' - vcs_type 'svn' vcs_path nil - plugin_path '/etc/gd-plugin.rb' - berks false - berks_bin '/opt/chefdk/bin/berks' - berks_config nil - track_symlinks false + vcs_type 'svn' + verbosity Logger::WARN end end