aggkit 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.dockerignore +20 -0
  3. data/.gitignore +109 -0
  4. data/.gitlab-ci.yml +66 -0
  5. data/.rspec +4 -0
  6. data/.rubocop.yml +98 -0
  7. data/.travis.yml +30 -0
  8. data/Gemfile +12 -0
  9. data/Gemfile.lock +55 -0
  10. data/README.md +96 -0
  11. data/aggkit.gemspec +38 -0
  12. data/bin/agg +167 -0
  13. data/bin/aggconsul +222 -0
  14. data/bin/agglock +71 -0
  15. data/bin/aggmerge +118 -0
  16. data/bin/aggwait +262 -0
  17. data/bin/consul.rb +222 -0
  18. data/bin/locker.rb +71 -0
  19. data/bin/merger.rb +118 -0
  20. data/bin/terminator.rb +71 -0
  21. data/bin/waiter.rb +262 -0
  22. data/docker/Dockerfile +112 -0
  23. data/docker/docker-compose.yml +12 -0
  24. data/docker/down.sh +4 -0
  25. data/docker/run_tests.sh +23 -0
  26. data/lib/aggkit/childprocess/abstract_io.rb +38 -0
  27. data/lib/aggkit/childprocess/abstract_process.rb +194 -0
  28. data/lib/aggkit/childprocess/errors.rb +28 -0
  29. data/lib/aggkit/childprocess/jruby/io.rb +17 -0
  30. data/lib/aggkit/childprocess/jruby/process.rb +161 -0
  31. data/lib/aggkit/childprocess/jruby/pump.rb +55 -0
  32. data/lib/aggkit/childprocess/jruby.rb +58 -0
  33. data/lib/aggkit/childprocess/tools/generator.rb +148 -0
  34. data/lib/aggkit/childprocess/unix/fork_exec_process.rb +72 -0
  35. data/lib/aggkit/childprocess/unix/io.rb +22 -0
  36. data/lib/aggkit/childprocess/unix/lib.rb +188 -0
  37. data/lib/aggkit/childprocess/unix/platform/i386-linux.rb +14 -0
  38. data/lib/aggkit/childprocess/unix/platform/i386-solaris.rb +13 -0
  39. data/lib/aggkit/childprocess/unix/platform/x86_64-linux.rb +14 -0
  40. data/lib/aggkit/childprocess/unix/platform/x86_64-macosx.rb +13 -0
  41. data/lib/aggkit/childprocess/unix/posix_spawn_process.rb +135 -0
  42. data/lib/aggkit/childprocess/unix/process.rb +91 -0
  43. data/lib/aggkit/childprocess/unix.rb +11 -0
  44. data/lib/aggkit/childprocess/version.rb +5 -0
  45. data/lib/aggkit/childprocess/windows/handle.rb +93 -0
  46. data/lib/aggkit/childprocess/windows/io.rb +25 -0
  47. data/lib/aggkit/childprocess/windows/lib.rb +418 -0
  48. data/lib/aggkit/childprocess/windows/process.rb +132 -0
  49. data/lib/aggkit/childprocess/windows/process_builder.rb +177 -0
  50. data/lib/aggkit/childprocess/windows/structs.rb +151 -0
  51. data/lib/aggkit/childprocess/windows.rb +35 -0
  52. data/lib/aggkit/childprocess.rb +213 -0
  53. data/lib/aggkit/env.rb +219 -0
  54. data/lib/aggkit/runner.rb +80 -0
  55. data/lib/aggkit/version.rb +5 -0
  56. data/lib/aggkit/watcher.rb +239 -0
  57. data/lib/aggkit.rb +15 -0
  58. metadata +196 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15b728b147389a89556cf28472062477aab155141d7ace18127c9815efaed608
4
+ data.tar.gz: eeb6be7bcf4547c69218f54ed15182a89f6235520c32d0a0c7e83adb356fc1c5
5
+ SHA512:
6
+ metadata.gz: b6816ebfa8a4daf33bc359a859a949147cbc242af2dff814b2d3a17b494bd6bded39c4117ccb968b53e434407edb665cb0232cd9ca4e6bfc9d91d80d2c097253
7
+ data.tar.gz: c106261c69ca34d8a0daf3e78d39ee1ed65357c81f23b2e6003aeb3eadc04c28562178c9ddd12ac983400ad9c21ad9c37b071aaa1a30772f32e2d911c5122898
data/.dockerignore ADDED
@@ -0,0 +1,20 @@
1
+ .git
2
+ log/*
3
+ tmp/*
4
+ doc/*
5
+ envs/*
6
+ Dockerfile
7
+ /node_modules
8
+ config/keys/*
9
+ config/database.yml
10
+ config/secrets.yml
11
+
12
+ .gitignore
13
+ .gitlab-ci.yml
14
+ base-service.yml
15
+ common-services.yml
16
+
17
+ vendor/cache
18
+
19
+ ca/db
20
+
data/.gitignore ADDED
@@ -0,0 +1,109 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # bundler state
8
+ /.bundle
9
+ vendor/cache
10
+ vendor/bundle
11
+ /vendor/ruby/
12
+
13
+ # minimal Rails specific artifacts
14
+ db/*.sqlite3
15
+ /db/*.sqlite3-journal
16
+ /log/*
17
+ /tmp/*
18
+
19
+ # add /config/database.yml if it contains passwords
20
+ # /config/database.yml
21
+
22
+ # various artifacts
23
+ **.war
24
+ *.rbc
25
+ *.sassc
26
+ .redcar/
27
+ .sass-cache
28
+ /config/config.yml
29
+ /coverage.data
30
+ /coverage/
31
+ /db/*.javadb/
32
+ /db/*.sqlite3
33
+ /config/database.yml
34
+ /config/secrets.yml
35
+ oxymoron.js
36
+ /doc/api/
37
+ /doc/app/
38
+ /doc/features.html
39
+ /doc/specs.html
40
+ /public/cache
41
+ /public/uploads
42
+ /public/stylesheets/compiled
43
+ /public/system/*
44
+ /spec/tmp/*
45
+ /cache
46
+ /capybara*
47
+ /capybara-*.html
48
+ /gems
49
+ /specifications
50
+ app/assets/fonts/disp-preview.html
51
+ rerun.txt
52
+ pickle-email-*.html
53
+ *.sublime-project
54
+ *.sublime-workspace
55
+ .zeus.sock
56
+ .byebug_history
57
+
58
+ #test
59
+ /docs
60
+
61
+
62
+ # If you find yourself ignoring temporary files generated by your text editor
63
+ # or operating system, you probably want to add a global ignore instead:
64
+ # git config --global core.excludesfile ~/.gitignore_global
65
+ #
66
+ # Here are some files you may want to ignore globally:
67
+
68
+ # scm revert files
69
+ **.orig
70
+
71
+ # Mac finder artifacts
72
+ .DS_Store
73
+
74
+ # Netbeans project directory
75
+ /nbproject/
76
+
77
+ # RubyMine project files
78
+ .idea
79
+
80
+ # Textmate project files
81
+ /*.tmproj
82
+
83
+ # vim artifacts
84
+ **.swp
85
+
86
+ # Environment files that may contain sensitive data
87
+ .env
88
+ .powenv
89
+
90
+ # tilde files are usually backup files from a text editor
91
+ *~
92
+ public/assets/
93
+ config/keys
94
+ .ruby-gemset
95
+ .ruby-version
96
+ spec/reports/
97
+ app/assets/javascripts/oxymoron.js
98
+
99
+ !/log/.keep
100
+ !/tmp/.keep
101
+
102
+ /node_modules
103
+ /yarn-error.log
104
+
105
+ .byebug_history
106
+ /public/packs
107
+ /public/packs-test
108
+ /node_modules
109
+ passenger.*.pid.lock
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,66 @@
1
+ before_script: &before_pipeline_script
2
+ # Install ssh-agent if not already installed, it is required by Docker.
3
+ # (change apt-get to yum if you use a CentOS-based image)
4
+ - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
5
+
6
+ # Run ssh-agent (inside the build environment)
7
+ - eval $(ssh-agent -s)
8
+
9
+ # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
10
+ - ssh-add <(echo "$SSH_PRIVATE_KEY")
11
+
12
+ # For Docker builds disable host key checking. Be aware that by adding that
13
+ # you are suspectible to man-in-the-middle attacks.
14
+ # WARNING: Use this only with the Docker executor, if you use it with shell
15
+ # you will overwrite your user's SSH config.
16
+ - mkdir -p ~/.ssh
17
+ - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
18
+ - mkdir -p ~/.docker
19
+
20
+ stages:
21
+ - build
22
+ - tests
23
+ - deploy
24
+
25
+ build:
26
+ image: rnds/ruby:2.3.0-test
27
+ stage: build
28
+ script:
29
+ - bundle install --jobs=3 --path vendor/bundle && bundle package --all
30
+ - gem build `ls | grep gemspec` && gem install `ls | grep -e '.gem$'`
31
+ cache:
32
+ paths:
33
+ - vendor/bundle
34
+ - vendor/cache
35
+ artifacts:
36
+ name: "gem"
37
+ paths:
38
+ - ./*.gem
39
+
40
+ test-2.1:
41
+ image: rnds/ruby:2.3.0-test
42
+ stage: tests
43
+ variables:
44
+ RUBY_VERSION: '2.1'
45
+ script:
46
+ - docker/run_tests.sh
47
+
48
+ test-2.5:
49
+ image: rnds/ruby:2.3.0-test
50
+ stage: tests
51
+ variables:
52
+ RUBY_VERSION: '2.5'
53
+ script:
54
+ - docker/run_tests.sh
55
+
56
+ pushgem:
57
+ image: rnds/ruby:2.3.0-test
58
+ stage: deploy
59
+ script:
60
+ - mkdir -p ~/.gem
61
+ - echo ":rubygems_api_key: ${RUBYGEMS_KEY}" > credentials
62
+ - export GEMFILE=`ls *.gem | tail -n 1`
63
+ - export GEMVERSION=`echo $GEMFILE | sed s/\.gem// | sed s/.*-//`
64
+ - gem yank aggkit -v ${GEMVERSION}
65
+ - gem push $GEMFILE
66
+
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --tty
3
+ --format progress
4
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,98 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - 'storage/**/*'
5
+ - 'docker/**/*'
6
+ - 'lib/aggkit/childprocess/**/*'
7
+ - 'lib/aggkit/childprocess,rb'
8
+
9
+
10
+ Security/YAMLLoad:
11
+ Enabled: false
12
+
13
+ Style/AsciiComments:
14
+ Enabled: false
15
+
16
+ Style/RedundantBegin:
17
+ Enabled: false
18
+
19
+ Style/GlobalVars:
20
+ AllowedVariables: ['$logger', '$root']
21
+
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - 'spec/**/*.rb'
25
+
26
+ Metrics/MethodLength:
27
+ Max: 20
28
+
29
+ Style/ClassAndModuleChildren:
30
+ EnforcedStyle: compact
31
+ Enabled: false
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Style/Lambda:
37
+ Enabled: false
38
+
39
+
40
+ Style/RaiseArgs:
41
+ EnforcedStyle: compact
42
+
43
+ Style/SpecialGlobalVars:
44
+ Enabled: false
45
+
46
+ Metrics/LineLength:
47
+ Max: 100
48
+
49
+ #Layout/IndentationWidth:
50
+ # Enabled: true
51
+
52
+ #Layout/IndentAssignment:
53
+ # Enabled: false
54
+ # IndentationWidth: false
55
+
56
+ #Layout/ElseAlignment:
57
+ # Enabled: false
58
+
59
+ #Layout/EndAlignment:
60
+ # Enabled: false
61
+
62
+ #Lint/AssignmentInCondition:
63
+ # Enabled: false
64
+
65
+ Layout/IndentationConsistency:
66
+ EnforcedStyle: rails
67
+
68
+ Layout/EmptyLines:
69
+ Enabled: false
70
+
71
+ Layout/EmptyLinesAroundClassBody:
72
+ EnforcedStyle: empty_lines
73
+
74
+ Layout/EmptyLinesAroundModuleBody:
75
+ EnforcedStyle: empty_lines
76
+
77
+ Layout/SpaceInsideBlockBraces:
78
+ EnforcedStyle: space
79
+ SpaceBeforeBlockParameters: false
80
+
81
+ Layout/SpaceAroundBlockParameters:
82
+ EnforcedStyleInsidePipes: no_space
83
+
84
+ Layout/SpaceBeforeBlockBraces:
85
+ Enabled: false
86
+
87
+ Style/NumericPredicate:
88
+ Enabled: false
89
+
90
+ Style/FrozenStringLiteralComment:
91
+ Enabled: false
92
+
93
+ Style/DoubleNegation:
94
+ Enabled: false
95
+
96
+ Style/SymbolArray:
97
+ Enabled: false
98
+
data/.travis.yml ADDED
@@ -0,0 +1,30 @@
1
+
2
+ language: ruby
3
+ rvm:
4
+ - 2.0
5
+ - 2.5
6
+
7
+ stage: test
8
+
9
+ before_install:
10
+ - sudo apt-get install -y libxml2-dev unzip curl
11
+
12
+ script:
13
+ - gem build `ls | grep gemspec`
14
+ - gem install `ls | grep -e '.gem$'`
15
+ - curl -sSLo /tmp/consul.zip https://releases.hashicorp.com/consul/1.2.1/consul_1.2.1_linux_amd64.zip
16
+ - sudo unzip -d /bin /tmp/consul.zip
17
+ - bundle exec rspec
18
+
19
+
20
+ jobs:
21
+ include:
22
+ - stage: gem release
23
+ rvm: 2.0
24
+ script: echo "Deploying to rubygems.org ..."
25
+ deploy:
26
+ provider: rubygems
27
+ api_key: $RUBYGEMS_KEY
28
+ gemspec: aggkit.gemspec
29
+
30
+
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aggredator.gemspec
4
+ gemspec
5
+
6
+
7
+ group :test do
8
+ gem 'awesome_print'
9
+ gem 'rspec'
10
+ gem 'rspec-retry'
11
+ gem 'rspec-set'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ aggkit (0.2.5)
5
+ diplomat
6
+ dotenv
7
+ json
8
+ tty-tree
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ awesome_print (1.8.0)
14
+ diff-lcs (1.3)
15
+ diplomat (2.0.2)
16
+ faraday (~> 0.9)
17
+ json
18
+ dotenv (2.6.0)
19
+ faraday (0.15.4)
20
+ multipart-post (>= 1.2, < 3)
21
+ json (2.1.0)
22
+ multipart-post (2.0.0)
23
+ rake (10.5.0)
24
+ rspec (3.8.0)
25
+ rspec-core (~> 3.8.0)
26
+ rspec-expectations (~> 3.8.0)
27
+ rspec-mocks (~> 3.8.0)
28
+ rspec-core (3.8.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-expectations (3.8.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-mocks (3.8.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-retry (0.6.1)
37
+ rspec-core (> 3.3)
38
+ rspec-set (0.1.3)
39
+ rspec-support (3.8.0)
40
+ tty-tree (0.2.0)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ aggkit!
47
+ awesome_print
48
+ bundler (~> 1.14)
49
+ rake (~> 10.0)
50
+ rspec
51
+ rspec-retry
52
+ rspec-set
53
+
54
+ BUNDLED WITH
55
+ 1.16.2
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # aggkit
2
+ Helper tools for work with docker and consul
3
+
4
+ [![Build Status](https://travis-ci.com/RnD-Soft/aggkit.svg?branch=master)](https://travis-ci.com/RnD-Soft/aggkit) [![Gem Version](https://badge.fury.io/rb/aggkit.svg)](https://badge.fury.io/rb/aggkit)
5
+
6
+ ## Scripts
7
+
8
+ Some useful scripts to run inside or outside container
9
+
10
+ ### waiter.rb
11
+
12
+ waiter.rb allows to wait many conditions:
13
+
14
+ * tcp port opening
15
+ * PostgreSQL database creation
16
+ * PostgreSQL table creation
17
+ * file creation
18
+ * healthy Consul service
19
+
20
+ ```
21
+ Usage: waiter.rb [options] -- exec
22
+ --tcp host:port Wait for tcp accepts on host:port
23
+ --db dbname Wait for PG database exists. Using --tcp to conenct PG
24
+ --tb tablename Wait for PG table exists. Using --tcp to conenct PG
25
+ -f, --file filename Wait for file exists.
26
+ --consul-addr addr=http://localhost:8500
27
+ HTTP addres to connect to consul
28
+ --consul Wait for local consul agent to be ready
29
+ --consul-service service Wait for service appear in consul
30
+ --consul-service-count count Wait for this count services appear in consul
31
+ --consul-tag tag User tag to filter services in consul
32
+ --user user username
33
+ --pass pass password
34
+ -t, --timeout secs=15 Total timeout
35
+ -i, --interval secs=2 Interval between attempts
36
+ -q, --quiet Do not output any status messages
37
+ ```
38
+
39
+
40
+ ### consul.rb
41
+
42
+ Helper to use Consul in 12Factor application. Inspired by https://github.com/hashicorp/envconsul:
43
+
44
+ * export Consul key/value as environment variables
45
+ * references in Consul key/value store
46
+ * read config file and store values in Consul
47
+ * read files and store it in Consul key/value store
48
+
49
+ ```
50
+ Usage: consul.rb [options] -- exec
51
+ --consul url Set up a custom Consul URL
52
+ --token token Connect into consul with custom access token (ACL)
53
+ --init [service] Initialize Consul services from config
54
+ --config file Read service configulation from file
55
+ --upload Upload files to variables
56
+ --show [service] Show service configulation from Consul
57
+ --override override existed keys
58
+ -d, --dereference dereference consul values in form of "consul://key/subkey"
59
+ --env prefix export KV values from prefix as env varaibles
60
+ --export add export to --env output
61
+ --pristine not include the parent processes' environment when exec child process
62
+ --put path:value put value to path
63
+ --get path get value from
64
+ ```
65
+
66
+ Example config.yml:
67
+ ```yaml
68
+ .dbconfig: &dbconfig
69
+ DATABASE_HOST:
70
+ value: db
71
+ DATABASE_NAME:
72
+ value: dbname
73
+
74
+ srv1: &srv1
75
+ <<: [*dbconfig]
76
+ LOG_LEVEL:
77
+ value: debug
78
+ CA_CERT:
79
+ file: /tmp/ca/cacert.pem
80
+ CLIENT_CERT:
81
+ value: consul://services/ca/private/cert.pem
82
+ CLIENT_KEY:
83
+ value: consul://services/ca/private/key.pem
84
+
85
+ srv2:
86
+ <<: *srv1
87
+ CA_CERT:
88
+ value: consul://services/env/srv1/ca_cert
89
+ ```
90
+
91
+ ### merger.rb
92
+
93
+ Merge docker-compose file of any version. Allow inheritance and extending services.
94
+ ```bash
95
+ COMPOSE_FILE=file1.yml:file2.yml merger.rb > /tmp/result.yml
96
+ ```
data/aggkit.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ $:.push File.expand_path('lib', __dir__)
2
+
3
+ # Maintain your gem's version:
4
+ require 'aggkit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'aggkit'
8
+ spec.version = Aggkit::VERSION
9
+ spec.license = 'MIT'
10
+ spec.authors = ['Godko Ivan', 'Samoilenko Yuri']
11
+ spec.email = ['igodko@rnds.pro', 'kinnalru@gmail.com']
12
+ spec.homepage = 'https://br.rnds.pro/aggredator/support/aggkit'
13
+ spec.required_ruby_version = '>= 2.0.0'
14
+
15
+ spec.summary = 'Helper scripts for work with docker and consul in Aggredator'
16
+ spec.description = 'Advanced docker and consul control scripts'
17
+
18
+ spec.bindir = 'bin'
19
+ spec.require_paths = ['lib']
20
+
21
+ if File.exist?(File.join(__dir__, '/', '.git'))
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+
26
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
27
+ end
28
+
29
+
30
+ spec.add_dependency 'diplomat'
31
+ spec.add_dependency 'dotenv'
32
+ spec.add_dependency 'json'
33
+ spec.add_dependency 'tty-tree'
34
+
35
+
36
+ spec.add_development_dependency 'bundler', '~> 1.14'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ end
data/bin/agg ADDED
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'tty-tree'
5
+ require 'aggkit/env'
6
+ require 'json'
7
+
8
+ @commands = {}
9
+
10
+ @exec = (begin
11
+ ARGV.join(' ').split(' -- ')[1].strip.tap do |cmd|
12
+ cmd.empty? ? nil : cmd
13
+ end
14
+ rescue StandardError
15
+ nil
16
+ end)
17
+
18
+ @opts = {}
19
+
20
+
21
+
22
+ exmaple_usage = "
23
+ #{'agg env --list'.ljust(30)} - list avaibale environments
24
+ #{'agg <envname> --show'.ljust(30)} - show environment details
25
+ #{'agg <envname> exec -- cmd'.ljust(30)} - exec command in environment
26
+ "
27
+
28
+ env_cmd_parser = OptionParser.new do |o|
29
+ o.banner = "Usage: agg env [options]"
30
+ o.on("-l", "--list", "list environments") do
31
+ @opts[:list] = true
32
+ end
33
+ o.on("-t", "--tree", "print as tree") do
34
+ @opts[:tree] = true
35
+ end
36
+ end
37
+
38
+ exec_cmd_parser = OptionParser.new do |o|
39
+ o.banner = "Usage: agg <envname> [options] exec [options] -- cmd"
40
+ o.on('-p', '--pristine', "not include the parent processes' environment when exec child process") do
41
+ @opts[:pristine] = true
42
+ end
43
+ o.on('--cd', "cd to envroot") do
44
+ @opts[:cd] = true
45
+ end
46
+ end
47
+
48
+ @single_subcommands ||= {
49
+ 'exec' => exec_cmd_parser
50
+ }
51
+
52
+ @global_subcommands ||= {
53
+ 'env' => env_cmd_parser
54
+ }
55
+
56
+ single_parser = OptionParser.new do |o|
57
+ o.banner = "Aggredator environment manager\nagg <envname> [options] [subcommand [options]]"
58
+
59
+ o.on("-s", "--show", "show envname details") do
60
+ @opts[:show] = true
61
+ end
62
+
63
+ o.on("-h", "--help", "show help") do
64
+ puts o.help
65
+ exit 0
66
+ end
67
+
68
+ o.separator ""
69
+ o.separator "Subcommands: #{@single_subcommands.keys.join(', ')}"
70
+ end
71
+
72
+ global_parser = OptionParser.new do |o|
73
+ o.banner = "Aggredator manager\nUsage: agg [envname] [options] [subcommand [options]]"
74
+
75
+ o.on("-h", "--help", "show help") do
76
+ puts o.help
77
+ exit 0
78
+ end
79
+
80
+ o.separator ""
81
+ o.separator "Subcommands: #{@global_subcommands.keys.join(', ')}"
82
+
83
+ o.separator ""
84
+ o.separator "Examples:\n#{exmaple_usage}"
85
+ end
86
+
87
+ if Aggkit::Env.list.include?(ARGV.first)
88
+ $env = Aggkit::Env.new(ARGV.shift)
89
+ end
90
+
91
+ if $env
92
+ $parser = single_parser
93
+ $parser.order!
94
+ @commands[:single] = true
95
+ @subcommands = @single_subcommands
96
+ else
97
+ $parser = global_parser
98
+ $parser.order!
99
+ @commands[:global] = true
100
+ @subcommands = @global_subcommands
101
+ end
102
+
103
+
104
+
105
+ if command = ARGV.shift
106
+ if @subcommands.has_key?(command)
107
+ $parser = @subcommands[command]
108
+ @commands[command.to_sym] = true
109
+ $parser.parse!
110
+ else
111
+ STDERR.puts "No such command: #{command.inspect}"
112
+ STDOUT.puts $parser.help
113
+ exit 1
114
+ end
115
+ end
116
+
117
+
118
+ def env_command opts
119
+ if opts[:list]
120
+ envs = Aggkit::Env.list
121
+ # if opts[:tree]
122
+ # data = envs.each_with_object([]) do |dir, ret|
123
+ # dir.split(File::SEPARATOR).reduce(ret) do |struct, token|
124
+ # puts "token: #{token} struct: #{struct}"
125
+ # struct << {token => []}
126
+ # puts " result: struct: #{struct}"
127
+ # struct.last[token]
128
+ # end
129
+ # end
130
+ # puts data
131
+ # puts TTY::Tree.new(data.first).render
132
+ # else
133
+ puts envs
134
+ # end
135
+ exit 0
136
+ end
137
+ end
138
+
139
+ def exec_command opts
140
+ raise "envname not specified!" unless $env
141
+ raise "cmd not specified!" unless @exec
142
+
143
+ e = $env.environment
144
+ e = ENV.to_h.merge(e) unless opts[:pristine]
145
+
146
+ Dir.chdir $env.env_root if opts[:cd]
147
+ exec(e, @exec, unsetenv_others: true)
148
+ end
149
+
150
+ if @commands[:global]
151
+ env_command(@opts) if @commands[:env]
152
+ end
153
+
154
+ if @commands[:single]
155
+ exec_command(@opts) if @commands[:exec]
156
+
157
+ if @opts[:show]
158
+ raise "envname not specified!" unless $env
159
+
160
+ puts JSON.pretty_generate($env.show)
161
+ exit 0
162
+ end
163
+ end
164
+
165
+
166
+ STDOUT.puts $parser.help
167
+ exit 1