Tagged: ruby Toggle Comment Threads | Keyboard Shortcuts

  • ThomasPowell 5:49 pm on May 6, 2022 Permalink | Reply
    Tags: , ruby, warnings   

    Quiet “Don’t run bundler as root” warnings, Bundler >= 2.3.7 

    Why you wouldn’t want to run bundler as root

    As a macOS user, you might want to be warned against running bundler as root (specifically against the system ruby), because you can cause subsequent problems for installing future gems.

    The closing of issue #2936 in the rubygems/bundler project added an error message:

    Don’t run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.

    But what if this is deployment to a Linux machine and installing to the system ruby is your intent?

    If you want to keep your logs clean of the warning message (especially if you have warnings highlighted), you can use one of the following options:

    The following sets in ~/.bundle/config (/root/.bundle/config if root/sudo)

    bundle config silence_root_warning true

    The following can also be used if you want to temporarily disable the warning (a la “I know what I’m doing”)

    export BUNDLE_SILENCE_ROOT_WARNING=true

    As seen below using the bundle config option will fix sudo bundle install to not show the warning.

    bundler as root warning

     
  • ThomasPowell 6:28 pm on May 5, 2022 Permalink | Reply
    Tags: , ruby,   

    I forgot how much setup was required to go from a base Ubuntu install to bundling and running ruby things:

    “mkmf.rb can’t find header files for ruby” and other header files messages with bundler… new to also install ruby-dev with your ruby package install in Ubuntu

    On Ubuntu… need the following:
    apt-get install -y ruby ruby-dev make gcc g++

     
  • ThomasPowell 7:38 am on April 25, 2022 Permalink | Reply
    Tags: ruby, support, time zones   

    Time zone conversion cheat sheet at the shell with Ruby 

    Why would you need a command line time zone conversion?

    Imagine that you’re troubleshooting a problem. Add that your team is distributed across multiple timezones. Now add that database times are generally recorded in UTC, but your alerting all displays in local time.

    If you’re trying to pinpoint when an event happened and comparing notes across systems and observations, you’re going to be doing some time zone conversion to validate that all the observations are related to the same time period. That can be a bit tedious, especially if you’re already juggling a lot of details about the problem. For me, I wrote a quick script to be a time converter calculator to as a guide to help me align time stamps from different sources.

    Parsing the time

    Once require 'time' is added to the script, Time.parse(time_string) will work. But I also wanted to support if you had a date or time offset, such as 2022-04-20 11:09 -0500 without having to put the argument in quotes, so I joined the arguments:

    # support calling as "convert_time 2022-04-20 11:09 -0500"
    arg_time=ARGV.join(' ')
    the_time=Time.parse(arg_time)

    The resultant parsed time will be interpreted as a local time unless a time zone offset was specified.

    Making things pretty

    I’m using the colorize gem for coloring the output. Note: Windows Terminal or similar is required for full color output.

    Converting timezones

    (tzinfo-data gem is required for Windows or else you’ll end up with a TZInfo::DataSourceNotFound)

    I’m using the tzinfo gem to convert times to specified times by grabbing a TZInfo::Timezone and calling to_local

    tz=TZInfo::Timezone.get(v)
    string="#{k}: => #{tz.to_local(the_time)}"
    

    Presentation

    I look through a hash of all of my “locations” (keys) and respective time zone representations (values) in time_zone_hash and present times for each as a time zone chart:

    time zone conversion for a parsed time and then time zones for each location I want to display
    convert_times output on Windows Terminal

    The Time Zone Conversion Code

    #!/usr/bin/env ruby
    require 'time'
    require 'tzinfo'
    require 'colorize'
    def usage
    puts %q(
    usage:
    convert_times {time string to parse, spaces ok}
    Examples:
    convert_times 13:30 # parses as local time "today"
    convert_times 2022-04-21 13:30 # parse as local time
    convert_times 2022-04-21 13:30 -0500 # parse as -0500 offset
    )
    end
    if ARGV.length == 0
    usage
    exit
    end
    arg_time=ARGV.join(' ')
    the_time=Time.parse(arg_time)
    puts ("=" * 80).colorize(:light_white)
    puts "Input time: #{the_time}".colorize(:light_white)
    puts ("=" * 80).colorize(:light_white)
    LOCAL_TIME='America/Chicago'
    # keys are the description of the time for display
    # values are time zone identifiers
    # (use TZInfo::Timezone.all_identifiers to find a valid time zone identifier)
    time_zone_hash = {
    "UTC" => 'Etc/GMT',
    "Brazil" => 'America/Sao_Paulo',
    "Your time" => LOCAL_TIME,
    "Datadog" => LOCAL_TIME,
    "Slack alerts" => LOCAL_TIME,
    "Database" => 'Etc/GMT',
    "Kansas City" => 'America/Chicago',
    "Denver" => 'America/Denver',
    "New York" => 'America/New_York',
    "Seattle" => 'America/Los_Angeles'
    }
    puts "database timestamps are in UTC".colorize(:light_red)
    time_zone_hash.each do |k,v|
    tz=TZInfo::Timezone.get(v)
    string="#{k}: => #{tz.to_local(the_time)}"
    case k
    when "UTC"
    puts string.colorize(color: :black, background: :light_green)
    when "Your time"
    puts string.colorize(:light_green)
    else
    puts string
    end
    end
    view raw convert_times hosted with ❤ by GitHub

    Next Steps

    I just had this in a script to call when comparing timestamps, but it could be bundled and the hash itself could be in a configuration or yaml file to clean up the time zone conversion.

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: