Guest User

Untitled

a guest
Oct 1st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. # This is sample script that let user input some values interactively
  2. # also it caches theses values in .vagrant/app_data.json so after restart
  3. # user will not need to enter again same values
  4. #
  5. # To use this class you need to use DataLoader.instance.ask everywhere where you needed to have dynamic data.
  6. #
  7. # Syntax: DataLoader.instance.ask(VARIABLE_NAME_TO_SAVE, PROMPT_MESSAGE, DATA_TYPE)
  8. # VARIABLE_NAME_TO_SAVE - dictionary key name where to save value
  9. # PROMPT_MESSAGE - prompt message that will be displayed before input
  10. # DATA_TYPE - :password or :text
  11. #
  12. # Use this to save data:
  13. # config.trigger.after :provision do |trigger|
  14. # trigger.info = "Data saved" if DataLoader.instance.save
  15. # end
  16.  
  17. # Add this class to beginning of your Vagrantfile:
  18. class DataLoader
  19. include Singleton
  20.  
  21. @@data = {}
  22. @@changed = false
  23.  
  24. def initialize
  25. load
  26. end
  27.  
  28. def ask(name, message, type)
  29. return @@data[name] if @@data.key?(name)
  30. require 'io/console'
  31. case type
  32. when :password
  33. @@data[name] = STDIN.getpass(message).chomp
  34. else
  35. print message
  36. @@data[name] = STDIN.gets.chomp
  37. end
  38. @@changed = true
  39. end
  40.  
  41. def save
  42. return false unless @@changed
  43. File.open(filename, 'w') do |file|
  44. file.write to_json
  45. end
  46. @@changed = false
  47. true
  48. end
  49.  
  50. :protected
  51.  
  52. def enc_data_org
  53. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  54. end
  55.  
  56. def enc_data_rnd
  57. "1aj6tRx7b39ASKeq5sYVOEFMoZX2N4DwyzndGJChLg0kfQPrilUTBIWpc8vHum"
  58. end
  59.  
  60. def to_json
  61. require 'json'
  62.  
  63. JSON.generate(@@data).tr enc_data_org, enc_data_rnd
  64. end
  65.  
  66. def filename
  67. File.join '.vagrant', 'app_data.json'
  68. end
  69.  
  70. def load
  71. if File.exist?(filename)
  72. require 'json'
  73.  
  74. puts "Reading saved app data..."
  75. contents = File.read(filename)
  76. decoded = contents.tr(enc_data_rnd, enc_data_org)
  77. @@data = JSON.parse(decoded)
  78. end
  79. end
  80. end
  81.  
  82. # Here is sample usage ('autologin to gitlab docker container registry'):
  83. Vagrant.configure("2") do |config|
  84. config.vm.box = "resmas/dockerlab"
  85.  
  86. config.vm.provision :docker
  87. config.vm.provision :docker_compose,
  88. compose_version: '1.22.0'
  89. config.vm.provision :docker_login,
  90. server: 'registry.gitlab.com',
  91. username: DataLoader.instance.ask('gitlab_user', 'GitLab login: ', :text),
  92. password: DataLoader.instance.ask('gitlab_password', 'GitLab password: ', :password)
  93.  
  94. config.trigger.after :provision do |trigger|
  95. trigger.info = "Data saved" if DataLoader.instance.save
  96. end
  97. end
Add Comment
Please, Sign In to add comment