Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
associated outputs concept
jets concurrency show lambda function name
jets git:push command
pretty time helper
update github issue template bug report
  • Loading branch information
tongueroo committed May 27, 2024
1 parent 254cc09 commit d1c7bbd
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Thanks!
Make sure that you've done all of these. To mark a checkbox done, replace [ ] with [x]. Or after you create the issue you can click the checkbox.
-->

- [ ] Upgrade Jets: Are you using the latest version of Jets? This allows Jets to fix issues fast. There's a `jets upgrade` command that makes this a simple task. There's also an Upgrading Guide: http://rubyonjets.com/docs/upgrading/
- [ ] Upgrade Jets: Are you using the latest version of Jets? This allows Jets to fix issues fast.
- [ ] Reproducibility: Are you reporting a bug others will be able to reproduce and not asking a question. If you're unsure or want to ask a question, do so on https://community.boltops.com
- [ ] Code sample: Have you put together a code sample to reproduce the issue and make it available? Code samples help speed up fixes dramatically. If it's an easily reproducible issue, then code samples are not needed. If you're unsure, please include a code sample.

Expand Down
8 changes: 2 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ gemspec

# required here for specs
group :development, :test do
gem "mysql2", "~> 0.5.2"
gem "dynomite", "~> 2.0.0"
end

group :test do
gem "actionpack", "~> 7.1.3" # jets shim specs
gem "dynomite", ">= 2.0.0"
gem "mysql2", ">= 0.5.2"
end
6 changes: 0 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ RSpec::Core::RakeTask.new

require_relative "lib/jets"

desc "Generates cli reference docs as markdown"
task :docs do
require "cli_markdown_jets"
CliMarkdown::Creator.new.create_all
end

# Thanks: https://docs.ruby-lang.org/en/2.1.0/RDoc/Task.html
require "rdoc/task"
require "jets/rdoc"
Expand Down
21 changes: 21 additions & 0 deletions lib/jets/cfn/resource/associated_outputs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Jets::Cfn::Resource
class AssociatedOutputs
extend Memoist

def initialize(outputs = {}, replacements = {})
@outputs = outputs
@replacements = replacements
end

def replacer
Replacer.new(@replacements)
end
memoize :replacer

def outputs
outputs = replacer.replace_placeholders(@outputs)
outputs.transform_values! { |value| value.camelize }
outputs.transform_keys! { |key| replacer.replace_value(key) }
end
end
end
3 changes: 3 additions & 0 deletions lib/jets/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class CLI < Jets::Thor::Base
desc "generate SUBCOMMAND", "generate subcommands"
subcommand "generate", Generate

desc "git SUBCOMMAND", "git subcommands"
subcommand "git", Git

desc "maintenance SUBCOMMAND", "maintenance subcommands"
subcommand "maintenance", Maintenance

Expand Down
3 changes: 3 additions & 0 deletions lib/jets/cli/ci/status.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class Jets::CLI::Ci
class Status < Base
include Jets::Util::FormatTime

def run
check_build_id!
run_with_exception_handling do
puts "Build id: #{build_id}"
resp = codebuild.batch_get_builds(ids: [build_id])
build = resp.builds.first
puts "Build end time: #{pretty_time(build.end_time)}"
puts "Build status: #{colored(build.build_status)}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jets/cli/concurrency/get.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(options = {})
def run
puts <<~EOL
Settings for Function: #{@lambda_function.name}
Reserved concurreny: #{reserved_concurrency}
Reserved concurrency: #{reserved_concurrency}
Provisioned concurrency: #{provisioned_concurrency}
EOL
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jets/cli/concurrency/set.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Jets::CLI::Concurrency
class Set < Get
def run
sure? "Will update the concurrency settings for #{Jets.project.namespace}"
puts "Updating concurrency settings for #{Jets.project.namespace}"
sure? "Will update the concurrency settings for #{@lambda_function.name}"
puts "Updating concurrency settings for #{@lambda_function.name}"

if @options[:reserved]
@lambda_function.reserved_concurrency = @options[:reserved]
Expand Down
4 changes: 2 additions & 2 deletions lib/jets/cli/concurrency/unset.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Jets::CLI::Concurrency
class Unset < Set
def run
sure? "Will unset the concurrency settings for #{Jets.project.namespace}"
puts "Unsetting concurrency settings for #{Jets.project.namespace}"
sure? "Will unset the concurrency settings for #{@lambda_function.name}"
puts "Unsetting concurrency settings for #{@lambda_function.name}"

if @options[:reserved]
@lambda_function.reserved_concurrency = nil
Expand Down
8 changes: 8 additions & 0 deletions lib/jets/cli/git.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Jets::CLI
class Git < Jets::Thor::Base
desc "push", "Runs git push and jets ci:logs"
def push(*args)
Push.new(options.merge(args: args)).run
end
end
end
65 changes: 65 additions & 0 deletions lib/jets/cli/git/push.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "open3"

class Jets::CLI::Git
class Push < Jets::CLI::Base
def initialize(options = {})
super
@args = options[:args] || []
end

def run
args = ["push"] + @args
puts "=> git #{args.join(" ")}"

IO.popen(["git", *args]) do |io|
io.each do |line|
puts line
end
end

return unless $?.success?

set_env_vars!
sleep 2 # wait ci to start
Jets::CLI::Ci::Logs.new(options).run
end

def set_env_vars!
env_vars = Jets.project.config.git.push.branch[push_branch] || {}
# IE: branch_name = {JETS_ENV: "xxx", AWS_PROFILE: "xxx"}
env_vars.each do |k, v|
ENV[k.to_s] = v
end
end

# man git-push
# git push
# git push origin
# git push origin :
# git push origin master
# git push origin HEAD
# git push mothership master:satellite/master dev:satellite/dev
# git push origin HEAD:master
# git push origin master:refs/heads/experimental
# git push origin :experimental
# git push origin +dev:master
def push_branch
args = @args.reject { |arg| arg.start_with?("-") } # remove options
case args.size
when 0
local.git_default_branch
when 1
local.git_current_branch
when 2
args.last
else
raise "ERROR: Too many arguments. Usage: jets git:push [REMOTE] [BRANCH]"
end
end

def local
Jets::Git::Local.new
end
memoize :local
end
end
14 changes: 14 additions & 0 deletions lib/jets/cli/help/concurrency/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Example

❯ jets concurrency:info
Concurrency for demo-dev
+---------------------------+----------+
| Function | Reserved |
+---------------------------+----------+
| controller | 25 |
| jets-prewarm_event-handle | 2 |
| total | 27 |
+---------------------------+----------+
Account Limits
Concurrent Executions: 1000
Unreserved Concurrent Executions: 730
27 changes: 3 additions & 24 deletions lib/jets/cli/release/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class Jets::CLI::Release
class History < Base
include Jets::Util::FormatTime
rescue_api_error

def run
Expand All @@ -29,11 +30,11 @@ def show_items(items)
items.each do |item|
version = item[:version]
status = item[:stack_status]
released_at = item[:pretty_created_at] || item[:created_at]
released_at = item[:created_at]
message = item[:message] || "Deployed"
message = message[0..50]

row = [version, status, format_time(released_at), message]
row = [version, status, pretty_time(released_at), message]
if @options[:sha]
sha = item[:git_sha].to_s[0..7] if item[:git_sha]
row << sha
Expand All @@ -42,27 +43,5 @@ def show_items(items)
end
presenter.show
end

def format_time(string)
if string.include?("ago") # IE: 5 minutes ago
string
else
utc = DateTime.parse(string)

tz_override = ENV["JETS_TZ"] # IE: America/Los_Angeles
local = if tz_override
tz = TZInfo::Timezone.get(tz_override)
tz.utc_to_local(utc)
else
utc.new_offset(DateTime.now.offset) # local time
end

if tz_override
local.strftime("%b %-d, %Y %-l:%M:%S%P")
else
local.strftime("%b %-d, %Y %H:%M:%S")
end
end
end
end
end
3 changes: 1 addition & 2 deletions lib/jets/cli/release/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def run
data = release_fields.map do |field|
# special cases for values
value = if field == :created_at
time_string = release[:pretty_created_at] || release[:created_at]
format_time(time_string)
pretty_time(release[:created_at])
else
release[field]
end
Expand Down
5 changes: 5 additions & 0 deletions lib/jets/core/config/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Project < Base
:ignore_paths,
:base64_encode,
:dotenv,
:git,
:tips
)
def initialize(*)
Expand Down Expand Up @@ -40,6 +41,10 @@ def initialize(*)

@base64_encode = true

@git = ActiveSupport::OrderedOptions.new
@git.push = ActiveSupport::OrderedOptions.new
@git.push.branch = ActiveSupport::OrderedOptions.new

@tips = ActiveSupport::OrderedOptions.new
@tips.enable = true
@tips.concurrency_change = true
Expand Down
4 changes: 4 additions & 0 deletions lib/jets/git/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ def git_default_branch
end
default
end

def git_current_branch
`rev-parse --abbrev-ref HEAD`.strip
end
end
end
3 changes: 2 additions & 1 deletion lib/jets/lambda/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Definition
attr_accessor :class_name, :type
attr_reader(
:meth, :properties, :provisioned_concurrency, :iam_policy, :managed_iam_policy,
:lang, :associated_resources
:lang, :associated_resources, :associated_outputs
)
def initialize(class_name, meth, options = {})
@class_name = class_name.to_s
Expand All @@ -18,6 +18,7 @@ def initialize(class_name, meth, options = {})
@managed_iam_policy = options[:managed_iam_policy]
@lang = options[:lang] || :ruby
@associated_resources = options[:associated_resources] || {}
@associated_outputs = options[:associated_outputs] || {}
@replacements = options[:replacements] || {} # added to baseline replacements
end

Expand Down
6 changes: 6 additions & 0 deletions lib/jets/lambda/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def associated_resources(*definitions)
# User-friendly short resource method. Users will use this.
alias_method :resource, :associated_resources

def associated_outputs(outputs = {})
@associated_outputs ||= []
@associated_outputs << outputs
end

# Using this odd way of setting these properties so we can keep the
# resource(*definitions) signature simple. Using keyword arguments at the end
# interfere with being able to pass in any keys for the properties hash at the end.
Expand Down Expand Up @@ -282,6 +287,7 @@ def register_definition(meth, lang = :ruby)
iam_policy: @iam_policy,
managed_iam_policy: @managed_iam_policy,
associated_resources: @associated_resources,
associated_outputs: @associated_outputs,
lang: lang,
replacements: replacements(meth))

Expand Down
52 changes: 52 additions & 0 deletions lib/jets/util/format_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Jets::Util
module FormatTime
def pretty_time(time)
datetime = case time
when Time
time.to_datetime
when String
DateTime.parse(time)
else
time
end

if datetime > 1.day.ago.utc
time_ago_in_words(datetime) + " ago"
else
tz_override = ENV["JETS_TZ"] # IE: America/Los_Angeles
local = if tz_override
tz = TZInfo::Timezone.get(tz_override)
tz.time_to_local(datetime)
else
datetime.new_offset(DateTime.now.offset) # local time
end

if tz_override
local.strftime("%b %-d, %Y %-l:%M:%S%P")
else
local.strftime("%b %-d, %Y %H:%M:%S")
end
end
end

# Simple implementation of time_ago_in_words so we dont have to include ActionView::Helpers::DateHelper
def time_ago_in_words(from_time, to_time = Time.now)
distance_in_seconds = (to_time - from_time).to_i
case distance_in_seconds
when 0..59
"#{distance_in_seconds} #{"second".pluralize(distance_in_seconds)}"
when 60..3599
minutes = distance_in_seconds / 60
"#{minutes} #{"minute".pluralize(minutes)}"
when 3600..86_399
hours = distance_in_seconds / 3600
"#{hours} #{"hour".pluralize(hours)}"
when 86_400..604_799
days = distance_in_seconds / 86_400
"#{days} #{"day".pluralize(days)}"
else
from_time.strftime("%B %d, %Y")
end
end
end
end

0 comments on commit d1c7bbd

Please sign in to comment.