apmate 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00c81f15c49086a7167c52064a1019cd158ec796
4
+ data.tar.gz: e8ada37329bd16fad2e53cbcea4fc161bde7a544
5
+ SHA512:
6
+ metadata.gz: 980af1481fc2670ae0bff677da1a621cc6aba7554507d569a8f1418b09668cfb0f390105d59539f23a1d73fdd828c9e4b7d0d8413c9c1dee005f0de01e9c9098
7
+ data.tar.gz: c19962324a209c0dcbf4bd9bfa9dba9dfd34d0fe2ac80cb4902b2cfe311e348d39b68a75efc3950a23d57ef249fa1dd80426251f7835b5f5c5155206658ea461
data/exe/apmate ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'apmate'
4
+
5
+ cli = Apmate::CLI.new
6
+ cli.run
data/lib/apmate.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "apmate/version"
2
+ require "apmate/cli"
data/lib/apmate/cli.rb ADDED
@@ -0,0 +1,212 @@
1
+ require 'active_support/inflector'
2
+ require 'cri'
3
+ require 'json'
4
+ require 'logger'
5
+ require 'net/http'
6
+ require 'openssl'
7
+ require 'pathname'
8
+ require 'pp'
9
+ require 'uri'
10
+ require 'yaml'
11
+
12
+
13
+ class Apmate::Logger
14
+ @@logger = nil
15
+
16
+ def self.logger
17
+ @@logger
18
+ end
19
+
20
+ def self.logger=(logger)
21
+ @@logger = logger
22
+ end
23
+
24
+ def self.log_level(opts)
25
+ if opts[:log]
26
+ if opts[:log] == true
27
+ @@logger = ::Logger.new(File.join Dir.pwd, 'apmate-cli.log')
28
+ else
29
+ @@logger = ::Logger.new(File.join Dir.pwd, opts[:log])
30
+ end
31
+ else
32
+ @@logger = ::Logger.new('/dev/null')
33
+ end
34
+ end
35
+
36
+ def self.verbosity(opts)
37
+ if opts[:verbose]
38
+ @@logger.level = ::Logger::DEBUG
39
+ else
40
+ @@logger.level = ::Logger::INFO
41
+ end
42
+ end
43
+ end
44
+
45
+ class Apmate::CLI
46
+ APMATE_BASE_URL = ENV['APMATE_BASE_URL'] || 'http://localhost'
47
+ APMATE_PORT = ENV['APMATE_PORT'] || '3000'
48
+
49
+ def self.apply_file(file, opts)
50
+ Apmate::Logger.logger.info "applying file #{file}"
51
+ YAML.load_stream(file.to_s).each do |yaml_doc|
52
+ yaml_hash = YAML.load_file yaml_doc
53
+ Apmate::Logger.logger.debug "yaml_hash: #{yaml_hash}"
54
+
55
+ apmate_post yaml_hash, opts
56
+ end
57
+ end
58
+
59
+ def self.apply_dir(dir, opts)
60
+ yaml_files = []
61
+ if Pathname.new(dir).relative?
62
+ Dir["#{Dir.pwd}/**/*.yaml"].each {|f| yaml_files << File.expand_path(f) }
63
+ else
64
+ Dir["#{dir}/**/*.yaml"].each {|f| yaml_files << File.expand_path(f) }
65
+ end
66
+
67
+ yaml_files.each do |file|
68
+ apply_file file, opts
69
+ end
70
+ end
71
+
72
+ def self.apmate_get(artifact_name, environment_name, opts)
73
+ Apmate::Logger.logger.debug "Beginning get request."
74
+ uri = URI("#{Apmate::CLI::APMATE_BASE_URL}:#{Apmate::CLI::APMATE_PORT}/api/artifacts")
75
+ Apmate::Logger.logger.debug "uri: #{uri}"
76
+ params = { :name => artifact_name, :environment_name => environment_name }
77
+ uri.query = URI.encode_www_form(params)
78
+ response = Net::HTTP.get_response(uri)
79
+ output = JSON.pretty_generate(JSON.parse(response.body))
80
+ log_output(output, opts)
81
+ end
82
+
83
+ def self.apmate_post(yaml_hash, opts)
84
+ Apmate::Logger.logger.debug "Beginning post request."
85
+
86
+ endpoint_object = yaml_hash['kind'].downcase.pluralize
87
+ Apmate::Logger.logger.debug "endpoint object: #{endpoint_object}"
88
+
89
+ uri = URI("#{Apmate::CLI::APMATE_BASE_URL}:#{Apmate::CLI::APMATE_PORT}/api/#{endpoint_object}/apply")
90
+ Apmate::Logger.logger.debug "uri: #{uri}"
91
+
92
+ payload = {'name': yaml_hash['metadata']['name']}.merge yaml_hash['spec']
93
+ Apmate::Logger.logger.debug "payload: #{payload}"
94
+
95
+ request = Net::HTTP::Post.new(uri)
96
+ request.content_type = 'application/json'
97
+ request.body = payload.to_json
98
+ response = Net::HTTP.start(uri.hostname, uri.port) do |http|
99
+ http.request(request)
100
+ end
101
+ output = JSON.pretty_generate(JSON.parse(response.body))
102
+ log_output(output, opts)
103
+ end
104
+
105
+ def self.log_output(output, opts)
106
+ Apmate::Logger.logger.info output
107
+ if opts[:output]
108
+ open(opts[:output], 'w') do |f|
109
+ f.puts output
110
+ end
111
+ elsif !opts[:quiet]
112
+ puts output
113
+ end
114
+ end
115
+
116
+ def run
117
+ command = Cri::Command.define do
118
+ name 'apmate'
119
+ usage 'apmate <command> [<args>]'
120
+ summary 'Environment datastore CLI'
121
+ description 'Apmate is responsible for managing data in the Apmate environment datastore.'
122
+
123
+ flag :h, :help, 'show help for this command' do |value, cmd|
124
+ puts cmd.help
125
+ exit 0
126
+ end
127
+ end
128
+
129
+ help_command = Cri::Command.define do
130
+ name 'help'
131
+ usage 'help'
132
+ summary 'Prints help screen.'
133
+ description 'This command prints the help screen.'
134
+
135
+ flag :h, :help, 'show help for this command' do |value, cmd|
136
+ puts cmd.help
137
+ exit 0
138
+ end
139
+
140
+ run do |opts, args, cmd|
141
+ command.run ['-h']
142
+ end
143
+ end
144
+
145
+ get_command = Cri::Command.define do
146
+ name 'get'
147
+ usage 'get <artifact_name> <environment_name>'
148
+ summary 'Returns the requested object from the datastore.'
149
+ description 'Returns the requested object from the datastore.'
150
+
151
+ flag :h, :help, 'show help for this command' do |value, cmd|
152
+ puts cmd.help
153
+ exit 0
154
+ end
155
+
156
+ run do |opts, args, cmd|
157
+ Apmate::Logger.log_level(opts)
158
+ Apmate::Logger.verbosity(opts)
159
+ Apmate::Logger.logger.debug "opts: #{opts}"
160
+ Apmate::Logger.logger.debug "args: #{args}"
161
+ Apmate::Logger.logger.debug "cmd: #{cmd.to_s}"
162
+ cmd.run(['-h']) unless args.count == 2
163
+ Apmate::CLI.apmate_get(args[0], args[1], opts)
164
+ end
165
+ end
166
+
167
+ apply_command = Cri::Command.define do
168
+ name 'apply'
169
+ usage 'apply [options] <arg>'
170
+ summary 'Creates or updates objects in the Apmate environment datastore.'
171
+ description 'Creates or updates objects in the Apmate environment datastore.'
172
+
173
+ flag :h, :help, 'show help for this command' do |value, cmd|
174
+ puts cmd.help
175
+ exit 0
176
+ end
177
+ flag :v, :verbose, 'be verbose'
178
+ flag :q, :quiet, 'Do not output Apmate reponse.'
179
+ required :o, :output, 'Name of file to write operation output.'
180
+ optional :l, :log, 'Write to log file if passed. Set a value for a custom location.'
181
+
182
+ run do |opts, args, cmd|
183
+ Apmate::Logger.log_level(opts)
184
+ Apmate::Logger.verbosity(opts)
185
+ Apmate::Logger.logger.debug "opts: #{opts}"
186
+ Apmate::Logger.logger.debug "args: #{args}"
187
+ Apmate::Logger.logger.debug "cmd: #{cmd.to_s}"
188
+ cmd.run(['-h']) unless args[0]
189
+ if args[0]
190
+ if File.directory? args[0]
191
+ Apmate::CLI.apply_dir args[0], opts
192
+ else
193
+ Apmate::CLI.apply_file args[0], opts
194
+ end
195
+ else
196
+ puts cmd.help
197
+ exit 0
198
+ end
199
+ end
200
+ end
201
+ [
202
+ help_command,
203
+ apply_command,
204
+ get_command
205
+ ].each do |cmd|
206
+ command.add_command cmd
207
+ end
208
+
209
+ command.run ARGV
210
+ Apmate::Logger.logger.close if Apmate::Logger.logger
211
+ end
212
+ end
@@ -0,0 +1,3 @@
1
+ module Apmate
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apmate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Baldridge
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - patrick.baldridge@mastercard.com
72
+ executables:
73
+ - apmate
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - exe/apmate
78
+ - lib/apmate.rb
79
+ - lib/apmate/cli.rb
80
+ - lib/apmate/version.rb
81
+ homepage:
82
+ licenses: []
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.11
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A CLI interface for Apmate Environment Datastore.
105
+ test_files: []