ufo 6.2.5 → 6.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69d0e411c4b48b8ec8a0a2148caf5501b3c038cb991585c4741027579a102e0c
4
- data.tar.gz: f080bdf97e783b18bc54723ced3be52557b0749094451561de41766c052aba0e
3
+ metadata.gz: 91051471ac72ee1a31948333cc0009d51f1d6ca1e1e170abe54d492a94a046f5
4
+ data.tar.gz: 4aa76fab580eb121d8e257fa7461d3be171334d2be1037bd0365988065e03f8a
5
5
  SHA512:
6
- metadata.gz: 860f1d782b915338e903d2c2c0cad43fbe4a915261b46a2b615a7d24870247170a3041de32b6e9de8b11161da1c8447dc3d12683e9179c8c6cd0af7ef3dc4d77
7
- data.tar.gz: c02bc1f698f85055e2e1c99b7fbf6cf730044e23b7584509a5c2e66bd79259097b2c5ccf1706b186a64b329db31e5678fe6eacb3f120724dbfa896a0ab6fbb00
6
+ metadata.gz: 90f134dd07a15a153b7e8f4fe82d4c12dccbaea80e2041e5ff23431bacd28a78a5077d1ba5e4a8508926bfb91510ff0113e185658d444981730dc32447f552f1
7
+ data.tar.gz: af3e344e51334b7c3dd6a82fb23403da39717fb77bb15883632146ca79a3b4edc57a61dedd260d42b52d6652c4a129389acbac1009444c9da140fefb1d77ace2
data/CHANGELOG.md CHANGED
@@ -3,6 +3,9 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [6.3.0] - 2022-03-25
7
+ - [#162](https://github.com/tongueroo/ufo/pull/162) hooks support
8
+
6
9
  ## [6.2.5] - 2022-03-24
7
10
  - [#159](https://github.com/tongueroo/ufo/pull/159) improve ufo call line
8
11
  - [#160](https://github.com/tongueroo/ufo/pull/160) conventionally lookup up secrets and env file
@@ -0,0 +1,9 @@
1
+ # Docs: https://ufoships.com/docs/config/hooks/docker/
2
+
3
+ before("build",
4
+ execute: "echo 'docker build before hook'",
5
+ )
6
+
7
+ after("build",
8
+ execute: "echo 'docker build after hook'",
9
+ )
@@ -0,0 +1,9 @@
1
+ # Docs: https://ufoships.com/docs/config/hooks/ufo/
2
+
3
+ before("ship",
4
+ execute: "echo 'ufo before ship hook'",
5
+ )
6
+
7
+ after("ship",
8
+ execute: "echo 'ufo after ship hook'",
9
+ )
@@ -4,7 +4,7 @@ module Ufo
4
4
  class Autoloader
5
5
  class Inflector < Zeitwerk::Inflector
6
6
  def camelize(basename, _abspath)
7
- map = { cli: "CLI", dsl: "DSL", version: "VERSION" }
7
+ map = { cli: "CLI", version: "VERSION" }
8
8
  map[basename.to_sym] || super
9
9
  end
10
10
  end
@@ -1,7 +1,7 @@
1
1
  module Ufo::Cfn::Stack::Builder::Resources::IamRoles
2
2
  class Base < Ufo::Cfn::Stack::Builder::Base
3
3
  def build
4
- return unless self.class.build? # important because it runs DSL#evaluate
4
+ return unless self.class.build? # important because it runs Dsl#evaluate
5
5
  Ufo::IamRole::Builder.new(self.class.role_type).build
6
6
  end
7
7
 
@@ -13,7 +13,7 @@ module Ufo::Cfn::Stack::Builder::Resources::IamRoles
13
13
  def build?
14
14
  path = lookup_path
15
15
  return unless path.nil? || File.exist?(path)
16
- Ufo::IamRole::DSL.new(path).evaluate # runs the role.rb and registers items
16
+ Ufo::IamRole::Dsl.new(path).evaluate # runs the role.rb and registers items
17
17
  Ufo::IamRole::Builder.new(role_type).build?
18
18
  end
19
19
 
data/lib/ufo/cfn/stack.rb CHANGED
@@ -25,6 +25,7 @@ module Ufo::Cfn
25
25
  class Stack < Base
26
26
  extend Memoist
27
27
  include Ufo::TaskDefinition::Helpers::AwsHelper
28
+ include Ufo::Hooks::Concern
28
29
 
29
30
  def deploy
30
31
  build
@@ -39,15 +40,14 @@ module Ufo::Cfn
39
40
 
40
41
  exit_with_message(@stack) if @stack && !updatable?(@stack)
41
42
 
42
- @stack ? perform(:update) : perform(:create)
43
-
44
- stop_old_tasks if @options[:stop_old_task]
45
-
46
- return unless @options[:wait]
47
- status.wait
43
+ run_hooks(name: "ship", file: "ufo.rb") do
44
+ @stack ? perform(:update) : perform(:create)
45
+ stop_old_tasks if @options[:stop_old_task]
46
+ return unless @options[:wait]
47
+ status.wait
48
+ end
48
49
 
49
50
  logger.info status.rollback_error_message if status.update_rollback?
50
-
51
51
  status.success?
52
52
  end
53
53
 
@@ -78,13 +78,17 @@ module Ufo::Cfn
78
78
  end
79
79
  end
80
80
 
81
+ # Run hooks here so both ufo docker and ufo ship runs it
82
+ # ufo docker => CLI::Build#build => Cfn::Stack#build
81
83
  def build
82
- vars = Vars.new(@options).values
83
- options_with_vars = @options.dup.merge(vars: vars)
84
- params = Params.new(options_with_vars)
85
- @parameters = params.build
86
- template = Template.new(options_with_vars)
87
- @template_body = template.body
84
+ run_hooks(name: "build", file: "ufo.rb") do
85
+ vars = Vars.new(@options).values
86
+ options_with_vars = @options.dup.merge(vars: vars)
87
+ params = Params.new(options_with_vars)
88
+ @parameters = params.build
89
+ template = Template.new(options_with_vars)
90
+ @template_body = template.body
91
+ end
88
92
  end
89
93
 
90
94
  def scheduling_strategy
data/lib/ufo/cli/build.rb CHANGED
@@ -7,11 +7,6 @@ class Ufo::CLI
7
7
  end
8
8
  alias_method :all, :build
9
9
 
10
- def for_deploy
11
- docker
12
- task_definition
13
- end
14
-
15
10
  def task_definition
16
11
  Ufo::TaskDefinition::Builder.new(@options).build
17
12
  end
@@ -1,27 +1,30 @@
1
1
  class Ufo::CLI
2
2
  class Destroy < Base
3
+ include Ufo::Hooks::Concern
4
+
3
5
  def run
4
6
  are_you_sure?
5
7
 
6
8
  stack = find_stack(@stack_name)
7
9
  unless stack
8
- puts "Stack #{@stack_name.color(:green)} does not exist."
10
+ logger.info "Stack #{@stack_name.color(:green)} does not exist."
9
11
  exit 1
10
12
  end
11
13
 
12
14
  if stack.stack_status =~ /_IN_PROGRESS$/
13
- puts "Cannot destroy service #{@service.color(:green)}"
14
- puts "Cannot delete stack #{@stack_name.color(:green)} in this state: #{stack.stack_status.color(:green)}"
15
- puts "If the stack is taking a long time, you can cancel the current operation with:"
16
- puts " ufo cancel #{@service}"
15
+ logger.info "Cannot destroy service #{@service.color(:green)}"
16
+ logger.info "Cannot delete stack #{@stack_name.color(:green)} in this state: #{stack.stack_status.color(:green)}"
17
+ logger.info "If the stack is taking a long time, you can cancel the current operation with:"
18
+ logger.info " ufo cancel #{@service}"
17
19
  return
18
20
  end
19
21
 
20
- cfn.delete_stack(stack_name: @stack_name)
21
- puts "Deleting stack #{@stack_name.color(:green)}"
22
-
23
- return unless @options[:wait]
24
- status.wait
22
+ run_hooks(name: "destroy", file: "ufo.rb") do
23
+ cfn.delete_stack(stack_name: @stack_name)
24
+ logger.info "Deleting stack #{@stack_name.color(:green)}"
25
+ return unless @options[:wait]
26
+ status.wait
27
+ end
25
28
  end
26
29
 
27
30
  def are_you_sure?
@@ -0,0 +1,7 @@
1
+ ## Examples
2
+
3
+ $ ufo new hook docker
4
+ create .ufo/config/hooks/docker.rb
5
+ $ ufo new hook ufo
6
+ create .ufo/config/hooks/ufo.rb
7
+ $
@@ -0,0 +1,18 @@
1
+ class Ufo::CLI::New
2
+ class Hook < Sequence
3
+ argument :type, default: "ufo", description: "IE: docker, ufo" # description doesnt really show up
4
+
5
+ def self.cli_options
6
+ [
7
+ [:force, aliases: ["y"], type: :boolean, desc: "Bypass overwrite are you sure prompt for existing files"],
8
+ ]
9
+ end
10
+ cli_options.each { |args| class_option(*args) }
11
+
12
+ public
13
+ def create_hook
14
+ set_template_source("hooks")
15
+ template "#{type}.rb", ".ufo/config/hooks/#{type}.rb"
16
+ end
17
+ end
18
+ end
data/lib/ufo/cli/new.rb CHANGED
@@ -1,17 +1,24 @@
1
1
  class Ufo::CLI
2
2
  class New < Ufo::Command
3
- desc "boot_hook", "Generate boot_hook file"
3
+ desc "boot_hook", "Generate boot_hook"
4
4
  long_desc Help.text("new/boot_hook")
5
5
  BootHook.cli_options.each do |args|
6
6
  option(*args)
7
7
  end
8
- register(BootHook, "boot_hook", "boot_hook", "Generate boot_hook file")
8
+ register(BootHook, "boot_hook", "boot_hook", "Generate boot_hook")
9
9
 
10
- desc "helper", "Generate helper file"
10
+ desc "helper", "Generate helper"
11
11
  long_desc Help.text("new/helper")
12
12
  Helper.cli_options.each do |args|
13
13
  option(*args)
14
14
  end
15
- register(Helper, "helper", "helper", "Generate helper file")
15
+ register(Helper, "helper", "helper", "Generate helper")
16
+
17
+ desc "hook", "Generate hook"
18
+ long_desc Help.text("new/hook")
19
+ Hook.cli_options.each do |args|
20
+ option(*args)
21
+ end
22
+ register(Hook, "hook", "hook", "Generate hook")
16
23
  end
17
24
  end
data/lib/ufo/config.rb CHANGED
@@ -85,6 +85,9 @@ module Ufo
85
85
  config.exec.command = "/bin/bash" # aws ecs execute-command cli
86
86
  config.exec.enabled = true # EcsService EnableExecuteCommand
87
87
 
88
+ config.hooks = ActiveSupport::OrderedOptions.new
89
+ config.hooks.show = true
90
+
88
91
  config.layering = ActiveSupport::OrderedOptions.new
89
92
  config.layering.show = parsed_layering_show
90
93
  config.layering.show_for_commands = parsed_layering_show_for
@@ -2,6 +2,7 @@ module Ufo::Docker
2
2
  class Builder
3
3
  extend Memoist
4
4
  include Concerns
5
+ include Ufo::Hooks::Concern
5
6
 
6
7
  delegate :push, to: :pusher
7
8
  def self.build(options={})
@@ -28,7 +29,10 @@ module Ufo::Docker
28
29
  update_auth_token
29
30
  command = "docker build #{build_options}-t #{docker_image} -f #{@dockerfile} ."
30
31
  log = ".ufo/log/docker.log" if @options[:quiet]
31
- success = execute(command, log: log)
32
+ success = nil
33
+ run_hooks(name: "build", file: "docker.rb") do
34
+ success = execute(command, log: log)
35
+ end
32
36
  unless success
33
37
  docker_version_success = system("docker version > /dev/null 2>&1")
34
38
  unless docker_version_success
@@ -1,6 +1,7 @@
1
1
  module Ufo::Docker
2
2
  class Pusher
3
3
  include Concerns
4
+ include Ufo::Hooks::Concern
4
5
 
5
6
  delegate :docker_image, to: :builder
6
7
  attr_reader :last_image_name
@@ -17,7 +18,10 @@ module Ufo::Docker
17
18
  logger.info "Pushing Docker Image"
18
19
  command = "docker push #{last_image_name}"
19
20
  log = ".ufo/log/docker.log" if @options[:quiet]
20
- success = execute(command, log: log)
21
+ success = nil
22
+ run_hooks(name: "push", file: "docker.rb") do
23
+ success = execute(command, log: log)
24
+ end
21
25
  unless success
22
26
  logger.info "ERROR: The docker image fail to push.".color(:red)
23
27
  exit 1
@@ -0,0 +1,51 @@
1
+ module Ufo::Hooks
2
+ class Builder
3
+ extend Memoist
4
+ include Dsl
5
+ include DslEvaluator
6
+ include Ufo::Utils::Logging
7
+
8
+ attr_accessor :name
9
+ def initialize(options={})
10
+ @options = options
11
+ @file = options[:file] # IE: docker.rb
12
+ @dsl_file = "#{Ufo.root}/.ufo/config/hooks/#{@file}"
13
+ @name = options[:name].to_s
14
+ @hooks = {before: {}, after: {}}
15
+ end
16
+
17
+ def build
18
+ evaluate_file(@dsl_file)
19
+ @hooks.deep_stringify_keys!
20
+ end
21
+ memoize :build
22
+
23
+ def run_hooks
24
+ build
25
+ run_each_hook("before")
26
+ out = yield if block_given?
27
+ run_each_hook("after")
28
+ out
29
+ end
30
+
31
+ def run_each_hook(type)
32
+ hooks = @hooks.dig(type, @name) || []
33
+ hooks.each do |hook|
34
+ run_hook(type, hook)
35
+ end
36
+ end
37
+
38
+ def run_hook(type, hook)
39
+ return unless run?(hook)
40
+
41
+ id = "#{type} #{@name}"
42
+ label = " label: #{hook["label"]}" if hook["label"]
43
+ logger.info "Hook: Running #{id} hook#{label}".color(:cyan) if Ufo.config.hooks.show
44
+ Runner.new(hook).run
45
+ end
46
+
47
+ def run?(hook)
48
+ !!hook["execute"]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,10 @@
1
+ module Ufo::Hooks
2
+ module Concern
3
+ # options example: {name: "build", file: "docker.rb"}
4
+ def run_hooks(options={}, &block)
5
+ hooks = Ufo::Hooks::Builder.new(options)
6
+ hooks.build # build hooks
7
+ hooks.run_hooks(&block)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Ufo::Hooks
2
+ module Dsl
3
+ def before(*commands, **props)
4
+ commands.each do |name|
5
+ each_hook(:before, name, props)
6
+ end
7
+ end
8
+
9
+ def after(*commands, **props)
10
+ commands.each do |name|
11
+ each_hook(:after, name, props)
12
+ end
13
+ end
14
+
15
+ def each_hook(type, name, props={})
16
+ @hooks[type][name] ||= []
17
+ @hooks[type][name] << props
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ module Ufo::Hooks
2
+ class Runner
3
+ include Ufo::Utils::Logging
4
+ include Ufo::Utils::Execute
5
+
6
+ attr_reader :hook
7
+ def initialize(hook)
8
+ @hook = hook
9
+ @execute = @hook["execute"]
10
+ end
11
+
12
+ def run
13
+ case @execute
14
+ when String
15
+ execute(@execute, exit_on_fail: @hook["exit_on_fail"])
16
+ when -> (e) { e.respond_to?(:public_instance_methods) && e.public_instance_methods.include?(:call) }
17
+ executor = @execute.new
18
+ when -> (e) { e.respond_to?(:call) }
19
+ executor = @execute
20
+ else
21
+ logger.warn "WARN: execute option not set for hook: #{@hook.inspect}"
22
+ end
23
+
24
+ return unless executor
25
+
26
+ meth = executor.method(:call)
27
+ case meth.arity
28
+ when 0
29
+ executor.call # backwards compatibility
30
+ when 1
31
+ executor.call(self)
32
+ else
33
+ raise "The #{executor} call method definition has been more than 1 arguments and is not supported"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  module Ufo::IamRole
2
- class DSL
2
+ class Dsl
3
3
  include DslEvaluator
4
4
  include Ufo::TaskDefinition::Helpers::AwsHelper
5
5
 
@@ -11,13 +11,13 @@ module Ufo::IamRole
11
11
  def register_policy(role_type, policy_name, *statements)
12
12
  statements.flatten!
13
13
  self.policies[role_type] ||= Set.new
14
- self.policies[role_type].add([policy_name, statements]) # using set so DSL can safely be evaluated multiple times
14
+ self.policies[role_type].add([policy_name, statements]) # using set so Dsl can safely be evaluated multiple times
15
15
  end
16
16
 
17
17
  def register_managed_policy(role_type, *policies)
18
18
  policies.flatten!
19
19
  self.managed_policies[role_type] ||= Set.new
20
- self.managed_policies[role_type].merge(policies) # using set so DSL can safely be evaluated multiple times
20
+ self.managed_policies[role_type].merge(policies) # using set so Dsl can safely be evaluated multiple times
21
21
  end
22
22
  end
23
23
  end
data/lib/ufo/param.rb CHANGED
@@ -18,8 +18,7 @@ module Ufo
18
18
  memoize :data
19
19
 
20
20
  def template_scope
21
- self # TODO: add access to helpers like network
22
- # @template_scope ||= Ufo::TemplateScope.new(Ufo::DSL::Helper.new, nil)
21
+ self
23
22
  end
24
23
  end
25
24
  end
data/lib/ufo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ufo
2
- VERSION = "6.2.5"
2
+ VERSION = "6.3.0"
3
3
  end
@@ -12,7 +12,7 @@ describe Ufo::IamRole::Builder do
12
12
  {:Action=>["cloudwatch:PutMetricData"], :Effect=>"Allow", :Resource=>"*"}
13
13
  )
14
14
  # Called twice on purpose to show that duplicated items in the set wont create doubles.
15
- # This allows the DSL evaluate to be ran multiple times.
15
+ # This allows the Dsl evaluate to be ran multiple times.
16
16
  Ufo::IamRole::Registry.register_policy("task_role",
17
17
  "CloudwatchWrite",
18
18
  {:Action=>["cloudwatch:PutMetricData"], :Effect=>"Allow", :Resource=>"*"}
@@ -1,9 +1,9 @@
1
- describe Ufo::IamRole::DSL do
1
+ describe Ufo::IamRole::Dsl do
2
2
  let(:dsl) { described_class.new(path) }
3
3
  let(:path) { "spec/fixtures/iam_roles/task_role.rb" }
4
4
 
5
5
  context "evaluate" do
6
- it "registers policies from role DSL" do
6
+ it "registers policies from role Dsl" do
7
7
  dsl.evaluate
8
8
  expect(Ufo::IamRole::Registry.policies).not_to be_empty
9
9
  expect(Ufo::IamRole::Registry.managed_policies).not_to be_empty
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ufo
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.2.5
4
+ version: 6.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-24 00:00:00.000000000 Z
11
+ date: 2022-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-logs
@@ -480,6 +480,8 @@ files:
480
480
  - lib/templates/boot_hook/.ufo/config/boot.rb
481
481
  - lib/templates/docker/Dockerfile
482
482
  - lib/templates/helper/%underscore_name%_helper.rb.tt
483
+ - lib/templates/hooks/docker.rb
484
+ - lib/templates/hooks/ufo.rb
483
485
  - lib/templates/init/.ufo/config.rb.tt
484
486
  - lib/templates/init/.ufo/config/web/base.rb
485
487
  - lib/templates/init/.ufo/config/web/dev.rb
@@ -559,6 +561,7 @@ files:
559
561
  - lib/ufo/cli/help/init.md
560
562
  - lib/ufo/cli/help/logs.md
561
563
  - lib/ufo/cli/help/new/boot_hook.md
564
+ - lib/ufo/cli/help/new/hook.md
562
565
  - lib/ufo/cli/help/ps.md
563
566
  - lib/ufo/cli/help/releases.md
564
567
  - lib/ufo/cli/help/rollback.md
@@ -570,6 +573,7 @@ files:
570
573
  - lib/ufo/cli/new/boot_hook.rb
571
574
  - lib/ufo/cli/new/concerns.rb
572
575
  - lib/ufo/cli/new/helper.rb
576
+ - lib/ufo/cli/new/hook.rb
573
577
  - lib/ufo/cli/new/init.rb
574
578
  - lib/ufo/cli/new/sequence.rb
575
579
  - lib/ufo/cli/opts.rb
@@ -614,6 +618,10 @@ files:
614
618
  - lib/ufo/ext.rb
615
619
  - lib/ufo/ext/core/module.rb
616
620
  - lib/ufo/ext/core/nil_class.rb
621
+ - lib/ufo/hooks/builder.rb
622
+ - lib/ufo/hooks/concern.rb
623
+ - lib/ufo/hooks/dsl.rb
624
+ - lib/ufo/hooks/runner.rb
617
625
  - lib/ufo/iam_role/builder.rb
618
626
  - lib/ufo/iam_role/dsl.rb
619
627
  - lib/ufo/iam_role/registry.rb