Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is sample script that let user input some values interactively
- # also it caches theses values in .vagrant/app_data.json so after restart
- # user will not need to enter again same values
- #
- # To use this class you need to use DataLoader.instance.ask everywhere where you needed to have dynamic data.
- #
- # Syntax: DataLoader.instance.ask(VARIABLE_NAME_TO_SAVE, PROMPT_MESSAGE, DATA_TYPE)
- # VARIABLE_NAME_TO_SAVE - dictionary key name where to save value
- # PROMPT_MESSAGE - prompt message that will be displayed before input
- # DATA_TYPE - :password or :text
- #
- # Use this to save data:
- # config.trigger.after :provision do |trigger|
- # trigger.info = "Data saved" if DataLoader.instance.save
- # end
- # Add this class to beginning of your Vagrantfile:
- class DataLoader
- include Singleton
- @@data = {}
- @@changed = false
- def initialize
- load
- end
- def ask(name, message, type)
- return @@data[name] if @@data.key?(name)
- require 'io/console'
- case type
- when :password
- @@data[name] = STDIN.getpass(message).chomp
- else
- print message
- @@data[name] = STDIN.gets.chomp
- end
- @@changed = true
- end
- def save
- return false unless @@changed
- File.open(filename, 'w') do |file|
- file.write to_json
- end
- @@changed = false
- true
- end
- :protected
- def enc_data_org
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- end
- def enc_data_rnd
- "1aj6tRx7b39ASKeq5sYVOEFMoZX2N4DwyzndGJChLg0kfQPrilUTBIWpc8vHum"
- end
- def to_json
- require 'json'
- JSON.generate(@@data).tr enc_data_org, enc_data_rnd
- end
- def filename
- File.join '.vagrant', 'app_data.json'
- end
- def load
- if File.exist?(filename)
- require 'json'
- puts "Reading saved app data..."
- contents = File.read(filename)
- decoded = contents.tr(enc_data_rnd, enc_data_org)
- @@data = JSON.parse(decoded)
- end
- end
- end
- # Here is sample usage ('autologin to gitlab docker container registry'):
- Vagrant.configure("2") do |config|
- config.vm.box = "resmas/dockerlab"
- config.vm.provision :docker
- config.vm.provision :docker_compose,
- compose_version: '1.22.0'
- config.vm.provision :docker_login,
- server: 'registry.gitlab.com',
- username: DataLoader.instance.ask('gitlab_user', 'GitLab login: ', :text),
- password: DataLoader.instance.ask('gitlab_password', 'GitLab password: ', :password)
- config.trigger.after :provision do |trigger|
- trigger.info = "Data saved" if DataLoader.instance.save
- end
- end
Add Comment
Please, Sign In to add comment