Skip to content

Commit

Permalink
Merge pull request #460 from puppetlabs/token-migration
Browse files Browse the repository at this point in the history
(DIO-2186) Token migration
  • Loading branch information
yachub authored Sep 17, 2021
2 parents f6eb636 + dd1b816 commit b9a5b52
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.bundle/
.vagrant/
results.xml
coverage/
vendor/
.dccache
.ruby-version
Gemfile.lock
Gemfile.local
vendor
Gemfile.lock
results.xml
/vmpooler.yaml
.bundle
coverage

86 changes: 86 additions & 0 deletions utils/vmp_utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'date'
require 'json'
require 'redis'
require 'thor'

class VMPUtils < Thor
desc 'export_tokens', 'Export VMPooler tokens'
long_desc <<-LONGDESC
vmp_utils export_tokens will export the tokens stored in redis as JSON
to the specified file. By default it will export all tokens used within
the last year. This can be overridden by setting the "since-date" option.
LONGDESC
option :since_date, default: (DateTime.now - 365).iso8601
option :redis_host, required: true
option :redis_password
option :redis_port, type: :numeric, default: 6379
option :output_file, required: true
def export_tokens
redis = Redis.new(host: options[:redis_host], port: options[:redis_port], password: options[:redis_password])
begin
redis.ping
puts "connection successful to #{options[:redis_host]}"
tokens_to_export = {}
date_filter = DateTime.parse(options[:since_date])
redis.scan_each(match: 'vmpooler__token__*') do |key|
print('.')
key_data = redis.hgetall(key)
export = false
if key_data.key?('last') && date_filter < DateTime.parse(key_data['last'])
export = true
elsif date_filter < DateTime.parse(key_data['created'])
export = true
end
tokens_to_export[key] = key_data if export
end
puts ''
File.open(options[:output_file], 'w') do |output|
output.puts JSON.pretty_generate(tokens_to_export)
end
rescue Redis::CannotConnectError => e
raise Thor::Error, e
rescue Redis::CommandError => e
raise Thor::Error, e
end
end

desc 'import_tokens', 'Import previously exported VMPooler tokens'
long_desc <<-LONGDESC
vmp_utils import_tokens will load tokens from a previous export into the
specified redis instance. It expects a JSON file like the one generated by
export_tokens to be provided as input.
LONGDESC
option :redis_host, required: true
option :redis_password
option :redis_port, type: :numeric, default: 6379
option :input_file, required: true
def import_tokens
raise Thor::Error, "input-file '#{options[:input_file]}' doesn't exist" unless File.exist?(options[:input_file])

begin
data_file = File.read(options[:input_file])
tokens_to_import = JSON.parse(data_file)
rescue JSON::ParserError
raise Thor::Error, "Unable to parse please verify that #{options[:input_file]}. Please make sure it is a valid JSON file"
end

redis = Redis.new(host: options[:redis_host], port: options[:redis_port], password: options[:redis_password])
begin
redis.ping
puts "connection successful to #{options[:redis_host]}"
tokens_to_import.each do |key, value|
puts "importing token for #{value['user']}"
redis.hset(key, value)
end
rescue Redis::CannotConnectError => e
raise Thor::Error, e
rescue Redis::CommandError => e
raise Thor::Error, e
end
end
end

VMPUtils.start(ARGV)
1 change: 1 addition & 0 deletions vmpooler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '>= 3.2'
s.add_development_dependency 'rubocop', '~> 1.1.0'
s.add_development_dependency 'simplecov', '>= 0.11.2'
s.add_development_dependency 'thor', '~> 1.0', '>= 1.0.1'
s.add_development_dependency 'yarjuf', '>= 2.0'
end

0 comments on commit b9a5b52

Please sign in to comment.