catlass 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80b1f102dc7c044821c0bdc06e5e8a799efe8320
4
+ data.tar.gz: cbd593f3437c1bbe3aec015f5ca0b860bebea212
5
+ SHA512:
6
+ metadata.gz: 8dee095dcabf1b46ce35091e8c8d2b382745645afe9d221cf2373a592bc119efa52b8557fbc5596a3823c485e4d35a308caf568c243673f01305feb488ef91d1
7
+ data.tar.gz: 37550928f22d24ab0372a9555b11108c0a734c1b00e4a97b9e616028a4b101125caea6dcf896ff0bc75dfc4e8488895b3f517cc3a6f09abfd667569b55064605
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ CAfile
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.15.2
@@ -0,0 +1,3 @@
1
+ ## 0.1.0
2
+
3
+ - Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at marcy9114@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in catlass.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Masashi Terui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,119 @@
1
+ # Catlass
2
+
3
+ Cloud Automation as Code with [Cloud Automator](https://cloudautomator.com/en/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'catlass'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install catlass
20
+
21
+ ## Usage
22
+
23
+ ### Comands
24
+
25
+ ```shell
26
+ $ catlass
27
+ Commands:
28
+ catlass apply # Apply the job definication
29
+ catlass export # Export the job definication
30
+ catlass help [COMMAND] # Describe available commands or one specific command
31
+
32
+ Options:
33
+ -f, [--file=FILE] # Job definication file
34
+ # Default: CAfile
35
+ [--color], [--no-color] # Disable colorize
36
+ # Default: true
37
+ ```
38
+
39
+ #### apply
40
+ Apply the job definication
41
+
42
+ ```shell
43
+ $ catlass help apply
44
+ Usage:
45
+ catlass apply
46
+
47
+ Options:
48
+ [--dry-run], [--no-dry-run] # Dry run (Only output the difference)
49
+ -f, [--file=FILE] # Job definication file
50
+ # Default: CAfile
51
+ [--color], [--no-color] # Disable colorize
52
+ # Default: true
53
+ ```
54
+
55
+ #### export
56
+ Export the job definication
57
+
58
+ ```
59
+ $ catlass help export
60
+
61
+ Usage:
62
+ catlass export
63
+
64
+ Options:
65
+ [--write], [--no-write] # Write the job definication to the file
66
+ [--split], [--no-split] # Split file by the jobs
67
+ -f, [--file=FILE] # Job definication file
68
+ # Default: CAfile
69
+ [--color], [--no-color] # Disable colorize
70
+ # Default: true
71
+ ```
72
+
73
+ ### Job definication file
74
+
75
+ ```rb
76
+ Job "Auto Stop" do
77
+ type "trigger_jobs"
78
+ attributes do
79
+ aws_account_id 999
80
+ rule_type "cron"
81
+ rule_value do
82
+ hour "19"
83
+ minutes "0"
84
+ time_zone "Tokyo"
85
+ schedule_type "weekly"
86
+ weekly_schedule "", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
87
+ national_holiday_schedule "false"
88
+ end
89
+ action_type "stop_instances"
90
+ action_value do
91
+ region "ap-northeast-1"
92
+ tag_key "auto-stop"
93
+ tag_value "on"
94
+ trace_status "false"
95
+ specify_instance "tag"
96
+ end
97
+ active false
98
+ end
99
+
100
+ end
101
+ ```
102
+
103
+ ## Development
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
106
+
107
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
108
+
109
+ ## Contributing
110
+
111
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcy-terui/catlass. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
116
+
117
+ ## Code of Conduct
118
+
119
+ Everyone interacting in the Catlass project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/marcy-terui/catlass/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "octorelease"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "catlass"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "catlass/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "catlass"
8
+ spec.version = Catlass::VERSION
9
+ spec.authors = ["Masashi Terui"]
10
+ spec.email = ["terui@serverworks.co.jp"]
11
+
12
+ spec.summary = %q{Cloud Automation as Code}
13
+ spec.description = %q{Cloud Automation as Code with Cloud Automator (https://cloudautomator.com/en/)}
14
+ spec.homepage = "https://githb.com/marcy-terui/catlass"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rest-client"
25
+ spec.add_dependency "dslh"
26
+ spec.add_dependency "thor"
27
+ spec.add_dependency "coderay"
28
+ spec.add_dependency "diffy"
29
+ spec.add_dependency "hashie"
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.15"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "octorelease"
35
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'catlass'
6
+
7
+ Catlass::Cli.start(ARGV)
@@ -0,0 +1,14 @@
1
+ require "catlass/version"
2
+
3
+ require 'catlass/logger'
4
+ require 'catlass/utils'
5
+
6
+ require 'catlass/actions'
7
+ require 'catlass/cli'
8
+ require 'catlass/client'
9
+ require 'catlass/converter'
10
+ require 'catlass/dsl_context'
11
+
12
+ module Catlass
13
+ # Your code goes here...
14
+ end
@@ -0,0 +1,96 @@
1
+ require 'yaml'
2
+ require 'json'
3
+
4
+ module Catlass
5
+ class Actions
6
+ include Catlass::Logger::Helper
7
+
8
+ def initialize(client, converter)
9
+ @client = client
10
+ @converter = converter
11
+ end
12
+
13
+ def export(options)
14
+ Catlass::TermColor.color = options['color']
15
+ jobs = @client.get_jobs
16
+
17
+ if options['write']
18
+ if options['split']
19
+ content = ''
20
+ jobs.each do |job|
21
+ name = @converter.filename(job['attributes']['name'])
22
+ _export_dsl_file(@converter.to_dsl(job), "#{name}.rb")
23
+ content << "require #{name.inspect}\n"
24
+ end
25
+ _export_dsl_file(content, options['file'])
26
+ else
27
+ _export_dsl_file(@converter.to_dsl_all(jobs), options['file'])
28
+ end
29
+ else
30
+ Catlass::Utils.print_ruby(@converter.to_dsl_all(jobs), color: options['color'])
31
+ end
32
+ end
33
+
34
+ def apply(options)
35
+ Catlass::TermColor.color = options['color']
36
+ dry_run = options['dry_run'] ? '[Dry run] ' : ''
37
+ local = @converter.dslfile_to_h(options['file'])
38
+ remote = @client.get_jobs
39
+
40
+ _apply_jobs(local, remote, dry_run, options)
41
+ end
42
+
43
+ def _apply_jobs(local, remote, dry_run, options)
44
+ local.each do |l|
45
+ r = _choice_by_name(remote, l)
46
+ job_name = l['attributes']['name']
47
+
48
+ if r.nil?
49
+ info("#{dry_run}Create the new job #{job_name.inspect}")
50
+ @client.create_job(l) if dry_run.empty?
51
+ else
52
+ job_id = r['id']
53
+ diff = Catlass::Utils.diff(@converter, r, l, options['color'])
54
+
55
+ if diff == "\n" or diff.empty?
56
+ info("#{dry_run}No changes '#{job_name}'")
57
+ else
58
+ warn("#{dry_run}Update the job #{job_name.inspect}")
59
+ STDERR.puts diff
60
+ @client.update_job(job_id, l) if dry_run.empty?
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ remote.each do |r|
67
+ if _choice_by_name(local, r).nil?
68
+ job_name = r['attributes']['name']
69
+ warn("#{dry_run}Delete the job #{job_name.inspect}")
70
+ @client.delete_job(r['id']) if dry_run.empty?
71
+ end
72
+ end
73
+ end
74
+
75
+ def _choice_by_name(jobs, target)
76
+ jobs.each do |d|
77
+ return d if d['attributes']['name'] == target['attributes']['name']
78
+ end
79
+ nil
80
+ end
81
+
82
+ def _export_dsl_file(dsl, filename)
83
+ dsl = <<-EOS
84
+ #! /usr/bin/env ruby
85
+
86
+ #{dsl}
87
+ EOS
88
+ _export_file(dsl, filename)
89
+ end
90
+
91
+ def _export_file(dsl, filename)
92
+ File.write(filename, dsl)
93
+ info("Write #{filename.inspect}")
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,30 @@
1
+ require 'thor'
2
+
3
+ module Catlass
4
+ class Cli < Thor
5
+ class_option :file, aliases: '-f', desc: 'Job definication file', type: :string, default: 'CAfile'
6
+ class_option :color, desc: 'Disable colorize', type: :boolean, default: $stdout.tty?
7
+
8
+ def initialize(*args)
9
+ @actions = Catlass::Actions.new(
10
+ Catlass::Client.new,
11
+ Catlass::Converter.new
12
+ )
13
+ super(*args)
14
+ end
15
+
16
+ desc "export", "Export the job definication"
17
+ option :write, desc: 'Write the job definication to the file', type: :boolean, default: false
18
+ option :split, desc: 'Split file by the jobs', type: :boolean, default: false
19
+ def export
20
+ @actions.export(options)
21
+ end
22
+
23
+ desc "apply", "Apply the job definication"
24
+ option :dry_run, desc: 'Dry run (Only output the difference)', type: :boolean, default: false
25
+ def apply
26
+ @actions.apply(options)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ require 'rest-client'
2
+
3
+ module Catlass
4
+ class Client
5
+
6
+ CA_BASE_URI = 'https://manager.cloudautomator.com/api/v1'
7
+
8
+ def initialize
9
+ @headers = {
10
+ content_type: :json,
11
+ accept: :json,
12
+ Authorization: "Bearer #{ENV['CA_API_KEY']}"
13
+ }
14
+ end
15
+
16
+ def set_options(options)
17
+ @options = options
18
+ end
19
+
20
+ def get_jobs(next_uri=nil)
21
+ uri = "#{CA_BASE_URI}/jobs"
22
+ uri = next_uri unless next_uri.nil?
23
+ jobs = JSON.parse(RestClient.get(uri, headers=@headers).body)
24
+ jobs['data'].concat(list_jobs(jobs['links']['next'])) if jobs['links'].has_key?('next')
25
+ jobs['data']
26
+ end
27
+
28
+ def create_job(job)
29
+ uri = "#{CA_BASE_URI}/jobs"
30
+ RestClient.post(uri, job['attributes'].to_json, headers=@headers)
31
+ end
32
+
33
+ def update_job(job_id, job)
34
+ uri = "#{CA_BASE_URI}/jobs/#{job_id}"
35
+ RestClient.patch(uri, job['attributes'].to_json, headers=@headers)
36
+ end
37
+
38
+ def delete_job(job_id)
39
+ uri = "#{CA_BASE_URI}/jobs/#{job_id}"
40
+ RestClient.delete(uri, headers=@headers)
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,72 @@
1
+ require 'dslh'
2
+
3
+ module Catlass
4
+ class Converter
5
+
6
+ def set_options(options)
7
+ @options = options
8
+ end
9
+
10
+ def to_dsl_all(docs)
11
+ dsls = []
12
+ docs.each do |doc|
13
+ dsls << to_dsl(doc)
14
+ end
15
+ dsls.join("\n")
16
+ end
17
+
18
+ def to_dsl(job)
19
+ exclude_key = proc do |k|
20
+ false
21
+ end
22
+
23
+ key_conv = proc do |k|
24
+ k = k.to_s
25
+ if k !~ /\A[_a-z]\w+\Z/i
26
+ "_(#{k.inspect})"
27
+ else
28
+ k
29
+ end
30
+
31
+ end
32
+
33
+ name = job['attributes']['name']
34
+ hash = convert_attributes(job)
35
+
36
+ dsl = Dslh.deval(
37
+ hash,
38
+ exclude_key: exclude_key,
39
+ use_heredoc_for_multi_line: true,
40
+ key_conv: key_conv,
41
+ initial_depth: 1
42
+ )
43
+ <<-EOS
44
+ Job #{name.inspect} do
45
+ #{dsl}
46
+ end
47
+ EOS
48
+ end
49
+
50
+ def dslfile_to_h(dsl_file)
51
+ context = DSLContext.new
52
+ context.eval_dsl(dsl_file)
53
+ end
54
+
55
+ def filename(name)
56
+ name.gsub!(/\W+/, '_')
57
+ end
58
+
59
+ def convert_attributes(hash)
60
+ hash.delete('id')
61
+ hash['attributes'].delete('name')
62
+ hash['attributes'].delete('created_at')
63
+ hash['attributes'].delete('updated_at')
64
+ hash['attributes']['rule_value'].delete('next_occurrence')
65
+ if hash['attributes']['rule_value']['weekly_schedule'].is_a?(String)
66
+ hash['attributes']['rule_value']['weekly_schedule'] = eval(hash['attributes']['rule_value']['weekly_schedule'])
67
+ end
68
+ hash
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,86 @@
1
+ require 'hashie'
2
+
3
+ module Catlass
4
+ class DSLContext
5
+ def initialize
6
+ @jobs = []
7
+ @templates = {}
8
+ @context = Hashie::Mash.new()
9
+ end
10
+
11
+ def eval_dsl(dsl_file)
12
+ @_dsl_file = dsl_file
13
+ instance_eval(File.read(dsl_file), dsl_file)
14
+ @jobs
15
+ end
16
+
17
+ def method_missing(method_name, *args, &block)
18
+ if [:Job].include?(method_name)
19
+ hash = dslh_eval(block)
20
+ hash['attributes']['name'] = args[0]
21
+ @jobs << hash
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def template(name, &block)
28
+ @templates[name.to_s] = block
29
+ end
30
+
31
+ def context
32
+ @context
33
+ end
34
+
35
+ def require(file)
36
+ jobfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@_dsl_file), file))
37
+
38
+ if File.exist?(jobfile)
39
+ instance_eval(File.read(jobfile), jobfile)
40
+ elsif File.exist?(jobfile + '.rb')
41
+ instance_eval(File.read(jobfile + '.rb'), jobfile + '.rb')
42
+ else
43
+ Kernel.require(file)
44
+ end
45
+ end
46
+
47
+ def dslh_eval(block)
48
+ scope_hook = proc do |scope|
49
+ scope.instance_eval(<<-'EOS')
50
+ def include_template(template_name, context = {})
51
+ tmplt = @templates[template_name.to_s]
52
+
53
+ unless tmplt
54
+ raise "Template '#{template_name}' is not defined"
55
+ end
56
+
57
+ context_orig = @context
58
+ @context = @context.merge(context)
59
+ instance_eval(&tmplt)
60
+ @context = context_orig
61
+ end
62
+
63
+ def context
64
+ @context
65
+ end
66
+
67
+ def __dsl(&block)
68
+ @__hash__ = JSON.generate(Dslh::ScopeBlock.nest(binding, 'block'))
69
+ end
70
+
71
+ def __script(str)
72
+ str.split(/\R/)
73
+ end
74
+
75
+ def __script_file(file)
76
+ File.read(file).split(/\R/)
77
+ end
78
+ EOS
79
+ end
80
+
81
+ scope_vars = {templates: @templates, context: @context}
82
+
83
+ Dslh.eval(allow_empty_args: true, scope_hook: scope_hook, scope_vars: scope_vars, &block)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,91 @@
1
+ require 'logger'
2
+ require 'singleton'
3
+
4
+ module Catlass
5
+ class TermColor
6
+ class << self
7
+ @@color = true
8
+
9
+ def color=(color)
10
+ @@color = color
11
+ end
12
+
13
+ def green(msg)
14
+ colorize 32, msg
15
+ end
16
+
17
+ def yellow(msg)
18
+ colorize 33, msg
19
+ end
20
+
21
+ def red(msg)
22
+ colorize 31, msg
23
+ end
24
+
25
+ def colorize(num, msg)
26
+ @@color ? "\e[#{num}m#{msg}\e[0m" : msg
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ class Logger < Logger
33
+ include Singleton
34
+
35
+ def initialize
36
+ super(STDERR)
37
+
38
+ self.formatter = proc do |severity, datetime, progname, msg|
39
+ "#{msg}\n"
40
+ end
41
+
42
+ self.level = Logger::INFO
43
+ end
44
+
45
+ def debug(progname = nil, method_name = nil, msg)
46
+ super(progname) { { method_name: method_name, message: msg } }
47
+ end
48
+
49
+ def info(msg)
50
+ super { Catlass::TermColor.green(msg) }
51
+ end
52
+
53
+ def warn(msg)
54
+ super { Catlass::TermColor.yellow(msg) }
55
+ end
56
+
57
+ def fatal(msg)
58
+ super { Catlass::TermColor.red(msg) }
59
+ end
60
+
61
+ def error(msg)
62
+ super { Catlass::TermColor.red(msg) }
63
+ end
64
+
65
+ module Helper
66
+
67
+ def log(level, message)
68
+ logger = Catlass::Logger.instance
69
+ logger.send(level, message)
70
+ end
71
+
72
+ def info(msg)
73
+ log(:info, msg)
74
+ end
75
+
76
+ def warn(msg)
77
+ log(:warn, msg)
78
+ end
79
+
80
+ def fatal(msg)
81
+ log(:error, msg)
82
+ end
83
+
84
+ def debug(msg)
85
+ log(:debug, msg)
86
+ end
87
+
88
+ module_function :log, :info, :warn, :fatal, :debug
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+ require 'diffy'
3
+ require 'coderay'
4
+
5
+ module Catlass
6
+ class Utils
7
+
8
+ def self.diff(converter, hash1, hash2, color=false)
9
+ diff = Diffy::Diff.new(
10
+ converter.to_dsl(hash1),
11
+ converter.to_dsl(hash2),
12
+ :diff => '-u'
13
+ )
14
+ color ? diff.to_s(:color) : diff.to_s
15
+ end
16
+
17
+ def self.print_ruby(ruby, color=false)
18
+ if color
19
+ puts CodeRay.scan(ruby, :ruby).terminal
20
+ else
21
+ puts ruby
22
+ end
23
+ end
24
+
25
+ def self.print_json(json, color=false)
26
+ if color
27
+ puts CodeRay.scan(json, :json).terminal
28
+ else
29
+ puts json
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Catlass
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catlass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masashi Terui
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dslh
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coderay
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: diffy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: hashie
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.15'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.15'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: octorelease
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Cloud Automation as Code with Cloud Automator (https://cloudautomator.com/en/)
154
+ email:
155
+ - terui@serverworks.co.jp
156
+ executables:
157
+ - catlass
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".rspec"
163
+ - ".travis.yml"
164
+ - CHANGELOG.md
165
+ - CODE_OF_CONDUCT.md
166
+ - Gemfile
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - catlass.gemspec
173
+ - exe/catlass
174
+ - lib/catlass.rb
175
+ - lib/catlass/actions.rb
176
+ - lib/catlass/cli.rb
177
+ - lib/catlass/client.rb
178
+ - lib/catlass/converter.rb
179
+ - lib/catlass/dsl_context.rb
180
+ - lib/catlass/logger.rb
181
+ - lib/catlass/utils.rb
182
+ - lib/catlass/version.rb
183
+ homepage: https://githb.com/marcy-terui/catlass
184
+ licenses:
185
+ - MIT
186
+ metadata: {}
187
+ post_install_message:
188
+ rdoc_options: []
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ requirements: []
202
+ rubyforge_project:
203
+ rubygems_version: 2.5.2
204
+ signing_key:
205
+ specification_version: 4
206
+ summary: Cloud Automation as Code
207
+ test_files: []