escli 1.0.0

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 (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +41 -0
  7. data/Rakefile +10 -0
  8. data/bin/escli +5 -0
  9. data/bin/setup +8 -0
  10. data/escli.gemspec +34 -0
  11. data/lib/canzea/cli/canzea.rb +323 -0
  12. data/lib/canzea/cli/escli.rb +207 -0
  13. data/lib/canzea/commands/add-env.rb +64 -0
  14. data/lib/canzea/commands/apply-config.rb +50 -0
  15. data/lib/canzea/commands/config-git-commit.rb +78 -0
  16. data/lib/canzea/commands/ecosystem/ecosystem.rb +66 -0
  17. data/lib/canzea/commands/ecosystem/resources.rb +82 -0
  18. data/lib/canzea/commands/gen-user.rb +22 -0
  19. data/lib/canzea/commands/get-catalog.rb +46 -0
  20. data/lib/canzea/commands/login.rb +50 -0
  21. data/lib/canzea/commands/prepare-plan.rb +82 -0
  22. data/lib/canzea/commands/push-config.rb +283 -0
  23. data/lib/canzea/commands/register-metadata.rb +79 -0
  24. data/lib/canzea/commands/remote-bootstrap.rb +38 -0
  25. data/lib/canzea/commands/remote-run.rb +37 -0
  26. data/lib/canzea/commands/update-config.rb +26 -0
  27. data/lib/canzea/config.rb +35 -0
  28. data/lib/canzea/core/audit.rb +86 -0
  29. data/lib/canzea/core/prepare-environment.rb +194 -0
  30. data/lib/canzea/core/registry.rb +191 -0
  31. data/lib/canzea/core/ssh-base-cmd-class.rb +103 -0
  32. data/lib/canzea/core/template-runner.rb +41 -0
  33. data/lib/canzea/core/trace-component.rb +171 -0
  34. data/lib/canzea/core/trace-runner.rb +108 -0
  35. data/lib/canzea/environment.rb +6 -0
  36. data/lib/canzea/helper-run-class.rb +89 -0
  37. data/lib/canzea/plan-step-class.rb +210 -0
  38. data/lib/canzea/registry.rb +12 -0
  39. data/lib/canzea/version.rb +3 -0
  40. metadata +201 -0
@@ -0,0 +1,207 @@
1
+ require 'cri'
2
+ require 'fileutils'
3
+ require 'pathname'
4
+ require 'git'
5
+ require 'io/console'
6
+ require "canzea/config"
7
+ require "canzea/version"
8
+ require "canzea/environment"
9
+ require "canzea/helper-run-class"
10
+ require "canzea/plan-step-class"
11
+ require "canzea/core/trace-component"
12
+ require "canzea/core/ssh-base-cmd-class"
13
+ require "canzea/core/template-runner"
14
+ require "canzea/commands/login"
15
+ require "canzea/commands/ecosystem/ecosystem"
16
+ require "canzea/commands/ecosystem/resources"
17
+
18
+
19
+ =begin
20
+
21
+ escli login
22
+ escli logout
23
+ escli ecosystem list
24
+ escli ecosystem set 23432894382932
25
+ escli resource plus test.yaml prod-1
26
+ escli resource apply f67c70b0-2706-48b5-ac4b-c76eeb72effe
27
+
28
+
29
+ escli workspace list
30
+
31
+
32
+ escli ecosystem create/destroy
33
+ escli workspace build
34
+ escli block install
35
+ escli block configure
36
+ escli project add
37
+
38
+ escli resource get block gocd/docker
39
+
40
+ escli resource list
41
+ escli resource check
42
+ escli resource plus
43
+ escli resource minus
44
+
45
+ escli job submit
46
+
47
+ escli ecosystem set 23432894382932
48
+ escli ecosystem get 23432894382932
49
+
50
+ All “helpers” have a GET and LIST
51
+
52
+ =end
53
+
54
+ module Canzea
55
+
56
+ root_cmd = Cri::Command.define do
57
+
58
+ name 'escli'
59
+ usage 'escli [options]'
60
+ summary 'escli cli'
61
+ description 'Canzea Ecosystem command line interface.'
62
+
63
+ flag :h, :help, 'show help for this command' do |value, cmd|
64
+ puts cmd.help
65
+ exit 0
66
+ end
67
+
68
+ flag :v, :version, 'Version' do |value, cmd|
69
+ puts "ES CLI: v#{Canzea::VERSION}"
70
+ end
71
+
72
+ flag nil, :reset, 'Refresh catalog to latest'
73
+ flag nil, :fv, 'Show version and the catalog version'
74
+
75
+ option nil, :config, 'Config', argument: :required
76
+ option nil, :catalogTag, 'Specific tag of the catalog', argument: :required
77
+
78
+ run do |opts, args, cmd|
79
+
80
+ test = opts.fetch(:test, false)
81
+
82
+ if opts[:config]
83
+ file = File.read(opts[:config])
84
+ Canzea::configure JSON.parse(file)
85
+ end
86
+
87
+ extraConfig = Canzea::config[:config_location] + "/config.json"
88
+ if File.exists?(extraConfig)
89
+ if (opts[:verbose])
90
+ puts "-- Reading #{extraConfig}"
91
+ end
92
+ file = File.read(extraConfig)
93
+ Canzea::configure JSON.parse(file)
94
+ end
95
+
96
+ if File.exists?(Canzea::config[:catalog_location]) == false
97
+ GetCatalog.new.do(opts.fetch(:catalogTag, nil));
98
+ end
99
+
100
+ if (opts[:reset])
101
+ GetCatalog.new.do(opts.fetch(:catalogTag, nil));
102
+ end
103
+
104
+ GetCatalog.new.state
105
+
106
+ if (opts[:fv])
107
+ puts "ES CLI: v#{Canzea::VERSION}"
108
+
109
+ puts "Catalog: #{ENV['CATALOG_BRANCH']} ( #{ENV['CATALOG_COMMIT']} )"
110
+ end
111
+
112
+
113
+ args.each do |arg|
114
+ puts "Publishing #{arg}…"
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ root_cmd.define_command do
121
+ name 'logout'
122
+ usage 'logout [options]'
123
+ summary 'Clears credentials'
124
+ description 'This command clears credentials.'
125
+
126
+ run do |opts, args, cmd|
127
+ Login.new.logout
128
+ end
129
+ end
130
+
131
+
132
+ root_cmd.define_command('login') do
133
+ name 'login'
134
+ usage 'login'
135
+ summary 'Authenticates and stores token.'
136
+
137
+ run do |opts, args, cmd|
138
+
139
+ print "Enter your username: "
140
+ username = STDIN.gets.chomp
141
+
142
+ print "Enter your password: "
143
+ password = STDIN.noecho(&:gets).chomp
144
+ puts ""
145
+ Login.new.do username, password
146
+ end
147
+ end
148
+
149
+ escmd = root_cmd.define_command('ecosystem') do
150
+ name 'ecosystem'
151
+ usage 'ecosystem [options]'
152
+ summary 'Manage an ecosystem (create, destroy, list, set)'
153
+
154
+ run do |opts, args, cmd|
155
+ end
156
+ end
157
+
158
+ escmd.define_command('set') do
159
+ usage 'set [id]'
160
+ summary 'Sets the ecosystem ID.'
161
+
162
+ run do |opts, args, cmd|
163
+ Ecosystem.new.set args[0]
164
+ end
165
+ end
166
+
167
+ escmd.define_command('list') do
168
+ usage 'list'
169
+ summary 'List all ecosystems for account'
170
+ run do |opts, args, cmd|
171
+ Ecosystem.new.list
172
+ end
173
+ end
174
+
175
+ escmd.define_command('create') do
176
+ usage 'create [options]'
177
+ summary 'Create an ecosystem'
178
+ end
179
+
180
+ escmd.define_command('destroy') do
181
+ usage 'destroy [options]'
182
+ summary 'Destroy an ecosystem'
183
+ end
184
+
185
+ escmd.define_command('info') do
186
+ usage 'info [options]'
187
+ summary 'Info about currently set ecosystem'
188
+ end
189
+
190
+ root_cmd.define_command('resource') do
191
+ name 'resource'
192
+ usage 'resource plus [manifest File] [segment]'
193
+ summary 'Apply resources to the environment'
194
+
195
+ run do |opts, args, cmd|
196
+ if args[0] == 'plus'
197
+ Resources.new.plus args[1], args[2]
198
+ end
199
+ if args[0] == 'apply'
200
+ Resources.new.apply args[1]
201
+ end
202
+ end
203
+ end
204
+
205
+ root_cmd.run(ARGV)
206
+
207
+ end
@@ -0,0 +1,64 @@
1
+ require 'json'
2
+ require 'logger'
3
+ require 'canzea/plan-step-class'
4
+
5
+ class AddEnv
6
+ def initialize (_raw = false)
7
+ @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
8
+ @raw = _raw
9
+ end
10
+
11
+ def add (key, value)
12
+ extraConfig = Canzea::config[:config_location] + "/env.json"
13
+ envs = loadFile()
14
+ envs["vars"][key] = value
15
+ File.write(extraConfig, JSON.generate(envs))
16
+ end
17
+
18
+ def addSecret (key, value)
19
+ extraConfig = Canzea::config[:config_location] + "/env.json"
20
+ envs = loadFile()
21
+ envs['secrets'][key] = value
22
+ File.write(extraConfig, JSON.generate(envs))
23
+ end
24
+
25
+ def loadFile()
26
+ extraConfig = Canzea::config[:config_location] + "/env.json"
27
+ if File.exists?(extraConfig)
28
+ file = File.read(extraConfig)
29
+ envs = JSON.parse(file)
30
+ else
31
+ envs = {"vars"=>{}, "secrets"=>{}}
32
+ end
33
+ return envs
34
+ end
35
+
36
+ def injectEnvironmentVariables()
37
+ extraConfig = Canzea::config[:config_location] + "/env.json"
38
+ @log.info "Looking at for env vars: #{extraConfig}"
39
+ if File.exists?(extraConfig)
40
+ pputs "-- Reading #{extraConfig}"
41
+ file = File.read(extraConfig)
42
+ envs = JSON.parse(file)
43
+ envs['vars'].keys.each { | key |
44
+ val = envs['vars'][key]
45
+ pputs "-- #{key} == #{val}"
46
+ @log.info "Setting: #{key} == #{val}"
47
+ ENV.store(key, val)
48
+ }
49
+ envs['secrets'].keys.each { | key |
50
+ val = envs['secrets'][key]
51
+ pputs "-- #{key} == XXXXXX"
52
+ @log.info "Setting: #{key} == XXXXXXX"
53
+ ENV.store(key, val)
54
+ }
55
+ end
56
+ end
57
+
58
+ def pputs (s)
59
+ if (@raw == false)
60
+ puts "#{s}"
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,50 @@
1
+ require 'json'
2
+ require 'logger'
3
+ require 'canzea/commands/add-env'
4
+ require 'canzea/plan-step-class'
5
+
6
+ class ApplyConfig
7
+ def initialize ()
8
+ @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
9
+ end
10
+
11
+ def do (gitRoot, test = false, stepNum = nil, task = nil)
12
+ ps = PlanStep.new
13
+
14
+ # Read the configuration file and make calls out to run
15
+
16
+ log "Processing #{gitRoot}/configure.json"
17
+
18
+ steps = JSON.parse(File.read("#{gitRoot}/configure.json"))
19
+
20
+
21
+ index = 1
22
+ steps["steps"].each { | step |
23
+
24
+ # Needs to be after each step because new env vars could be set (i.e./ CONSUL_URL)
25
+ AddEnv.new.injectEnvironmentVariables()
26
+
27
+ ref = "Step #{index.to_s.rjust(2, "0")} / #{steps['steps'].length}"
28
+
29
+ role = step['role']
30
+ solution = step['solution']
31
+
32
+ ENV['ES_STEP'] = "#{index}";
33
+
34
+ if (stepNum == nil or index >= Integer(stepNum))
35
+ log " [#{ref}] Configure for #{role} and #{solution}"
36
+ ps.runPhaseConfigure role, solution, test, (task == nil ? 1:task), ref
37
+ task = 1
38
+ else
39
+ log " [#{ref}] Configure for #{role} and #{solution} SKIP"
40
+ end
41
+ index = index + 1
42
+ }
43
+ end
44
+
45
+ def log (msg)
46
+ puts msg
47
+ @log.info(msg)
48
+ end
49
+
50
+ end
@@ -0,0 +1,78 @@
1
+ require "git"
2
+ require "fileutils"
3
+ require "pathname"
4
+
5
+ class ConfigGitCommit
6
+ def initialize ()
7
+ @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
8
+ end
9
+
10
+ def do(gitRoot, gitUri, sourcePath, template, basePath, parameters)
11
+ begin
12
+ log "Using basePath #{basePath}"
13
+
14
+ if (File.exists?(gitRoot) == false)
15
+ log "Cloning git repository #{gitUri}"
16
+ Git.config do |config|
17
+ config.ssh_key = "#{Dir.home}/.ssh/id_rsa"
18
+ end
19
+ g = Git.clone(gitUri, "", :path => gitRoot)
20
+ end
21
+
22
+ # if file exists on file system and does not exist in gitRoot, then commit the "oem" file
23
+
24
+ if (File.exists?(sourcePath))
25
+ dest = "#{gitRoot}/#{basePath}#{Pathname.new(sourcePath).realpath}"
26
+ else
27
+ dest = "#{gitRoot}/#{basePath}#{sourcePath}"
28
+ end
29
+ log "Writing to #{dest}"
30
+
31
+ if (File.exist?(dest) == false and File.exist?(sourcePath))
32
+ FileUtils.mkdir_p Pathname.new(dest).dirname
33
+ FileUtils.cp sourcePath, dest
34
+
35
+ log "Recording in repo the default file first: #{sourcePath}"
36
+ if (Canzea::config[:track_changes_in_git])
37
+ g = Git.init(gitRoot)
38
+ g.add(:all=>true)
39
+ g.commit("Original default #{sourcePath}")
40
+ end
41
+ end
42
+
43
+ if template
44
+ log "Processing template #{template}"
45
+ FileUtils.mkdir_p Pathname.new(sourcePath).dirname
46
+ t = Template.new
47
+ t.processAndWriteToFile template, sourcePath, parameters
48
+ end
49
+ FileUtils.mkdir_p Pathname.new(dest).dirname
50
+ FileUtils.cp sourcePath, dest, :verbose => true
51
+
52
+ if (Canzea::config[:track_changes_in_git])
53
+ g = Git.init(gitRoot)
54
+ g.add(:all=>true)
55
+ log "#{g.status.changed.size() + g.status.added.size()}"
56
+ if (g.status.changed.size() > 0 or g.status.added.size() > 0)
57
+ g.commit("Config update #{sourcePath}")
58
+ end
59
+ end
60
+
61
+ if (Canzea::config[:track_changes_in_git])
62
+ g = Git.init(gitRoot)
63
+ g.push
64
+ end
65
+ rescue => exception
66
+ @log.error("ConfigGitCommit Failed")
67
+ @log.error(exception.to_s)
68
+ @log.error(exception.backtrace)
69
+ abort()
70
+ end
71
+ end
72
+
73
+ def log (msg)
74
+ puts msg
75
+ @log.info(msg)
76
+ end
77
+
78
+ end
@@ -0,0 +1,66 @@
1
+
2
+ =begin
3
+
4
+ curl -v -X GET -H "Content-Type: application/json" https://canzea.com/console-app/api/api/v2/saasexpress/ecosystem
5
+
6
+ Canzea::config[:canzea_platform_uri] + "/api/v2/saasexpress/ecosystem"
7
+
8
+ escli ecosystem set 5b9c602fe4b02b1389591ecc
9
+
10
+ =end
11
+
12
+ require 'net/http'
13
+ require 'json'
14
+ require 'canzea/commands/login'
15
+
16
+ class Ecosystem
17
+ def set(id)
18
+ token = Login.new.get()
19
+
20
+ uri = URI(Canzea::config[:canzea_platform_uri] + "/api/v2/saasexpress/ecosystem/#{id}")
21
+
22
+ req = Net::HTTP::Get.new(uri, 'Authorization' => "Bearer #{token}")
23
+
24
+ https = Net::HTTP.new(uri.hostname,uri.port)
25
+ https.use_ssl = uri.instance_of? URI::HTTPS
26
+
27
+ res = https.request(req)
28
+
29
+ case res
30
+ when Net::HTTPSuccess, Net::HTTPRedirection
31
+ puts JSON.pretty_generate(JSON.parse(res.body))
32
+ es = JSON.parse(res.body)
33
+
34
+ scope = Canzea::config[:config_location] + '/.scope'
35
+ File.open(scope, 'w') { |file| file.write({id:id}.to_json) }
36
+ puts "Ecosystem set to #{es['ecosystemRefId']} with domain #{es['fqdomainName']}"
37
+ else
38
+ puts res.body
39
+ end
40
+
41
+ end
42
+
43
+ def list()
44
+ token = Login.new.get()
45
+
46
+ uri = URI(Canzea::config[:canzea_platform_uri] + "/api/v2/saasexpress/ecosystem/summary")
47
+
48
+ req = Net::HTTP::Get.new(uri, 'Authorization' => "Bearer #{token}")
49
+
50
+ https = Net::HTTP.new(uri.hostname,uri.port)
51
+ https.use_ssl = uri.instance_of? URI::HTTPS
52
+
53
+ res = https.request(req)
54
+
55
+ case res
56
+ when Net::HTTPSuccess, Net::HTTPRedirection
57
+ list = JSON.parse(res.body)
58
+ list.each { | es |
59
+ printf "Ecosystem: %-25s %-15s %-15s %-8s %s\n", es['_id'], es['status'], es['fqdomainName'], es['ecosystemRefId'], es['name']
60
+ }
61
+ else
62
+ puts res.body
63
+ end
64
+
65
+ end
66
+ end